blob: 3d6b6e783c2a2fde4485f407a4494f810211fdc4 [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 Browna47425a2012-04-13 04:09:27 -070039// Log debug messages about the vibrator.
40#define DEBUG_VIBRATOR 0
41
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042#include "InputReader.h"
43
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044#include <cutils/log.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080045#include <androidfw/Keyboard.h>
46#include <androidfw/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047
48#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070049#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070050#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051#include <errno.h>
52#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070053#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070054
Jeff Brown8d608662010-08-30 03:02:23 -070055#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070056#define INDENT2 " "
57#define INDENT3 " "
58#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070059#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070060
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070061namespace android {
62
Jeff Brownace13b12011-03-09 17:39:48 -080063// --- Constants ---
64
Jeff Brown80fd47c2011-05-24 01:07:44 -070065// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
66static const size_t MAX_SLOTS = 32;
67
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070068// --- Static Functions ---
69
70template<typename T>
71inline static T abs(const T& value) {
72 return value < 0 ? - value : value;
73}
74
75template<typename T>
76inline static T min(const T& a, const T& b) {
77 return a < b ? a : b;
78}
79
Jeff Brown5c225b12010-06-16 01:53:36 -070080template<typename T>
81inline static void swap(T& a, T& b) {
82 T temp = a;
83 a = b;
84 b = temp;
85}
86
Jeff Brown8d608662010-08-30 03:02:23 -070087inline static float avg(float x, float y) {
88 return (x + y) / 2;
89}
90
Jeff Brown2352b972011-04-12 22:39:53 -070091inline static float distance(float x1, float y1, float x2, float y2) {
92 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080093}
94
Jeff Brown517bb4c2011-01-14 19:09:23 -080095inline static int32_t signExtendNybble(int32_t value) {
96 return value >= 8 ? value - 16 : value;
97}
98
Jeff Brownef3d7e82010-09-30 14:33:04 -070099static inline const char* toString(bool value) {
100 return value ? "true" : "false";
101}
102
Jeff Brown9626b142011-03-03 02:09:54 -0800103static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
104 const int32_t map[][4], size_t mapSize) {
105 if (orientation != DISPLAY_ORIENTATION_0) {
106 for (size_t i = 0; i < mapSize; i++) {
107 if (value == map[i][0]) {
108 return map[i][orientation];
109 }
110 }
111 }
112 return value;
113}
114
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700115static const int32_t keyCodeRotationMap[][4] = {
116 // key codes enumerated counter-clockwise with the original (unrotated) key first
117 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700118 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
119 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
120 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
121 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700122};
Jeff Brown9626b142011-03-03 02:09:54 -0800123static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700124 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
125
Jeff Brown60691392011-07-15 19:08:26 -0700126static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800127 return rotateValueUsingRotationMap(keyCode, orientation,
128 keyCodeRotationMap, keyCodeRotationMapSize);
129}
130
Jeff Brown612891e2011-07-15 20:44:17 -0700131static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
132 float temp;
133 switch (orientation) {
134 case DISPLAY_ORIENTATION_90:
135 temp = *deltaX;
136 *deltaX = *deltaY;
137 *deltaY = -temp;
138 break;
139
140 case DISPLAY_ORIENTATION_180:
141 *deltaX = -*deltaX;
142 *deltaY = -*deltaY;
143 break;
144
145 case DISPLAY_ORIENTATION_270:
146 temp = *deltaX;
147 *deltaX = -*deltaY;
148 *deltaY = temp;
149 break;
150 }
151}
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
154 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
155}
156
Jeff Brownefd32662011-03-08 15:13:06 -0800157// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700158// button states. This determines whether the event is reported as a touch event.
159static bool isPointerDown(int32_t buttonState) {
160 return buttonState &
161 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700162 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800163}
164
Jeff Brown2352b972011-04-12 22:39:53 -0700165static float calculateCommonVector(float a, float b) {
166 if (a > 0 && b > 0) {
167 return a < b ? a : b;
168 } else if (a < 0 && b < 0) {
169 return a > b ? a : b;
170 } else {
171 return 0;
172 }
173}
174
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700175static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
176 nsecs_t when, int32_t deviceId, uint32_t source,
177 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
178 int32_t buttonState, int32_t keyCode) {
179 if (
180 (action == AKEY_EVENT_ACTION_DOWN
181 && !(lastButtonState & buttonState)
182 && (currentButtonState & buttonState))
183 || (action == AKEY_EVENT_ACTION_UP
184 && (lastButtonState & buttonState)
185 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700186 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700187 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700188 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700189 }
190}
191
192static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
193 nsecs_t when, int32_t deviceId, uint32_t source,
194 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
198 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
199 lastButtonState, currentButtonState,
200 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
201}
202
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700203
Jeff Brown65fd2512011-08-18 11:20:58 -0700204// --- InputReaderConfiguration ---
205
Jeff Brownd728bf52012-09-08 18:05:28 -0700206bool InputReaderConfiguration::getDisplayInfo(bool external, DisplayViewport* outViewport) const {
207 const DisplayViewport& viewport = external ? mExternalDisplay : mInternalDisplay;
208 if (viewport.displayId >= 0) {
209 *outViewport = viewport;
210 return true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700211 }
212 return false;
213}
214
Jeff Brownd728bf52012-09-08 18:05:28 -0700215void InputReaderConfiguration::setDisplayInfo(bool external, const DisplayViewport& viewport) {
216 DisplayViewport& v = external ? mExternalDisplay : mInternalDisplay;
217 v = viewport;
Jeff Brown65fd2512011-08-18 11:20:58 -0700218}
219
220
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700221// --- InputReader ---
222
223InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700224 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700225 const sp<InputListenerInterface>& listener) :
226 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700227 mGlobalMetaState(0), mGeneration(1),
228 mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700229 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700230 mQueuedListener = new QueuedInputListener(listener);
231
232 { // acquire lock
233 AutoMutex _l(mLock);
234
235 refreshConfigurationLocked(0);
236 updateGlobalMetaStateLocked();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700237 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700238}
239
240InputReader::~InputReader() {
241 for (size_t i = 0; i < mDevices.size(); i++) {
242 delete mDevices.valueAt(i);
243 }
244}
245
246void InputReader::loopOnce() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700247 int32_t oldGeneration;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700248 int32_t timeoutMillis;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700249 bool inputDevicesChanged = false;
250 Vector<InputDeviceInfo> inputDevices;
Jeff Brown474dcb52011-06-14 20:22:50 -0700251 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700252 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700253
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700254 oldGeneration = mGeneration;
255 timeoutMillis = -1;
256
Jeff Brownbe1aa822011-07-27 16:04:54 -0700257 uint32_t changes = mConfigurationChangesToRefresh;
258 if (changes) {
259 mConfigurationChangesToRefresh = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700260 timeoutMillis = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700261 refreshConfigurationLocked(changes);
Jeff Browna47425a2012-04-13 04:09:27 -0700262 } else if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700263 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
264 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
265 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700266 } // release lock
267
Jeff Brownb7198742011-03-18 18:14:26 -0700268 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700269
270 { // acquire lock
271 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800272 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700273
274 if (count) {
275 processEventsLocked(mEventBuffer, count);
276 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700277
278 if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700279 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800280 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700281#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800282 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700283#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800284 mNextTimeout = LLONG_MAX;
285 timeoutExpiredLocked(now);
286 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700287 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700288
289 if (oldGeneration != mGeneration) {
290 inputDevicesChanged = true;
291 getInputDevicesLocked(inputDevices);
292 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700293 } // release lock
294
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700295 // Send out a message that the describes the changed input devices.
296 if (inputDevicesChanged) {
297 mPolicy->notifyInputDevicesChanged(inputDevices);
298 }
299
Jeff Brownbe1aa822011-07-27 16:04:54 -0700300 // Flush queued events out to the listener.
301 // This must happen outside of the lock because the listener could potentially call
302 // back into the InputReader's methods, such as getScanCodeState, or become blocked
303 // on another thread similarly waiting to acquire the InputReader lock thereby
304 // resulting in a deadlock. This situation is actually quite plausible because the
305 // listener is actually the input dispatcher, which calls into the window manager,
306 // which occasionally calls into the input reader.
307 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700308}
309
Jeff Brownbe1aa822011-07-27 16:04:54 -0700310void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700311 for (const RawEvent* rawEvent = rawEvents; count;) {
312 int32_t type = rawEvent->type;
313 size_t batchSize = 1;
314 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
315 int32_t deviceId = rawEvent->deviceId;
316 while (batchSize < count) {
317 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
318 || rawEvent[batchSize].deviceId != deviceId) {
319 break;
320 }
321 batchSize += 1;
322 }
323#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000324 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700325#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700326 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700327 } else {
328 switch (rawEvent->type) {
329 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700330 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700331 break;
332 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700333 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700334 break;
335 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700336 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700337 break;
338 default:
Steve Blockec193de2012-01-09 18:35:44 +0000339 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700340 break;
341 }
342 }
343 count -= batchSize;
344 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700345 }
346}
347
Jeff Brown65fd2512011-08-18 11:20:58 -0700348void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700349 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
350 if (deviceIndex >= 0) {
351 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
352 return;
353 }
354
Jeff Browne38fdfa2012-04-06 14:51:01 -0700355 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
357
Jeff Browne38fdfa2012-04-06 14:51:01 -0700358 InputDevice* device = createDeviceLocked(deviceId, identifier, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700359 device->configure(when, &mConfig, 0);
360 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700361
Jeff Brown8d608662010-08-30 03:02:23 -0700362 if (device->isIgnored()) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700363 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
364 identifier.name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700365 } else {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700366 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
367 identifier.name.string(), device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700368 }
369
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700370 mDevices.add(deviceId, device);
371 bumpGenerationLocked();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700372}
373
Jeff Brown65fd2512011-08-18 11:20:58 -0700374void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700376 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700377 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000378 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700379 return;
380 }
381
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700382 device = mDevices.valueAt(deviceIndex);
383 mDevices.removeItemsAt(deviceIndex, 1);
384 bumpGenerationLocked();
385
Jeff Brown6d0fec22010-07-23 21:28:06 -0700386 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000387 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 device->getId(), device->getName().string());
389 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000390 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700391 device->getId(), device->getName().string(), device->getSources());
392 }
393
Jeff Brown65fd2512011-08-18 11:20:58 -0700394 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700396}
397
Jeff Brownbe1aa822011-07-27 16:04:54 -0700398InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700399 const InputDeviceIdentifier& identifier, uint32_t classes) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700400 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
401 identifier, classes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700402
Jeff Brown56194eb2011-03-02 19:23:13 -0800403 // External devices.
404 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
405 device->setExternal(true);
406 }
407
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408 // Switch-like devices.
409 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
410 device->addMapper(new SwitchInputMapper(device));
411 }
412
Jeff Browna47425a2012-04-13 04:09:27 -0700413 // Vibrator-like devices.
414 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
415 device->addMapper(new VibratorInputMapper(device));
416 }
417
Jeff Brown6d0fec22010-07-23 21:28:06 -0700418 // 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 Brownbe1aa822011-07-27 16:04:54 -0700458void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700459 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700460 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
461 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000462 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700463 return;
464 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465
Jeff Brownbe1aa822011-07-27 16:04:54 -0700466 InputDevice* device = mDevices.valueAt(deviceIndex);
467 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000468 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700469 return;
470 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700471
Jeff Brownbe1aa822011-07-27 16:04:54 -0700472 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700473}
474
Jeff Brownbe1aa822011-07-27 16:04:54 -0700475void InputReader::timeoutExpiredLocked(nsecs_t when) {
476 for (size_t i = 0; i < mDevices.size(); i++) {
477 InputDevice* device = mDevices.valueAt(i);
478 if (!device->isIgnored()) {
479 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700480 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700481 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700482}
483
Jeff Brownbe1aa822011-07-27 16:04:54 -0700484void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700485 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700486 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487
Jeff Brown6d0fec22010-07-23 21:28:06 -0700488 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700489 NotifyConfigurationChangedArgs args(when);
490 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700491}
492
Jeff Brownbe1aa822011-07-27 16:04:54 -0700493void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700494 mPolicy->getReaderConfiguration(&mConfig);
495 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
496
Jeff Brown474dcb52011-06-14 20:22:50 -0700497 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000498 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700499 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700500
501 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
502 mEventHub->requestReopenDevices();
503 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700504 for (size_t i = 0; i < mDevices.size(); i++) {
505 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700506 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700507 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700508 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509 }
510}
511
Jeff Brownbe1aa822011-07-27 16:04:54 -0700512void InputReader::updateGlobalMetaStateLocked() {
513 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700514
Jeff Brownbe1aa822011-07-27 16:04:54 -0700515 for (size_t i = 0; i < mDevices.size(); i++) {
516 InputDevice* device = mDevices.valueAt(i);
517 mGlobalMetaState |= device->getMetaState();
518 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700519}
520
Jeff Brownbe1aa822011-07-27 16:04:54 -0700521int32_t InputReader::getGlobalMetaStateLocked() {
522 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700523}
524
Jeff Brownbe1aa822011-07-27 16:04:54 -0700525void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800526 mDisableVirtualKeysTimeout = time;
527}
528
Jeff Brownbe1aa822011-07-27 16:04:54 -0700529bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800530 InputDevice* device, int32_t keyCode, int32_t scanCode) {
531 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000532 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800533 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
534 device->getName().string(),
535 (mDisableVirtualKeysTimeout - now) * 0.000001,
536 keyCode, scanCode);
537 return true;
538 } else {
539 return false;
540 }
541}
542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543void InputReader::fadePointerLocked() {
544 for (size_t i = 0; i < mDevices.size(); i++) {
545 InputDevice* device = mDevices.valueAt(i);
546 device->fadePointer();
547 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800548}
549
Jeff Brownbe1aa822011-07-27 16:04:54 -0700550void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700551 if (when < mNextTimeout) {
552 mNextTimeout = when;
Jeff Browna47425a2012-04-13 04:09:27 -0700553 mEventHub->wake();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700554 }
555}
556
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700557int32_t InputReader::bumpGenerationLocked() {
558 return ++mGeneration;
559}
560
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700561void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700562 AutoMutex _l(mLock);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700563 getInputDevicesLocked(outInputDevices);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700564}
565
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700566void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) {
567 outInputDevices.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700568
Jeff Brownbe1aa822011-07-27 16:04:54 -0700569 size_t numDevices = mDevices.size();
570 for (size_t i = 0; i < numDevices; i++) {
571 InputDevice* device = mDevices.valueAt(i);
572 if (!device->isIgnored()) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700573 outInputDevices.push();
574 device->getDeviceInfo(&outInputDevices.editTop());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700576 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700577}
578
579int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
580 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 AutoMutex _l(mLock);
582
583 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700584}
585
586int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
587 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700588 AutoMutex _l(mLock);
589
590 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700591}
592
593int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700594 AutoMutex _l(mLock);
595
596 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700597}
598
Jeff Brownbe1aa822011-07-27 16:04:54 -0700599int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700601 int32_t result = AKEY_STATE_UNKNOWN;
602 if (deviceId >= 0) {
603 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
604 if (deviceIndex >= 0) {
605 InputDevice* device = mDevices.valueAt(deviceIndex);
606 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
607 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700608 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700609 }
610 } else {
611 size_t numDevices = mDevices.size();
612 for (size_t i = 0; i < numDevices; i++) {
613 InputDevice* device = mDevices.valueAt(i);
614 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800615 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
616 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
617 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
618 if (currentResult >= AKEY_STATE_DOWN) {
619 return currentResult;
620 } else if (currentResult == AKEY_STATE_UP) {
621 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700622 }
623 }
624 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700625 }
626 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700627}
628
629bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
630 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631 AutoMutex _l(mLock);
632
Jeff Brown6d0fec22010-07-23 21:28:06 -0700633 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700634 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700635}
636
Jeff Brownbe1aa822011-07-27 16:04:54 -0700637bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
638 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
639 bool result = false;
640 if (deviceId >= 0) {
641 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
642 if (deviceIndex >= 0) {
643 InputDevice* device = mDevices.valueAt(deviceIndex);
644 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
645 result = device->markSupportedKeyCodes(sourceMask,
646 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700647 }
648 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700649 } else {
650 size_t numDevices = mDevices.size();
651 for (size_t i = 0; i < numDevices; i++) {
652 InputDevice* device = mDevices.valueAt(i);
653 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
654 result |= device->markSupportedKeyCodes(sourceMask,
655 numCodes, keyCodes, outFlags);
656 }
657 }
658 }
659 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700660}
661
Jeff Brown474dcb52011-06-14 20:22:50 -0700662void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700663 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700664
Jeff Brownbe1aa822011-07-27 16:04:54 -0700665 if (changes) {
666 bool needWake = !mConfigurationChangesToRefresh;
667 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700668
669 if (needWake) {
670 mEventHub->wake();
671 }
672 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700673}
674
Jeff Browna47425a2012-04-13 04:09:27 -0700675void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
676 ssize_t repeat, int32_t token) {
677 AutoMutex _l(mLock);
678
679 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
680 if (deviceIndex >= 0) {
681 InputDevice* device = mDevices.valueAt(deviceIndex);
682 device->vibrate(pattern, patternSize, repeat, token);
683 }
684}
685
686void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
687 AutoMutex _l(mLock);
688
689 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
690 if (deviceIndex >= 0) {
691 InputDevice* device = mDevices.valueAt(deviceIndex);
692 device->cancelVibrate(token);
693 }
694}
695
Jeff Brownb88102f2010-09-08 11:49:43 -0700696void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700697 AutoMutex _l(mLock);
698
Jeff Brownf2f487182010-10-01 17:46:21 -0700699 mEventHub->dump(dump);
700 dump.append("\n");
701
702 dump.append("Input Reader State:\n");
703
Jeff Brownbe1aa822011-07-27 16:04:54 -0700704 for (size_t i = 0; i < mDevices.size(); i++) {
705 mDevices.valueAt(i)->dump(dump);
706 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700707
708 dump.append(INDENT "Configuration:\n");
709 dump.append(INDENT2 "ExcludedDeviceNames: [");
710 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
711 if (i != 0) {
712 dump.append(", ");
713 }
714 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
715 }
716 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700717 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
718 mConfig.virtualKeyQuietTime * 0.000001f);
719
Jeff Brown19c97d462011-06-01 12:33:19 -0700720 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
721 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
722 mConfig.pointerVelocityControlParameters.scale,
723 mConfig.pointerVelocityControlParameters.lowThreshold,
724 mConfig.pointerVelocityControlParameters.highThreshold,
725 mConfig.pointerVelocityControlParameters.acceleration);
726
727 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
728 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
729 mConfig.wheelVelocityControlParameters.scale,
730 mConfig.wheelVelocityControlParameters.lowThreshold,
731 mConfig.wheelVelocityControlParameters.highThreshold,
732 mConfig.wheelVelocityControlParameters.acceleration);
733
Jeff Brown214eaf42011-05-26 19:17:02 -0700734 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700735 dump.appendFormat(INDENT3 "Enabled: %s\n",
736 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700737 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
738 mConfig.pointerGestureQuietInterval * 0.000001f);
739 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
740 mConfig.pointerGestureDragMinSwitchSpeed);
741 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
742 mConfig.pointerGestureTapInterval * 0.000001f);
743 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
744 mConfig.pointerGestureTapDragInterval * 0.000001f);
745 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
746 mConfig.pointerGestureTapSlop);
747 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
748 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700749 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
750 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700751 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
752 mConfig.pointerGestureSwipeTransitionAngleCosine);
753 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
754 mConfig.pointerGestureSwipeMaxWidthRatio);
755 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
756 mConfig.pointerGestureMovementSpeedRatio);
757 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
758 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700759}
760
Jeff Brown89ef0722011-08-10 16:25:21 -0700761void InputReader::monitor() {
762 // Acquire and release the lock to ensure that the reader has not deadlocked.
763 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800764 mEventHub->wake();
765 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700766 mLock.unlock();
767
768 // Check the EventHub
769 mEventHub->monitor();
770}
771
Jeff Brown6d0fec22010-07-23 21:28:06 -0700772
Jeff Brownbe1aa822011-07-27 16:04:54 -0700773// --- InputReader::ContextImpl ---
774
775InputReader::ContextImpl::ContextImpl(InputReader* reader) :
776 mReader(reader) {
777}
778
779void InputReader::ContextImpl::updateGlobalMetaState() {
780 // lock is already held by the input loop
781 mReader->updateGlobalMetaStateLocked();
782}
783
784int32_t InputReader::ContextImpl::getGlobalMetaState() {
785 // lock is already held by the input loop
786 return mReader->getGlobalMetaStateLocked();
787}
788
789void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
790 // lock is already held by the input loop
791 mReader->disableVirtualKeysUntilLocked(time);
792}
793
794bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
795 InputDevice* device, int32_t keyCode, int32_t scanCode) {
796 // lock is already held by the input loop
797 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
798}
799
800void InputReader::ContextImpl::fadePointer() {
801 // lock is already held by the input loop
802 mReader->fadePointerLocked();
803}
804
805void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
806 // lock is already held by the input loop
807 mReader->requestTimeoutAtTimeLocked(when);
808}
809
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700810int32_t InputReader::ContextImpl::bumpGeneration() {
811 // lock is already held by the input loop
812 return mReader->bumpGenerationLocked();
813}
814
Jeff Brownbe1aa822011-07-27 16:04:54 -0700815InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
816 return mReader->mPolicy.get();
817}
818
819InputListenerInterface* InputReader::ContextImpl::getListener() {
820 return mReader->mQueuedListener.get();
821}
822
823EventHubInterface* InputReader::ContextImpl::getEventHub() {
824 return mReader->mEventHub.get();
825}
826
827
Jeff Brown6d0fec22010-07-23 21:28:06 -0700828// --- InputReaderThread ---
829
830InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
831 Thread(/*canCallJava*/ true), mReader(reader) {
832}
833
834InputReaderThread::~InputReaderThread() {
835}
836
837bool InputReaderThread::threadLoop() {
838 mReader->loopOnce();
839 return true;
840}
841
842
843// --- InputDevice ---
844
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700845InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700846 const InputDeviceIdentifier& identifier, uint32_t classes) :
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700847 mContext(context), mId(id), mGeneration(generation),
848 mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285af2011-08-31 12:56:34 -0700849 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700850}
851
852InputDevice::~InputDevice() {
853 size_t numMappers = mMappers.size();
854 for (size_t i = 0; i < numMappers; i++) {
855 delete mMappers[i];
856 }
857 mMappers.clear();
858}
859
Jeff Brownef3d7e82010-09-30 14:33:04 -0700860void InputDevice::dump(String8& dump) {
861 InputDeviceInfo deviceInfo;
862 getDeviceInfo(& deviceInfo);
863
Jeff Brown90655042010-12-02 13:50:46 -0800864 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700865 deviceInfo.getDisplayName().string());
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700866 dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration);
Jeff Brown56194eb2011-03-02 19:23:13 -0800867 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700868 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
869 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800870
Jeff Brownefd32662011-03-08 15:13:06 -0800871 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800872 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700873 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800874 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800875 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
876 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800877 char name[32];
878 if (label) {
879 strncpy(name, label, sizeof(name));
880 name[sizeof(name) - 1] = '\0';
881 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800882 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800883 }
Jeff Brownefd32662011-03-08 15:13:06 -0800884 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
Michael Wrightc6091c62013-04-01 20:56:04 -0700885 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
886 name, range.source, range.min, range.max, range.flat, range.fuzz,
887 range.resolution);
Jeff Browncc0c1592011-02-19 05:07:28 -0800888 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700889 }
890
891 size_t numMappers = mMappers.size();
892 for (size_t i = 0; i < numMappers; i++) {
893 InputMapper* mapper = mMappers[i];
894 mapper->dump(dump);
895 }
896}
897
Jeff Brown6d0fec22010-07-23 21:28:06 -0700898void InputDevice::addMapper(InputMapper* mapper) {
899 mMappers.add(mapper);
900}
901
Jeff Brown65fd2512011-08-18 11:20:58 -0700902void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700903 mSources = 0;
904
Jeff Brown474dcb52011-06-14 20:22:50 -0700905 if (!isIgnored()) {
906 if (!changes) { // first time only
907 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
908 }
909
Jeff Brown6ec6f792012-04-17 16:52:41 -0700910 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Jeff Brown61c08242012-04-19 11:14:33 -0700911 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
912 sp<KeyCharacterMap> keyboardLayout =
913 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor);
914 if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
915 bumpGeneration();
916 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700917 }
918 }
919
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700920 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
921 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
922 String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
923 if (mAlias != alias) {
924 mAlias = alias;
925 bumpGeneration();
926 }
927 }
928 }
929
Jeff Brown474dcb52011-06-14 20:22:50 -0700930 size_t numMappers = mMappers.size();
931 for (size_t i = 0; i < numMappers; i++) {
932 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700933 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700934 mSources |= mapper->getSources();
935 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700936 }
937}
938
Jeff Brown65fd2512011-08-18 11:20:58 -0700939void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700940 size_t numMappers = mMappers.size();
941 for (size_t i = 0; i < numMappers; i++) {
942 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700943 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700944 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700945
946 mContext->updateGlobalMetaState();
947
948 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700949}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700950
Jeff Brownb7198742011-03-18 18:14:26 -0700951void InputDevice::process(const RawEvent* rawEvents, size_t count) {
952 // Process all of the events in order for each mapper.
953 // We cannot simply ask each mapper to process them in bulk because mappers may
954 // have side-effects that must be interleaved. For example, joystick movement events and
955 // gamepad button presses are handled by different mappers but they should be dispatched
956 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700957 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700958 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
959#if DEBUG_RAW_EVENTS
Jeff Brownf33b2b22012-10-05 17:59:56 -0700960 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld",
961 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
962 rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700963#endif
964
Jeff Brown80fd47c2011-05-24 01:07:44 -0700965 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700966 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700967 mDropUntilNextSync = false;
968#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000969 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700970#endif
971 } else {
972#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000973 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700974#endif
975 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700976 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700977 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700978 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700979 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700980 } else {
981 for (size_t i = 0; i < numMappers; i++) {
982 InputMapper* mapper = mMappers[i];
983 mapper->process(rawEvent);
984 }
Jeff Brownb7198742011-03-18 18:14:26 -0700985 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700986 }
987}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700988
Jeff Brownaa3855d2011-03-17 01:34:19 -0700989void InputDevice::timeoutExpired(nsecs_t when) {
990 size_t numMappers = mMappers.size();
991 for (size_t i = 0; i < numMappers; i++) {
992 InputMapper* mapper = mMappers[i];
993 mapper->timeoutExpired(when);
994 }
995}
996
Jeff Brown6d0fec22010-07-23 21:28:06 -0700997void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Browndaa37532012-05-01 15:54:03 -0700998 outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700999
1000 size_t numMappers = mMappers.size();
1001 for (size_t i = 0; i < numMappers; i++) {
1002 InputMapper* mapper = mMappers[i];
1003 mapper->populateDeviceInfo(outDeviceInfo);
1004 }
1005}
1006
1007int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1008 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1009}
1010
1011int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1012 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1013}
1014
1015int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1016 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1017}
1018
1019int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1020 int32_t result = AKEY_STATE_UNKNOWN;
1021 size_t numMappers = mMappers.size();
1022 for (size_t i = 0; i < numMappers; i++) {
1023 InputMapper* mapper = mMappers[i];
1024 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001025 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1026 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1027 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1028 if (currentResult >= AKEY_STATE_DOWN) {
1029 return currentResult;
1030 } else if (currentResult == AKEY_STATE_UP) {
1031 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001032 }
1033 }
1034 }
1035 return result;
1036}
1037
1038bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1039 const int32_t* keyCodes, uint8_t* outFlags) {
1040 bool result = false;
1041 size_t numMappers = mMappers.size();
1042 for (size_t i = 0; i < numMappers; i++) {
1043 InputMapper* mapper = mMappers[i];
1044 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1045 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1046 }
1047 }
1048 return result;
1049}
1050
Jeff Browna47425a2012-04-13 04:09:27 -07001051void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1052 int32_t token) {
1053 size_t numMappers = mMappers.size();
1054 for (size_t i = 0; i < numMappers; i++) {
1055 InputMapper* mapper = mMappers[i];
1056 mapper->vibrate(pattern, patternSize, repeat, token);
1057 }
1058}
1059
1060void InputDevice::cancelVibrate(int32_t token) {
1061 size_t numMappers = mMappers.size();
1062 for (size_t i = 0; i < numMappers; i++) {
1063 InputMapper* mapper = mMappers[i];
1064 mapper->cancelVibrate(token);
1065 }
1066}
1067
Jeff Brown6d0fec22010-07-23 21:28:06 -07001068int32_t InputDevice::getMetaState() {
1069 int32_t result = 0;
1070 size_t numMappers = mMappers.size();
1071 for (size_t i = 0; i < numMappers; i++) {
1072 InputMapper* mapper = mMappers[i];
1073 result |= mapper->getMetaState();
1074 }
1075 return result;
1076}
1077
Jeff Brown05dc66a2011-03-02 14:41:58 -08001078void InputDevice::fadePointer() {
1079 size_t numMappers = mMappers.size();
1080 for (size_t i = 0; i < numMappers; i++) {
1081 InputMapper* mapper = mMappers[i];
1082 mapper->fadePointer();
1083 }
1084}
1085
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001086void InputDevice::bumpGeneration() {
1087 mGeneration = mContext->bumpGeneration();
1088}
1089
Jeff Brown65fd2512011-08-18 11:20:58 -07001090void InputDevice::notifyReset(nsecs_t when) {
1091 NotifyDeviceResetArgs args(when, mId);
1092 mContext->getListener()->notifyDeviceReset(&args);
1093}
1094
Jeff Brown6d0fec22010-07-23 21:28:06 -07001095
Jeff Brown49754db2011-07-01 17:37:58 -07001096// --- CursorButtonAccumulator ---
1097
1098CursorButtonAccumulator::CursorButtonAccumulator() {
1099 clearButtons();
1100}
1101
Jeff Brown65fd2512011-08-18 11:20:58 -07001102void CursorButtonAccumulator::reset(InputDevice* device) {
1103 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1104 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1105 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1106 mBtnBack = device->isKeyPressed(BTN_BACK);
1107 mBtnSide = device->isKeyPressed(BTN_SIDE);
1108 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1109 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1110 mBtnTask = device->isKeyPressed(BTN_TASK);
1111}
1112
Jeff Brown49754db2011-07-01 17:37:58 -07001113void CursorButtonAccumulator::clearButtons() {
1114 mBtnLeft = 0;
1115 mBtnRight = 0;
1116 mBtnMiddle = 0;
1117 mBtnBack = 0;
1118 mBtnSide = 0;
1119 mBtnForward = 0;
1120 mBtnExtra = 0;
1121 mBtnTask = 0;
1122}
1123
1124void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1125 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001126 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001127 case BTN_LEFT:
1128 mBtnLeft = rawEvent->value;
1129 break;
1130 case BTN_RIGHT:
1131 mBtnRight = rawEvent->value;
1132 break;
1133 case BTN_MIDDLE:
1134 mBtnMiddle = rawEvent->value;
1135 break;
1136 case BTN_BACK:
1137 mBtnBack = rawEvent->value;
1138 break;
1139 case BTN_SIDE:
1140 mBtnSide = rawEvent->value;
1141 break;
1142 case BTN_FORWARD:
1143 mBtnForward = rawEvent->value;
1144 break;
1145 case BTN_EXTRA:
1146 mBtnExtra = rawEvent->value;
1147 break;
1148 case BTN_TASK:
1149 mBtnTask = rawEvent->value;
1150 break;
1151 }
1152 }
1153}
1154
1155uint32_t CursorButtonAccumulator::getButtonState() const {
1156 uint32_t result = 0;
1157 if (mBtnLeft) {
1158 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1159 }
1160 if (mBtnRight) {
1161 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1162 }
1163 if (mBtnMiddle) {
1164 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1165 }
1166 if (mBtnBack || mBtnSide) {
1167 result |= AMOTION_EVENT_BUTTON_BACK;
1168 }
1169 if (mBtnForward || mBtnExtra) {
1170 result |= AMOTION_EVENT_BUTTON_FORWARD;
1171 }
1172 return result;
1173}
1174
1175
1176// --- CursorMotionAccumulator ---
1177
Jeff Brown65fd2512011-08-18 11:20:58 -07001178CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001179 clearRelativeAxes();
1180}
1181
Jeff Brown65fd2512011-08-18 11:20:58 -07001182void CursorMotionAccumulator::reset(InputDevice* device) {
1183 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001184}
1185
1186void CursorMotionAccumulator::clearRelativeAxes() {
1187 mRelX = 0;
1188 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001189}
1190
1191void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1192 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001193 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001194 case REL_X:
1195 mRelX = rawEvent->value;
1196 break;
1197 case REL_Y:
1198 mRelY = rawEvent->value;
1199 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001200 }
1201 }
1202}
1203
1204void CursorMotionAccumulator::finishSync() {
1205 clearRelativeAxes();
1206}
1207
1208
1209// --- CursorScrollAccumulator ---
1210
1211CursorScrollAccumulator::CursorScrollAccumulator() :
1212 mHaveRelWheel(false), mHaveRelHWheel(false) {
1213 clearRelativeAxes();
1214}
1215
1216void CursorScrollAccumulator::configure(InputDevice* device) {
1217 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1218 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1219}
1220
1221void CursorScrollAccumulator::reset(InputDevice* device) {
1222 clearRelativeAxes();
1223}
1224
1225void CursorScrollAccumulator::clearRelativeAxes() {
1226 mRelWheel = 0;
1227 mRelHWheel = 0;
1228}
1229
1230void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1231 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001232 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001233 case REL_WHEEL:
1234 mRelWheel = rawEvent->value;
1235 break;
1236 case REL_HWHEEL:
1237 mRelHWheel = rawEvent->value;
1238 break;
1239 }
1240 }
1241}
1242
Jeff Brown65fd2512011-08-18 11:20:58 -07001243void CursorScrollAccumulator::finishSync() {
1244 clearRelativeAxes();
1245}
1246
Jeff Brown49754db2011-07-01 17:37:58 -07001247
1248// --- TouchButtonAccumulator ---
1249
1250TouchButtonAccumulator::TouchButtonAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001251 mHaveBtnTouch(false), mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001252 clearButtons();
1253}
1254
1255void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001256 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
Jeff Brown00710e92012-04-19 15:18:26 -07001257 mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1258 || device->hasKey(BTN_TOOL_RUBBER)
1259 || device->hasKey(BTN_TOOL_BRUSH)
1260 || device->hasKey(BTN_TOOL_PENCIL)
1261 || device->hasKey(BTN_TOOL_AIRBRUSH);
Jeff Brown65fd2512011-08-18 11:20:58 -07001262}
1263
1264void TouchButtonAccumulator::reset(InputDevice* device) {
1265 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1266 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1267 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1268 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1269 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1270 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1271 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1272 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1273 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1274 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1275 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001276 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1277 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1278 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001279}
1280
1281void TouchButtonAccumulator::clearButtons() {
1282 mBtnTouch = 0;
1283 mBtnStylus = 0;
1284 mBtnStylus2 = 0;
1285 mBtnToolFinger = 0;
1286 mBtnToolPen = 0;
1287 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001288 mBtnToolBrush = 0;
1289 mBtnToolPencil = 0;
1290 mBtnToolAirbrush = 0;
1291 mBtnToolMouse = 0;
1292 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001293 mBtnToolDoubleTap = 0;
1294 mBtnToolTripleTap = 0;
1295 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001296}
1297
1298void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1299 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001300 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001301 case BTN_TOUCH:
1302 mBtnTouch = rawEvent->value;
1303 break;
1304 case BTN_STYLUS:
1305 mBtnStylus = rawEvent->value;
1306 break;
1307 case BTN_STYLUS2:
1308 mBtnStylus2 = rawEvent->value;
1309 break;
1310 case BTN_TOOL_FINGER:
1311 mBtnToolFinger = rawEvent->value;
1312 break;
1313 case BTN_TOOL_PEN:
1314 mBtnToolPen = rawEvent->value;
1315 break;
1316 case BTN_TOOL_RUBBER:
1317 mBtnToolRubber = rawEvent->value;
1318 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001319 case BTN_TOOL_BRUSH:
1320 mBtnToolBrush = rawEvent->value;
1321 break;
1322 case BTN_TOOL_PENCIL:
1323 mBtnToolPencil = rawEvent->value;
1324 break;
1325 case BTN_TOOL_AIRBRUSH:
1326 mBtnToolAirbrush = rawEvent->value;
1327 break;
1328 case BTN_TOOL_MOUSE:
1329 mBtnToolMouse = rawEvent->value;
1330 break;
1331 case BTN_TOOL_LENS:
1332 mBtnToolLens = rawEvent->value;
1333 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001334 case BTN_TOOL_DOUBLETAP:
1335 mBtnToolDoubleTap = rawEvent->value;
1336 break;
1337 case BTN_TOOL_TRIPLETAP:
1338 mBtnToolTripleTap = rawEvent->value;
1339 break;
1340 case BTN_TOOL_QUADTAP:
1341 mBtnToolQuadTap = rawEvent->value;
1342 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001343 }
1344 }
1345}
1346
1347uint32_t TouchButtonAccumulator::getButtonState() const {
1348 uint32_t result = 0;
1349 if (mBtnStylus) {
1350 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1351 }
1352 if (mBtnStylus2) {
1353 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1354 }
1355 return result;
1356}
1357
1358int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001359 if (mBtnToolMouse || mBtnToolLens) {
1360 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1361 }
Jeff Brown49754db2011-07-01 17:37:58 -07001362 if (mBtnToolRubber) {
1363 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1364 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001365 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001366 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1367 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001368 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001369 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1370 }
1371 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1372}
1373
Jeff Brownd87c6d52011-08-10 14:55:59 -07001374bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001375 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1376 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001377 || mBtnToolMouse || mBtnToolLens
1378 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001379}
1380
1381bool TouchButtonAccumulator::isHovering() const {
1382 return mHaveBtnTouch && !mBtnTouch;
1383}
1384
Jeff Brown00710e92012-04-19 15:18:26 -07001385bool TouchButtonAccumulator::hasStylus() const {
1386 return mHaveStylus;
1387}
1388
Jeff Brown49754db2011-07-01 17:37:58 -07001389
Jeff Brownbe1aa822011-07-27 16:04:54 -07001390// --- RawPointerAxes ---
1391
1392RawPointerAxes::RawPointerAxes() {
1393 clear();
1394}
1395
1396void RawPointerAxes::clear() {
1397 x.clear();
1398 y.clear();
1399 pressure.clear();
1400 touchMajor.clear();
1401 touchMinor.clear();
1402 toolMajor.clear();
1403 toolMinor.clear();
1404 orientation.clear();
1405 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001406 tiltX.clear();
1407 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001408 trackingId.clear();
1409 slot.clear();
1410}
1411
1412
1413// --- RawPointerData ---
1414
1415RawPointerData::RawPointerData() {
1416 clear();
1417}
1418
1419void RawPointerData::clear() {
1420 pointerCount = 0;
1421 clearIdBits();
1422}
1423
1424void RawPointerData::copyFrom(const RawPointerData& other) {
1425 pointerCount = other.pointerCount;
1426 hoveringIdBits = other.hoveringIdBits;
1427 touchingIdBits = other.touchingIdBits;
1428
1429 for (uint32_t i = 0; i < pointerCount; i++) {
1430 pointers[i] = other.pointers[i];
1431
1432 int id = pointers[i].id;
1433 idToIndex[id] = other.idToIndex[id];
1434 }
1435}
1436
1437void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1438 float x = 0, y = 0;
1439 uint32_t count = touchingIdBits.count();
1440 if (count) {
1441 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1442 uint32_t id = idBits.clearFirstMarkedBit();
1443 const Pointer& pointer = pointerForId(id);
1444 x += pointer.x;
1445 y += pointer.y;
1446 }
1447 x /= count;
1448 y /= count;
1449 }
1450 *outX = x;
1451 *outY = y;
1452}
1453
1454
1455// --- CookedPointerData ---
1456
1457CookedPointerData::CookedPointerData() {
1458 clear();
1459}
1460
1461void CookedPointerData::clear() {
1462 pointerCount = 0;
1463 hoveringIdBits.clear();
1464 touchingIdBits.clear();
1465}
1466
1467void CookedPointerData::copyFrom(const CookedPointerData& other) {
1468 pointerCount = other.pointerCount;
1469 hoveringIdBits = other.hoveringIdBits;
1470 touchingIdBits = other.touchingIdBits;
1471
1472 for (uint32_t i = 0; i < pointerCount; i++) {
1473 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1474 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1475
1476 int id = pointerProperties[i].id;
1477 idToIndex[id] = other.idToIndex[id];
1478 }
1479}
1480
1481
Jeff Brown49754db2011-07-01 17:37:58 -07001482// --- SingleTouchMotionAccumulator ---
1483
1484SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1485 clearAbsoluteAxes();
1486}
1487
Jeff Brown65fd2512011-08-18 11:20:58 -07001488void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1489 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1490 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1491 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1492 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1493 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1494 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1495 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1496}
1497
Jeff Brown49754db2011-07-01 17:37:58 -07001498void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1499 mAbsX = 0;
1500 mAbsY = 0;
1501 mAbsPressure = 0;
1502 mAbsToolWidth = 0;
1503 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001504 mAbsTiltX = 0;
1505 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001506}
1507
1508void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1509 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001510 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001511 case ABS_X:
1512 mAbsX = rawEvent->value;
1513 break;
1514 case ABS_Y:
1515 mAbsY = rawEvent->value;
1516 break;
1517 case ABS_PRESSURE:
1518 mAbsPressure = rawEvent->value;
1519 break;
1520 case ABS_TOOL_WIDTH:
1521 mAbsToolWidth = rawEvent->value;
1522 break;
1523 case ABS_DISTANCE:
1524 mAbsDistance = rawEvent->value;
1525 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001526 case ABS_TILT_X:
1527 mAbsTiltX = rawEvent->value;
1528 break;
1529 case ABS_TILT_Y:
1530 mAbsTiltY = rawEvent->value;
1531 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001532 }
1533 }
1534}
1535
1536
1537// --- MultiTouchMotionAccumulator ---
1538
1539MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001540 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1541 mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001542}
1543
1544MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1545 delete[] mSlots;
1546}
1547
Jeff Brown00710e92012-04-19 15:18:26 -07001548void MultiTouchMotionAccumulator::configure(InputDevice* device,
1549 size_t slotCount, bool usingSlotsProtocol) {
Jeff Brown49754db2011-07-01 17:37:58 -07001550 mSlotCount = slotCount;
1551 mUsingSlotsProtocol = usingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -07001552 mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Jeff Brown49754db2011-07-01 17:37:58 -07001553
1554 delete[] mSlots;
1555 mSlots = new Slot[slotCount];
1556}
1557
Jeff Brown65fd2512011-08-18 11:20:58 -07001558void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1559 // Unfortunately there is no way to read the initial contents of the slots.
1560 // So when we reset the accumulator, we must assume they are all zeroes.
1561 if (mUsingSlotsProtocol) {
1562 // Query the driver for the current slot index and use it as the initial slot
1563 // before we start reading events from the device. It is possible that the
1564 // current slot index will not be the same as it was when the first event was
1565 // written into the evdev buffer, which means the input mapper could start
1566 // out of sync with the initial state of the events in the evdev buffer.
1567 // In the extremely unlikely case that this happens, the data from
1568 // two slots will be confused until the next ABS_MT_SLOT event is received.
1569 // This can cause the touch point to "jump", but at least there will be
1570 // no stuck touches.
1571 int32_t initialSlot;
1572 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1573 ABS_MT_SLOT, &initialSlot);
1574 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001575 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001576 initialSlot = -1;
1577 }
1578 clearSlots(initialSlot);
1579 } else {
1580 clearSlots(-1);
1581 }
1582}
1583
Jeff Brown49754db2011-07-01 17:37:58 -07001584void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001585 if (mSlots) {
1586 for (size_t i = 0; i < mSlotCount; i++) {
1587 mSlots[i].clear();
1588 }
Jeff Brown49754db2011-07-01 17:37:58 -07001589 }
1590 mCurrentSlot = initialSlot;
1591}
1592
1593void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1594 if (rawEvent->type == EV_ABS) {
1595 bool newSlot = false;
1596 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001597 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001598 mCurrentSlot = rawEvent->value;
1599 newSlot = true;
1600 }
1601 } else if (mCurrentSlot < 0) {
1602 mCurrentSlot = 0;
1603 }
1604
1605 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1606#if DEBUG_POINTERS
1607 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001608 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001609 "should be between 0 and %d; ignoring this slot.",
1610 mCurrentSlot, mSlotCount - 1);
1611 }
1612#endif
1613 } else {
1614 Slot* slot = &mSlots[mCurrentSlot];
1615
Jeff Brown49ccac52012-04-11 18:27:33 -07001616 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001617 case ABS_MT_POSITION_X:
1618 slot->mInUse = true;
1619 slot->mAbsMTPositionX = rawEvent->value;
1620 break;
1621 case ABS_MT_POSITION_Y:
1622 slot->mInUse = true;
1623 slot->mAbsMTPositionY = rawEvent->value;
1624 break;
1625 case ABS_MT_TOUCH_MAJOR:
1626 slot->mInUse = true;
1627 slot->mAbsMTTouchMajor = rawEvent->value;
1628 break;
1629 case ABS_MT_TOUCH_MINOR:
1630 slot->mInUse = true;
1631 slot->mAbsMTTouchMinor = rawEvent->value;
1632 slot->mHaveAbsMTTouchMinor = true;
1633 break;
1634 case ABS_MT_WIDTH_MAJOR:
1635 slot->mInUse = true;
1636 slot->mAbsMTWidthMajor = rawEvent->value;
1637 break;
1638 case ABS_MT_WIDTH_MINOR:
1639 slot->mInUse = true;
1640 slot->mAbsMTWidthMinor = rawEvent->value;
1641 slot->mHaveAbsMTWidthMinor = true;
1642 break;
1643 case ABS_MT_ORIENTATION:
1644 slot->mInUse = true;
1645 slot->mAbsMTOrientation = rawEvent->value;
1646 break;
1647 case ABS_MT_TRACKING_ID:
1648 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001649 // The slot is no longer in use but it retains its previous contents,
1650 // which may be reused for subsequent touches.
1651 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001652 } else {
1653 slot->mInUse = true;
1654 slot->mAbsMTTrackingId = rawEvent->value;
1655 }
1656 break;
1657 case ABS_MT_PRESSURE:
1658 slot->mInUse = true;
1659 slot->mAbsMTPressure = rawEvent->value;
1660 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001661 case ABS_MT_DISTANCE:
1662 slot->mInUse = true;
1663 slot->mAbsMTDistance = rawEvent->value;
1664 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001665 case ABS_MT_TOOL_TYPE:
1666 slot->mInUse = true;
1667 slot->mAbsMTToolType = rawEvent->value;
1668 slot->mHaveAbsMTToolType = true;
1669 break;
1670 }
1671 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001672 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001673 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1674 mCurrentSlot += 1;
1675 }
1676}
1677
Jeff Brown65fd2512011-08-18 11:20:58 -07001678void MultiTouchMotionAccumulator::finishSync() {
1679 if (!mUsingSlotsProtocol) {
1680 clearSlots(-1);
1681 }
1682}
1683
Jeff Brown00710e92012-04-19 15:18:26 -07001684bool MultiTouchMotionAccumulator::hasStylus() const {
1685 return mHaveStylus;
1686}
1687
Jeff Brown49754db2011-07-01 17:37:58 -07001688
1689// --- MultiTouchMotionAccumulator::Slot ---
1690
1691MultiTouchMotionAccumulator::Slot::Slot() {
1692 clear();
1693}
1694
Jeff Brown49754db2011-07-01 17:37:58 -07001695void MultiTouchMotionAccumulator::Slot::clear() {
1696 mInUse = false;
1697 mHaveAbsMTTouchMinor = false;
1698 mHaveAbsMTWidthMinor = false;
1699 mHaveAbsMTToolType = false;
1700 mAbsMTPositionX = 0;
1701 mAbsMTPositionY = 0;
1702 mAbsMTTouchMajor = 0;
1703 mAbsMTTouchMinor = 0;
1704 mAbsMTWidthMajor = 0;
1705 mAbsMTWidthMinor = 0;
1706 mAbsMTOrientation = 0;
1707 mAbsMTTrackingId = -1;
1708 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001709 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001710 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001711}
1712
1713int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1714 if (mHaveAbsMTToolType) {
1715 switch (mAbsMTToolType) {
1716 case MT_TOOL_FINGER:
1717 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1718 case MT_TOOL_PEN:
1719 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1720 }
1721 }
1722 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1723}
1724
1725
Jeff Brown6d0fec22010-07-23 21:28:06 -07001726// --- InputMapper ---
1727
1728InputMapper::InputMapper(InputDevice* device) :
1729 mDevice(device), mContext(device->getContext()) {
1730}
1731
1732InputMapper::~InputMapper() {
1733}
1734
1735void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1736 info->addSource(getSources());
1737}
1738
Jeff Brownef3d7e82010-09-30 14:33:04 -07001739void InputMapper::dump(String8& dump) {
1740}
1741
Jeff Brown65fd2512011-08-18 11:20:58 -07001742void InputMapper::configure(nsecs_t when,
1743 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001744}
1745
Jeff Brown65fd2512011-08-18 11:20:58 -07001746void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001747}
1748
Jeff Brownaa3855d2011-03-17 01:34:19 -07001749void InputMapper::timeoutExpired(nsecs_t when) {
1750}
1751
Jeff Brown6d0fec22010-07-23 21:28:06 -07001752int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1753 return AKEY_STATE_UNKNOWN;
1754}
1755
1756int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1757 return AKEY_STATE_UNKNOWN;
1758}
1759
1760int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1761 return AKEY_STATE_UNKNOWN;
1762}
1763
1764bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1765 const int32_t* keyCodes, uint8_t* outFlags) {
1766 return false;
1767}
1768
Jeff Browna47425a2012-04-13 04:09:27 -07001769void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1770 int32_t token) {
1771}
1772
1773void InputMapper::cancelVibrate(int32_t token) {
1774}
1775
Jeff Brown6d0fec22010-07-23 21:28:06 -07001776int32_t InputMapper::getMetaState() {
1777 return 0;
1778}
1779
Jeff Brown05dc66a2011-03-02 14:41:58 -08001780void InputMapper::fadePointer() {
1781}
1782
Jeff Brownbe1aa822011-07-27 16:04:54 -07001783status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1784 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1785}
1786
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001787void InputMapper::bumpGeneration() {
1788 mDevice->bumpGeneration();
1789}
1790
Jeff Browncb1404e2011-01-15 18:14:15 -08001791void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1792 const RawAbsoluteAxisInfo& axis, const char* name) {
1793 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001794 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1795 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001796 } else {
1797 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1798 }
1799}
1800
Jeff Brown6d0fec22010-07-23 21:28:06 -07001801
1802// --- SwitchInputMapper ---
1803
1804SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
Jeff Brownbcc046a2012-09-27 20:46:43 -07001805 InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001806}
1807
1808SwitchInputMapper::~SwitchInputMapper() {
1809}
1810
1811uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001812 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001813}
1814
1815void SwitchInputMapper::process(const RawEvent* rawEvent) {
1816 switch (rawEvent->type) {
1817 case EV_SW:
Jeff Brownbcc046a2012-09-27 20:46:43 -07001818 processSwitch(rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001819 break;
Jeff Brownbcc046a2012-09-27 20:46:43 -07001820
1821 case EV_SYN:
1822 if (rawEvent->code == SYN_REPORT) {
1823 sync(rawEvent->when);
1824 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001825 }
1826}
1827
Jeff Brownbcc046a2012-09-27 20:46:43 -07001828void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
1829 if (switchCode >= 0 && switchCode < 32) {
1830 if (switchValue) {
1831 mUpdatedSwitchValues |= 1 << switchCode;
1832 }
1833 mUpdatedSwitchMask |= 1 << switchCode;
1834 }
1835}
1836
1837void SwitchInputMapper::sync(nsecs_t when) {
1838 if (mUpdatedSwitchMask) {
1839 NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask);
1840 getListener()->notifySwitch(&args);
1841
1842 mUpdatedSwitchValues = 0;
1843 mUpdatedSwitchMask = 0;
1844 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001845}
1846
1847int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1848 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1849}
1850
1851
Jeff Browna47425a2012-04-13 04:09:27 -07001852// --- VibratorInputMapper ---
1853
1854VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1855 InputMapper(device), mVibrating(false) {
1856}
1857
1858VibratorInputMapper::~VibratorInputMapper() {
1859}
1860
1861uint32_t VibratorInputMapper::getSources() {
1862 return 0;
1863}
1864
1865void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1866 InputMapper::populateDeviceInfo(info);
1867
1868 info->setVibrator(true);
1869}
1870
1871void VibratorInputMapper::process(const RawEvent* rawEvent) {
1872 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1873}
1874
1875void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1876 int32_t token) {
1877#if DEBUG_VIBRATOR
1878 String8 patternStr;
1879 for (size_t i = 0; i < patternSize; i++) {
1880 if (i != 0) {
1881 patternStr.append(", ");
1882 }
1883 patternStr.appendFormat("%lld", pattern[i]);
1884 }
1885 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1886 getDeviceId(), patternStr.string(), repeat, token);
1887#endif
1888
1889 mVibrating = true;
1890 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1891 mPatternSize = patternSize;
1892 mRepeat = repeat;
1893 mToken = token;
1894 mIndex = -1;
1895
1896 nextStep();
1897}
1898
1899void VibratorInputMapper::cancelVibrate(int32_t token) {
1900#if DEBUG_VIBRATOR
1901 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1902#endif
1903
1904 if (mVibrating && mToken == token) {
1905 stopVibrating();
1906 }
1907}
1908
1909void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1910 if (mVibrating) {
1911 if (when >= mNextStepTime) {
1912 nextStep();
1913 } else {
1914 getContext()->requestTimeoutAtTime(mNextStepTime);
1915 }
1916 }
1917}
1918
1919void VibratorInputMapper::nextStep() {
1920 mIndex += 1;
1921 if (size_t(mIndex) >= mPatternSize) {
1922 if (mRepeat < 0) {
1923 // We are done.
1924 stopVibrating();
1925 return;
1926 }
1927 mIndex = mRepeat;
1928 }
1929
1930 bool vibratorOn = mIndex & 1;
1931 nsecs_t duration = mPattern[mIndex];
1932 if (vibratorOn) {
1933#if DEBUG_VIBRATOR
1934 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1935 getDeviceId(), duration);
1936#endif
1937 getEventHub()->vibrate(getDeviceId(), duration);
1938 } else {
1939#if DEBUG_VIBRATOR
1940 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1941#endif
1942 getEventHub()->cancelVibrate(getDeviceId());
1943 }
1944 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1945 mNextStepTime = now + duration;
1946 getContext()->requestTimeoutAtTime(mNextStepTime);
1947#if DEBUG_VIBRATOR
1948 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1949#endif
1950}
1951
1952void VibratorInputMapper::stopVibrating() {
1953 mVibrating = false;
1954#if DEBUG_VIBRATOR
1955 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1956#endif
1957 getEventHub()->cancelVibrate(getDeviceId());
1958}
1959
1960void VibratorInputMapper::dump(String8& dump) {
1961 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1962 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1963}
1964
1965
Jeff Brown6d0fec22010-07-23 21:28:06 -07001966// --- KeyboardInputMapper ---
1967
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001968KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001969 uint32_t source, int32_t keyboardType) :
1970 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001971 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001972}
1973
1974KeyboardInputMapper::~KeyboardInputMapper() {
1975}
1976
Jeff Brown6d0fec22010-07-23 21:28:06 -07001977uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001978 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001979}
1980
1981void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1982 InputMapper::populateDeviceInfo(info);
1983
1984 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001985 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001986}
1987
Jeff Brownef3d7e82010-09-30 14:33:04 -07001988void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001989 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1990 dumpParameters(dump);
1991 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001992 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001993 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1994 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1995 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001996}
1997
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001998
Jeff Brown65fd2512011-08-18 11:20:58 -07001999void KeyboardInputMapper::configure(nsecs_t when,
2000 const InputReaderConfiguration* config, uint32_t changes) {
2001 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002002
Jeff Brown474dcb52011-06-14 20:22:50 -07002003 if (!changes) { // first time only
2004 // Configure basic parameters.
2005 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002006 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002007
Jeff Brown65fd2512011-08-18 11:20:58 -07002008 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002009 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2010 DisplayViewport v;
2011 if (config->getDisplayInfo(false /*external*/, &v)) {
2012 mOrientation = v.orientation;
2013 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002014 mOrientation = DISPLAY_ORIENTATION_0;
2015 }
2016 } else {
2017 mOrientation = DISPLAY_ORIENTATION_0;
2018 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002019 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002020}
2021
2022void KeyboardInputMapper::configureParameters() {
2023 mParameters.orientationAware = false;
2024 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2025 mParameters.orientationAware);
2026
Jeff Brownd728bf52012-09-08 18:05:28 -07002027 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002028 if (mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002029 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002030 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002031}
2032
2033void KeyboardInputMapper::dumpParameters(String8& dump) {
2034 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002035 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2036 toString(mParameters.hasAssociatedDisplay));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002037 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2038 toString(mParameters.orientationAware));
2039}
2040
Jeff Brown65fd2512011-08-18 11:20:58 -07002041void KeyboardInputMapper::reset(nsecs_t when) {
2042 mMetaState = AMETA_NONE;
2043 mDownTime = 0;
2044 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002045 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002046
Jeff Brownbe1aa822011-07-27 16:04:54 -07002047 resetLedState();
2048
Jeff Brown65fd2512011-08-18 11:20:58 -07002049 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002050}
2051
2052void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2053 switch (rawEvent->type) {
2054 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002055 int32_t scanCode = rawEvent->code;
2056 int32_t usageCode = mCurrentHidUsage;
2057 mCurrentHidUsage = 0;
2058
Jeff Brown6d0fec22010-07-23 21:28:06 -07002059 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002060 int32_t keyCode;
2061 uint32_t flags;
2062 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2063 keyCode = AKEYCODE_UNKNOWN;
2064 flags = 0;
2065 }
2066 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002067 }
2068 break;
2069 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002070 case EV_MSC: {
2071 if (rawEvent->code == MSC_SCAN) {
2072 mCurrentHidUsage = rawEvent->value;
2073 }
2074 break;
2075 }
2076 case EV_SYN: {
2077 if (rawEvent->code == SYN_REPORT) {
2078 mCurrentHidUsage = 0;
2079 }
2080 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002081 }
2082}
2083
2084bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2085 return scanCode < BTN_MOUSE
2086 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002087 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002088 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002089}
2090
Jeff Brown6328cdc2010-07-29 18:18:33 -07002091void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2092 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002093
Jeff Brownbe1aa822011-07-27 16:04:54 -07002094 if (down) {
2095 // Rotate key codes according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002096 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002097 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002098 }
Jeff Brownfe508922011-01-18 15:10:10 -08002099
Jeff Brownbe1aa822011-07-27 16:04:54 -07002100 // Add key down.
2101 ssize_t keyDownIndex = findKeyDown(scanCode);
2102 if (keyDownIndex >= 0) {
2103 // key repeat, be sure to use same keycode as before in case of rotation
2104 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002105 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002106 // key down
2107 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2108 && mContext->shouldDropVirtualKey(when,
2109 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002110 return;
2111 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002112
2113 mKeyDowns.push();
2114 KeyDown& keyDown = mKeyDowns.editTop();
2115 keyDown.keyCode = keyCode;
2116 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002117 }
2118
Jeff Brownbe1aa822011-07-27 16:04:54 -07002119 mDownTime = when;
2120 } else {
2121 // Remove key down.
2122 ssize_t keyDownIndex = findKeyDown(scanCode);
2123 if (keyDownIndex >= 0) {
2124 // key up, be sure to use same keycode as before in case of rotation
2125 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2126 mKeyDowns.removeAt(size_t(keyDownIndex));
2127 } else {
2128 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002129 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002130 "keyCode=%d, scanCode=%d",
2131 getDeviceName().string(), keyCode, scanCode);
2132 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002133 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002134 }
Jeff Brownfd0358292010-06-30 16:10:35 -07002135
Jeff Brownbe1aa822011-07-27 16:04:54 -07002136 bool metaStateChanged = false;
2137 int32_t oldMetaState = mMetaState;
2138 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2139 if (oldMetaState != newMetaState) {
2140 mMetaState = newMetaState;
2141 metaStateChanged = true;
2142 updateLedState(false);
2143 }
2144
2145 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002146
Jeff Brown56194eb2011-03-02 19:23:13 -08002147 // Key down on external an keyboard should wake the device.
2148 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2149 // For internal keyboards, the key layout file should specify the policy flags for
2150 // each wake key individually.
2151 // TODO: Use the input device configuration to control this behavior more finely.
2152 if (down && getDevice()->isExternal()
2153 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2154 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2155 }
2156
Jeff Brown6328cdc2010-07-29 18:18:33 -07002157 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002158 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002159 }
2160
Jeff Brown05dc66a2011-03-02 14:41:58 -08002161 if (down && !isMetaKey(keyCode)) {
2162 getContext()->fadePointer();
2163 }
2164
Jeff Brownbe1aa822011-07-27 16:04:54 -07002165 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002166 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2167 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002168 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002169}
2170
Jeff Brownbe1aa822011-07-27 16:04:54 -07002171ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2172 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002173 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002174 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002175 return i;
2176 }
2177 }
2178 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002179}
2180
Jeff Brown6d0fec22010-07-23 21:28:06 -07002181int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2182 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2183}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002184
Jeff Brown6d0fec22010-07-23 21:28:06 -07002185int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2186 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2187}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002188
Jeff Brown6d0fec22010-07-23 21:28:06 -07002189bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2190 const int32_t* keyCodes, uint8_t* outFlags) {
2191 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2192}
2193
2194int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002195 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002196}
2197
Jeff Brownbe1aa822011-07-27 16:04:54 -07002198void KeyboardInputMapper::resetLedState() {
2199 initializeLedState(mCapsLockLedState, LED_CAPSL);
2200 initializeLedState(mNumLockLedState, LED_NUML);
2201 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002202
Jeff Brownbe1aa822011-07-27 16:04:54 -07002203 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002204}
2205
Jeff Brownbe1aa822011-07-27 16:04:54 -07002206void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002207 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2208 ledState.on = false;
2209}
2210
Jeff Brownbe1aa822011-07-27 16:04:54 -07002211void KeyboardInputMapper::updateLedState(bool reset) {
2212 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002213 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002215 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002217 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002218}
2219
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002221 int32_t led, int32_t modifier, bool reset) {
2222 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002223 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002224 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002225 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2226 ledState.on = desiredState;
2227 }
2228 }
2229}
2230
Jeff Brown6d0fec22010-07-23 21:28:06 -07002231
Jeff Brown83c09682010-12-23 17:50:18 -08002232// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002233
Jeff Brown83c09682010-12-23 17:50:18 -08002234CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002235 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002236}
2237
Jeff Brown83c09682010-12-23 17:50:18 -08002238CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002239}
2240
Jeff Brown83c09682010-12-23 17:50:18 -08002241uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002242 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002243}
2244
Jeff Brown83c09682010-12-23 17:50:18 -08002245void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002246 InputMapper::populateDeviceInfo(info);
2247
Jeff Brown83c09682010-12-23 17:50:18 -08002248 if (mParameters.mode == Parameters::MODE_POINTER) {
2249 float minX, minY, maxX, maxY;
2250 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002251 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f, 0.0f);
2252 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002253 }
2254 } else {
Michael Wrightc6091c62013-04-01 20:56:04 -07002255 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale, 0.0f);
2256 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002257 }
Michael Wrightc6091c62013-04-01 20:56:04 -07002258 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002259
Jeff Brown65fd2512011-08-18 11:20:58 -07002260 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002261 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002262 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002263 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002264 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002265 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002266}
2267
Jeff Brown83c09682010-12-23 17:50:18 -08002268void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002269 dump.append(INDENT2 "Cursor Input Mapper:\n");
2270 dumpParameters(dump);
2271 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2272 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2273 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2274 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2275 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002276 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002277 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002278 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002279 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2280 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002281 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002282 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2283 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2284 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002285}
2286
Jeff Brown65fd2512011-08-18 11:20:58 -07002287void CursorInputMapper::configure(nsecs_t when,
2288 const InputReaderConfiguration* config, uint32_t changes) {
2289 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002290
Jeff Brown474dcb52011-06-14 20:22:50 -07002291 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002292 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002293
Jeff Brown474dcb52011-06-14 20:22:50 -07002294 // Configure basic parameters.
2295 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002296
Jeff Brown474dcb52011-06-14 20:22:50 -07002297 // Configure device mode.
2298 switch (mParameters.mode) {
2299 case Parameters::MODE_POINTER:
2300 mSource = AINPUT_SOURCE_MOUSE;
2301 mXPrecision = 1.0f;
2302 mYPrecision = 1.0f;
2303 mXScale = 1.0f;
2304 mYScale = 1.0f;
2305 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2306 break;
2307 case Parameters::MODE_NAVIGATION:
2308 mSource = AINPUT_SOURCE_TRACKBALL;
2309 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2310 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2311 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2312 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2313 break;
2314 }
2315
2316 mVWheelScale = 1.0f;
2317 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002318 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002319
Jeff Brown474dcb52011-06-14 20:22:50 -07002320 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2321 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2322 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2323 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2324 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002325
2326 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002327 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2328 DisplayViewport v;
2329 if (config->getDisplayInfo(false /*external*/, &v)) {
2330 mOrientation = v.orientation;
2331 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002332 mOrientation = DISPLAY_ORIENTATION_0;
2333 }
2334 } else {
2335 mOrientation = DISPLAY_ORIENTATION_0;
2336 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002337 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002338 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002339}
2340
Jeff Brown83c09682010-12-23 17:50:18 -08002341void CursorInputMapper::configureParameters() {
2342 mParameters.mode = Parameters::MODE_POINTER;
2343 String8 cursorModeString;
2344 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2345 if (cursorModeString == "navigation") {
2346 mParameters.mode = Parameters::MODE_NAVIGATION;
2347 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002348 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002349 }
2350 }
2351
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002352 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002353 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002354 mParameters.orientationAware);
2355
Jeff Brownd728bf52012-09-08 18:05:28 -07002356 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002357 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002358 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002359 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002360}
2361
Jeff Brown83c09682010-12-23 17:50:18 -08002362void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002363 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002364 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2365 toString(mParameters.hasAssociatedDisplay));
Jeff Brown83c09682010-12-23 17:50:18 -08002366
2367 switch (mParameters.mode) {
2368 case Parameters::MODE_POINTER:
2369 dump.append(INDENT4 "Mode: pointer\n");
2370 break;
2371 case Parameters::MODE_NAVIGATION:
2372 dump.append(INDENT4 "Mode: navigation\n");
2373 break;
2374 default:
Steve Blockec193de2012-01-09 18:35:44 +00002375 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002376 }
2377
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002378 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2379 toString(mParameters.orientationAware));
2380}
2381
Jeff Brown65fd2512011-08-18 11:20:58 -07002382void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002383 mButtonState = 0;
2384 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002385
Jeff Brownbe1aa822011-07-27 16:04:54 -07002386 mPointerVelocityControl.reset();
2387 mWheelXVelocityControl.reset();
2388 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002389
Jeff Brown65fd2512011-08-18 11:20:58 -07002390 mCursorButtonAccumulator.reset(getDevice());
2391 mCursorMotionAccumulator.reset(getDevice());
2392 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002393
Jeff Brown65fd2512011-08-18 11:20:58 -07002394 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002395}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002396
Jeff Brown83c09682010-12-23 17:50:18 -08002397void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002398 mCursorButtonAccumulator.process(rawEvent);
2399 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002400 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002401
Jeff Brown49ccac52012-04-11 18:27:33 -07002402 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002403 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002404 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002405}
2406
Jeff Brown83c09682010-12-23 17:50:18 -08002407void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002408 int32_t lastButtonState = mButtonState;
2409 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2410 mButtonState = currentButtonState;
2411
2412 bool wasDown = isPointerDown(lastButtonState);
2413 bool down = isPointerDown(currentButtonState);
2414 bool downChanged;
2415 if (!wasDown && down) {
2416 mDownTime = when;
2417 downChanged = true;
2418 } else if (wasDown && !down) {
2419 downChanged = true;
2420 } else {
2421 downChanged = false;
2422 }
2423 nsecs_t downTime = mDownTime;
2424 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002425 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002426
2427 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2428 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2429 bool moved = deltaX != 0 || deltaY != 0;
2430
Jeff Brown65fd2512011-08-18 11:20:58 -07002431 // Rotate delta according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002432 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay
Jeff Brownbe1aa822011-07-27 16:04:54 -07002433 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002434 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002435 }
2436
Jeff Brown65fd2512011-08-18 11:20:58 -07002437 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002438 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002439 pointerProperties.clear();
2440 pointerProperties.id = 0;
2441 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2442
Jeff Brown6328cdc2010-07-29 18:18:33 -07002443 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002444 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002445
Jeff Brown65fd2512011-08-18 11:20:58 -07002446 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2447 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002449
Jeff Brownbe1aa822011-07-27 16:04:54 -07002450 mWheelYVelocityControl.move(when, NULL, &vscroll);
2451 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002452
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002454
Jeff Brown83d616a2012-09-09 20:33:43 -07002455 int32_t displayId;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002456 if (mPointerController != NULL) {
2457 if (moved || scrolled || buttonsChanged) {
2458 mPointerController->setPresentation(
2459 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002460
Jeff Brownbe1aa822011-07-27 16:04:54 -07002461 if (moved) {
2462 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002463 }
2464
Jeff Brownbe1aa822011-07-27 16:04:54 -07002465 if (buttonsChanged) {
2466 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002467 }
Jeff Brownefd32662011-03-08 15:13:06 -08002468
Jeff Brownbe1aa822011-07-27 16:04:54 -07002469 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002470 }
2471
Jeff Brownbe1aa822011-07-27 16:04:54 -07002472 float x, y;
2473 mPointerController->getPosition(&x, &y);
2474 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2475 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83d616a2012-09-09 20:33:43 -07002476 displayId = ADISPLAY_ID_DEFAULT;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002477 } else {
2478 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2479 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83d616a2012-09-09 20:33:43 -07002480 displayId = ADISPLAY_ID_NONE;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002481 }
2482
2483 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002484
Jeff Brown56194eb2011-03-02 19:23:13 -08002485 // Moving an external trackball or mouse should wake the device.
2486 // We don't do this for internal cursor devices to prevent them from waking up
2487 // the device in your pocket.
2488 // TODO: Use the input device configuration to control this behavior more finely.
2489 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002490 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002491 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2492 }
2493
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002494 // Synthesize key down from buttons if needed.
2495 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2496 policyFlags, lastButtonState, currentButtonState);
2497
2498 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002499 if (downChanged || moved || scrolled || buttonsChanged) {
2500 int32_t metaState = mContext->getGlobalMetaState();
2501 int32_t motionEventAction;
2502 if (downChanged) {
2503 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2504 } else if (down || mPointerController == NULL) {
2505 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2506 } else {
2507 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2508 }
Jeff Brownb6997262010-10-08 22:31:17 -07002509
Jeff Brownbe1aa822011-07-27 16:04:54 -07002510 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2511 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07002512 displayId, 1, &pointerProperties, &pointerCoords,
2513 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002514 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002515
Jeff Brownbe1aa822011-07-27 16:04:54 -07002516 // Send hover move after UP to tell the application that the mouse is hovering now.
2517 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2518 && mPointerController != NULL) {
2519 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2520 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2521 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002522 displayId, 1, &pointerProperties, &pointerCoords,
2523 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002524 getListener()->notifyMotion(&hoverArgs);
2525 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002526
Jeff Brownbe1aa822011-07-27 16:04:54 -07002527 // Send scroll events.
2528 if (scrolled) {
2529 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2530 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2531
2532 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2533 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2534 AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002535 displayId, 1, &pointerProperties, &pointerCoords,
2536 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002537 getListener()->notifyMotion(&scrollArgs);
2538 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002539 }
Jeff Browna032cc02011-03-07 16:56:21 -08002540
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002541 // Synthesize key up from buttons if needed.
2542 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2543 policyFlags, lastButtonState, currentButtonState);
2544
Jeff Brown65fd2512011-08-18 11:20:58 -07002545 mCursorMotionAccumulator.finishSync();
2546 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002547}
2548
Jeff Brown83c09682010-12-23 17:50:18 -08002549int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002550 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2551 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2552 } else {
2553 return AKEY_STATE_UNKNOWN;
2554 }
2555}
2556
Jeff Brown05dc66a2011-03-02 14:41:58 -08002557void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002558 if (mPointerController != NULL) {
2559 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2560 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002561}
2562
Jeff Brown6d0fec22010-07-23 21:28:06 -07002563
2564// --- TouchInputMapper ---
2565
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002566TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002567 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002568 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brown83d616a2012-09-09 20:33:43 -07002569 mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0),
2570 mSurfaceOrientation(DISPLAY_ORIENTATION_0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002571}
2572
2573TouchInputMapper::~TouchInputMapper() {
2574}
2575
2576uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002577 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002578}
2579
2580void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2581 InputMapper::populateDeviceInfo(info);
2582
Jeff Brown65fd2512011-08-18 11:20:58 -07002583 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2584 info->addMotionRange(mOrientedRanges.x);
2585 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002586 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002587
Jeff Brown65fd2512011-08-18 11:20:58 -07002588 if (mOrientedRanges.haveSize) {
2589 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002590 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002591
2592 if (mOrientedRanges.haveTouchSize) {
2593 info->addMotionRange(mOrientedRanges.touchMajor);
2594 info->addMotionRange(mOrientedRanges.touchMinor);
2595 }
2596
2597 if (mOrientedRanges.haveToolSize) {
2598 info->addMotionRange(mOrientedRanges.toolMajor);
2599 info->addMotionRange(mOrientedRanges.toolMinor);
2600 }
2601
2602 if (mOrientedRanges.haveOrientation) {
2603 info->addMotionRange(mOrientedRanges.orientation);
2604 }
2605
2606 if (mOrientedRanges.haveDistance) {
2607 info->addMotionRange(mOrientedRanges.distance);
2608 }
2609
2610 if (mOrientedRanges.haveTilt) {
2611 info->addMotionRange(mOrientedRanges.tilt);
2612 }
2613
2614 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002615 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
2616 0.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07002617 }
2618 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002619 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
2620 0.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07002621 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002622 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002623}
2624
Jeff Brownef3d7e82010-09-30 14:33:04 -07002625void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002626 dump.append(INDENT2 "Touch Input Mapper:\n");
2627 dumpParameters(dump);
2628 dumpVirtualKeys(dump);
2629 dumpRawPointerAxes(dump);
2630 dumpCalibration(dump);
2631 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002632
Jeff Brownbe1aa822011-07-27 16:04:54 -07002633 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown83d616a2012-09-09 20:33:43 -07002634 dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate);
2635 dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002636 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2637 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2638 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2639 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2640 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002641 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2642 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2643 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2644 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002645 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2646 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2647 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2648 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2649 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002650
Jeff Brownbe1aa822011-07-27 16:04:54 -07002651 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002652
Jeff Brownbe1aa822011-07-27 16:04:54 -07002653 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2654 mLastRawPointerData.pointerCount);
2655 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2656 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2657 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2658 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002659 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2660 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002661 pointer.id, pointer.x, pointer.y, pointer.pressure,
2662 pointer.touchMajor, pointer.touchMinor,
2663 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002664 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002665 pointer.toolType, toString(pointer.isHovering));
2666 }
2667
2668 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2669 mLastCookedPointerData.pointerCount);
2670 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2671 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2672 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2673 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2674 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002675 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2676 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002677 pointerProperties.id,
2678 pointerCoords.getX(),
2679 pointerCoords.getY(),
2680 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2681 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2682 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2683 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2684 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2685 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002686 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002687 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2688 pointerProperties.toolType,
2689 toString(mLastCookedPointerData.isHovering(i)));
2690 }
2691
Jeff Brown65fd2512011-08-18 11:20:58 -07002692 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002693 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2694 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002695 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002696 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002697 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002698 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002699 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002700 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002701 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002702 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2703 mPointerGestureMaxSwipeWidth);
Jeff Brown4dac9012013-04-10 01:03:19 -07002704 } else if (mDeviceMode == DEVICE_MODE_NAVIGATION) {
2705 dump.appendFormat(INDENT3 "Navigation Gesture Detector:\n");
2706 dump.appendFormat(INDENT4 "AssistStartY: %0.3f\n",
2707 mNavigationAssistStartY);
2708 dump.appendFormat(INDENT4 "AssistEndY: %0.3f\n",
2709 mNavigationAssistEndY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002710 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002711}
2712
Jeff Brown65fd2512011-08-18 11:20:58 -07002713void TouchInputMapper::configure(nsecs_t when,
2714 const InputReaderConfiguration* config, uint32_t changes) {
2715 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002716
Jeff Brown474dcb52011-06-14 20:22:50 -07002717 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002718
Jeff Brown474dcb52011-06-14 20:22:50 -07002719 if (!changes) { // first time only
2720 // Configure basic parameters.
2721 configureParameters();
2722
Jeff Brown65fd2512011-08-18 11:20:58 -07002723 // Configure common accumulators.
2724 mCursorScrollAccumulator.configure(getDevice());
2725 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002726
2727 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002728 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002729
2730 // Prepare input device calibration.
2731 parseCalibration();
2732 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002733 }
2734
Jeff Brown474dcb52011-06-14 20:22:50 -07002735 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002736 // Update pointer speed.
2737 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2738 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2739 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002740 }
Jeff Brown8d608662010-08-30 03:02:23 -07002741
Jeff Brown65fd2512011-08-18 11:20:58 -07002742 bool resetNeeded = false;
2743 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002744 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2745 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002746 // Configure device sources, surface dimensions, orientation and
2747 // scaling factors.
2748 configureSurface(when, &resetNeeded);
2749 }
2750
2751 if (changes && resetNeeded) {
2752 // Send reset, unless this is the first time the device has been configured,
2753 // in which case the reader will call reset itself after all mappers are ready.
2754 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002755 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002756}
2757
Jeff Brown8d608662010-08-30 03:02:23 -07002758void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002759 // Use the pointer presentation mode for devices that do not support distinct
2760 // multitouch. The spot-based presentation relies on being able to accurately
2761 // locate two or more fingers on the touch pad.
2762 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2763 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002764
Jeff Brown538881e2011-05-25 18:23:38 -07002765 String8 gestureModeString;
2766 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2767 gestureModeString)) {
2768 if (gestureModeString == "pointer") {
2769 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2770 } else if (gestureModeString == "spots") {
2771 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2772 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002773 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002774 }
2775 }
2776
Jeff Browndeffe072011-08-26 18:38:46 -07002777 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2778 // The device is a touch screen.
2779 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2780 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2781 // The device is a pointing device like a track pad.
2782 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2783 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002784 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2785 // The device is a cursor device with a touch pad attached.
2786 // By default don't use the touch pad to move the pointer.
2787 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2788 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002789 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002790 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2791 }
2792
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002793 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002794 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2795 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002796 if (deviceTypeString == "touchScreen") {
2797 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002798 } else if (deviceTypeString == "touchPad") {
2799 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002800 } else if (deviceTypeString == "touchNavigation") {
2801 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION;
Jeff Brownace13b12011-03-09 17:39:48 -08002802 } else if (deviceTypeString == "pointer") {
2803 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002804 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002805 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002806 }
2807 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002808
Jeff Brownefd32662011-03-08 15:13:06 -08002809 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002810 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2811 mParameters.orientationAware);
2812
Jeff Brownd728bf52012-09-08 18:05:28 -07002813 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002814 mParameters.associatedDisplayIsExternal = false;
2815 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002816 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002817 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002818 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002819 mParameters.associatedDisplayIsExternal =
2820 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2821 && getDevice()->isExternal();
Jeff Brownbc68a592011-07-25 12:58:12 -07002822 }
Jeff Brown8d608662010-08-30 03:02:23 -07002823}
2824
Jeff Brownef3d7e82010-09-30 14:33:04 -07002825void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002826 dump.append(INDENT3 "Parameters:\n");
2827
Jeff Brown538881e2011-05-25 18:23:38 -07002828 switch (mParameters.gestureMode) {
2829 case Parameters::GESTURE_MODE_POINTER:
2830 dump.append(INDENT4 "GestureMode: pointer\n");
2831 break;
2832 case Parameters::GESTURE_MODE_SPOTS:
2833 dump.append(INDENT4 "GestureMode: spots\n");
2834 break;
2835 default:
2836 assert(false);
2837 }
2838
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002839 switch (mParameters.deviceType) {
2840 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2841 dump.append(INDENT4 "DeviceType: touchScreen\n");
2842 break;
2843 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2844 dump.append(INDENT4 "DeviceType: touchPad\n");
2845 break;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002846 case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION:
2847 dump.append(INDENT4 "DeviceType: touchNavigation\n");
2848 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002849 case Parameters::DEVICE_TYPE_POINTER:
2850 dump.append(INDENT4 "DeviceType: pointer\n");
2851 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002852 default:
Steve Blockec193de2012-01-09 18:35:44 +00002853 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002854 }
2855
Jeff Brown83d616a2012-09-09 20:33:43 -07002856 dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n",
Jeff Brownd728bf52012-09-08 18:05:28 -07002857 toString(mParameters.hasAssociatedDisplay),
2858 toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002859 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2860 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002861}
2862
Jeff Brownbe1aa822011-07-27 16:04:54 -07002863void TouchInputMapper::configureRawPointerAxes() {
2864 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002865}
2866
Jeff Brownbe1aa822011-07-27 16:04:54 -07002867void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2868 dump.append(INDENT3 "Raw Touch Axes:\n");
2869 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2870 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2871 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2872 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2873 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2874 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2875 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2876 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2877 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002878 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2879 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002880 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2881 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002882}
2883
Jeff Brown65fd2512011-08-18 11:20:58 -07002884void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2885 int32_t oldDeviceMode = mDeviceMode;
2886
2887 // Determine device mode.
2888 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2889 && mConfig.pointerGesturesEnabled) {
2890 mSource = AINPUT_SOURCE_MOUSE;
2891 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002892 if (hasStylus()) {
2893 mSource |= AINPUT_SOURCE_STYLUS;
2894 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002895 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownd728bf52012-09-08 18:05:28 -07002896 && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002897 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2898 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002899 if (hasStylus()) {
2900 mSource |= AINPUT_SOURCE_STYLUS;
2901 }
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002902 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) {
2903 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Jeff Brown4dac9012013-04-10 01:03:19 -07002904 mDeviceMode = DEVICE_MODE_NAVIGATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002905 } else {
2906 mSource = AINPUT_SOURCE_TOUCHPAD;
2907 mDeviceMode = DEVICE_MODE_UNSCALED;
2908 }
2909
Jeff Brown9626b142011-03-03 02:09:54 -08002910 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002911 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002912 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002913 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002914 mDeviceMode = DEVICE_MODE_DISABLED;
2915 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002916 }
2917
Jeff Brown83d616a2012-09-09 20:33:43 -07002918 // Raw width and height in the natural orientation.
2919 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2920 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2921
Jeff Brown65fd2512011-08-18 11:20:58 -07002922 // Get associated display dimensions.
Jeff Brown83d616a2012-09-09 20:33:43 -07002923 bool viewportChanged = false;
2924 DisplayViewport newViewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07002925 if (mParameters.hasAssociatedDisplay) {
Jeff Brown83d616a2012-09-09 20:33:43 -07002926 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002927 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brownd728bf52012-09-08 18:05:28 -07002928 "display. The device will be inoperable until the display size "
Jeff Brown65fd2512011-08-18 11:20:58 -07002929 "becomes available.",
Jeff Brownd728bf52012-09-08 18:05:28 -07002930 getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002931 mDeviceMode = DEVICE_MODE_DISABLED;
2932 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002933 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002934 } else {
Jeff Brown83d616a2012-09-09 20:33:43 -07002935 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
2936 }
2937 if (mViewport != newViewport) {
2938 mViewport = newViewport;
2939 viewportChanged = true;
2940
2941 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2942 // Convert rotated viewport to natural surface coordinates.
2943 int32_t naturalLogicalWidth, naturalLogicalHeight;
2944 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
2945 int32_t naturalPhysicalLeft, naturalPhysicalTop;
2946 int32_t naturalDeviceWidth, naturalDeviceHeight;
2947 switch (mViewport.orientation) {
2948 case DISPLAY_ORIENTATION_90:
2949 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2950 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2951 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2952 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2953 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
2954 naturalPhysicalTop = mViewport.physicalLeft;
2955 naturalDeviceWidth = mViewport.deviceHeight;
2956 naturalDeviceHeight = mViewport.deviceWidth;
2957 break;
2958 case DISPLAY_ORIENTATION_180:
2959 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2960 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2961 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2962 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2963 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
2964 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
2965 naturalDeviceWidth = mViewport.deviceWidth;
2966 naturalDeviceHeight = mViewport.deviceHeight;
2967 break;
2968 case DISPLAY_ORIENTATION_270:
2969 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2970 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2971 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2972 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2973 naturalPhysicalLeft = mViewport.physicalTop;
2974 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
2975 naturalDeviceWidth = mViewport.deviceHeight;
2976 naturalDeviceHeight = mViewport.deviceWidth;
2977 break;
2978 case DISPLAY_ORIENTATION_0:
2979 default:
2980 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2981 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2982 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2983 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2984 naturalPhysicalLeft = mViewport.physicalLeft;
2985 naturalPhysicalTop = mViewport.physicalTop;
2986 naturalDeviceWidth = mViewport.deviceWidth;
2987 naturalDeviceHeight = mViewport.deviceHeight;
2988 break;
2989 }
2990
2991 mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
2992 mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
2993 mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
2994 mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
2995
2996 mSurfaceOrientation = mParameters.orientationAware ?
2997 mViewport.orientation : DISPLAY_ORIENTATION_0;
2998 } else {
2999 mSurfaceWidth = rawWidth;
3000 mSurfaceHeight = rawHeight;
3001 mSurfaceLeft = 0;
3002 mSurfaceTop = 0;
3003 mSurfaceOrientation = DISPLAY_ORIENTATION_0;
3004 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003005 }
3006
3007 // If moving between pointer modes, need to reset some state.
3008 bool deviceModeChanged;
3009 if (mDeviceMode != oldDeviceMode) {
3010 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07003011 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08003012 }
3013
Jeff Browndaf4a122011-08-26 17:14:14 -07003014 // Create pointer controller if needed.
3015 if (mDeviceMode == DEVICE_MODE_POINTER ||
3016 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
3017 if (mPointerController == NULL) {
3018 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
3019 }
3020 } else {
3021 mPointerController.clear();
3022 }
3023
Jeff Brown83d616a2012-09-09 20:33:43 -07003024 if (viewportChanged || deviceModeChanged) {
3025 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
3026 "display id %d",
3027 getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight,
3028 mSurfaceOrientation, mDeviceMode, mViewport.displayId);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003029
Jeff Brown8d608662010-08-30 03:02:23 -07003030 // Configure X and Y factors.
Jeff Brown83d616a2012-09-09 20:33:43 -07003031 mXScale = float(mSurfaceWidth) / rawWidth;
3032 mYScale = float(mSurfaceHeight) / rawHeight;
3033 mXTranslate = -mSurfaceLeft;
3034 mYTranslate = -mSurfaceTop;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003035 mXPrecision = 1.0f / mXScale;
3036 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003037
Jeff Brownbe1aa822011-07-27 16:04:54 -07003038 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07003039 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003040 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07003041 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08003042
Jeff Brownbe1aa822011-07-27 16:04:54 -07003043 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003044
Jeff Brown8d608662010-08-30 03:02:23 -07003045 // Scale factor for terms that are not oriented in a particular axis.
3046 // If the pixels are square then xScale == yScale otherwise we fake it
3047 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003048 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003049
Jeff Brown8d608662010-08-30 03:02:23 -07003050 // Size of diagonal axis.
Jeff Brown83d616a2012-09-09 20:33:43 -07003051 float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003052
Jeff Browna1f89ce2011-08-11 00:05:01 -07003053 // Size factors.
3054 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
3055 if (mRawPointerAxes.touchMajor.valid
3056 && mRawPointerAxes.touchMajor.maxValue != 0) {
3057 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
3058 } else if (mRawPointerAxes.toolMajor.valid
3059 && mRawPointerAxes.toolMajor.maxValue != 0) {
3060 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
3061 } else {
3062 mSizeScale = 0.0f;
3063 }
3064
Jeff Brownbe1aa822011-07-27 16:04:54 -07003065 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003066 mOrientedRanges.haveToolSize = true;
3067 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08003068
Jeff Brownbe1aa822011-07-27 16:04:54 -07003069 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003070 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003071 mOrientedRanges.touchMajor.min = 0;
3072 mOrientedRanges.touchMajor.max = diagonalSize;
3073 mOrientedRanges.touchMajor.flat = 0;
3074 mOrientedRanges.touchMajor.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003075 mOrientedRanges.touchMajor.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003076
Jeff Brownbe1aa822011-07-27 16:04:54 -07003077 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
3078 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08003079
Jeff Brownbe1aa822011-07-27 16:04:54 -07003080 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003081 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003082 mOrientedRanges.toolMajor.min = 0;
3083 mOrientedRanges.toolMajor.max = diagonalSize;
3084 mOrientedRanges.toolMajor.flat = 0;
3085 mOrientedRanges.toolMajor.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003086 mOrientedRanges.toolMajor.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003087
Jeff Brownbe1aa822011-07-27 16:04:54 -07003088 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3089 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003090
3091 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003092 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003093 mOrientedRanges.size.min = 0;
3094 mOrientedRanges.size.max = 1.0;
3095 mOrientedRanges.size.flat = 0;
3096 mOrientedRanges.size.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003097 mOrientedRanges.size.resolution = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003098 } else {
3099 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003100 }
3101
3102 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003103 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003104 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3105 || mCalibration.pressureCalibration
3106 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3107 if (mCalibration.havePressureScale) {
3108 mPressureScale = mCalibration.pressureScale;
3109 } else if (mRawPointerAxes.pressure.valid
3110 && mRawPointerAxes.pressure.maxValue != 0) {
3111 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003112 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003113 }
Jeff Brown8d608662010-08-30 03:02:23 -07003114
Jeff Brown65fd2512011-08-18 11:20:58 -07003115 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3116 mOrientedRanges.pressure.source = mSource;
3117 mOrientedRanges.pressure.min = 0;
3118 mOrientedRanges.pressure.max = 1.0;
3119 mOrientedRanges.pressure.flat = 0;
3120 mOrientedRanges.pressure.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003121 mOrientedRanges.pressure.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003122
Jeff Brown65fd2512011-08-18 11:20:58 -07003123 // Tilt
3124 mTiltXCenter = 0;
3125 mTiltXScale = 0;
3126 mTiltYCenter = 0;
3127 mTiltYScale = 0;
3128 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3129 if (mHaveTilt) {
3130 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3131 mRawPointerAxes.tiltX.maxValue);
3132 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3133 mRawPointerAxes.tiltY.maxValue);
3134 mTiltXScale = M_PI / 180;
3135 mTiltYScale = M_PI / 180;
3136
3137 mOrientedRanges.haveTilt = true;
3138
3139 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3140 mOrientedRanges.tilt.source = mSource;
3141 mOrientedRanges.tilt.min = 0;
3142 mOrientedRanges.tilt.max = M_PI_2;
3143 mOrientedRanges.tilt.flat = 0;
3144 mOrientedRanges.tilt.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003145 mOrientedRanges.tilt.resolution = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003146 }
3147
Jeff Brown8d608662010-08-30 03:02:23 -07003148 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07003149 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003150 if (mHaveTilt) {
3151 mOrientedRanges.haveOrientation = true;
3152
3153 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3154 mOrientedRanges.orientation.source = mSource;
3155 mOrientedRanges.orientation.min = -M_PI;
3156 mOrientedRanges.orientation.max = M_PI;
3157 mOrientedRanges.orientation.flat = 0;
3158 mOrientedRanges.orientation.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003159 mOrientedRanges.orientation.resolution = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003160 } else if (mCalibration.orientationCalibration !=
3161 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003162 if (mCalibration.orientationCalibration
3163 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003164 if (mRawPointerAxes.orientation.valid) {
Jeff Brown037f7272012-06-25 17:31:23 -07003165 if (mRawPointerAxes.orientation.maxValue > 0) {
3166 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
3167 } else if (mRawPointerAxes.orientation.minValue < 0) {
3168 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
3169 } else {
3170 mOrientationScale = 0;
3171 }
Jeff Brown8d608662010-08-30 03:02:23 -07003172 }
3173 }
3174
Jeff Brownbe1aa822011-07-27 16:04:54 -07003175 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003176
Jeff Brownbe1aa822011-07-27 16:04:54 -07003177 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003178 mOrientedRanges.orientation.source = mSource;
3179 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003180 mOrientedRanges.orientation.max = M_PI_2;
3181 mOrientedRanges.orientation.flat = 0;
3182 mOrientedRanges.orientation.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003183 mOrientedRanges.orientation.resolution = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003184 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003185
3186 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003187 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003188 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3189 if (mCalibration.distanceCalibration
3190 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3191 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003192 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003193 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003194 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003195 }
3196 }
3197
Jeff Brownbe1aa822011-07-27 16:04:54 -07003198 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003199
Jeff Brownbe1aa822011-07-27 16:04:54 -07003200 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003201 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003202 mOrientedRanges.distance.min =
3203 mRawPointerAxes.distance.minValue * mDistanceScale;
3204 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003205 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003206 mOrientedRanges.distance.flat = 0;
3207 mOrientedRanges.distance.fuzz =
3208 mRawPointerAxes.distance.fuzz * mDistanceScale;
Michael Wrightc6091c62013-04-01 20:56:04 -07003209 mOrientedRanges.distance.resolution = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003210 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003211
Jeff Brown83d616a2012-09-09 20:33:43 -07003212 // Compute oriented precision, scales and ranges.
Jeff Brown9626b142011-03-03 02:09:54 -08003213 // Note that the maximum value reported is an inclusive maximum value so it is one
3214 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003215 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003216 case DISPLAY_ORIENTATION_90:
3217 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003218 mOrientedXPrecision = mYPrecision;
3219 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003220
Jeff Brown83d616a2012-09-09 20:33:43 -07003221 mOrientedRanges.x.min = mYTranslate;
3222 mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003223 mOrientedRanges.x.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003224 mOrientedRanges.x.fuzz = 0;
3225 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003226
Jeff Brown83d616a2012-09-09 20:33:43 -07003227 mOrientedRanges.y.min = mXTranslate;
3228 mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003229 mOrientedRanges.y.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003230 mOrientedRanges.y.fuzz = 0;
3231 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003232 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003233
Jeff Brown6d0fec22010-07-23 21:28:06 -07003234 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003235 mOrientedXPrecision = mXPrecision;
3236 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003237
Jeff Brown83d616a2012-09-09 20:33:43 -07003238 mOrientedRanges.x.min = mXTranslate;
3239 mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003240 mOrientedRanges.x.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003241 mOrientedRanges.x.fuzz = 0;
3242 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003243
Jeff Brown83d616a2012-09-09 20:33:43 -07003244 mOrientedRanges.y.min = mYTranslate;
3245 mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003246 mOrientedRanges.y.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003247 mOrientedRanges.y.fuzz = 0;
3248 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003249 break;
3250 }
Jeff Brownace13b12011-03-09 17:39:48 -08003251
Jeff Brown65fd2512011-08-18 11:20:58 -07003252 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown4dac9012013-04-10 01:03:19 -07003253 // Compute pointer gesture detection parameters.
Jeff Brown2352b972011-04-12 22:39:53 -07003254 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003255 float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003256
Jeff Brown2352b972011-04-12 22:39:53 -07003257 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003258 // given area relative to the diagonal size of the display when no acceleration
3259 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003260 // Assume that the touch pad has a square aspect ratio such that movements in
3261 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003262 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003263 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003264 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003265
3266 // Scale zooms to cover a smaller range of the display than movements do.
3267 // This value determines the area around the pointer that is affected by freeform
3268 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003269 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003270 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003271 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003272
Jeff Brown2352b972011-04-12 22:39:53 -07003273 // Max width between pointers to detect a swipe gesture is more than some fraction
3274 // of the diagonal axis of the touch pad. Touches that are wider than this are
3275 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003276 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003277 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003278
Jeff Brown4dac9012013-04-10 01:03:19 -07003279 // Abort current pointer usages because the state has changed.
3280 abortPointerUsage(when, 0 /*policyFlags*/);
3281 } else if (mDeviceMode == DEVICE_MODE_NAVIGATION) {
3282 // Compute navigation parameters.
3283 mNavigationAssistStartY = mSurfaceHeight * 0.9f;
3284 mNavigationAssistEndY = mSurfaceHeight * 0.5f;
3285 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003286
3287 // Inform the dispatcher about the changes.
3288 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003289 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003290 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003291}
3292
Jeff Brownbe1aa822011-07-27 16:04:54 -07003293void TouchInputMapper::dumpSurface(String8& dump) {
Jeff Brown83d616a2012-09-09 20:33:43 -07003294 dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, "
3295 "logicalFrame=[%d, %d, %d, %d], "
3296 "physicalFrame=[%d, %d, %d, %d], "
3297 "deviceSize=[%d, %d]\n",
3298 mViewport.displayId, mViewport.orientation,
3299 mViewport.logicalLeft, mViewport.logicalTop,
3300 mViewport.logicalRight, mViewport.logicalBottom,
3301 mViewport.physicalLeft, mViewport.physicalTop,
3302 mViewport.physicalRight, mViewport.physicalBottom,
3303 mViewport.deviceWidth, mViewport.deviceHeight);
3304
Jeff Brownbe1aa822011-07-27 16:04:54 -07003305 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3306 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003307 dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft);
3308 dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003309 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003310}
3311
Jeff Brownbe1aa822011-07-27 16:04:54 -07003312void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003313 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003314 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003315
Jeff Brownbe1aa822011-07-27 16:04:54 -07003316 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003317
Jeff Brown6328cdc2010-07-29 18:18:33 -07003318 if (virtualKeyDefinitions.size() == 0) {
3319 return;
3320 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003321
Jeff Brownbe1aa822011-07-27 16:04:54 -07003322 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003323
Jeff Brownbe1aa822011-07-27 16:04:54 -07003324 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3325 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3326 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3327 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003328
3329 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003330 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003331 virtualKeyDefinitions[i];
3332
Jeff Brownbe1aa822011-07-27 16:04:54 -07003333 mVirtualKeys.add();
3334 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003335
3336 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3337 int32_t keyCode;
3338 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003339 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003340 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003341 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003342 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003343 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003344 }
3345
Jeff Brown6328cdc2010-07-29 18:18:33 -07003346 virtualKey.keyCode = keyCode;
3347 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003348
Jeff Brown6328cdc2010-07-29 18:18:33 -07003349 // convert the key definition's display coordinates into touch coordinates for a hit box
3350 int32_t halfWidth = virtualKeyDefinition.width / 2;
3351 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003352
Jeff Brown6328cdc2010-07-29 18:18:33 -07003353 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003354 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003355 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003356 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003357 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003358 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003359 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003360 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003361 }
3362}
3363
Jeff Brownbe1aa822011-07-27 16:04:54 -07003364void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3365 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003366 dump.append(INDENT3 "Virtual Keys:\n");
3367
Jeff Brownbe1aa822011-07-27 16:04:54 -07003368 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3369 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003370 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3371 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3372 i, virtualKey.scanCode, virtualKey.keyCode,
3373 virtualKey.hitLeft, virtualKey.hitRight,
3374 virtualKey.hitTop, virtualKey.hitBottom);
3375 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003376 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003377}
3378
Jeff Brown8d608662010-08-30 03:02:23 -07003379void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003380 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003381 Calibration& out = mCalibration;
3382
Jeff Browna1f89ce2011-08-11 00:05:01 -07003383 // Size
3384 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3385 String8 sizeCalibrationString;
3386 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3387 if (sizeCalibrationString == "none") {
3388 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3389 } else if (sizeCalibrationString == "geometric") {
3390 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3391 } else if (sizeCalibrationString == "diameter") {
3392 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
Jeff Brown037f7272012-06-25 17:31:23 -07003393 } else if (sizeCalibrationString == "box") {
3394 out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003395 } else if (sizeCalibrationString == "area") {
3396 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3397 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003398 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003399 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003400 }
3401 }
3402
Jeff Browna1f89ce2011-08-11 00:05:01 -07003403 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3404 out.sizeScale);
3405 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3406 out.sizeBias);
3407 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3408 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003409
3410 // Pressure
3411 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3412 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003413 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003414 if (pressureCalibrationString == "none") {
3415 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3416 } else if (pressureCalibrationString == "physical") {
3417 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3418 } else if (pressureCalibrationString == "amplitude") {
3419 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3420 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003421 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003422 pressureCalibrationString.string());
3423 }
3424 }
3425
Jeff Brown8d608662010-08-30 03:02:23 -07003426 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3427 out.pressureScale);
3428
Jeff Brown8d608662010-08-30 03:02:23 -07003429 // Orientation
3430 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3431 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003432 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003433 if (orientationCalibrationString == "none") {
3434 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3435 } else if (orientationCalibrationString == "interpolated") {
3436 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003437 } else if (orientationCalibrationString == "vector") {
3438 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003439 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003440 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003441 orientationCalibrationString.string());
3442 }
3443 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003444
3445 // Distance
3446 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3447 String8 distanceCalibrationString;
3448 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3449 if (distanceCalibrationString == "none") {
3450 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3451 } else if (distanceCalibrationString == "scaled") {
3452 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3453 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003454 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003455 distanceCalibrationString.string());
3456 }
3457 }
3458
3459 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3460 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003461}
3462
3463void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003464 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003465 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3466 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3467 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003468 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003469 } else {
3470 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3471 }
Jeff Brown8d608662010-08-30 03:02:23 -07003472
Jeff Browna1f89ce2011-08-11 00:05:01 -07003473 // Pressure
3474 if (mRawPointerAxes.pressure.valid) {
3475 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3476 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3477 }
3478 } else {
3479 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003480 }
3481
3482 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003483 if (mRawPointerAxes.orientation.valid) {
3484 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003485 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003486 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003487 } else {
3488 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003489 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003490
3491 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003492 if (mRawPointerAxes.distance.valid) {
3493 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003494 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003495 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003496 } else {
3497 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003498 }
Jeff Brown8d608662010-08-30 03:02:23 -07003499}
3500
Jeff Brownef3d7e82010-09-30 14:33:04 -07003501void TouchInputMapper::dumpCalibration(String8& dump) {
3502 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003503
Jeff Browna1f89ce2011-08-11 00:05:01 -07003504 // Size
3505 switch (mCalibration.sizeCalibration) {
3506 case Calibration::SIZE_CALIBRATION_NONE:
3507 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003508 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003509 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3510 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003511 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003512 case Calibration::SIZE_CALIBRATION_DIAMETER:
3513 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3514 break;
Jeff Brown037f7272012-06-25 17:31:23 -07003515 case Calibration::SIZE_CALIBRATION_BOX:
3516 dump.append(INDENT4 "touch.size.calibration: box\n");
3517 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003518 case Calibration::SIZE_CALIBRATION_AREA:
3519 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003520 break;
3521 default:
Steve Blockec193de2012-01-09 18:35:44 +00003522 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003523 }
3524
Jeff Browna1f89ce2011-08-11 00:05:01 -07003525 if (mCalibration.haveSizeScale) {
3526 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3527 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003528 }
3529
Jeff Browna1f89ce2011-08-11 00:05:01 -07003530 if (mCalibration.haveSizeBias) {
3531 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3532 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003533 }
3534
Jeff Browna1f89ce2011-08-11 00:05:01 -07003535 if (mCalibration.haveSizeIsSummed) {
3536 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3537 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003538 }
3539
3540 // Pressure
3541 switch (mCalibration.pressureCalibration) {
3542 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003543 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003544 break;
3545 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003546 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003547 break;
3548 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003549 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003550 break;
3551 default:
Steve Blockec193de2012-01-09 18:35:44 +00003552 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003553 }
3554
Jeff Brown8d608662010-08-30 03:02:23 -07003555 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003556 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3557 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003558 }
3559
Jeff Brown8d608662010-08-30 03:02:23 -07003560 // Orientation
3561 switch (mCalibration.orientationCalibration) {
3562 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003563 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003564 break;
3565 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003566 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003567 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003568 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3569 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3570 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003571 default:
Steve Blockec193de2012-01-09 18:35:44 +00003572 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003573 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003574
3575 // Distance
3576 switch (mCalibration.distanceCalibration) {
3577 case Calibration::DISTANCE_CALIBRATION_NONE:
3578 dump.append(INDENT4 "touch.distance.calibration: none\n");
3579 break;
3580 case Calibration::DISTANCE_CALIBRATION_SCALED:
3581 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3582 break;
3583 default:
Steve Blockec193de2012-01-09 18:35:44 +00003584 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003585 }
3586
3587 if (mCalibration.haveDistanceScale) {
3588 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3589 mCalibration.distanceScale);
3590 }
Jeff Brown8d608662010-08-30 03:02:23 -07003591}
3592
Jeff Brown65fd2512011-08-18 11:20:58 -07003593void TouchInputMapper::reset(nsecs_t when) {
3594 mCursorButtonAccumulator.reset(getDevice());
3595 mCursorScrollAccumulator.reset(getDevice());
3596 mTouchButtonAccumulator.reset(getDevice());
3597
3598 mPointerVelocityControl.reset();
3599 mWheelXVelocityControl.reset();
3600 mWheelYVelocityControl.reset();
3601
Jeff Brownbe1aa822011-07-27 16:04:54 -07003602 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003603 mLastRawPointerData.clear();
3604 mCurrentCookedPointerData.clear();
3605 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003606 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003607 mLastButtonState = 0;
3608 mCurrentRawVScroll = 0;
3609 mCurrentRawHScroll = 0;
3610 mCurrentFingerIdBits.clear();
3611 mLastFingerIdBits.clear();
3612 mCurrentStylusIdBits.clear();
3613 mLastStylusIdBits.clear();
3614 mCurrentMouseIdBits.clear();
3615 mLastMouseIdBits.clear();
3616 mPointerUsage = POINTER_USAGE_NONE;
3617 mSentHoverEnter = false;
3618 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003619
Jeff Brown65fd2512011-08-18 11:20:58 -07003620 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003621
Jeff Brown65fd2512011-08-18 11:20:58 -07003622 mPointerGesture.reset();
3623 mPointerSimple.reset();
Jeff Brown4dac9012013-04-10 01:03:19 -07003624 mNavigation.reset();
Jeff Brown65fd2512011-08-18 11:20:58 -07003625
3626 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003627 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3628 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003629 }
3630
Jeff Brown65fd2512011-08-18 11:20:58 -07003631 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003632}
3633
Jeff Brown65fd2512011-08-18 11:20:58 -07003634void TouchInputMapper::process(const RawEvent* rawEvent) {
3635 mCursorButtonAccumulator.process(rawEvent);
3636 mCursorScrollAccumulator.process(rawEvent);
3637 mTouchButtonAccumulator.process(rawEvent);
3638
Jeff Brown49ccac52012-04-11 18:27:33 -07003639 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003640 sync(rawEvent->when);
3641 }
3642}
3643
3644void TouchInputMapper::sync(nsecs_t when) {
3645 // Sync button state.
3646 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3647 | mCursorButtonAccumulator.getButtonState();
3648
3649 // Sync scroll state.
3650 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3651 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3652 mCursorScrollAccumulator.finishSync();
3653
3654 // Sync touch state.
3655 bool havePointerIds = true;
3656 mCurrentRawPointerData.clear();
3657 syncTouch(when, &havePointerIds);
3658
Jeff Brownaa3855d2011-03-17 01:34:19 -07003659#if DEBUG_RAW_EVENTS
3660 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003661 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003662 mLastRawPointerData.pointerCount,
3663 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003664 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003665 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003666 "hovering ids 0x%08x -> 0x%08x",
3667 mLastRawPointerData.pointerCount,
3668 mCurrentRawPointerData.pointerCount,
3669 mLastRawPointerData.touchingIdBits.value,
3670 mCurrentRawPointerData.touchingIdBits.value,
3671 mLastRawPointerData.hoveringIdBits.value,
3672 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003673 }
3674#endif
3675
Jeff Brown65fd2512011-08-18 11:20:58 -07003676 // Reset state that we will compute below.
3677 mCurrentFingerIdBits.clear();
3678 mCurrentStylusIdBits.clear();
3679 mCurrentMouseIdBits.clear();
3680 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003681
Jeff Brown65fd2512011-08-18 11:20:58 -07003682 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3683 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003684 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003685 mCurrentButtonState = 0;
3686 } else {
3687 // Preprocess pointer data.
3688 if (!havePointerIds) {
3689 assignPointerIds();
3690 }
3691
3692 // Handle policy on initial down or hover events.
3693 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003694 bool initialDown = mLastRawPointerData.pointerCount == 0
3695 && mCurrentRawPointerData.pointerCount != 0;
3696 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3697 if (initialDown || buttonsPressed) {
3698 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003699 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003700 getContext()->fadePointer();
3701 }
3702
3703 // Initial downs on external touch devices should wake the device.
3704 // We don't do this for internal touch screens to prevent them from waking
3705 // up in your pocket.
3706 // TODO: Use the input device configuration to control this behavior more finely.
3707 if (getDevice()->isExternal()) {
3708 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3709 }
3710 }
3711
3712 // Synthesize key down from raw buttons if needed.
3713 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3714 policyFlags, mLastButtonState, mCurrentButtonState);
3715
3716 // Consume raw off-screen touches before cooking pointer data.
3717 // If touches are consumed, subsequent code will not receive any pointer data.
3718 if (consumeRawTouches(when, policyFlags)) {
3719 mCurrentRawPointerData.clear();
3720 }
3721
3722 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3723 // with cooked pointer data that has the same ids and indices as the raw data.
3724 // The following code can use either the raw or cooked data, as needed.
3725 cookPointerData();
3726
3727 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003728 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003729 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3730 uint32_t id = idBits.clearFirstMarkedBit();
3731 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3732 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3733 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3734 mCurrentStylusIdBits.markBit(id);
3735 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3736 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3737 mCurrentFingerIdBits.markBit(id);
3738 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3739 mCurrentMouseIdBits.markBit(id);
3740 }
3741 }
3742 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3743 uint32_t id = idBits.clearFirstMarkedBit();
3744 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3745 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3746 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3747 mCurrentStylusIdBits.markBit(id);
3748 }
3749 }
3750
3751 // Stylus takes precedence over all tools, then mouse, then finger.
3752 PointerUsage pointerUsage = mPointerUsage;
3753 if (!mCurrentStylusIdBits.isEmpty()) {
3754 mCurrentMouseIdBits.clear();
3755 mCurrentFingerIdBits.clear();
3756 pointerUsage = POINTER_USAGE_STYLUS;
3757 } else if (!mCurrentMouseIdBits.isEmpty()) {
3758 mCurrentFingerIdBits.clear();
3759 pointerUsage = POINTER_USAGE_MOUSE;
3760 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3761 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003762 }
3763
3764 dispatchPointerUsage(when, policyFlags, pointerUsage);
3765 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003766 if (mDeviceMode == DEVICE_MODE_DIRECT
3767 && mConfig.showTouches && mPointerController != NULL) {
3768 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3769 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3770
3771 mPointerController->setButtonState(mCurrentButtonState);
3772 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3773 mCurrentCookedPointerData.idToIndex,
3774 mCurrentCookedPointerData.touchingIdBits);
Jeff Brown4dac9012013-04-10 01:03:19 -07003775 } else if (mDeviceMode == DEVICE_MODE_NAVIGATION) {
3776 dispatchNavigationAssist(when, policyFlags);
Jeff Browndaf4a122011-08-26 17:14:14 -07003777 }
3778
Jeff Brown65fd2512011-08-18 11:20:58 -07003779 dispatchHoverExit(when, policyFlags);
3780 dispatchTouches(when, policyFlags);
3781 dispatchHoverEnterAndMove(when, policyFlags);
3782 }
3783
3784 // Synthesize key up from raw buttons if needed.
3785 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3786 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003787 }
3788
Jeff Brown6328cdc2010-07-29 18:18:33 -07003789 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003790 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3791 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3792 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003793 mLastFingerIdBits = mCurrentFingerIdBits;
3794 mLastStylusIdBits = mCurrentStylusIdBits;
3795 mLastMouseIdBits = mCurrentMouseIdBits;
3796
3797 // Clear some transient state.
3798 mCurrentRawVScroll = 0;
3799 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003800}
3801
Jeff Brown79ac9692011-04-19 21:20:10 -07003802void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003803 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003804 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3805 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3806 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003807 }
3808}
3809
Jeff Brownbe1aa822011-07-27 16:04:54 -07003810bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3811 // Check for release of a virtual key.
3812 if (mCurrentVirtualKey.down) {
3813 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3814 // Pointer went up while virtual key was down.
3815 mCurrentVirtualKey.down = false;
3816 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003817#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003818 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003819 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003820#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003821 dispatchVirtualKey(when, policyFlags,
3822 AKEY_EVENT_ACTION_UP,
3823 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003824 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003825 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003826 }
3827
Jeff Brownbe1aa822011-07-27 16:04:54 -07003828 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3829 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3830 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3831 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3832 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3833 // Pointer is still within the space of the virtual key.
3834 return true;
3835 }
3836 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003837
Jeff Brownbe1aa822011-07-27 16:04:54 -07003838 // Pointer left virtual key area or another pointer also went down.
3839 // Send key cancellation but do not consume the touch yet.
3840 // This is useful when the user swipes through from the virtual key area
3841 // into the main display surface.
3842 mCurrentVirtualKey.down = false;
3843 if (!mCurrentVirtualKey.ignored) {
3844#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003845 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003846 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3847#endif
3848 dispatchVirtualKey(when, policyFlags,
3849 AKEY_EVENT_ACTION_UP,
3850 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3851 | AKEY_EVENT_FLAG_CANCELED);
3852 }
3853 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003854
Jeff Brownbe1aa822011-07-27 16:04:54 -07003855 if (mLastRawPointerData.touchingIdBits.isEmpty()
3856 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3857 // Pointer just went down. Check for virtual key press or off-screen touches.
3858 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3859 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3860 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3861 // If exactly one pointer went down, check for virtual key hit.
3862 // Otherwise we will drop the entire stroke.
3863 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3864 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3865 if (virtualKey) {
3866 mCurrentVirtualKey.down = true;
3867 mCurrentVirtualKey.downTime = when;
3868 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3869 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3870 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3871 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3872
3873 if (!mCurrentVirtualKey.ignored) {
3874#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003875 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003876 mCurrentVirtualKey.keyCode,
3877 mCurrentVirtualKey.scanCode);
3878#endif
3879 dispatchVirtualKey(when, policyFlags,
3880 AKEY_EVENT_ACTION_DOWN,
3881 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3882 }
3883 }
3884 }
3885 return true;
3886 }
3887 }
3888
Jeff Brownfe508922011-01-18 15:10:10 -08003889 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003890 // most recent touch within the screen area. The idea is to filter out stray
3891 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003892 //
3893 // Problems we're trying to solve:
3894 //
3895 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3896 // virtual key area that is implemented by a separate touch panel and accidentally
3897 // triggers a virtual key.
3898 //
3899 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3900 // area and accidentally triggers a virtual key. This often happens when virtual keys
3901 // are layed out below the screen near to where the on screen keyboard's space bar
3902 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003903 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003904 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003905 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003906 return false;
3907}
3908
3909void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3910 int32_t keyEventAction, int32_t keyEventFlags) {
3911 int32_t keyCode = mCurrentVirtualKey.keyCode;
3912 int32_t scanCode = mCurrentVirtualKey.scanCode;
3913 nsecs_t downTime = mCurrentVirtualKey.downTime;
3914 int32_t metaState = mContext->getGlobalMetaState();
3915 policyFlags |= POLICY_FLAG_VIRTUAL;
3916
3917 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3918 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3919 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003920}
3921
Jeff Brown6d0fec22010-07-23 21:28:06 -07003922void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003923 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3924 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003925 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003926 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003927
3928 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003929 if (!currentIdBits.isEmpty()) {
3930 // No pointer id changes so this is a move event.
3931 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003932 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003933 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3934 AMOTION_EVENT_EDGE_FLAG_NONE,
3935 mCurrentCookedPointerData.pointerProperties,
3936 mCurrentCookedPointerData.pointerCoords,
3937 mCurrentCookedPointerData.idToIndex,
3938 currentIdBits, -1,
3939 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3940 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003941 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003942 // There may be pointers going up and pointers going down and pointers moving
3943 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003944 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3945 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003946 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003947 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003948
Jeff Brownace13b12011-03-09 17:39:48 -08003949 // Update last coordinates of pointers that have moved so that we observe the new
3950 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003951 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003952 mCurrentCookedPointerData.pointerProperties,
3953 mCurrentCookedPointerData.pointerCoords,
3954 mCurrentCookedPointerData.idToIndex,
3955 mLastCookedPointerData.pointerProperties,
3956 mLastCookedPointerData.pointerCoords,
3957 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003958 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003959 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003960 moveNeeded = true;
3961 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003962
Jeff Brownace13b12011-03-09 17:39:48 -08003963 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003964 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003965 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003966
Jeff Brown65fd2512011-08-18 11:20:58 -07003967 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003968 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003969 mLastCookedPointerData.pointerProperties,
3970 mLastCookedPointerData.pointerCoords,
3971 mLastCookedPointerData.idToIndex,
3972 dispatchedIdBits, upId,
3973 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003974 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003975 }
3976
Jeff Brownc3db8582010-10-20 15:33:38 -07003977 // Dispatch move events if any of the remaining pointers moved from their old locations.
3978 // Although applications receive new locations as part of individual pointer up
3979 // events, they do not generally handle them except when presented in a move event.
3980 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003981 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003982 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003983 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003984 mCurrentCookedPointerData.pointerProperties,
3985 mCurrentCookedPointerData.pointerCoords,
3986 mCurrentCookedPointerData.idToIndex,
3987 dispatchedIdBits, -1,
3988 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003989 }
3990
3991 // Dispatch pointer down events using the new pointer locations.
3992 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003993 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003994 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003995
Jeff Brownace13b12011-03-09 17:39:48 -08003996 if (dispatchedIdBits.count() == 1) {
3997 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003998 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003999 }
4000
Jeff Brown65fd2512011-08-18 11:20:58 -07004001 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004002 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004003 mCurrentCookedPointerData.pointerProperties,
4004 mCurrentCookedPointerData.pointerCoords,
4005 mCurrentCookedPointerData.idToIndex,
4006 dispatchedIdBits, downId,
4007 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004008 }
4009 }
Jeff Brownace13b12011-03-09 17:39:48 -08004010}
4011
Jeff Brownbe1aa822011-07-27 16:04:54 -07004012void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
4013 if (mSentHoverEnter &&
4014 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
4015 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
4016 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07004017 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004018 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
4019 mLastCookedPointerData.pointerProperties,
4020 mLastCookedPointerData.pointerCoords,
4021 mLastCookedPointerData.idToIndex,
4022 mLastCookedPointerData.hoveringIdBits, -1,
4023 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4024 mSentHoverEnter = false;
4025 }
4026}
Jeff Brownace13b12011-03-09 17:39:48 -08004027
Jeff Brownbe1aa822011-07-27 16:04:54 -07004028void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
4029 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
4030 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
4031 int32_t metaState = getContext()->getGlobalMetaState();
4032 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004033 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004034 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
4035 mCurrentCookedPointerData.pointerProperties,
4036 mCurrentCookedPointerData.pointerCoords,
4037 mCurrentCookedPointerData.idToIndex,
4038 mCurrentCookedPointerData.hoveringIdBits, -1,
4039 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4040 mSentHoverEnter = true;
4041 }
Jeff Brownace13b12011-03-09 17:39:48 -08004042
Jeff Brown65fd2512011-08-18 11:20:58 -07004043 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004044 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
4045 mCurrentCookedPointerData.pointerProperties,
4046 mCurrentCookedPointerData.pointerCoords,
4047 mCurrentCookedPointerData.idToIndex,
4048 mCurrentCookedPointerData.hoveringIdBits, -1,
4049 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4050 }
4051}
4052
4053void TouchInputMapper::cookPointerData() {
4054 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4055
4056 mCurrentCookedPointerData.clear();
4057 mCurrentCookedPointerData.pointerCount = currentPointerCount;
4058 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
4059 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
4060
4061 // Walk through the the active pointers and map device coordinates onto
4062 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08004063 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004064 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004065
Jeff Browna1f89ce2011-08-11 00:05:01 -07004066 // Size
4067 float touchMajor, touchMinor, toolMajor, toolMinor, size;
4068 switch (mCalibration.sizeCalibration) {
4069 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
4070 case Calibration::SIZE_CALIBRATION_DIAMETER:
Jeff Brown037f7272012-06-25 17:31:23 -07004071 case Calibration::SIZE_CALIBRATION_BOX:
Jeff Browna1f89ce2011-08-11 00:05:01 -07004072 case Calibration::SIZE_CALIBRATION_AREA:
4073 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
4074 touchMajor = in.touchMajor;
4075 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
4076 toolMajor = in.toolMajor;
4077 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
4078 size = mRawPointerAxes.touchMinor.valid
4079 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4080 } else if (mRawPointerAxes.touchMajor.valid) {
4081 toolMajor = touchMajor = in.touchMajor;
4082 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
4083 ? in.touchMinor : in.touchMajor;
4084 size = mRawPointerAxes.touchMinor.valid
4085 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4086 } else if (mRawPointerAxes.toolMajor.valid) {
4087 touchMajor = toolMajor = in.toolMajor;
4088 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
4089 ? in.toolMinor : in.toolMajor;
4090 size = mRawPointerAxes.toolMinor.valid
4091 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004092 } else {
Steve Blockec193de2012-01-09 18:35:44 +00004093 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07004094 "Size calibration should have been resolved to NONE.");
4095 touchMajor = 0;
4096 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004097 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004098 toolMinor = 0;
4099 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004100 }
Jeff Brownace13b12011-03-09 17:39:48 -08004101
Jeff Browna1f89ce2011-08-11 00:05:01 -07004102 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
4103 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
4104 if (touchingCount > 1) {
4105 touchMajor /= touchingCount;
4106 touchMinor /= touchingCount;
4107 toolMajor /= touchingCount;
4108 toolMinor /= touchingCount;
4109 size /= touchingCount;
4110 }
4111 }
Jeff Brownace13b12011-03-09 17:39:48 -08004112
Jeff Browna1f89ce2011-08-11 00:05:01 -07004113 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4114 touchMajor *= mGeometricScale;
4115 touchMinor *= mGeometricScale;
4116 toolMajor *= mGeometricScale;
4117 toolMinor *= mGeometricScale;
4118 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4119 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004120 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004121 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4122 toolMinor = toolMajor;
4123 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4124 touchMinor = touchMajor;
4125 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004126 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004127
4128 mCalibration.applySizeScaleAndBias(&touchMajor);
4129 mCalibration.applySizeScaleAndBias(&touchMinor);
4130 mCalibration.applySizeScaleAndBias(&toolMajor);
4131 mCalibration.applySizeScaleAndBias(&toolMinor);
4132 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004133 break;
4134 default:
4135 touchMajor = 0;
4136 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004137 toolMajor = 0;
4138 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004139 size = 0;
4140 break;
4141 }
4142
Jeff Browna1f89ce2011-08-11 00:05:01 -07004143 // Pressure
4144 float pressure;
4145 switch (mCalibration.pressureCalibration) {
4146 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4147 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4148 pressure = in.pressure * mPressureScale;
4149 break;
4150 default:
4151 pressure = in.isHovering ? 0 : 1;
4152 break;
4153 }
4154
Jeff Brown65fd2512011-08-18 11:20:58 -07004155 // Tilt and Orientation
4156 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004157 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004158 if (mHaveTilt) {
4159 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4160 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4161 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4162 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4163 } else {
4164 tilt = 0;
4165
4166 switch (mCalibration.orientationCalibration) {
4167 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown037f7272012-06-25 17:31:23 -07004168 orientation = in.orientation * mOrientationScale;
Jeff Brown65fd2512011-08-18 11:20:58 -07004169 break;
4170 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4171 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4172 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4173 if (c1 != 0 || c2 != 0) {
4174 orientation = atan2f(c1, c2) * 0.5f;
4175 float confidence = hypotf(c1, c2);
4176 float scale = 1.0f + confidence / 16.0f;
4177 touchMajor *= scale;
4178 touchMinor /= scale;
4179 toolMajor *= scale;
4180 toolMinor /= scale;
4181 } else {
4182 orientation = 0;
4183 }
4184 break;
4185 }
4186 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004187 orientation = 0;
4188 }
Jeff Brownace13b12011-03-09 17:39:48 -08004189 }
4190
Jeff Brown80fd47c2011-05-24 01:07:44 -07004191 // Distance
4192 float distance;
4193 switch (mCalibration.distanceCalibration) {
4194 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004195 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004196 break;
4197 default:
4198 distance = 0;
4199 }
4200
Jeff Brownace13b12011-03-09 17:39:48 -08004201 // X and Y
4202 // Adjust coords for surface orientation.
4203 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004204 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004205 case DISPLAY_ORIENTATION_90:
Jeff Brown83d616a2012-09-09 20:33:43 -07004206 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4207 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004208 orientation -= M_PI_2;
4209 if (orientation < - M_PI_2) {
4210 orientation += M_PI;
4211 }
4212 break;
4213 case DISPLAY_ORIENTATION_180:
Jeff Brown83d616a2012-09-09 20:33:43 -07004214 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
4215 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004216 break;
4217 case DISPLAY_ORIENTATION_270:
Jeff Brown83d616a2012-09-09 20:33:43 -07004218 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
4219 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004220 orientation += M_PI_2;
4221 if (orientation > M_PI_2) {
4222 orientation -= M_PI;
4223 }
4224 break;
4225 default:
Jeff Brown83d616a2012-09-09 20:33:43 -07004226 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4227 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004228 break;
4229 }
4230
4231 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004232 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004233 out.clear();
4234 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4235 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4236 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4237 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4238 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4239 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4240 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4241 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4242 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004243 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004244 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004245
4246 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004247 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4248 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004249 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004250 properties.id = id;
4251 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004252
Jeff Brownbe1aa822011-07-27 16:04:54 -07004253 // Write id index.
4254 mCurrentCookedPointerData.idToIndex[id] = i;
4255 }
Jeff Brownace13b12011-03-09 17:39:48 -08004256}
4257
Jeff Brown65fd2512011-08-18 11:20:58 -07004258void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4259 PointerUsage pointerUsage) {
4260 if (pointerUsage != mPointerUsage) {
4261 abortPointerUsage(when, policyFlags);
4262 mPointerUsage = pointerUsage;
4263 }
4264
4265 switch (mPointerUsage) {
4266 case POINTER_USAGE_GESTURES:
4267 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4268 break;
4269 case POINTER_USAGE_STYLUS:
4270 dispatchPointerStylus(when, policyFlags);
4271 break;
4272 case POINTER_USAGE_MOUSE:
4273 dispatchPointerMouse(when, policyFlags);
4274 break;
4275 default:
4276 break;
4277 }
4278}
4279
4280void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4281 switch (mPointerUsage) {
4282 case POINTER_USAGE_GESTURES:
4283 abortPointerGestures(when, policyFlags);
4284 break;
4285 case POINTER_USAGE_STYLUS:
4286 abortPointerStylus(when, policyFlags);
4287 break;
4288 case POINTER_USAGE_MOUSE:
4289 abortPointerMouse(when, policyFlags);
4290 break;
4291 default:
4292 break;
4293 }
4294
4295 mPointerUsage = POINTER_USAGE_NONE;
4296}
4297
Jeff Brown79ac9692011-04-19 21:20:10 -07004298void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4299 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004300 // Update current gesture coordinates.
4301 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004302 bool sendEvents = preparePointerGestures(when,
4303 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4304 if (!sendEvents) {
4305 return;
4306 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004307 if (finishPreviousGesture) {
4308 cancelPreviousGesture = false;
4309 }
Jeff Brownace13b12011-03-09 17:39:48 -08004310
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004311 // Update the pointer presentation and spots.
4312 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4313 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4314 if (finishPreviousGesture || cancelPreviousGesture) {
4315 mPointerController->clearSpots();
4316 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004317 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4318 mPointerGesture.currentGestureIdToIndex,
4319 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004320 } else {
4321 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4322 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004323
Jeff Brown538881e2011-05-25 18:23:38 -07004324 // Show or hide the pointer if needed.
4325 switch (mPointerGesture.currentGestureMode) {
4326 case PointerGesture::NEUTRAL:
4327 case PointerGesture::QUIET:
4328 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4329 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4330 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4331 // Remind the user of where the pointer is after finishing a gesture with spots.
4332 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4333 }
4334 break;
4335 case PointerGesture::TAP:
4336 case PointerGesture::TAP_DRAG:
4337 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4338 case PointerGesture::HOVER:
4339 case PointerGesture::PRESS:
4340 // Unfade the pointer when the current gesture manipulates the
4341 // area directly under the pointer.
4342 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4343 break;
4344 case PointerGesture::SWIPE:
4345 case PointerGesture::FREEFORM:
4346 // Fade the pointer when the current gesture manipulates a different
4347 // area and there are spots to guide the user experience.
4348 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4349 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4350 } else {
4351 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4352 }
4353 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004354 }
4355
Jeff Brownace13b12011-03-09 17:39:48 -08004356 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004357 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004358 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004359
4360 // Update last coordinates of pointers that have moved so that we observe the new
4361 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004362 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4363 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4364 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004365 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004366 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4367 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4368 bool moveNeeded = false;
4369 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004370 && !mPointerGesture.lastGestureIdBits.isEmpty()
4371 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004372 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4373 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004374 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004375 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004376 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004377 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4378 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004379 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004380 moveNeeded = true;
4381 }
Jeff Brownace13b12011-03-09 17:39:48 -08004382 }
4383
4384 // Send motion events for all pointers that went up or were canceled.
4385 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4386 if (!dispatchedGestureIdBits.isEmpty()) {
4387 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004388 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004389 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4390 AMOTION_EVENT_EDGE_FLAG_NONE,
4391 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004392 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4393 dispatchedGestureIdBits, -1,
4394 0, 0, mPointerGesture.downTime);
4395
4396 dispatchedGestureIdBits.clear();
4397 } else {
4398 BitSet32 upGestureIdBits;
4399 if (finishPreviousGesture) {
4400 upGestureIdBits = dispatchedGestureIdBits;
4401 } else {
4402 upGestureIdBits.value = dispatchedGestureIdBits.value
4403 & ~mPointerGesture.currentGestureIdBits.value;
4404 }
4405 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004406 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004407
Jeff Brown65fd2512011-08-18 11:20:58 -07004408 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004409 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004410 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4411 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004412 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4413 dispatchedGestureIdBits, id,
4414 0, 0, mPointerGesture.downTime);
4415
4416 dispatchedGestureIdBits.clearBit(id);
4417 }
4418 }
4419 }
4420
4421 // Send motion events for all pointers that moved.
4422 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004423 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004424 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4425 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004426 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4427 dispatchedGestureIdBits, -1,
4428 0, 0, mPointerGesture.downTime);
4429 }
4430
4431 // Send motion events for all pointers that went down.
4432 if (down) {
4433 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4434 & ~dispatchedGestureIdBits.value);
4435 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004436 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004437 dispatchedGestureIdBits.markBit(id);
4438
Jeff Brownace13b12011-03-09 17:39:48 -08004439 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004440 mPointerGesture.downTime = when;
4441 }
4442
Jeff Brown65fd2512011-08-18 11:20:58 -07004443 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004444 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004445 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004446 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4447 dispatchedGestureIdBits, id,
4448 0, 0, mPointerGesture.downTime);
4449 }
4450 }
4451
Jeff Brownace13b12011-03-09 17:39:48 -08004452 // Send motion events for hover.
4453 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004454 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004455 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4456 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4457 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004458 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4459 mPointerGesture.currentGestureIdBits, -1,
4460 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004461 } else if (dispatchedGestureIdBits.isEmpty()
4462 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4463 // Synthesize a hover move event after all pointers go up to indicate that
4464 // the pointer is hovering again even if the user is not currently touching
4465 // the touch pad. This ensures that a view will receive a fresh hover enter
4466 // event after a tap.
4467 float x, y;
4468 mPointerController->getPosition(&x, &y);
4469
4470 PointerProperties pointerProperties;
4471 pointerProperties.clear();
4472 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004473 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004474
4475 PointerCoords pointerCoords;
4476 pointerCoords.clear();
4477 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4478 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4479
Jeff Brown65fd2512011-08-18 11:20:58 -07004480 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004481 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4482 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07004483 mViewport.displayId, 1, &pointerProperties, &pointerCoords,
4484 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004485 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004486 }
4487
4488 // Update state.
4489 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4490 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004491 mPointerGesture.lastGestureIdBits.clear();
4492 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004493 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4494 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004495 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004496 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004497 mPointerGesture.lastGestureProperties[index].copyFrom(
4498 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004499 mPointerGesture.lastGestureCoords[index].copyFrom(
4500 mPointerGesture.currentGestureCoords[index]);
4501 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004502 }
4503 }
4504}
4505
Jeff Brown65fd2512011-08-18 11:20:58 -07004506void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4507 // Cancel previously dispatches pointers.
4508 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4509 int32_t metaState = getContext()->getGlobalMetaState();
4510 int32_t buttonState = mCurrentButtonState;
4511 dispatchMotion(when, policyFlags, mSource,
4512 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4513 AMOTION_EVENT_EDGE_FLAG_NONE,
4514 mPointerGesture.lastGestureProperties,
4515 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4516 mPointerGesture.lastGestureIdBits, -1,
4517 0, 0, mPointerGesture.downTime);
4518 }
4519
4520 // Reset the current pointer gesture.
4521 mPointerGesture.reset();
4522 mPointerVelocityControl.reset();
4523
4524 // Remove any current spots.
4525 if (mPointerController != NULL) {
4526 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4527 mPointerController->clearSpots();
4528 }
4529}
4530
Jeff Brown79ac9692011-04-19 21:20:10 -07004531bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4532 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004533 *outCancelPreviousGesture = false;
4534 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004535
Jeff Brown79ac9692011-04-19 21:20:10 -07004536 // Handle TAP timeout.
4537 if (isTimeout) {
4538#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004539 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004540#endif
4541
4542 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004543 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004544 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004545 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004546 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004547 } else {
4548 // The tap is finished.
4549#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004550 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004551#endif
4552 *outFinishPreviousGesture = true;
4553
4554 mPointerGesture.activeGestureId = -1;
4555 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4556 mPointerGesture.currentGestureIdBits.clear();
4557
Jeff Brown65fd2512011-08-18 11:20:58 -07004558 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004559 return true;
4560 }
4561 }
4562
4563 // We did not handle this timeout.
4564 return false;
4565 }
4566
Jeff Brown65fd2512011-08-18 11:20:58 -07004567 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4568 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4569
Jeff Brownace13b12011-03-09 17:39:48 -08004570 // Update the velocity tracker.
4571 {
4572 VelocityTracker::Position positions[MAX_POINTERS];
4573 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004574 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004575 uint32_t id = idBits.clearFirstMarkedBit();
4576 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004577 positions[count].x = pointer.x * mPointerXMovementScale;
4578 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004579 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004580 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004581 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004582 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004583
Jeff Brownace13b12011-03-09 17:39:48 -08004584 // Pick a new active touch id if needed.
4585 // Choose an arbitrary pointer that just went down, if there is one.
4586 // Otherwise choose an arbitrary remaining pointer.
4587 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004588 // We keep the same active touch id for as long as possible.
4589 bool activeTouchChanged = false;
4590 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4591 int32_t activeTouchId = lastActiveTouchId;
4592 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004593 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004594 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004595 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004596 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004597 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004598 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004599 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004600 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004601 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004602 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004603 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004604 } else {
4605 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004606 }
4607 }
4608
4609 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004610 bool isQuietTime = false;
4611 if (activeTouchId < 0) {
4612 mPointerGesture.resetQuietTime();
4613 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004614 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004615 if (!isQuietTime) {
4616 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4617 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4618 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004619 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004620 // Enter quiet time when exiting swipe or freeform state.
4621 // This is to prevent accidentally entering the hover state and flinging the
4622 // pointer when finishing a swipe and there is still one pointer left onscreen.
4623 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004624 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004625 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004626 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004627 // Enter quiet time when releasing the button and there are still two or more
4628 // fingers down. This may indicate that one finger was used to press the button
4629 // but it has not gone up yet.
4630 isQuietTime = true;
4631 }
4632 if (isQuietTime) {
4633 mPointerGesture.quietTime = when;
4634 }
Jeff Brownace13b12011-03-09 17:39:48 -08004635 }
4636 }
4637
4638 // Switch states based on button and pointer state.
4639 if (isQuietTime) {
4640 // Case 1: Quiet time. (QUIET)
4641#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004642 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004643 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004644#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004645 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4646 *outFinishPreviousGesture = true;
4647 }
Jeff Brownace13b12011-03-09 17:39:48 -08004648
4649 mPointerGesture.activeGestureId = -1;
4650 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004651 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004652
Jeff Brown65fd2512011-08-18 11:20:58 -07004653 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004654 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004655 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004656 // The pointer follows the active touch point.
4657 // Emit DOWN, MOVE, UP events at the pointer location.
4658 //
4659 // Only the active touch matters; other fingers are ignored. This policy helps
4660 // to handle the case where the user places a second finger on the touch pad
4661 // to apply the necessary force to depress an integrated button below the surface.
4662 // We don't want the second finger to be delivered to applications.
4663 //
4664 // For this to work well, we need to make sure to track the pointer that is really
4665 // active. If the user first puts one finger down to click then adds another
4666 // finger to drag then the active pointer should switch to the finger that is
4667 // being dragged.
4668#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004669 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004670 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004671#endif
4672 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004673 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004674 *outFinishPreviousGesture = true;
4675 mPointerGesture.activeGestureId = 0;
4676 }
4677
4678 // Switch pointers if needed.
4679 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004680 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004681 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004682 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004683 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004684 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004685 float vx, vy;
4686 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4687 float speed = hypotf(vx, vy);
4688 if (speed > bestSpeed) {
4689 bestId = id;
4690 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004691 }
Jeff Brown8d608662010-08-30 03:02:23 -07004692 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004693 }
4694 if (bestId >= 0 && bestId != activeTouchId) {
4695 mPointerGesture.activeTouchId = activeTouchId = bestId;
4696 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004697#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004698 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004699 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004700#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004701 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004702 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004703
Jeff Brown65fd2512011-08-18 11:20:58 -07004704 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004705 const RawPointerData::Pointer& currentPointer =
4706 mCurrentRawPointerData.pointerForId(activeTouchId);
4707 const RawPointerData::Pointer& lastPointer =
4708 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004709 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4710 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004711
Jeff Brownbe1aa822011-07-27 16:04:54 -07004712 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004713 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004714
4715 // Move the pointer using a relative motion.
4716 // When using spots, the click will occur at the position of the anchor
4717 // spot and all other spots will move there.
4718 mPointerController->move(deltaX, deltaY);
4719 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004720 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004721 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004722
Jeff Brownace13b12011-03-09 17:39:48 -08004723 float x, y;
4724 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004725
Jeff Brown79ac9692011-04-19 21:20:10 -07004726 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004727 mPointerGesture.currentGestureIdBits.clear();
4728 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4729 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004730 mPointerGesture.currentGestureProperties[0].clear();
4731 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004732 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004733 mPointerGesture.currentGestureCoords[0].clear();
4734 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4735 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4736 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004737 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004738 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004739 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4740 *outFinishPreviousGesture = true;
4741 }
Jeff Brownace13b12011-03-09 17:39:48 -08004742
Jeff Brown79ac9692011-04-19 21:20:10 -07004743 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004744 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004745 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004746 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4747 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004748 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004749 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004750 float x, y;
4751 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004752 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4753 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004754#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004755 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004756#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004757
4758 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004759 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004760 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004761
Jeff Brownace13b12011-03-09 17:39:48 -08004762 mPointerGesture.activeGestureId = 0;
4763 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004764 mPointerGesture.currentGestureIdBits.clear();
4765 mPointerGesture.currentGestureIdBits.markBit(
4766 mPointerGesture.activeGestureId);
4767 mPointerGesture.currentGestureIdToIndex[
4768 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004769 mPointerGesture.currentGestureProperties[0].clear();
4770 mPointerGesture.currentGestureProperties[0].id =
4771 mPointerGesture.activeGestureId;
4772 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004773 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004774 mPointerGesture.currentGestureCoords[0].clear();
4775 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004776 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004777 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004778 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004779 mPointerGesture.currentGestureCoords[0].setAxisValue(
4780 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004781
Jeff Brownace13b12011-03-09 17:39:48 -08004782 tapped = true;
4783 } else {
4784#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004785 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004786 x - mPointerGesture.tapX,
4787 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004788#endif
4789 }
4790 } else {
4791#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004792 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004793 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004794#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004795 }
Jeff Brownace13b12011-03-09 17:39:48 -08004796 }
Jeff Brown2352b972011-04-12 22:39:53 -07004797
Jeff Brown65fd2512011-08-18 11:20:58 -07004798 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004799
Jeff Brownace13b12011-03-09 17:39:48 -08004800 if (!tapped) {
4801#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004802 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004803#endif
4804 mPointerGesture.activeGestureId = -1;
4805 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004806 mPointerGesture.currentGestureIdBits.clear();
4807 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004808 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004809 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004810 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004811 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4812 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004813 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004814
Jeff Brown79ac9692011-04-19 21:20:10 -07004815 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4816 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004817 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004818 float x, y;
4819 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004820 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4821 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004822 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4823 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004824#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004825 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004826 x - mPointerGesture.tapX,
4827 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004828#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004829 }
4830 } else {
4831#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004832 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004833 (when - mPointerGesture.tapUpTime) * 0.000001f);
4834#endif
4835 }
4836 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4837 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4838 }
Jeff Brownace13b12011-03-09 17:39:48 -08004839
Jeff Brown65fd2512011-08-18 11:20:58 -07004840 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004841 const RawPointerData::Pointer& currentPointer =
4842 mCurrentRawPointerData.pointerForId(activeTouchId);
4843 const RawPointerData::Pointer& lastPointer =
4844 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004845 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004846 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004847 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004848 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004849
Jeff Brownbe1aa822011-07-27 16:04:54 -07004850 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004851 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004852
Jeff Brown2352b972011-04-12 22:39:53 -07004853 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004854 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004855 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004856 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004857 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004858 }
4859
Jeff Brown79ac9692011-04-19 21:20:10 -07004860 bool down;
4861 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4862#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004863 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004864#endif
4865 down = true;
4866 } else {
4867#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004868 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004869#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004870 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4871 *outFinishPreviousGesture = true;
4872 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004873 mPointerGesture.activeGestureId = 0;
4874 down = false;
4875 }
Jeff Brownace13b12011-03-09 17:39:48 -08004876
4877 float x, y;
4878 mPointerController->getPosition(&x, &y);
4879
Jeff Brownace13b12011-03-09 17:39:48 -08004880 mPointerGesture.currentGestureIdBits.clear();
4881 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4882 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004883 mPointerGesture.currentGestureProperties[0].clear();
4884 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4885 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004886 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004887 mPointerGesture.currentGestureCoords[0].clear();
4888 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4889 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004890 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4891 down ? 1.0f : 0.0f);
4892
Jeff Brown65fd2512011-08-18 11:20:58 -07004893 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004894 mPointerGesture.resetTap();
4895 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004896 mPointerGesture.tapX = x;
4897 mPointerGesture.tapY = y;
4898 }
Jeff Brownace13b12011-03-09 17:39:48 -08004899 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004900 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4901 // We need to provide feedback for each finger that goes down so we cannot wait
4902 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004903 //
Jeff Brown2352b972011-04-12 22:39:53 -07004904 // The ambiguous case is deciding what to do when there are two fingers down but they
4905 // have not moved enough to determine whether they are part of a drag or part of a
4906 // freeform gesture, or just a press or long-press at the pointer location.
4907 //
4908 // When there are two fingers we start with the PRESS hypothesis and we generate a
4909 // down at the pointer location.
4910 //
4911 // When the two fingers move enough or when additional fingers are added, we make
4912 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004913 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004914
Jeff Brown214eaf42011-05-26 19:17:02 -07004915 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004916 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004917 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004918 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4919 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004920 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004921 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004922 // Additional pointers have gone down but not yet settled.
4923 // Reset the gesture.
4924#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004925 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004926 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004927 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004928 * 0.000001f);
4929#endif
4930 *outCancelPreviousGesture = true;
4931 } else {
4932 // Continue previous gesture.
4933 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4934 }
4935
4936 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004937 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4938 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004939 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004940 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004941
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004942 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004943#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004944 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004945 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004946 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004947 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004948#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004949 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4950 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004951 &mPointerGesture.referenceTouchY);
4952 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4953 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004954 }
Jeff Brownace13b12011-03-09 17:39:48 -08004955
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004956 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004957 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004958 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4959 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004960 mPointerGesture.referenceDeltas[id].dx = 0;
4961 mPointerGesture.referenceDeltas[id].dy = 0;
4962 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004963 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004964
4965 // Add delta for all fingers and calculate a common movement delta.
4966 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004967 BitSet32 commonIdBits(mLastFingerIdBits.value
4968 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004969 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4970 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004971 uint32_t id = idBits.clearFirstMarkedBit();
4972 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4973 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004974 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4975 delta.dx += cpd.x - lpd.x;
4976 delta.dy += cpd.y - lpd.y;
4977
4978 if (first) {
4979 commonDeltaX = delta.dx;
4980 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004981 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004982 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4983 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4984 }
4985 }
Jeff Brownace13b12011-03-09 17:39:48 -08004986
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004987 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4988 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4989 float dist[MAX_POINTER_ID + 1];
4990 int32_t distOverThreshold = 0;
4991 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004992 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004993 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004994 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4995 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004996 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004997 distOverThreshold += 1;
4998 }
4999 }
5000
5001 // Only transition when at least two pointers have moved further than
5002 // the minimum distance threshold.
5003 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005004 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005005 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07005006#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005007 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005008 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005009#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005010 *outCancelPreviousGesture = true;
5011 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5012 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005013 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07005014 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005015 uint32_t id1 = idBits.clearFirstMarkedBit();
5016 uint32_t id2 = idBits.firstMarkedBit();
5017 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
5018 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
5019 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
5020 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
5021 // There are two pointers but they are too far apart for a SWIPE,
5022 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005023#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005024 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005025 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005026#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005027 *outCancelPreviousGesture = true;
5028 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5029 } else {
5030 // There are two pointers. Wait for both pointers to start moving
5031 // before deciding whether this is a SWIPE or FREEFORM gesture.
5032 float dist1 = dist[id1];
5033 float dist2 = dist[id2];
5034 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
5035 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
5036 // Calculate the dot product of the displacement vectors.
5037 // When the vectors are oriented in approximately the same direction,
5038 // the angle betweeen them is near zero and the cosine of the angle
5039 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
5040 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
5041 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07005042 float dx1 = delta1.dx * mPointerXZoomScale;
5043 float dy1 = delta1.dy * mPointerYZoomScale;
5044 float dx2 = delta2.dx * mPointerXZoomScale;
5045 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005046 float dot = dx1 * dx2 + dy1 * dy2;
5047 float cosine = dot / (dist1 * dist2); // denominator always > 0
5048 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
5049 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005050#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005051 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005052 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5053 "cosine %0.3f >= %0.3f",
5054 dist1, mConfig.pointerGestureMultitouchMinDistance,
5055 dist2, mConfig.pointerGestureMultitouchMinDistance,
5056 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005057#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005058 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
5059 } else {
5060 // Pointers are moving in different directions. Switch to FREEFORM.
5061#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005062 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005063 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5064 "cosine %0.3f < %0.3f",
5065 dist1, mConfig.pointerGestureMultitouchMinDistance,
5066 dist2, mConfig.pointerGestureMultitouchMinDistance,
5067 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
5068#endif
5069 *outCancelPreviousGesture = true;
5070 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5071 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005072 }
Jeff Brownace13b12011-03-09 17:39:48 -08005073 }
5074 }
Jeff Brownace13b12011-03-09 17:39:48 -08005075 }
5076 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07005077 // Switch from SWIPE to FREEFORM if additional pointers go down.
5078 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07005079 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07005080#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005081 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005082 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005083#endif
Jeff Brownace13b12011-03-09 17:39:48 -08005084 *outCancelPreviousGesture = true;
5085 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07005086 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005087 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005088
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005089 // Move the reference points based on the overall group motion of the fingers
5090 // except in PRESS mode while waiting for a transition to occur.
5091 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
5092 && (commonDeltaX || commonDeltaY)) {
5093 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005094 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07005095 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005096 delta.dx = 0;
5097 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07005098 }
5099
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005100 mPointerGesture.referenceTouchX += commonDeltaX;
5101 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07005102
Jeff Brown65fd2512011-08-18 11:20:58 -07005103 commonDeltaX *= mPointerXMovementScale;
5104 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07005105
Jeff Brownbe1aa822011-07-27 16:04:54 -07005106 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07005107 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07005108
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005109 mPointerGesture.referenceGestureX += commonDeltaX;
5110 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005111 }
5112
5113 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005114 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5115 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5116 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005117#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005118 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005119 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005120 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005121#endif
Steve Blockec193de2012-01-09 18:35:44 +00005122 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005123
5124 mPointerGesture.currentGestureIdBits.clear();
5125 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5126 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005127 mPointerGesture.currentGestureProperties[0].clear();
5128 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5129 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005130 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005131 mPointerGesture.currentGestureCoords[0].clear();
5132 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5133 mPointerGesture.referenceGestureX);
5134 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5135 mPointerGesture.referenceGestureY);
5136 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005137 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5138 // FREEFORM mode.
5139#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005140 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005141 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005142 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005143#endif
Steve Blockec193de2012-01-09 18:35:44 +00005144 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005145
Jeff Brownace13b12011-03-09 17:39:48 -08005146 mPointerGesture.currentGestureIdBits.clear();
5147
5148 BitSet32 mappedTouchIdBits;
5149 BitSet32 usedGestureIdBits;
5150 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5151 // Initially, assign the active gesture id to the active touch point
5152 // if there is one. No other touch id bits are mapped yet.
5153 if (!*outCancelPreviousGesture) {
5154 mappedTouchIdBits.markBit(activeTouchId);
5155 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5156 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5157 mPointerGesture.activeGestureId;
5158 } else {
5159 mPointerGesture.activeGestureId = -1;
5160 }
5161 } else {
5162 // Otherwise, assume we mapped all touches from the previous frame.
5163 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005164 mappedTouchIdBits.value = mLastFingerIdBits.value
5165 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005166 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5167
5168 // Check whether we need to choose a new active gesture id because the
5169 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005170 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5171 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005172 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005173 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005174 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5175 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5176 mPointerGesture.activeGestureId = -1;
5177 break;
5178 }
5179 }
5180 }
5181
5182#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005183 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005184 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5185 "activeGestureId=%d",
5186 mappedTouchIdBits.value, usedGestureIdBits.value,
5187 mPointerGesture.activeGestureId);
5188#endif
5189
Jeff Brown65fd2512011-08-18 11:20:58 -07005190 BitSet32 idBits(mCurrentFingerIdBits);
5191 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005192 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005193 uint32_t gestureId;
5194 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005195 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005196 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5197#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005198 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005199 "new mapping for touch id %d -> gesture id %d",
5200 touchId, gestureId);
5201#endif
5202 } else {
5203 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5204#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005205 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005206 "existing mapping for touch id %d -> gesture id %d",
5207 touchId, gestureId);
5208#endif
5209 }
5210 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5211 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5212
Jeff Brownbe1aa822011-07-27 16:04:54 -07005213 const RawPointerData::Pointer& pointer =
5214 mCurrentRawPointerData.pointerForId(touchId);
5215 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005216 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005217 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005218 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005219 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005220
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005221 mPointerGesture.currentGestureProperties[i].clear();
5222 mPointerGesture.currentGestureProperties[i].id = gestureId;
5223 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005224 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005225 mPointerGesture.currentGestureCoords[i].clear();
5226 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005227 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005228 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005229 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005230 mPointerGesture.currentGestureCoords[i].setAxisValue(
5231 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5232 }
5233
5234 if (mPointerGesture.activeGestureId < 0) {
5235 mPointerGesture.activeGestureId =
5236 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5237#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005238 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005239 "activeGestureId=%d", mPointerGesture.activeGestureId);
5240#endif
5241 }
Jeff Brown2352b972011-04-12 22:39:53 -07005242 }
Jeff Brownace13b12011-03-09 17:39:48 -08005243 }
5244
Jeff Brownbe1aa822011-07-27 16:04:54 -07005245 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005246
Jeff Brownace13b12011-03-09 17:39:48 -08005247#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005248 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005249 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5250 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005251 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005252 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5253 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005254 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005255 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005256 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005257 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005258 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005259 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005260 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5261 id, index, properties.toolType,
5262 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005263 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5264 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5265 }
5266 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005267 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005268 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005269 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005270 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005271 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005272 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5273 id, index, properties.toolType,
5274 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005275 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5276 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5277 }
5278#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005279 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005280}
5281
Jeff Brown65fd2512011-08-18 11:20:58 -07005282void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5283 mPointerSimple.currentCoords.clear();
5284 mPointerSimple.currentProperties.clear();
5285
5286 bool down, hovering;
5287 if (!mCurrentStylusIdBits.isEmpty()) {
5288 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5289 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5290 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5291 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5292 mPointerController->setPosition(x, y);
5293
5294 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5295 down = !hovering;
5296
5297 mPointerController->getPosition(&x, &y);
5298 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5299 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5300 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5301 mPointerSimple.currentProperties.id = 0;
5302 mPointerSimple.currentProperties.toolType =
5303 mCurrentCookedPointerData.pointerProperties[index].toolType;
5304 } else {
5305 down = false;
5306 hovering = false;
5307 }
5308
5309 dispatchPointerSimple(when, policyFlags, down, hovering);
5310}
5311
5312void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5313 abortPointerSimple(when, policyFlags);
5314}
5315
5316void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5317 mPointerSimple.currentCoords.clear();
5318 mPointerSimple.currentProperties.clear();
5319
5320 bool down, hovering;
5321 if (!mCurrentMouseIdBits.isEmpty()) {
5322 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5323 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5324 if (mLastMouseIdBits.hasBit(id)) {
5325 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5326 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5327 - mLastRawPointerData.pointers[lastIndex].x)
5328 * mPointerXMovementScale;
5329 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5330 - mLastRawPointerData.pointers[lastIndex].y)
5331 * mPointerYMovementScale;
5332
5333 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5334 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5335
5336 mPointerController->move(deltaX, deltaY);
5337 } else {
5338 mPointerVelocityControl.reset();
5339 }
5340
5341 down = isPointerDown(mCurrentButtonState);
5342 hovering = !down;
5343
5344 float x, y;
5345 mPointerController->getPosition(&x, &y);
5346 mPointerSimple.currentCoords.copyFrom(
5347 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5348 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5349 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5350 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5351 hovering ? 0.0f : 1.0f);
5352 mPointerSimple.currentProperties.id = 0;
5353 mPointerSimple.currentProperties.toolType =
5354 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5355 } else {
5356 mPointerVelocityControl.reset();
5357
5358 down = false;
5359 hovering = false;
5360 }
5361
5362 dispatchPointerSimple(when, policyFlags, down, hovering);
5363}
5364
5365void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5366 abortPointerSimple(when, policyFlags);
5367
5368 mPointerVelocityControl.reset();
5369}
5370
5371void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5372 bool down, bool hovering) {
5373 int32_t metaState = getContext()->getGlobalMetaState();
5374
5375 if (mPointerController != NULL) {
5376 if (down || hovering) {
5377 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5378 mPointerController->clearSpots();
5379 mPointerController->setButtonState(mCurrentButtonState);
5380 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5381 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5382 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5383 }
5384 }
5385
5386 if (mPointerSimple.down && !down) {
5387 mPointerSimple.down = false;
5388
5389 // Send up.
5390 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5391 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005392 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005393 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5394 mOrientedXPrecision, mOrientedYPrecision,
5395 mPointerSimple.downTime);
5396 getListener()->notifyMotion(&args);
5397 }
5398
5399 if (mPointerSimple.hovering && !hovering) {
5400 mPointerSimple.hovering = false;
5401
5402 // Send hover exit.
5403 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5404 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005405 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005406 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5407 mOrientedXPrecision, mOrientedYPrecision,
5408 mPointerSimple.downTime);
5409 getListener()->notifyMotion(&args);
5410 }
5411
5412 if (down) {
5413 if (!mPointerSimple.down) {
5414 mPointerSimple.down = true;
5415 mPointerSimple.downTime = when;
5416
5417 // Send down.
5418 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5419 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005420 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005421 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5422 mOrientedXPrecision, mOrientedYPrecision,
5423 mPointerSimple.downTime);
5424 getListener()->notifyMotion(&args);
5425 }
5426
5427 // Send move.
5428 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5429 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005430 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005431 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5432 mOrientedXPrecision, mOrientedYPrecision,
5433 mPointerSimple.downTime);
5434 getListener()->notifyMotion(&args);
5435 }
5436
5437 if (hovering) {
5438 if (!mPointerSimple.hovering) {
5439 mPointerSimple.hovering = true;
5440
5441 // Send hover enter.
5442 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5443 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005444 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005445 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5446 mOrientedXPrecision, mOrientedYPrecision,
5447 mPointerSimple.downTime);
5448 getListener()->notifyMotion(&args);
5449 }
5450
5451 // Send hover move.
5452 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5453 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005454 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005455 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5456 mOrientedXPrecision, mOrientedYPrecision,
5457 mPointerSimple.downTime);
5458 getListener()->notifyMotion(&args);
5459 }
5460
5461 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5462 float vscroll = mCurrentRawVScroll;
5463 float hscroll = mCurrentRawHScroll;
5464 mWheelYVelocityControl.move(when, NULL, &vscroll);
5465 mWheelXVelocityControl.move(when, &hscroll, NULL);
5466
5467 // Send scroll.
5468 PointerCoords pointerCoords;
5469 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5470 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5471 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5472
5473 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5474 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005475 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005476 1, &mPointerSimple.currentProperties, &pointerCoords,
5477 mOrientedXPrecision, mOrientedYPrecision,
5478 mPointerSimple.downTime);
5479 getListener()->notifyMotion(&args);
5480 }
5481
5482 // Save state.
5483 if (down || hovering) {
5484 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5485 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5486 } else {
5487 mPointerSimple.reset();
5488 }
5489}
5490
5491void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5492 mPointerSimple.currentCoords.clear();
5493 mPointerSimple.currentProperties.clear();
5494
5495 dispatchPointerSimple(when, policyFlags, false, false);
5496}
5497
Jeff Brown4dac9012013-04-10 01:03:19 -07005498void TouchInputMapper::dispatchNavigationAssist(nsecs_t when, uint32_t policyFlags) {
5499 if (mCurrentCookedPointerData.touchingIdBits.count() == 1) {
5500 if (mLastCookedPointerData.touchingIdBits.isEmpty()) {
5501 // First pointer down.
5502 uint32_t id = mCurrentCookedPointerData.touchingIdBits.firstMarkedBit();
5503 const PointerCoords& coords = mCurrentCookedPointerData.pointerCoordsForId(id);
5504 if (coords.getY() >= mNavigationAssistStartY) {
5505 // Start tracking the possible assist swipe.
5506 mNavigation.activeAssistId = id;
5507 return;
5508 }
5509 } else if (mNavigation.activeAssistId >= 0
5510 && mCurrentCookedPointerData.touchingIdBits.hasBit(mNavigation.activeAssistId)) {
5511 const PointerCoords& coords = mCurrentCookedPointerData.pointerCoordsForId(
5512 mNavigation.activeAssistId);
5513 if (coords.getY() > mNavigationAssistEndY) {
5514 // Swipe is still in progress.
5515 return;
5516 }
5517
5518 // Detected assist swipe.
5519 int32_t metaState = mContext->getGlobalMetaState();
5520 NotifyKeyArgs downArgs(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD,
5521 policyFlags | POLICY_FLAG_VIRTUAL,
5522 AKEY_EVENT_ACTION_DOWN, 0, AKEYCODE_ASSIST, 0, metaState, when);
5523 getListener()->notifyKey(&downArgs);
5524
5525 NotifyKeyArgs upArgs(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD,
5526 policyFlags | POLICY_FLAG_VIRTUAL,
5527 AKEY_EVENT_ACTION_UP, 0, AKEYCODE_ASSIST, 0, metaState, when);
5528 getListener()->notifyKey(&upArgs);
5529 }
5530 }
5531
5532 // Cancel the assist swipe.
5533 mNavigation.activeAssistId = -1;
5534}
5535
Jeff Brownace13b12011-03-09 17:39:48 -08005536void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005537 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5538 const PointerProperties* properties, const PointerCoords* coords,
5539 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005540 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5541 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005542 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005543 uint32_t pointerCount = 0;
5544 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005545 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005546 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005547 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005548 pointerCoords[pointerCount].copyFrom(coords[index]);
5549
5550 if (changedId >= 0 && id == uint32_t(changedId)) {
5551 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5552 }
5553
5554 pointerCount += 1;
5555 }
5556
Steve Blockec193de2012-01-09 18:35:44 +00005557 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005558
5559 if (changedId >= 0 && pointerCount == 1) {
5560 // Replace initial down and final up action.
5561 // We can compare the action without masking off the changed pointer index
5562 // because we know the index is 0.
5563 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5564 action = AMOTION_EVENT_ACTION_DOWN;
5565 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5566 action = AMOTION_EVENT_ACTION_UP;
5567 } else {
5568 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005569 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005570 }
5571 }
5572
Jeff Brownbe1aa822011-07-27 16:04:54 -07005573 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005574 action, flags, metaState, buttonState, edgeFlags,
Jeff Brown83d616a2012-09-09 20:33:43 -07005575 mViewport.displayId, pointerCount, pointerProperties, pointerCoords,
5576 xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005577 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005578}
5579
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005580bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005581 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005582 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5583 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005584 bool changed = false;
5585 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005586 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005587 uint32_t inIndex = inIdToIndex[id];
5588 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005589
5590 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005591 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005592 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005593 PointerCoords& curOutCoords = outCoords[outIndex];
5594
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005595 if (curInProperties != curOutProperties) {
5596 curOutProperties.copyFrom(curInProperties);
5597 changed = true;
5598 }
5599
Jeff Brownace13b12011-03-09 17:39:48 -08005600 if (curInCoords != curOutCoords) {
5601 curOutCoords.copyFrom(curInCoords);
5602 changed = true;
5603 }
5604 }
5605 return changed;
5606}
5607
5608void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005609 if (mPointerController != NULL) {
5610 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5611 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005612}
5613
Jeff Brownbe1aa822011-07-27 16:04:54 -07005614bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5615 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5616 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005617}
5618
Jeff Brownbe1aa822011-07-27 16:04:54 -07005619const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005620 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005621 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005622 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005623 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005624
5625#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005626 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005627 "left=%d, top=%d, right=%d, bottom=%d",
5628 x, y,
5629 virtualKey.keyCode, virtualKey.scanCode,
5630 virtualKey.hitLeft, virtualKey.hitTop,
5631 virtualKey.hitRight, virtualKey.hitBottom);
5632#endif
5633
5634 if (virtualKey.isHit(x, y)) {
5635 return & virtualKey;
5636 }
5637 }
5638
5639 return NULL;
5640}
5641
Jeff Brownbe1aa822011-07-27 16:04:54 -07005642void TouchInputMapper::assignPointerIds() {
5643 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5644 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5645
5646 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005647
5648 if (currentPointerCount == 0) {
5649 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005650 return;
5651 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005652
Jeff Brownbe1aa822011-07-27 16:04:54 -07005653 if (lastPointerCount == 0) {
5654 // All pointers are new.
5655 for (uint32_t i = 0; i < currentPointerCount; i++) {
5656 uint32_t id = i;
5657 mCurrentRawPointerData.pointers[i].id = id;
5658 mCurrentRawPointerData.idToIndex[id] = i;
5659 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5660 }
5661 return;
5662 }
5663
5664 if (currentPointerCount == 1 && lastPointerCount == 1
5665 && mCurrentRawPointerData.pointers[0].toolType
5666 == mLastRawPointerData.pointers[0].toolType) {
5667 // Only one pointer and no change in count so it must have the same id as before.
5668 uint32_t id = mLastRawPointerData.pointers[0].id;
5669 mCurrentRawPointerData.pointers[0].id = id;
5670 mCurrentRawPointerData.idToIndex[id] = 0;
5671 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5672 return;
5673 }
5674
5675 // General case.
5676 // We build a heap of squared euclidean distances between current and last pointers
5677 // associated with the current and last pointer indices. Then, we find the best
5678 // match (by distance) for each current pointer.
5679 // The pointers must have the same tool type but it is possible for them to
5680 // transition from hovering to touching or vice-versa while retaining the same id.
5681 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5682
5683 uint32_t heapSize = 0;
5684 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5685 currentPointerIndex++) {
5686 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5687 lastPointerIndex++) {
5688 const RawPointerData::Pointer& currentPointer =
5689 mCurrentRawPointerData.pointers[currentPointerIndex];
5690 const RawPointerData::Pointer& lastPointer =
5691 mLastRawPointerData.pointers[lastPointerIndex];
5692 if (currentPointer.toolType == lastPointer.toolType) {
5693 int64_t deltaX = currentPointer.x - lastPointer.x;
5694 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005695
5696 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5697
5698 // Insert new element into the heap (sift up).
5699 heap[heapSize].currentPointerIndex = currentPointerIndex;
5700 heap[heapSize].lastPointerIndex = lastPointerIndex;
5701 heap[heapSize].distance = distance;
5702 heapSize += 1;
5703 }
5704 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005705 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005706
Jeff Brownbe1aa822011-07-27 16:04:54 -07005707 // Heapify
5708 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5709 startIndex -= 1;
5710 for (uint32_t parentIndex = startIndex; ;) {
5711 uint32_t childIndex = parentIndex * 2 + 1;
5712 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005713 break;
5714 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005715
5716 if (childIndex + 1 < heapSize
5717 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5718 childIndex += 1;
5719 }
5720
5721 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5722 break;
5723 }
5724
5725 swap(heap[parentIndex], heap[childIndex]);
5726 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005727 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005728 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005729
5730#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005731 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005732 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005733 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005734 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5735 heap[i].distance);
5736 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005737#endif
5738
Jeff Brownbe1aa822011-07-27 16:04:54 -07005739 // Pull matches out by increasing order of distance.
5740 // To avoid reassigning pointers that have already been matched, the loop keeps track
5741 // of which last and current pointers have been matched using the matchedXXXBits variables.
5742 // It also tracks the used pointer id bits.
5743 BitSet32 matchedLastBits(0);
5744 BitSet32 matchedCurrentBits(0);
5745 BitSet32 usedIdBits(0);
5746 bool first = true;
5747 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5748 while (heapSize > 0) {
5749 if (first) {
5750 // The first time through the loop, we just consume the root element of
5751 // the heap (the one with smallest distance).
5752 first = false;
5753 } else {
5754 // Previous iterations consumed the root element of the heap.
5755 // Pop root element off of the heap (sift down).
5756 heap[0] = heap[heapSize];
5757 for (uint32_t parentIndex = 0; ;) {
5758 uint32_t childIndex = parentIndex * 2 + 1;
5759 if (childIndex >= heapSize) {
5760 break;
5761 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005762
Jeff Brownbe1aa822011-07-27 16:04:54 -07005763 if (childIndex + 1 < heapSize
5764 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5765 childIndex += 1;
5766 }
5767
5768 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5769 break;
5770 }
5771
5772 swap(heap[parentIndex], heap[childIndex]);
5773 parentIndex = childIndex;
5774 }
5775
5776#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005777 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005778 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005779 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005780 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5781 heap[i].distance);
5782 }
5783#endif
5784 }
5785
5786 heapSize -= 1;
5787
5788 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5789 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5790
5791 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5792 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5793
5794 matchedCurrentBits.markBit(currentPointerIndex);
5795 matchedLastBits.markBit(lastPointerIndex);
5796
5797 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5798 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5799 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5800 mCurrentRawPointerData.markIdBit(id,
5801 mCurrentRawPointerData.isHovering(currentPointerIndex));
5802 usedIdBits.markBit(id);
5803
5804#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005805 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005806 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5807#endif
5808 break;
5809 }
5810 }
5811
5812 // Assign fresh ids to pointers that were not matched in the process.
5813 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5814 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5815 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5816
5817 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5818 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5819 mCurrentRawPointerData.markIdBit(id,
5820 mCurrentRawPointerData.isHovering(currentPointerIndex));
5821
5822#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005823 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005824 currentPointerIndex, id);
5825#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005826 }
5827}
5828
Jeff Brown6d0fec22010-07-23 21:28:06 -07005829int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005830 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5831 return AKEY_STATE_VIRTUAL;
5832 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005833
Jeff Brownbe1aa822011-07-27 16:04:54 -07005834 size_t numVirtualKeys = mVirtualKeys.size();
5835 for (size_t i = 0; i < numVirtualKeys; i++) {
5836 const VirtualKey& virtualKey = mVirtualKeys[i];
5837 if (virtualKey.keyCode == keyCode) {
5838 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005839 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005840 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005841
5842 return AKEY_STATE_UNKNOWN;
5843}
5844
5845int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005846 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5847 return AKEY_STATE_VIRTUAL;
5848 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005849
Jeff Brownbe1aa822011-07-27 16:04:54 -07005850 size_t numVirtualKeys = mVirtualKeys.size();
5851 for (size_t i = 0; i < numVirtualKeys; i++) {
5852 const VirtualKey& virtualKey = mVirtualKeys[i];
5853 if (virtualKey.scanCode == scanCode) {
5854 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005855 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005856 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005857
5858 return AKEY_STATE_UNKNOWN;
5859}
5860
5861bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5862 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005863 size_t numVirtualKeys = mVirtualKeys.size();
5864 for (size_t i = 0; i < numVirtualKeys; i++) {
5865 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005866
Jeff Brownbe1aa822011-07-27 16:04:54 -07005867 for (size_t i = 0; i < numCodes; i++) {
5868 if (virtualKey.keyCode == keyCodes[i]) {
5869 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005870 }
5871 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005872 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005873
5874 return true;
5875}
5876
5877
5878// --- SingleTouchInputMapper ---
5879
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005880SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5881 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005882}
5883
5884SingleTouchInputMapper::~SingleTouchInputMapper() {
5885}
5886
Jeff Brown65fd2512011-08-18 11:20:58 -07005887void SingleTouchInputMapper::reset(nsecs_t when) {
5888 mSingleTouchMotionAccumulator.reset(getDevice());
5889
5890 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005891}
5892
Jeff Brown6d0fec22010-07-23 21:28:06 -07005893void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005894 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005895
Jeff Brown65fd2512011-08-18 11:20:58 -07005896 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005897}
5898
Jeff Brown65fd2512011-08-18 11:20:58 -07005899void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005900 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005901 mCurrentRawPointerData.pointerCount = 1;
5902 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005903
Jeff Brown65fd2512011-08-18 11:20:58 -07005904 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5905 && (mTouchButtonAccumulator.isHovering()
5906 || (mRawPointerAxes.pressure.valid
5907 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005908 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005909
Jeff Brownbe1aa822011-07-27 16:04:54 -07005910 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005911 outPointer.id = 0;
5912 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5913 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5914 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5915 outPointer.touchMajor = 0;
5916 outPointer.touchMinor = 0;
5917 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5918 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5919 outPointer.orientation = 0;
5920 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005921 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5922 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005923 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5924 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5925 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5926 }
5927 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005928 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005929}
5930
Jeff Brownbe1aa822011-07-27 16:04:54 -07005931void SingleTouchInputMapper::configureRawPointerAxes() {
5932 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005933
Jeff Brownbe1aa822011-07-27 16:04:54 -07005934 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5935 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5936 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5937 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5938 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005939 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5940 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005941}
5942
Jeff Brown00710e92012-04-19 15:18:26 -07005943bool SingleTouchInputMapper::hasStylus() const {
5944 return mTouchButtonAccumulator.hasStylus();
5945}
5946
Jeff Brown6d0fec22010-07-23 21:28:06 -07005947
5948// --- MultiTouchInputMapper ---
5949
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005950MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005951 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005952}
5953
5954MultiTouchInputMapper::~MultiTouchInputMapper() {
5955}
5956
Jeff Brown65fd2512011-08-18 11:20:58 -07005957void MultiTouchInputMapper::reset(nsecs_t when) {
5958 mMultiTouchMotionAccumulator.reset(getDevice());
5959
Jeff Brown6894a292011-07-01 17:59:27 -07005960 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005961
Jeff Brown65fd2512011-08-18 11:20:58 -07005962 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005963}
5964
5965void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005966 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005967
Jeff Brown65fd2512011-08-18 11:20:58 -07005968 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005969}
5970
Jeff Brown65fd2512011-08-18 11:20:58 -07005971void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005972 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005973 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005974 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005975
Jeff Brown80fd47c2011-05-24 01:07:44 -07005976 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005977 const MultiTouchMotionAccumulator::Slot* inSlot =
5978 mMultiTouchMotionAccumulator.getSlot(inIndex);
5979 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005980 continue;
5981 }
5982
Jeff Brown80fd47c2011-05-24 01:07:44 -07005983 if (outCount >= MAX_POINTERS) {
5984#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005985 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005986 "ignoring the rest.",
5987 getDeviceName().string(), MAX_POINTERS);
5988#endif
5989 break; // too many fingers!
5990 }
5991
Jeff Brownbe1aa822011-07-27 16:04:54 -07005992 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005993 outPointer.x = inSlot->getX();
5994 outPointer.y = inSlot->getY();
5995 outPointer.pressure = inSlot->getPressure();
5996 outPointer.touchMajor = inSlot->getTouchMajor();
5997 outPointer.touchMinor = inSlot->getTouchMinor();
5998 outPointer.toolMajor = inSlot->getToolMajor();
5999 outPointer.toolMinor = inSlot->getToolMinor();
6000 outPointer.orientation = inSlot->getOrientation();
6001 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07006002 outPointer.tiltX = 0;
6003 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006004
Jeff Brown49754db2011-07-01 17:37:58 -07006005 outPointer.toolType = inSlot->getToolType();
6006 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
6007 outPointer.toolType = mTouchButtonAccumulator.getToolType();
6008 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
6009 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
6010 }
Jeff Brown8d608662010-08-30 03:02:23 -07006011 }
6012
Jeff Brown65fd2512011-08-18 11:20:58 -07006013 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
6014 && (mTouchButtonAccumulator.isHovering()
6015 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07006016 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006017
Jeff Brown8d608662010-08-30 03:02:23 -07006018 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07006019 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07006020 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07006021 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07006022 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07006023 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07006024 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07006025 if (mPointerTrackingIdMap[n] == trackingId) {
6026 id = n;
6027 }
6028 }
6029
6030 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07006031 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07006032 mPointerTrackingIdMap[id] = trackingId;
6033 }
6034 }
6035 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07006036 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006037 mCurrentRawPointerData.clearIdBits();
6038 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07006039 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07006040 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006041 mCurrentRawPointerData.idToIndex[id] = outCount;
6042 mCurrentRawPointerData.markIdBit(id, isHovering);
6043 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006044 }
6045 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006046
Jeff Brown6d0fec22010-07-23 21:28:06 -07006047 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006048 }
6049
Jeff Brownbe1aa822011-07-27 16:04:54 -07006050 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006051 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07006052
Jeff Brown65fd2512011-08-18 11:20:58 -07006053 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006054}
6055
Jeff Brownbe1aa822011-07-27 16:04:54 -07006056void MultiTouchInputMapper::configureRawPointerAxes() {
6057 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006058
Jeff Brownbe1aa822011-07-27 16:04:54 -07006059 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
6060 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
6061 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
6062 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
6063 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
6064 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
6065 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
6066 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
6067 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
6068 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
6069 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006070
Jeff Brownbe1aa822011-07-27 16:04:54 -07006071 if (mRawPointerAxes.trackingId.valid
6072 && mRawPointerAxes.slot.valid
6073 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
6074 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07006075 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00006076 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006077 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07006078 getDeviceName().string(), slotCount, MAX_SLOTS);
6079 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07006080 }
Jeff Brown00710e92012-04-19 15:18:26 -07006081 mMultiTouchMotionAccumulator.configure(getDevice(),
6082 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006083 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07006084 mMultiTouchMotionAccumulator.configure(getDevice(),
6085 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006086 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07006087}
6088
Jeff Brown00710e92012-04-19 15:18:26 -07006089bool MultiTouchInputMapper::hasStylus() const {
6090 return mMultiTouchMotionAccumulator.hasStylus()
6091 || mTouchButtonAccumulator.hasStylus();
6092}
6093
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006094
Jeff Browncb1404e2011-01-15 18:14:15 -08006095// --- JoystickInputMapper ---
6096
6097JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
6098 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006099}
6100
6101JoystickInputMapper::~JoystickInputMapper() {
6102}
6103
6104uint32_t JoystickInputMapper::getSources() {
6105 return AINPUT_SOURCE_JOYSTICK;
6106}
6107
6108void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
6109 InputMapper::populateDeviceInfo(info);
6110
Jeff Brown6f2fba42011-02-19 01:08:02 -08006111 for (size_t i = 0; i < mAxes.size(); i++) {
6112 const Axis& axis = mAxes.valueAt(i);
Michael Wright2b08c612013-04-24 20:05:10 -07006113 addMotionRange(axis.axisInfo.axis, axis, info);
6114
Jeff Brown85297452011-03-04 13:07:49 -08006115 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Michael Wright2b08c612013-04-24 20:05:10 -07006116 addMotionRange(axis.axisInfo.highAxis, axis, info);
6117
Jeff Brown85297452011-03-04 13:07:49 -08006118 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006119 }
6120}
6121
Michael Wright2b08c612013-04-24 20:05:10 -07006122void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis,
6123 InputDeviceInfo* info) {
6124 info->addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK,
6125 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
6126 /* In order to ease the transition for developers from using the old axes
6127 * to the newer, more semantically correct axes, we'll continue to register
6128 * the old axes as duplicates of their corresponding new ones. */
6129 int32_t compatAxis = getCompatAxis(axisId);
6130 if (compatAxis >= 0) {
6131 info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK,
6132 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
6133 }
6134}
6135
6136/* A mapping from axes the joystick actually has to the axes that should be
6137 * artificially created for compatibility purposes.
6138 * Returns -1 if no compatibility axis is needed. */
6139int32_t JoystickInputMapper::getCompatAxis(int32_t axis) {
6140 switch(axis) {
6141 case AMOTION_EVENT_AXIS_LTRIGGER:
6142 return AMOTION_EVENT_AXIS_BRAKE;
6143 case AMOTION_EVENT_AXIS_RTRIGGER:
6144 return AMOTION_EVENT_AXIS_GAS;
6145 }
6146 return -1;
6147}
6148
Jeff Browncb1404e2011-01-15 18:14:15 -08006149void JoystickInputMapper::dump(String8& dump) {
6150 dump.append(INDENT2 "Joystick Input Mapper:\n");
6151
Jeff Brown6f2fba42011-02-19 01:08:02 -08006152 dump.append(INDENT3 "Axes:\n");
6153 size_t numAxes = mAxes.size();
6154 for (size_t i = 0; i < numAxes; i++) {
6155 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006156 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006157 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08006158 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006159 } else {
Jeff Brown85297452011-03-04 13:07:49 -08006160 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006161 }
Jeff Brown85297452011-03-04 13:07:49 -08006162 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6163 label = getAxisLabel(axis.axisInfo.highAxis);
6164 if (label) {
6165 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
6166 } else {
6167 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
6168 axis.axisInfo.splitValue);
6169 }
6170 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
6171 dump.append(" (invert)");
6172 }
6173
Michael Wrightc6091c62013-04-01 20:56:04 -07006174 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n",
6175 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
Jeff Brown85297452011-03-04 13:07:49 -08006176 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
6177 "highScale=%0.5f, highOffset=%0.5f\n",
6178 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07006179 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
6180 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006181 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006182 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006183 }
6184}
6185
Jeff Brown65fd2512011-08-18 11:20:58 -07006186void JoystickInputMapper::configure(nsecs_t when,
6187 const InputReaderConfiguration* config, uint32_t changes) {
6188 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006189
Jeff Brown474dcb52011-06-14 20:22:50 -07006190 if (!changes) { // first time only
6191 // Collect all axes.
6192 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07006193 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6194 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6195 continue; // axis must be claimed by a different device
6196 }
6197
Jeff Brown474dcb52011-06-14 20:22:50 -07006198 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006199 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006200 if (rawAxisInfo.valid) {
6201 // Map axis.
6202 AxisInfo axisInfo;
6203 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6204 if (!explicitlyMapped) {
6205 // Axis is not explicitly mapped, will choose a generic axis later.
6206 axisInfo.mode = AxisInfo::MODE_NORMAL;
6207 axisInfo.axis = -1;
6208 }
6209
6210 // Apply flat override.
6211 int32_t rawFlat = axisInfo.flatOverride < 0
6212 ? rawAxisInfo.flat : axisInfo.flatOverride;
6213
6214 // Calculate scaling factors and limits.
6215 Axis axis;
6216 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6217 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6218 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6219 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6220 scale, 0.0f, highScale, 0.0f,
Michael Wrightc6091c62013-04-01 20:56:04 -07006221 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6222 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006223 } else if (isCenteredAxis(axisInfo.axis)) {
6224 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6225 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6226 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6227 scale, offset, scale, offset,
Michael Wrightc6091c62013-04-01 20:56:04 -07006228 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6229 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006230 } else {
6231 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6232 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6233 scale, 0.0f, scale, 0.0f,
Michael Wrightc6091c62013-04-01 20:56:04 -07006234 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6235 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006236 }
6237
6238 // To eliminate noise while the joystick is at rest, filter out small variations
6239 // in axis values up front.
6240 axis.filter = axis.flat * 0.25f;
6241
6242 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006243 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006244 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006245
Jeff Brown474dcb52011-06-14 20:22:50 -07006246 // If there are too many axes, start dropping them.
6247 // Prefer to keep explicitly mapped axes.
6248 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006249 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006250 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6251 pruneAxes(true);
6252 pruneAxes(false);
6253 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006254
Jeff Brown474dcb52011-06-14 20:22:50 -07006255 // Assign generic axis ids to remaining axes.
6256 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6257 size_t numAxes = mAxes.size();
6258 for (size_t i = 0; i < numAxes; i++) {
6259 Axis& axis = mAxes.editValueAt(i);
6260 if (axis.axisInfo.axis < 0) {
6261 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6262 && haveAxis(nextGenericAxisId)) {
6263 nextGenericAxisId += 1;
6264 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006265
Jeff Brown474dcb52011-06-14 20:22:50 -07006266 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6267 axis.axisInfo.axis = nextGenericAxisId;
6268 nextGenericAxisId += 1;
6269 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006270 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006271 "have already been assigned to other axes.",
6272 getDeviceName().string(), mAxes.keyAt(i));
6273 mAxes.removeItemsAt(i--);
6274 numAxes -= 1;
6275 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006276 }
6277 }
6278 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006279}
6280
Jeff Brown85297452011-03-04 13:07:49 -08006281bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006282 size_t numAxes = mAxes.size();
6283 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006284 const Axis& axis = mAxes.valueAt(i);
6285 if (axis.axisInfo.axis == axisId
6286 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6287 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006288 return true;
6289 }
6290 }
6291 return false;
6292}
Jeff Browncb1404e2011-01-15 18:14:15 -08006293
Jeff Brown6f2fba42011-02-19 01:08:02 -08006294void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6295 size_t i = mAxes.size();
6296 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6297 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6298 continue;
6299 }
Steve Block6215d3f2012-01-04 20:05:49 +00006300 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006301 getDeviceName().string(), mAxes.keyAt(i));
6302 mAxes.removeItemsAt(i);
6303 }
6304}
6305
6306bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6307 switch (axis) {
6308 case AMOTION_EVENT_AXIS_X:
6309 case AMOTION_EVENT_AXIS_Y:
6310 case AMOTION_EVENT_AXIS_Z:
6311 case AMOTION_EVENT_AXIS_RX:
6312 case AMOTION_EVENT_AXIS_RY:
6313 case AMOTION_EVENT_AXIS_RZ:
6314 case AMOTION_EVENT_AXIS_HAT_X:
6315 case AMOTION_EVENT_AXIS_HAT_Y:
6316 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006317 case AMOTION_EVENT_AXIS_RUDDER:
6318 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006319 return true;
6320 default:
6321 return false;
6322 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006323}
6324
Jeff Brown65fd2512011-08-18 11:20:58 -07006325void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006326 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006327 size_t numAxes = mAxes.size();
6328 for (size_t i = 0; i < numAxes; i++) {
6329 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006330 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006331 }
6332
Jeff Brown65fd2512011-08-18 11:20:58 -07006333 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006334}
6335
6336void JoystickInputMapper::process(const RawEvent* rawEvent) {
6337 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006338 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006339 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006340 if (index >= 0) {
6341 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006342 float newValue, highNewValue;
6343 switch (axis.axisInfo.mode) {
6344 case AxisInfo::MODE_INVERT:
6345 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6346 * axis.scale + axis.offset;
6347 highNewValue = 0.0f;
6348 break;
6349 case AxisInfo::MODE_SPLIT:
6350 if (rawEvent->value < axis.axisInfo.splitValue) {
6351 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6352 * axis.scale + axis.offset;
6353 highNewValue = 0.0f;
6354 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6355 newValue = 0.0f;
6356 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6357 * axis.highScale + axis.highOffset;
6358 } else {
6359 newValue = 0.0f;
6360 highNewValue = 0.0f;
6361 }
6362 break;
6363 default:
6364 newValue = rawEvent->value * axis.scale + axis.offset;
6365 highNewValue = 0.0f;
6366 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006367 }
Jeff Brown85297452011-03-04 13:07:49 -08006368 axis.newValue = newValue;
6369 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006370 }
6371 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006372 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006373
6374 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006375 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006376 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006377 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006378 break;
6379 }
6380 break;
6381 }
6382}
6383
Jeff Brown6f2fba42011-02-19 01:08:02 -08006384void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006385 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006386 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006387 }
6388
6389 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006390 int32_t buttonState = 0;
6391
6392 PointerProperties pointerProperties;
6393 pointerProperties.clear();
6394 pointerProperties.id = 0;
6395 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006396
Jeff Brown6f2fba42011-02-19 01:08:02 -08006397 PointerCoords pointerCoords;
6398 pointerCoords.clear();
6399
6400 size_t numAxes = mAxes.size();
6401 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006402 const Axis& axis = mAxes.valueAt(i);
Michael Wright2b08c612013-04-24 20:05:10 -07006403 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue);
Jeff Brown85297452011-03-04 13:07:49 -08006404 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Michael Wright2b08c612013-04-24 20:05:10 -07006405 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis,
6406 axis.highCurrentValue);
Jeff Brown85297452011-03-04 13:07:49 -08006407 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006408 }
6409
Jeff Brown83d616a2012-09-09 20:33:43 -07006410 // Moving a joystick axis should not wake the device because joysticks can
Jeff Brown56194eb2011-03-02 19:23:13 -08006411 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6412 // button will likely wake the device.
6413 // TODO: Use the input device configuration to control this behavior more finely.
6414 uint32_t policyFlags = 0;
6415
Jeff Brownbe1aa822011-07-27 16:04:54 -07006416 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006417 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07006418 ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006419 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006420}
6421
Michael Wright2b08c612013-04-24 20:05:10 -07006422void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords,
6423 int32_t axis, float value) {
6424 pointerCoords->setAxisValue(axis, value);
6425 /* In order to ease the transition for developers from using the old axes
6426 * to the newer, more semantically correct axes, we'll continue to produce
6427 * values for the old axes as mirrors of the value of their corresponding
6428 * new axes. */
6429 int32_t compatAxis = getCompatAxis(axis);
6430 if (compatAxis >= 0) {
6431 pointerCoords->setAxisValue(compatAxis, value);
6432 }
6433}
6434
Jeff Brown85297452011-03-04 13:07:49 -08006435bool JoystickInputMapper::filterAxes(bool force) {
6436 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006437 size_t numAxes = mAxes.size();
6438 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006439 Axis& axis = mAxes.editValueAt(i);
6440 if (force || hasValueChangedSignificantly(axis.filter,
6441 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6442 axis.currentValue = axis.newValue;
6443 atLeastOneSignificantChange = true;
6444 }
6445 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6446 if (force || hasValueChangedSignificantly(axis.filter,
6447 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6448 axis.highCurrentValue = axis.highNewValue;
6449 atLeastOneSignificantChange = true;
6450 }
6451 }
6452 }
6453 return atLeastOneSignificantChange;
6454}
6455
6456bool JoystickInputMapper::hasValueChangedSignificantly(
6457 float filter, float newValue, float currentValue, float min, float max) {
6458 if (newValue != currentValue) {
6459 // Filter out small changes in value unless the value is converging on the axis
6460 // bounds or center point. This is intended to reduce the amount of information
6461 // sent to applications by particularly noisy joysticks (such as PS3).
6462 if (fabs(newValue - currentValue) > filter
6463 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6464 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6465 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6466 return true;
6467 }
6468 }
6469 return false;
6470}
6471
6472bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6473 float filter, float newValue, float currentValue, float thresholdValue) {
6474 float newDistance = fabs(newValue - thresholdValue);
6475 if (newDistance < filter) {
6476 float oldDistance = fabs(currentValue - thresholdValue);
6477 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006478 return true;
6479 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006480 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006481 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006482}
6483
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006484} // namespace android