blob: 43d76bbd9e44d87c8f697065f398841c6f735667 [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 Brownfd035822010-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, "
885 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
886 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800887 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700888 }
889
890 size_t numMappers = mMappers.size();
891 for (size_t i = 0; i < numMappers; i++) {
892 InputMapper* mapper = mMappers[i];
893 mapper->dump(dump);
894 }
895}
896
Jeff Brown6d0fec22010-07-23 21:28:06 -0700897void InputDevice::addMapper(InputMapper* mapper) {
898 mMappers.add(mapper);
899}
900
Jeff Brown65fd2512011-08-18 11:20:58 -0700901void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700902 mSources = 0;
903
Jeff Brown474dcb52011-06-14 20:22:50 -0700904 if (!isIgnored()) {
905 if (!changes) { // first time only
906 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
907 }
908
Jeff Brown6ec6f792012-04-17 16:52:41 -0700909 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Jeff Brown61c08242012-04-19 11:14:33 -0700910 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
911 sp<KeyCharacterMap> keyboardLayout =
912 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor);
913 if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
914 bumpGeneration();
915 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700916 }
917 }
918
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700919 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
920 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
921 String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
922 if (mAlias != alias) {
923 mAlias = alias;
924 bumpGeneration();
925 }
926 }
927 }
928
Jeff Brown474dcb52011-06-14 20:22:50 -0700929 size_t numMappers = mMappers.size();
930 for (size_t i = 0; i < numMappers; i++) {
931 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700932 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700933 mSources |= mapper->getSources();
934 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700935 }
936}
937
Jeff Brown65fd2512011-08-18 11:20:58 -0700938void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700939 size_t numMappers = mMappers.size();
940 for (size_t i = 0; i < numMappers; i++) {
941 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700942 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700943 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700944
945 mContext->updateGlobalMetaState();
946
947 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700948}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700949
Jeff Brownb7198742011-03-18 18:14:26 -0700950void InputDevice::process(const RawEvent* rawEvents, size_t count) {
951 // Process all of the events in order for each mapper.
952 // We cannot simply ask each mapper to process them in bulk because mappers may
953 // have side-effects that must be interleaved. For example, joystick movement events and
954 // gamepad button presses are handled by different mappers but they should be dispatched
955 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700956 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700957 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
958#if DEBUG_RAW_EVENTS
Jeff Brownf33b2b22012-10-05 17:59:56 -0700959 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld",
960 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
961 rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700962#endif
963
Jeff Brown80fd47c2011-05-24 01:07:44 -0700964 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700965 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700966 mDropUntilNextSync = false;
967#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000968 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700969#endif
970 } else {
971#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000972 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700973#endif
974 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700975 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700976 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700977 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700978 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700979 } else {
980 for (size_t i = 0; i < numMappers; i++) {
981 InputMapper* mapper = mMappers[i];
982 mapper->process(rawEvent);
983 }
Jeff Brownb7198742011-03-18 18:14:26 -0700984 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700985 }
986}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700987
Jeff Brownaa3855d2011-03-17 01:34:19 -0700988void InputDevice::timeoutExpired(nsecs_t when) {
989 size_t numMappers = mMappers.size();
990 for (size_t i = 0; i < numMappers; i++) {
991 InputMapper* mapper = mMappers[i];
992 mapper->timeoutExpired(when);
993 }
994}
995
Jeff Brown6d0fec22010-07-23 21:28:06 -0700996void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Browndaa37532012-05-01 15:54:03 -0700997 outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700998
999 size_t numMappers = mMappers.size();
1000 for (size_t i = 0; i < numMappers; i++) {
1001 InputMapper* mapper = mMappers[i];
1002 mapper->populateDeviceInfo(outDeviceInfo);
1003 }
1004}
1005
1006int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1007 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1008}
1009
1010int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1011 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1012}
1013
1014int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1015 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1016}
1017
1018int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1019 int32_t result = AKEY_STATE_UNKNOWN;
1020 size_t numMappers = mMappers.size();
1021 for (size_t i = 0; i < numMappers; i++) {
1022 InputMapper* mapper = mMappers[i];
1023 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001024 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1025 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1026 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1027 if (currentResult >= AKEY_STATE_DOWN) {
1028 return currentResult;
1029 } else if (currentResult == AKEY_STATE_UP) {
1030 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031 }
1032 }
1033 }
1034 return result;
1035}
1036
1037bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1038 const int32_t* keyCodes, uint8_t* outFlags) {
1039 bool result = false;
1040 size_t numMappers = mMappers.size();
1041 for (size_t i = 0; i < numMappers; i++) {
1042 InputMapper* mapper = mMappers[i];
1043 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1044 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1045 }
1046 }
1047 return result;
1048}
1049
Jeff Browna47425a2012-04-13 04:09:27 -07001050void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1051 int32_t token) {
1052 size_t numMappers = mMappers.size();
1053 for (size_t i = 0; i < numMappers; i++) {
1054 InputMapper* mapper = mMappers[i];
1055 mapper->vibrate(pattern, patternSize, repeat, token);
1056 }
1057}
1058
1059void InputDevice::cancelVibrate(int32_t token) {
1060 size_t numMappers = mMappers.size();
1061 for (size_t i = 0; i < numMappers; i++) {
1062 InputMapper* mapper = mMappers[i];
1063 mapper->cancelVibrate(token);
1064 }
1065}
1066
Jeff Brown6d0fec22010-07-23 21:28:06 -07001067int32_t InputDevice::getMetaState() {
1068 int32_t result = 0;
1069 size_t numMappers = mMappers.size();
1070 for (size_t i = 0; i < numMappers; i++) {
1071 InputMapper* mapper = mMappers[i];
1072 result |= mapper->getMetaState();
1073 }
1074 return result;
1075}
1076
Jeff Brown05dc66a2011-03-02 14:41:58 -08001077void InputDevice::fadePointer() {
1078 size_t numMappers = mMappers.size();
1079 for (size_t i = 0; i < numMappers; i++) {
1080 InputMapper* mapper = mMappers[i];
1081 mapper->fadePointer();
1082 }
1083}
1084
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001085void InputDevice::bumpGeneration() {
1086 mGeneration = mContext->bumpGeneration();
1087}
1088
Jeff Brown65fd2512011-08-18 11:20:58 -07001089void InputDevice::notifyReset(nsecs_t when) {
1090 NotifyDeviceResetArgs args(when, mId);
1091 mContext->getListener()->notifyDeviceReset(&args);
1092}
1093
Jeff Brown6d0fec22010-07-23 21:28:06 -07001094
Jeff Brown49754db2011-07-01 17:37:58 -07001095// --- CursorButtonAccumulator ---
1096
1097CursorButtonAccumulator::CursorButtonAccumulator() {
1098 clearButtons();
1099}
1100
Jeff Brown65fd2512011-08-18 11:20:58 -07001101void CursorButtonAccumulator::reset(InputDevice* device) {
1102 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1103 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1104 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1105 mBtnBack = device->isKeyPressed(BTN_BACK);
1106 mBtnSide = device->isKeyPressed(BTN_SIDE);
1107 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1108 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1109 mBtnTask = device->isKeyPressed(BTN_TASK);
1110}
1111
Jeff Brown49754db2011-07-01 17:37:58 -07001112void CursorButtonAccumulator::clearButtons() {
1113 mBtnLeft = 0;
1114 mBtnRight = 0;
1115 mBtnMiddle = 0;
1116 mBtnBack = 0;
1117 mBtnSide = 0;
1118 mBtnForward = 0;
1119 mBtnExtra = 0;
1120 mBtnTask = 0;
1121}
1122
1123void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1124 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001125 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001126 case BTN_LEFT:
1127 mBtnLeft = rawEvent->value;
1128 break;
1129 case BTN_RIGHT:
1130 mBtnRight = rawEvent->value;
1131 break;
1132 case BTN_MIDDLE:
1133 mBtnMiddle = rawEvent->value;
1134 break;
1135 case BTN_BACK:
1136 mBtnBack = rawEvent->value;
1137 break;
1138 case BTN_SIDE:
1139 mBtnSide = rawEvent->value;
1140 break;
1141 case BTN_FORWARD:
1142 mBtnForward = rawEvent->value;
1143 break;
1144 case BTN_EXTRA:
1145 mBtnExtra = rawEvent->value;
1146 break;
1147 case BTN_TASK:
1148 mBtnTask = rawEvent->value;
1149 break;
1150 }
1151 }
1152}
1153
1154uint32_t CursorButtonAccumulator::getButtonState() const {
1155 uint32_t result = 0;
1156 if (mBtnLeft) {
1157 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1158 }
1159 if (mBtnRight) {
1160 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1161 }
1162 if (mBtnMiddle) {
1163 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1164 }
1165 if (mBtnBack || mBtnSide) {
1166 result |= AMOTION_EVENT_BUTTON_BACK;
1167 }
1168 if (mBtnForward || mBtnExtra) {
1169 result |= AMOTION_EVENT_BUTTON_FORWARD;
1170 }
1171 return result;
1172}
1173
1174
1175// --- CursorMotionAccumulator ---
1176
Jeff Brown65fd2512011-08-18 11:20:58 -07001177CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001178 clearRelativeAxes();
1179}
1180
Jeff Brown65fd2512011-08-18 11:20:58 -07001181void CursorMotionAccumulator::reset(InputDevice* device) {
1182 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001183}
1184
1185void CursorMotionAccumulator::clearRelativeAxes() {
1186 mRelX = 0;
1187 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001188}
1189
1190void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1191 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001192 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001193 case REL_X:
1194 mRelX = rawEvent->value;
1195 break;
1196 case REL_Y:
1197 mRelY = rawEvent->value;
1198 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001199 }
1200 }
1201}
1202
1203void CursorMotionAccumulator::finishSync() {
1204 clearRelativeAxes();
1205}
1206
1207
1208// --- CursorScrollAccumulator ---
1209
1210CursorScrollAccumulator::CursorScrollAccumulator() :
1211 mHaveRelWheel(false), mHaveRelHWheel(false) {
1212 clearRelativeAxes();
1213}
1214
1215void CursorScrollAccumulator::configure(InputDevice* device) {
1216 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1217 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1218}
1219
1220void CursorScrollAccumulator::reset(InputDevice* device) {
1221 clearRelativeAxes();
1222}
1223
1224void CursorScrollAccumulator::clearRelativeAxes() {
1225 mRelWheel = 0;
1226 mRelHWheel = 0;
1227}
1228
1229void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1230 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001231 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001232 case REL_WHEEL:
1233 mRelWheel = rawEvent->value;
1234 break;
1235 case REL_HWHEEL:
1236 mRelHWheel = rawEvent->value;
1237 break;
1238 }
1239 }
1240}
1241
Jeff Brown65fd2512011-08-18 11:20:58 -07001242void CursorScrollAccumulator::finishSync() {
1243 clearRelativeAxes();
1244}
1245
Jeff Brown49754db2011-07-01 17:37:58 -07001246
1247// --- TouchButtonAccumulator ---
1248
1249TouchButtonAccumulator::TouchButtonAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001250 mHaveBtnTouch(false), mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001251 clearButtons();
1252}
1253
1254void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001255 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
Jeff Brown00710e92012-04-19 15:18:26 -07001256 mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1257 || device->hasKey(BTN_TOOL_RUBBER)
1258 || device->hasKey(BTN_TOOL_BRUSH)
1259 || device->hasKey(BTN_TOOL_PENCIL)
1260 || device->hasKey(BTN_TOOL_AIRBRUSH);
Jeff Brown65fd2512011-08-18 11:20:58 -07001261}
1262
1263void TouchButtonAccumulator::reset(InputDevice* device) {
1264 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1265 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1266 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1267 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1268 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1269 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1270 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1271 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1272 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1273 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1274 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001275 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1276 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1277 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001278}
1279
1280void TouchButtonAccumulator::clearButtons() {
1281 mBtnTouch = 0;
1282 mBtnStylus = 0;
1283 mBtnStylus2 = 0;
1284 mBtnToolFinger = 0;
1285 mBtnToolPen = 0;
1286 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001287 mBtnToolBrush = 0;
1288 mBtnToolPencil = 0;
1289 mBtnToolAirbrush = 0;
1290 mBtnToolMouse = 0;
1291 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001292 mBtnToolDoubleTap = 0;
1293 mBtnToolTripleTap = 0;
1294 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001295}
1296
1297void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1298 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001299 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001300 case BTN_TOUCH:
1301 mBtnTouch = rawEvent->value;
1302 break;
1303 case BTN_STYLUS:
1304 mBtnStylus = rawEvent->value;
1305 break;
1306 case BTN_STYLUS2:
1307 mBtnStylus2 = rawEvent->value;
1308 break;
1309 case BTN_TOOL_FINGER:
1310 mBtnToolFinger = rawEvent->value;
1311 break;
1312 case BTN_TOOL_PEN:
1313 mBtnToolPen = rawEvent->value;
1314 break;
1315 case BTN_TOOL_RUBBER:
1316 mBtnToolRubber = rawEvent->value;
1317 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001318 case BTN_TOOL_BRUSH:
1319 mBtnToolBrush = rawEvent->value;
1320 break;
1321 case BTN_TOOL_PENCIL:
1322 mBtnToolPencil = rawEvent->value;
1323 break;
1324 case BTN_TOOL_AIRBRUSH:
1325 mBtnToolAirbrush = rawEvent->value;
1326 break;
1327 case BTN_TOOL_MOUSE:
1328 mBtnToolMouse = rawEvent->value;
1329 break;
1330 case BTN_TOOL_LENS:
1331 mBtnToolLens = rawEvent->value;
1332 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001333 case BTN_TOOL_DOUBLETAP:
1334 mBtnToolDoubleTap = rawEvent->value;
1335 break;
1336 case BTN_TOOL_TRIPLETAP:
1337 mBtnToolTripleTap = rawEvent->value;
1338 break;
1339 case BTN_TOOL_QUADTAP:
1340 mBtnToolQuadTap = rawEvent->value;
1341 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001342 }
1343 }
1344}
1345
1346uint32_t TouchButtonAccumulator::getButtonState() const {
1347 uint32_t result = 0;
1348 if (mBtnStylus) {
1349 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1350 }
1351 if (mBtnStylus2) {
1352 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1353 }
1354 return result;
1355}
1356
1357int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001358 if (mBtnToolMouse || mBtnToolLens) {
1359 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1360 }
Jeff Brown49754db2011-07-01 17:37:58 -07001361 if (mBtnToolRubber) {
1362 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1363 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001364 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001365 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1366 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001367 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001368 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1369 }
1370 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1371}
1372
Jeff Brownd87c6d52011-08-10 14:55:59 -07001373bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001374 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1375 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001376 || mBtnToolMouse || mBtnToolLens
1377 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001378}
1379
1380bool TouchButtonAccumulator::isHovering() const {
1381 return mHaveBtnTouch && !mBtnTouch;
1382}
1383
Jeff Brown00710e92012-04-19 15:18:26 -07001384bool TouchButtonAccumulator::hasStylus() const {
1385 return mHaveStylus;
1386}
1387
Jeff Brown49754db2011-07-01 17:37:58 -07001388
Jeff Brownbe1aa822011-07-27 16:04:54 -07001389// --- RawPointerAxes ---
1390
1391RawPointerAxes::RawPointerAxes() {
1392 clear();
1393}
1394
1395void RawPointerAxes::clear() {
1396 x.clear();
1397 y.clear();
1398 pressure.clear();
1399 touchMajor.clear();
1400 touchMinor.clear();
1401 toolMajor.clear();
1402 toolMinor.clear();
1403 orientation.clear();
1404 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001405 tiltX.clear();
1406 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001407 trackingId.clear();
1408 slot.clear();
1409}
1410
1411
1412// --- RawPointerData ---
1413
1414RawPointerData::RawPointerData() {
1415 clear();
1416}
1417
1418void RawPointerData::clear() {
1419 pointerCount = 0;
1420 clearIdBits();
1421}
1422
1423void RawPointerData::copyFrom(const RawPointerData& other) {
1424 pointerCount = other.pointerCount;
1425 hoveringIdBits = other.hoveringIdBits;
1426 touchingIdBits = other.touchingIdBits;
1427
1428 for (uint32_t i = 0; i < pointerCount; i++) {
1429 pointers[i] = other.pointers[i];
1430
1431 int id = pointers[i].id;
1432 idToIndex[id] = other.idToIndex[id];
1433 }
1434}
1435
1436void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1437 float x = 0, y = 0;
1438 uint32_t count = touchingIdBits.count();
1439 if (count) {
1440 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1441 uint32_t id = idBits.clearFirstMarkedBit();
1442 const Pointer& pointer = pointerForId(id);
1443 x += pointer.x;
1444 y += pointer.y;
1445 }
1446 x /= count;
1447 y /= count;
1448 }
1449 *outX = x;
1450 *outY = y;
1451}
1452
1453
1454// --- CookedPointerData ---
1455
1456CookedPointerData::CookedPointerData() {
1457 clear();
1458}
1459
1460void CookedPointerData::clear() {
1461 pointerCount = 0;
1462 hoveringIdBits.clear();
1463 touchingIdBits.clear();
1464}
1465
1466void CookedPointerData::copyFrom(const CookedPointerData& other) {
1467 pointerCount = other.pointerCount;
1468 hoveringIdBits = other.hoveringIdBits;
1469 touchingIdBits = other.touchingIdBits;
1470
1471 for (uint32_t i = 0; i < pointerCount; i++) {
1472 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1473 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1474
1475 int id = pointerProperties[i].id;
1476 idToIndex[id] = other.idToIndex[id];
1477 }
1478}
1479
1480
Jeff Brown49754db2011-07-01 17:37:58 -07001481// --- SingleTouchMotionAccumulator ---
1482
1483SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1484 clearAbsoluteAxes();
1485}
1486
Jeff Brown65fd2512011-08-18 11:20:58 -07001487void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1488 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1489 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1490 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1491 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1492 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1493 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1494 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1495}
1496
Jeff Brown49754db2011-07-01 17:37:58 -07001497void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1498 mAbsX = 0;
1499 mAbsY = 0;
1500 mAbsPressure = 0;
1501 mAbsToolWidth = 0;
1502 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001503 mAbsTiltX = 0;
1504 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001505}
1506
1507void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1508 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001509 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001510 case ABS_X:
1511 mAbsX = rawEvent->value;
1512 break;
1513 case ABS_Y:
1514 mAbsY = rawEvent->value;
1515 break;
1516 case ABS_PRESSURE:
1517 mAbsPressure = rawEvent->value;
1518 break;
1519 case ABS_TOOL_WIDTH:
1520 mAbsToolWidth = rawEvent->value;
1521 break;
1522 case ABS_DISTANCE:
1523 mAbsDistance = rawEvent->value;
1524 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001525 case ABS_TILT_X:
1526 mAbsTiltX = rawEvent->value;
1527 break;
1528 case ABS_TILT_Y:
1529 mAbsTiltY = rawEvent->value;
1530 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001531 }
1532 }
1533}
1534
1535
1536// --- MultiTouchMotionAccumulator ---
1537
1538MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001539 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1540 mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001541}
1542
1543MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1544 delete[] mSlots;
1545}
1546
Jeff Brown00710e92012-04-19 15:18:26 -07001547void MultiTouchMotionAccumulator::configure(InputDevice* device,
1548 size_t slotCount, bool usingSlotsProtocol) {
Jeff Brown49754db2011-07-01 17:37:58 -07001549 mSlotCount = slotCount;
1550 mUsingSlotsProtocol = usingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -07001551 mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Jeff Brown49754db2011-07-01 17:37:58 -07001552
1553 delete[] mSlots;
1554 mSlots = new Slot[slotCount];
1555}
1556
Jeff Brown65fd2512011-08-18 11:20:58 -07001557void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1558 // Unfortunately there is no way to read the initial contents of the slots.
1559 // So when we reset the accumulator, we must assume they are all zeroes.
1560 if (mUsingSlotsProtocol) {
1561 // Query the driver for the current slot index and use it as the initial slot
1562 // before we start reading events from the device. It is possible that the
1563 // current slot index will not be the same as it was when the first event was
1564 // written into the evdev buffer, which means the input mapper could start
1565 // out of sync with the initial state of the events in the evdev buffer.
1566 // In the extremely unlikely case that this happens, the data from
1567 // two slots will be confused until the next ABS_MT_SLOT event is received.
1568 // This can cause the touch point to "jump", but at least there will be
1569 // no stuck touches.
1570 int32_t initialSlot;
1571 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1572 ABS_MT_SLOT, &initialSlot);
1573 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001574 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001575 initialSlot = -1;
1576 }
1577 clearSlots(initialSlot);
1578 } else {
1579 clearSlots(-1);
1580 }
1581}
1582
Jeff Brown49754db2011-07-01 17:37:58 -07001583void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001584 if (mSlots) {
1585 for (size_t i = 0; i < mSlotCount; i++) {
1586 mSlots[i].clear();
1587 }
Jeff Brown49754db2011-07-01 17:37:58 -07001588 }
1589 mCurrentSlot = initialSlot;
1590}
1591
1592void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1593 if (rawEvent->type == EV_ABS) {
1594 bool newSlot = false;
1595 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001596 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001597 mCurrentSlot = rawEvent->value;
1598 newSlot = true;
1599 }
1600 } else if (mCurrentSlot < 0) {
1601 mCurrentSlot = 0;
1602 }
1603
1604 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1605#if DEBUG_POINTERS
1606 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001607 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001608 "should be between 0 and %d; ignoring this slot.",
1609 mCurrentSlot, mSlotCount - 1);
1610 }
1611#endif
1612 } else {
1613 Slot* slot = &mSlots[mCurrentSlot];
1614
Jeff Brown49ccac52012-04-11 18:27:33 -07001615 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001616 case ABS_MT_POSITION_X:
1617 slot->mInUse = true;
1618 slot->mAbsMTPositionX = rawEvent->value;
1619 break;
1620 case ABS_MT_POSITION_Y:
1621 slot->mInUse = true;
1622 slot->mAbsMTPositionY = rawEvent->value;
1623 break;
1624 case ABS_MT_TOUCH_MAJOR:
1625 slot->mInUse = true;
1626 slot->mAbsMTTouchMajor = rawEvent->value;
1627 break;
1628 case ABS_MT_TOUCH_MINOR:
1629 slot->mInUse = true;
1630 slot->mAbsMTTouchMinor = rawEvent->value;
1631 slot->mHaveAbsMTTouchMinor = true;
1632 break;
1633 case ABS_MT_WIDTH_MAJOR:
1634 slot->mInUse = true;
1635 slot->mAbsMTWidthMajor = rawEvent->value;
1636 break;
1637 case ABS_MT_WIDTH_MINOR:
1638 slot->mInUse = true;
1639 slot->mAbsMTWidthMinor = rawEvent->value;
1640 slot->mHaveAbsMTWidthMinor = true;
1641 break;
1642 case ABS_MT_ORIENTATION:
1643 slot->mInUse = true;
1644 slot->mAbsMTOrientation = rawEvent->value;
1645 break;
1646 case ABS_MT_TRACKING_ID:
1647 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001648 // The slot is no longer in use but it retains its previous contents,
1649 // which may be reused for subsequent touches.
1650 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001651 } else {
1652 slot->mInUse = true;
1653 slot->mAbsMTTrackingId = rawEvent->value;
1654 }
1655 break;
1656 case ABS_MT_PRESSURE:
1657 slot->mInUse = true;
1658 slot->mAbsMTPressure = rawEvent->value;
1659 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001660 case ABS_MT_DISTANCE:
1661 slot->mInUse = true;
1662 slot->mAbsMTDistance = rawEvent->value;
1663 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001664 case ABS_MT_TOOL_TYPE:
1665 slot->mInUse = true;
1666 slot->mAbsMTToolType = rawEvent->value;
1667 slot->mHaveAbsMTToolType = true;
1668 break;
1669 }
1670 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001671 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001672 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1673 mCurrentSlot += 1;
1674 }
1675}
1676
Jeff Brown65fd2512011-08-18 11:20:58 -07001677void MultiTouchMotionAccumulator::finishSync() {
1678 if (!mUsingSlotsProtocol) {
1679 clearSlots(-1);
1680 }
1681}
1682
Jeff Brown00710e92012-04-19 15:18:26 -07001683bool MultiTouchMotionAccumulator::hasStylus() const {
1684 return mHaveStylus;
1685}
1686
Jeff Brown49754db2011-07-01 17:37:58 -07001687
1688// --- MultiTouchMotionAccumulator::Slot ---
1689
1690MultiTouchMotionAccumulator::Slot::Slot() {
1691 clear();
1692}
1693
Jeff Brown49754db2011-07-01 17:37:58 -07001694void MultiTouchMotionAccumulator::Slot::clear() {
1695 mInUse = false;
1696 mHaveAbsMTTouchMinor = false;
1697 mHaveAbsMTWidthMinor = false;
1698 mHaveAbsMTToolType = false;
1699 mAbsMTPositionX = 0;
1700 mAbsMTPositionY = 0;
1701 mAbsMTTouchMajor = 0;
1702 mAbsMTTouchMinor = 0;
1703 mAbsMTWidthMajor = 0;
1704 mAbsMTWidthMinor = 0;
1705 mAbsMTOrientation = 0;
1706 mAbsMTTrackingId = -1;
1707 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001708 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001709 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001710}
1711
1712int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1713 if (mHaveAbsMTToolType) {
1714 switch (mAbsMTToolType) {
1715 case MT_TOOL_FINGER:
1716 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1717 case MT_TOOL_PEN:
1718 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1719 }
1720 }
1721 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1722}
1723
1724
Jeff Brown6d0fec22010-07-23 21:28:06 -07001725// --- InputMapper ---
1726
1727InputMapper::InputMapper(InputDevice* device) :
1728 mDevice(device), mContext(device->getContext()) {
1729}
1730
1731InputMapper::~InputMapper() {
1732}
1733
1734void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1735 info->addSource(getSources());
1736}
1737
Jeff Brownef3d7e82010-09-30 14:33:04 -07001738void InputMapper::dump(String8& dump) {
1739}
1740
Jeff Brown65fd2512011-08-18 11:20:58 -07001741void InputMapper::configure(nsecs_t when,
1742 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001743}
1744
Jeff Brown65fd2512011-08-18 11:20:58 -07001745void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001746}
1747
Jeff Brownaa3855d2011-03-17 01:34:19 -07001748void InputMapper::timeoutExpired(nsecs_t when) {
1749}
1750
Jeff Brown6d0fec22010-07-23 21:28:06 -07001751int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1752 return AKEY_STATE_UNKNOWN;
1753}
1754
1755int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1756 return AKEY_STATE_UNKNOWN;
1757}
1758
1759int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1760 return AKEY_STATE_UNKNOWN;
1761}
1762
1763bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1764 const int32_t* keyCodes, uint8_t* outFlags) {
1765 return false;
1766}
1767
Jeff Browna47425a2012-04-13 04:09:27 -07001768void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1769 int32_t token) {
1770}
1771
1772void InputMapper::cancelVibrate(int32_t token) {
1773}
1774
Jeff Brown6d0fec22010-07-23 21:28:06 -07001775int32_t InputMapper::getMetaState() {
1776 return 0;
1777}
1778
Jeff Brown05dc66a2011-03-02 14:41:58 -08001779void InputMapper::fadePointer() {
1780}
1781
Jeff Brownbe1aa822011-07-27 16:04:54 -07001782status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1783 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1784}
1785
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001786void InputMapper::bumpGeneration() {
1787 mDevice->bumpGeneration();
1788}
1789
Jeff Browncb1404e2011-01-15 18:14:15 -08001790void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1791 const RawAbsoluteAxisInfo& axis, const char* name) {
1792 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001793 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1794 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001795 } else {
1796 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1797 }
1798}
1799
Jeff Brown6d0fec22010-07-23 21:28:06 -07001800
1801// --- SwitchInputMapper ---
1802
1803SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
Jeff Brownbcc046a2012-09-27 20:46:43 -07001804 InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001805}
1806
1807SwitchInputMapper::~SwitchInputMapper() {
1808}
1809
1810uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001811 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001812}
1813
1814void SwitchInputMapper::process(const RawEvent* rawEvent) {
1815 switch (rawEvent->type) {
1816 case EV_SW:
Jeff Brownbcc046a2012-09-27 20:46:43 -07001817 processSwitch(rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001818 break;
Jeff Brownbcc046a2012-09-27 20:46:43 -07001819
1820 case EV_SYN:
1821 if (rawEvent->code == SYN_REPORT) {
1822 sync(rawEvent->when);
1823 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001824 }
1825}
1826
Jeff Brownbcc046a2012-09-27 20:46:43 -07001827void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
1828 if (switchCode >= 0 && switchCode < 32) {
1829 if (switchValue) {
1830 mUpdatedSwitchValues |= 1 << switchCode;
1831 }
1832 mUpdatedSwitchMask |= 1 << switchCode;
1833 }
1834}
1835
1836void SwitchInputMapper::sync(nsecs_t when) {
1837 if (mUpdatedSwitchMask) {
1838 NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask);
1839 getListener()->notifySwitch(&args);
1840
1841 mUpdatedSwitchValues = 0;
1842 mUpdatedSwitchMask = 0;
1843 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001844}
1845
1846int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1847 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1848}
1849
1850
Jeff Browna47425a2012-04-13 04:09:27 -07001851// --- VibratorInputMapper ---
1852
1853VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1854 InputMapper(device), mVibrating(false) {
1855}
1856
1857VibratorInputMapper::~VibratorInputMapper() {
1858}
1859
1860uint32_t VibratorInputMapper::getSources() {
1861 return 0;
1862}
1863
1864void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1865 InputMapper::populateDeviceInfo(info);
1866
1867 info->setVibrator(true);
1868}
1869
1870void VibratorInputMapper::process(const RawEvent* rawEvent) {
1871 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1872}
1873
1874void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1875 int32_t token) {
1876#if DEBUG_VIBRATOR
1877 String8 patternStr;
1878 for (size_t i = 0; i < patternSize; i++) {
1879 if (i != 0) {
1880 patternStr.append(", ");
1881 }
1882 patternStr.appendFormat("%lld", pattern[i]);
1883 }
1884 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1885 getDeviceId(), patternStr.string(), repeat, token);
1886#endif
1887
1888 mVibrating = true;
1889 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1890 mPatternSize = patternSize;
1891 mRepeat = repeat;
1892 mToken = token;
1893 mIndex = -1;
1894
1895 nextStep();
1896}
1897
1898void VibratorInputMapper::cancelVibrate(int32_t token) {
1899#if DEBUG_VIBRATOR
1900 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1901#endif
1902
1903 if (mVibrating && mToken == token) {
1904 stopVibrating();
1905 }
1906}
1907
1908void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1909 if (mVibrating) {
1910 if (when >= mNextStepTime) {
1911 nextStep();
1912 } else {
1913 getContext()->requestTimeoutAtTime(mNextStepTime);
1914 }
1915 }
1916}
1917
1918void VibratorInputMapper::nextStep() {
1919 mIndex += 1;
1920 if (size_t(mIndex) >= mPatternSize) {
1921 if (mRepeat < 0) {
1922 // We are done.
1923 stopVibrating();
1924 return;
1925 }
1926 mIndex = mRepeat;
1927 }
1928
1929 bool vibratorOn = mIndex & 1;
1930 nsecs_t duration = mPattern[mIndex];
1931 if (vibratorOn) {
1932#if DEBUG_VIBRATOR
1933 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1934 getDeviceId(), duration);
1935#endif
1936 getEventHub()->vibrate(getDeviceId(), duration);
1937 } else {
1938#if DEBUG_VIBRATOR
1939 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1940#endif
1941 getEventHub()->cancelVibrate(getDeviceId());
1942 }
1943 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1944 mNextStepTime = now + duration;
1945 getContext()->requestTimeoutAtTime(mNextStepTime);
1946#if DEBUG_VIBRATOR
1947 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1948#endif
1949}
1950
1951void VibratorInputMapper::stopVibrating() {
1952 mVibrating = false;
1953#if DEBUG_VIBRATOR
1954 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1955#endif
1956 getEventHub()->cancelVibrate(getDeviceId());
1957}
1958
1959void VibratorInputMapper::dump(String8& dump) {
1960 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1961 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1962}
1963
1964
Jeff Brown6d0fec22010-07-23 21:28:06 -07001965// --- KeyboardInputMapper ---
1966
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001967KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001968 uint32_t source, int32_t keyboardType) :
1969 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001971}
1972
1973KeyboardInputMapper::~KeyboardInputMapper() {
1974}
1975
Jeff Brown6d0fec22010-07-23 21:28:06 -07001976uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001977 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001978}
1979
1980void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1981 InputMapper::populateDeviceInfo(info);
1982
1983 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001984 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001985}
1986
Jeff Brownef3d7e82010-09-30 14:33:04 -07001987void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001988 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1989 dumpParameters(dump);
1990 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001991 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001992 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1993 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1994 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001995}
1996
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001997
Jeff Brown65fd2512011-08-18 11:20:58 -07001998void KeyboardInputMapper::configure(nsecs_t when,
1999 const InputReaderConfiguration* config, uint32_t changes) {
2000 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002001
Jeff Brown474dcb52011-06-14 20:22:50 -07002002 if (!changes) { // first time only
2003 // Configure basic parameters.
2004 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002005 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002006
Jeff Brown65fd2512011-08-18 11:20:58 -07002007 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002008 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2009 DisplayViewport v;
2010 if (config->getDisplayInfo(false /*external*/, &v)) {
2011 mOrientation = v.orientation;
2012 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002013 mOrientation = DISPLAY_ORIENTATION_0;
2014 }
2015 } else {
2016 mOrientation = DISPLAY_ORIENTATION_0;
2017 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002018 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002019}
2020
2021void KeyboardInputMapper::configureParameters() {
2022 mParameters.orientationAware = false;
2023 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2024 mParameters.orientationAware);
2025
Jeff Brownd728bf52012-09-08 18:05:28 -07002026 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002027 if (mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002028 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002029 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002030}
2031
2032void KeyboardInputMapper::dumpParameters(String8& dump) {
2033 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002034 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2035 toString(mParameters.hasAssociatedDisplay));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002036 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2037 toString(mParameters.orientationAware));
2038}
2039
Jeff Brown65fd2512011-08-18 11:20:58 -07002040void KeyboardInputMapper::reset(nsecs_t when) {
2041 mMetaState = AMETA_NONE;
2042 mDownTime = 0;
2043 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002044 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002045
Jeff Brownbe1aa822011-07-27 16:04:54 -07002046 resetLedState();
2047
Jeff Brown65fd2512011-08-18 11:20:58 -07002048 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002049}
2050
2051void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2052 switch (rawEvent->type) {
2053 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002054 int32_t scanCode = rawEvent->code;
2055 int32_t usageCode = mCurrentHidUsage;
2056 mCurrentHidUsage = 0;
2057
Jeff Brown6d0fec22010-07-23 21:28:06 -07002058 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002059 int32_t keyCode;
2060 uint32_t flags;
2061 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2062 keyCode = AKEYCODE_UNKNOWN;
2063 flags = 0;
2064 }
2065 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002066 }
2067 break;
2068 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002069 case EV_MSC: {
2070 if (rawEvent->code == MSC_SCAN) {
2071 mCurrentHidUsage = rawEvent->value;
2072 }
2073 break;
2074 }
2075 case EV_SYN: {
2076 if (rawEvent->code == SYN_REPORT) {
2077 mCurrentHidUsage = 0;
2078 }
2079 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002080 }
2081}
2082
2083bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2084 return scanCode < BTN_MOUSE
2085 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002086 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002087 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002088}
2089
Jeff Brown6328cdc2010-07-29 18:18:33 -07002090void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2091 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002092
Jeff Brownbe1aa822011-07-27 16:04:54 -07002093 if (down) {
2094 // Rotate key codes according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002095 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002096 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002097 }
Jeff Brownfe508922011-01-18 15:10:10 -08002098
Jeff Brownbe1aa822011-07-27 16:04:54 -07002099 // Add key down.
2100 ssize_t keyDownIndex = findKeyDown(scanCode);
2101 if (keyDownIndex >= 0) {
2102 // key repeat, be sure to use same keycode as before in case of rotation
2103 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002104 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002105 // key down
2106 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2107 && mContext->shouldDropVirtualKey(when,
2108 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002109 return;
2110 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002111
2112 mKeyDowns.push();
2113 KeyDown& keyDown = mKeyDowns.editTop();
2114 keyDown.keyCode = keyCode;
2115 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002116 }
2117
Jeff Brownbe1aa822011-07-27 16:04:54 -07002118 mDownTime = when;
2119 } else {
2120 // Remove key down.
2121 ssize_t keyDownIndex = findKeyDown(scanCode);
2122 if (keyDownIndex >= 0) {
2123 // key up, be sure to use same keycode as before in case of rotation
2124 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2125 mKeyDowns.removeAt(size_t(keyDownIndex));
2126 } else {
2127 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002128 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002129 "keyCode=%d, scanCode=%d",
2130 getDeviceName().string(), keyCode, scanCode);
2131 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002132 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002133 }
Jeff Brownfd035822010-06-30 16:10:35 -07002134
Jeff Brownbe1aa822011-07-27 16:04:54 -07002135 bool metaStateChanged = false;
2136 int32_t oldMetaState = mMetaState;
2137 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2138 if (oldMetaState != newMetaState) {
2139 mMetaState = newMetaState;
2140 metaStateChanged = true;
2141 updateLedState(false);
2142 }
2143
2144 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002145
Jeff Brown56194eb2011-03-02 19:23:13 -08002146 // Key down on external an keyboard should wake the device.
2147 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2148 // For internal keyboards, the key layout file should specify the policy flags for
2149 // each wake key individually.
2150 // TODO: Use the input device configuration to control this behavior more finely.
2151 if (down && getDevice()->isExternal()
2152 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2153 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2154 }
2155
Jeff Brown6328cdc2010-07-29 18:18:33 -07002156 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002157 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002158 }
2159
Jeff Brown05dc66a2011-03-02 14:41:58 -08002160 if (down && !isMetaKey(keyCode)) {
2161 getContext()->fadePointer();
2162 }
2163
Jeff Brownbe1aa822011-07-27 16:04:54 -07002164 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002165 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2166 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002167 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002168}
2169
Jeff Brownbe1aa822011-07-27 16:04:54 -07002170ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2171 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002172 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002173 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002174 return i;
2175 }
2176 }
2177 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002178}
2179
Jeff Brown6d0fec22010-07-23 21:28:06 -07002180int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2181 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2182}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002183
Jeff Brown6d0fec22010-07-23 21:28:06 -07002184int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2185 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2186}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002187
Jeff Brown6d0fec22010-07-23 21:28:06 -07002188bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2189 const int32_t* keyCodes, uint8_t* outFlags) {
2190 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2191}
2192
2193int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002194 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002195}
2196
Jeff Brownbe1aa822011-07-27 16:04:54 -07002197void KeyboardInputMapper::resetLedState() {
2198 initializeLedState(mCapsLockLedState, LED_CAPSL);
2199 initializeLedState(mNumLockLedState, LED_NUML);
2200 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002201
Jeff Brownbe1aa822011-07-27 16:04:54 -07002202 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002203}
2204
Jeff Brownbe1aa822011-07-27 16:04:54 -07002205void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002206 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2207 ledState.on = false;
2208}
2209
Jeff Brownbe1aa822011-07-27 16:04:54 -07002210void KeyboardInputMapper::updateLedState(bool reset) {
2211 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002212 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002213 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002214 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002215 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002216 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002217}
2218
Jeff Brownbe1aa822011-07-27 16:04:54 -07002219void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002220 int32_t led, int32_t modifier, bool reset) {
2221 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002222 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002223 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002224 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2225 ledState.on = desiredState;
2226 }
2227 }
2228}
2229
Jeff Brown6d0fec22010-07-23 21:28:06 -07002230
Jeff Brown83c09682010-12-23 17:50:18 -08002231// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002232
Jeff Brown83c09682010-12-23 17:50:18 -08002233CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002234 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002235}
2236
Jeff Brown83c09682010-12-23 17:50:18 -08002237CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002238}
2239
Jeff Brown83c09682010-12-23 17:50:18 -08002240uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002241 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002242}
2243
Jeff Brown83c09682010-12-23 17:50:18 -08002244void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002245 InputMapper::populateDeviceInfo(info);
2246
Jeff Brown83c09682010-12-23 17:50:18 -08002247 if (mParameters.mode == Parameters::MODE_POINTER) {
2248 float minX, minY, maxX, maxY;
2249 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002250 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2251 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002252 }
2253 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002254 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2255 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002256 }
Jeff Brownefd32662011-03-08 15:13:06 -08002257 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002258
Jeff Brown65fd2512011-08-18 11:20:58 -07002259 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002260 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002261 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002262 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002263 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002264 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002265}
2266
Jeff Brown83c09682010-12-23 17:50:18 -08002267void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002268 dump.append(INDENT2 "Cursor Input Mapper:\n");
2269 dumpParameters(dump);
2270 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2271 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2272 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2273 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2274 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002275 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002276 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002277 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002278 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2279 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002280 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002281 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2282 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2283 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002284}
2285
Jeff Brown65fd2512011-08-18 11:20:58 -07002286void CursorInputMapper::configure(nsecs_t when,
2287 const InputReaderConfiguration* config, uint32_t changes) {
2288 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002289
Jeff Brown474dcb52011-06-14 20:22:50 -07002290 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002291 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002292
Jeff Brown474dcb52011-06-14 20:22:50 -07002293 // Configure basic parameters.
2294 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002295
Jeff Brown474dcb52011-06-14 20:22:50 -07002296 // Configure device mode.
2297 switch (mParameters.mode) {
2298 case Parameters::MODE_POINTER:
2299 mSource = AINPUT_SOURCE_MOUSE;
2300 mXPrecision = 1.0f;
2301 mYPrecision = 1.0f;
2302 mXScale = 1.0f;
2303 mYScale = 1.0f;
2304 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2305 break;
2306 case Parameters::MODE_NAVIGATION:
2307 mSource = AINPUT_SOURCE_TRACKBALL;
2308 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2309 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2310 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2311 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2312 break;
2313 }
2314
2315 mVWheelScale = 1.0f;
2316 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002317 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002318
Jeff Brown474dcb52011-06-14 20:22:50 -07002319 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2320 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2321 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2322 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2323 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002324
2325 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002326 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2327 DisplayViewport v;
2328 if (config->getDisplayInfo(false /*external*/, &v)) {
2329 mOrientation = v.orientation;
2330 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002331 mOrientation = DISPLAY_ORIENTATION_0;
2332 }
2333 } else {
2334 mOrientation = DISPLAY_ORIENTATION_0;
2335 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002336 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002337 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002338}
2339
Jeff Brown83c09682010-12-23 17:50:18 -08002340void CursorInputMapper::configureParameters() {
2341 mParameters.mode = Parameters::MODE_POINTER;
2342 String8 cursorModeString;
2343 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2344 if (cursorModeString == "navigation") {
2345 mParameters.mode = Parameters::MODE_NAVIGATION;
2346 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002347 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002348 }
2349 }
2350
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002351 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002352 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002353 mParameters.orientationAware);
2354
Jeff Brownd728bf52012-09-08 18:05:28 -07002355 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002356 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002357 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002358 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002359}
2360
Jeff Brown83c09682010-12-23 17:50:18 -08002361void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002362 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002363 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2364 toString(mParameters.hasAssociatedDisplay));
Jeff Brown83c09682010-12-23 17:50:18 -08002365
2366 switch (mParameters.mode) {
2367 case Parameters::MODE_POINTER:
2368 dump.append(INDENT4 "Mode: pointer\n");
2369 break;
2370 case Parameters::MODE_NAVIGATION:
2371 dump.append(INDENT4 "Mode: navigation\n");
2372 break;
2373 default:
Steve Blockec193de2012-01-09 18:35:44 +00002374 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002375 }
2376
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002377 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2378 toString(mParameters.orientationAware));
2379}
2380
Jeff Brown65fd2512011-08-18 11:20:58 -07002381void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002382 mButtonState = 0;
2383 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002384
Jeff Brownbe1aa822011-07-27 16:04:54 -07002385 mPointerVelocityControl.reset();
2386 mWheelXVelocityControl.reset();
2387 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002388
Jeff Brown65fd2512011-08-18 11:20:58 -07002389 mCursorButtonAccumulator.reset(getDevice());
2390 mCursorMotionAccumulator.reset(getDevice());
2391 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002392
Jeff Brown65fd2512011-08-18 11:20:58 -07002393 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002394}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002395
Jeff Brown83c09682010-12-23 17:50:18 -08002396void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002397 mCursorButtonAccumulator.process(rawEvent);
2398 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002399 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002400
Jeff Brown49ccac52012-04-11 18:27:33 -07002401 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002402 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002403 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002404}
2405
Jeff Brown83c09682010-12-23 17:50:18 -08002406void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002407 int32_t lastButtonState = mButtonState;
2408 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2409 mButtonState = currentButtonState;
2410
2411 bool wasDown = isPointerDown(lastButtonState);
2412 bool down = isPointerDown(currentButtonState);
2413 bool downChanged;
2414 if (!wasDown && down) {
2415 mDownTime = when;
2416 downChanged = true;
2417 } else if (wasDown && !down) {
2418 downChanged = true;
2419 } else {
2420 downChanged = false;
2421 }
2422 nsecs_t downTime = mDownTime;
2423 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002424 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002425
2426 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2427 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2428 bool moved = deltaX != 0 || deltaY != 0;
2429
Jeff Brown65fd2512011-08-18 11:20:58 -07002430 // Rotate delta according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002431 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay
Jeff Brownbe1aa822011-07-27 16:04:54 -07002432 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002433 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002434 }
2435
Jeff Brown65fd2512011-08-18 11:20:58 -07002436 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002437 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002438 pointerProperties.clear();
2439 pointerProperties.id = 0;
2440 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2441
Jeff Brown6328cdc2010-07-29 18:18:33 -07002442 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002443 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002444
Jeff Brown65fd2512011-08-18 11:20:58 -07002445 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2446 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002447 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002448
Jeff Brownbe1aa822011-07-27 16:04:54 -07002449 mWheelYVelocityControl.move(when, NULL, &vscroll);
2450 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002451
Jeff Brownbe1aa822011-07-27 16:04:54 -07002452 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002453
Jeff Brown83d616a2012-09-09 20:33:43 -07002454 int32_t displayId;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002455 if (mPointerController != NULL) {
2456 if (moved || scrolled || buttonsChanged) {
2457 mPointerController->setPresentation(
2458 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002459
Jeff Brownbe1aa822011-07-27 16:04:54 -07002460 if (moved) {
2461 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002462 }
2463
Jeff Brownbe1aa822011-07-27 16:04:54 -07002464 if (buttonsChanged) {
2465 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002466 }
Jeff Brownefd32662011-03-08 15:13:06 -08002467
Jeff Brownbe1aa822011-07-27 16:04:54 -07002468 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002469 }
2470
Jeff Brownbe1aa822011-07-27 16:04:54 -07002471 float x, y;
2472 mPointerController->getPosition(&x, &y);
2473 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2474 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83d616a2012-09-09 20:33:43 -07002475 displayId = ADISPLAY_ID_DEFAULT;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002476 } else {
2477 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2478 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83d616a2012-09-09 20:33:43 -07002479 displayId = ADISPLAY_ID_NONE;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002480 }
2481
2482 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002483
Jeff Brown56194eb2011-03-02 19:23:13 -08002484 // Moving an external trackball or mouse should wake the device.
2485 // We don't do this for internal cursor devices to prevent them from waking up
2486 // the device in your pocket.
2487 // TODO: Use the input device configuration to control this behavior more finely.
2488 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002489 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002490 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2491 }
2492
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002493 // Synthesize key down from buttons if needed.
2494 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2495 policyFlags, lastButtonState, currentButtonState);
2496
2497 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002498 if (downChanged || moved || scrolled || buttonsChanged) {
2499 int32_t metaState = mContext->getGlobalMetaState();
2500 int32_t motionEventAction;
2501 if (downChanged) {
2502 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2503 } else if (down || mPointerController == NULL) {
2504 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2505 } else {
2506 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2507 }
Jeff Brownb6997262010-10-08 22:31:17 -07002508
Jeff Brownbe1aa822011-07-27 16:04:54 -07002509 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2510 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07002511 displayId, 1, &pointerProperties, &pointerCoords,
2512 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002513 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002514
Jeff Brownbe1aa822011-07-27 16:04:54 -07002515 // Send hover move after UP to tell the application that the mouse is hovering now.
2516 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2517 && mPointerController != NULL) {
2518 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2519 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2520 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002521 displayId, 1, &pointerProperties, &pointerCoords,
2522 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002523 getListener()->notifyMotion(&hoverArgs);
2524 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002525
Jeff Brownbe1aa822011-07-27 16:04:54 -07002526 // Send scroll events.
2527 if (scrolled) {
2528 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2529 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2530
2531 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2532 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2533 AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002534 displayId, 1, &pointerProperties, &pointerCoords,
2535 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002536 getListener()->notifyMotion(&scrollArgs);
2537 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002538 }
Jeff Browna032cc02011-03-07 16:56:21 -08002539
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002540 // Synthesize key up from buttons if needed.
2541 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2542 policyFlags, lastButtonState, currentButtonState);
2543
Jeff Brown65fd2512011-08-18 11:20:58 -07002544 mCursorMotionAccumulator.finishSync();
2545 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002546}
2547
Jeff Brown83c09682010-12-23 17:50:18 -08002548int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002549 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2550 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2551 } else {
2552 return AKEY_STATE_UNKNOWN;
2553 }
2554}
2555
Jeff Brown05dc66a2011-03-02 14:41:58 -08002556void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002557 if (mPointerController != NULL) {
2558 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2559 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002560}
2561
Jeff Brown6d0fec22010-07-23 21:28:06 -07002562
2563// --- TouchInputMapper ---
2564
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002565TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002566 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002567 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brown83d616a2012-09-09 20:33:43 -07002568 mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0),
2569 mSurfaceOrientation(DISPLAY_ORIENTATION_0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002570}
2571
2572TouchInputMapper::~TouchInputMapper() {
2573}
2574
2575uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002576 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002577}
2578
2579void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2580 InputMapper::populateDeviceInfo(info);
2581
Jeff Brown65fd2512011-08-18 11:20:58 -07002582 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2583 info->addMotionRange(mOrientedRanges.x);
2584 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002585 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002586
Jeff Brown65fd2512011-08-18 11:20:58 -07002587 if (mOrientedRanges.haveSize) {
2588 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002589 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002590
2591 if (mOrientedRanges.haveTouchSize) {
2592 info->addMotionRange(mOrientedRanges.touchMajor);
2593 info->addMotionRange(mOrientedRanges.touchMinor);
2594 }
2595
2596 if (mOrientedRanges.haveToolSize) {
2597 info->addMotionRange(mOrientedRanges.toolMajor);
2598 info->addMotionRange(mOrientedRanges.toolMinor);
2599 }
2600
2601 if (mOrientedRanges.haveOrientation) {
2602 info->addMotionRange(mOrientedRanges.orientation);
2603 }
2604
2605 if (mOrientedRanges.haveDistance) {
2606 info->addMotionRange(mOrientedRanges.distance);
2607 }
2608
2609 if (mOrientedRanges.haveTilt) {
2610 info->addMotionRange(mOrientedRanges.tilt);
2611 }
2612
2613 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2614 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2615 }
2616 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2617 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2618 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002619 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002620}
2621
Jeff Brownef3d7e82010-09-30 14:33:04 -07002622void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002623 dump.append(INDENT2 "Touch Input Mapper:\n");
2624 dumpParameters(dump);
2625 dumpVirtualKeys(dump);
2626 dumpRawPointerAxes(dump);
2627 dumpCalibration(dump);
2628 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002629
Jeff Brownbe1aa822011-07-27 16:04:54 -07002630 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown83d616a2012-09-09 20:33:43 -07002631 dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate);
2632 dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002633 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2634 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2635 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2636 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2637 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002638 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2639 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2640 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2641 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002642 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2643 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2644 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2645 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2646 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002647
Jeff Brownbe1aa822011-07-27 16:04:54 -07002648 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002649
Jeff Brownbe1aa822011-07-27 16:04:54 -07002650 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2651 mLastRawPointerData.pointerCount);
2652 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2653 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2654 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2655 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002656 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2657 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002658 pointer.id, pointer.x, pointer.y, pointer.pressure,
2659 pointer.touchMajor, pointer.touchMinor,
2660 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002661 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002662 pointer.toolType, toString(pointer.isHovering));
2663 }
2664
2665 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2666 mLastCookedPointerData.pointerCount);
2667 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2668 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2669 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2670 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2671 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002672 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2673 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002674 pointerProperties.id,
2675 pointerCoords.getX(),
2676 pointerCoords.getY(),
2677 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2678 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2679 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2680 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2681 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2682 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002683 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002684 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2685 pointerProperties.toolType,
2686 toString(mLastCookedPointerData.isHovering(i)));
2687 }
2688
Jeff Brown65fd2512011-08-18 11:20:58 -07002689 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002690 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2691 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002692 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002693 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002694 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002695 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002696 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002697 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002698 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002699 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2700 mPointerGestureMaxSwipeWidth);
2701 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002702}
2703
Jeff Brown65fd2512011-08-18 11:20:58 -07002704void TouchInputMapper::configure(nsecs_t when,
2705 const InputReaderConfiguration* config, uint32_t changes) {
2706 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002707
Jeff Brown474dcb52011-06-14 20:22:50 -07002708 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002709
Jeff Brown474dcb52011-06-14 20:22:50 -07002710 if (!changes) { // first time only
2711 // Configure basic parameters.
2712 configureParameters();
2713
Jeff Brown65fd2512011-08-18 11:20:58 -07002714 // Configure common accumulators.
2715 mCursorScrollAccumulator.configure(getDevice());
2716 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002717
2718 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002719 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002720
2721 // Prepare input device calibration.
2722 parseCalibration();
2723 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002724 }
2725
Jeff Brown474dcb52011-06-14 20:22:50 -07002726 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002727 // Update pointer speed.
2728 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2729 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2730 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002731 }
Jeff Brown8d608662010-08-30 03:02:23 -07002732
Jeff Brown65fd2512011-08-18 11:20:58 -07002733 bool resetNeeded = false;
2734 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002735 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2736 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002737 // Configure device sources, surface dimensions, orientation and
2738 // scaling factors.
2739 configureSurface(when, &resetNeeded);
2740 }
2741
2742 if (changes && resetNeeded) {
2743 // Send reset, unless this is the first time the device has been configured,
2744 // in which case the reader will call reset itself after all mappers are ready.
2745 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002746 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002747}
2748
Jeff Brown8d608662010-08-30 03:02:23 -07002749void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002750 // Use the pointer presentation mode for devices that do not support distinct
2751 // multitouch. The spot-based presentation relies on being able to accurately
2752 // locate two or more fingers on the touch pad.
2753 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2754 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002755
Jeff Brown538881e2011-05-25 18:23:38 -07002756 String8 gestureModeString;
2757 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2758 gestureModeString)) {
2759 if (gestureModeString == "pointer") {
2760 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2761 } else if (gestureModeString == "spots") {
2762 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2763 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002764 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002765 }
2766 }
2767
Jeff Browndeffe072011-08-26 18:38:46 -07002768 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2769 // The device is a touch screen.
2770 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2771 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2772 // The device is a pointing device like a track pad.
2773 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2774 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002775 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2776 // The device is a cursor device with a touch pad attached.
2777 // By default don't use the touch pad to move the pointer.
2778 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2779 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002780 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002781 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2782 }
2783
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002784 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002785 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2786 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002787 if (deviceTypeString == "touchScreen") {
2788 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002789 } else if (deviceTypeString == "touchPad") {
2790 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002791 } else if (deviceTypeString == "touchNavigation") {
2792 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION;
Jeff Brownace13b12011-03-09 17:39:48 -08002793 } else if (deviceTypeString == "pointer") {
2794 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002795 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002796 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002797 }
2798 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002799
Jeff Brownefd32662011-03-08 15:13:06 -08002800 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002801 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2802 mParameters.orientationAware);
2803
Jeff Brownd728bf52012-09-08 18:05:28 -07002804 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002805 mParameters.associatedDisplayIsExternal = false;
2806 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002807 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002808 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002809 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002810 mParameters.associatedDisplayIsExternal =
2811 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2812 && getDevice()->isExternal();
Jeff Brownbc68a592011-07-25 12:58:12 -07002813 }
Jeff Brown8d608662010-08-30 03:02:23 -07002814}
2815
Jeff Brownef3d7e82010-09-30 14:33:04 -07002816void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002817 dump.append(INDENT3 "Parameters:\n");
2818
Jeff Brown538881e2011-05-25 18:23:38 -07002819 switch (mParameters.gestureMode) {
2820 case Parameters::GESTURE_MODE_POINTER:
2821 dump.append(INDENT4 "GestureMode: pointer\n");
2822 break;
2823 case Parameters::GESTURE_MODE_SPOTS:
2824 dump.append(INDENT4 "GestureMode: spots\n");
2825 break;
2826 default:
2827 assert(false);
2828 }
2829
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002830 switch (mParameters.deviceType) {
2831 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2832 dump.append(INDENT4 "DeviceType: touchScreen\n");
2833 break;
2834 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2835 dump.append(INDENT4 "DeviceType: touchPad\n");
2836 break;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002837 case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION:
2838 dump.append(INDENT4 "DeviceType: touchNavigation\n");
2839 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002840 case Parameters::DEVICE_TYPE_POINTER:
2841 dump.append(INDENT4 "DeviceType: pointer\n");
2842 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002843 default:
Steve Blockec193de2012-01-09 18:35:44 +00002844 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002845 }
2846
Jeff Brown83d616a2012-09-09 20:33:43 -07002847 dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n",
Jeff Brownd728bf52012-09-08 18:05:28 -07002848 toString(mParameters.hasAssociatedDisplay),
2849 toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002850 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2851 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002852}
2853
Jeff Brownbe1aa822011-07-27 16:04:54 -07002854void TouchInputMapper::configureRawPointerAxes() {
2855 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002856}
2857
Jeff Brownbe1aa822011-07-27 16:04:54 -07002858void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2859 dump.append(INDENT3 "Raw Touch Axes:\n");
2860 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2861 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2862 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2863 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2864 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2865 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2866 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2867 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2868 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002869 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2870 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002871 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2872 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002873}
2874
Jeff Brown65fd2512011-08-18 11:20:58 -07002875void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2876 int32_t oldDeviceMode = mDeviceMode;
2877
2878 // Determine device mode.
2879 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2880 && mConfig.pointerGesturesEnabled) {
2881 mSource = AINPUT_SOURCE_MOUSE;
2882 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002883 if (hasStylus()) {
2884 mSource |= AINPUT_SOURCE_STYLUS;
2885 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002886 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownd728bf52012-09-08 18:05:28 -07002887 && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002888 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2889 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002890 if (hasStylus()) {
2891 mSource |= AINPUT_SOURCE_STYLUS;
2892 }
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002893 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) {
2894 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
2895 mDeviceMode = DEVICE_MODE_UNSCALED;
Jeff Brown65fd2512011-08-18 11:20:58 -07002896 } else {
2897 mSource = AINPUT_SOURCE_TOUCHPAD;
2898 mDeviceMode = DEVICE_MODE_UNSCALED;
2899 }
2900
Jeff Brown9626b142011-03-03 02:09:54 -08002901 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002902 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002903 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002904 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002905 mDeviceMode = DEVICE_MODE_DISABLED;
2906 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002907 }
2908
Jeff Brown83d616a2012-09-09 20:33:43 -07002909 // Raw width and height in the natural orientation.
2910 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2911 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2912
Jeff Brown65fd2512011-08-18 11:20:58 -07002913 // Get associated display dimensions.
Jeff Brown83d616a2012-09-09 20:33:43 -07002914 bool viewportChanged = false;
2915 DisplayViewport newViewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07002916 if (mParameters.hasAssociatedDisplay) {
Jeff Brown83d616a2012-09-09 20:33:43 -07002917 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002918 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brownd728bf52012-09-08 18:05:28 -07002919 "display. The device will be inoperable until the display size "
Jeff Brown65fd2512011-08-18 11:20:58 -07002920 "becomes available.",
Jeff Brownd728bf52012-09-08 18:05:28 -07002921 getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002922 mDeviceMode = DEVICE_MODE_DISABLED;
2923 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002924 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002925 } else {
Jeff Brown83d616a2012-09-09 20:33:43 -07002926 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
2927 }
2928 if (mViewport != newViewport) {
2929 mViewport = newViewport;
2930 viewportChanged = true;
2931
2932 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2933 // Convert rotated viewport to natural surface coordinates.
2934 int32_t naturalLogicalWidth, naturalLogicalHeight;
2935 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
2936 int32_t naturalPhysicalLeft, naturalPhysicalTop;
2937 int32_t naturalDeviceWidth, naturalDeviceHeight;
2938 switch (mViewport.orientation) {
2939 case DISPLAY_ORIENTATION_90:
2940 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2941 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2942 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2943 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2944 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
2945 naturalPhysicalTop = mViewport.physicalLeft;
2946 naturalDeviceWidth = mViewport.deviceHeight;
2947 naturalDeviceHeight = mViewport.deviceWidth;
2948 break;
2949 case DISPLAY_ORIENTATION_180:
2950 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2951 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2952 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2953 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2954 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
2955 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
2956 naturalDeviceWidth = mViewport.deviceWidth;
2957 naturalDeviceHeight = mViewport.deviceHeight;
2958 break;
2959 case DISPLAY_ORIENTATION_270:
2960 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2961 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2962 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2963 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2964 naturalPhysicalLeft = mViewport.physicalTop;
2965 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
2966 naturalDeviceWidth = mViewport.deviceHeight;
2967 naturalDeviceHeight = mViewport.deviceWidth;
2968 break;
2969 case DISPLAY_ORIENTATION_0:
2970 default:
2971 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2972 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2973 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2974 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2975 naturalPhysicalLeft = mViewport.physicalLeft;
2976 naturalPhysicalTop = mViewport.physicalTop;
2977 naturalDeviceWidth = mViewport.deviceWidth;
2978 naturalDeviceHeight = mViewport.deviceHeight;
2979 break;
2980 }
2981
2982 mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
2983 mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
2984 mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
2985 mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
2986
2987 mSurfaceOrientation = mParameters.orientationAware ?
2988 mViewport.orientation : DISPLAY_ORIENTATION_0;
2989 } else {
2990 mSurfaceWidth = rawWidth;
2991 mSurfaceHeight = rawHeight;
2992 mSurfaceLeft = 0;
2993 mSurfaceTop = 0;
2994 mSurfaceOrientation = DISPLAY_ORIENTATION_0;
2995 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002996 }
2997
2998 // If moving between pointer modes, need to reset some state.
2999 bool deviceModeChanged;
3000 if (mDeviceMode != oldDeviceMode) {
3001 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07003002 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08003003 }
3004
Jeff Browndaf4a122011-08-26 17:14:14 -07003005 // Create pointer controller if needed.
3006 if (mDeviceMode == DEVICE_MODE_POINTER ||
3007 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
3008 if (mPointerController == NULL) {
3009 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
3010 }
3011 } else {
3012 mPointerController.clear();
3013 }
3014
Jeff Brown83d616a2012-09-09 20:33:43 -07003015 if (viewportChanged || deviceModeChanged) {
3016 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
3017 "display id %d",
3018 getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight,
3019 mSurfaceOrientation, mDeviceMode, mViewport.displayId);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003020
Jeff Brown8d608662010-08-30 03:02:23 -07003021 // Configure X and Y factors.
Jeff Brown83d616a2012-09-09 20:33:43 -07003022 mXScale = float(mSurfaceWidth) / rawWidth;
3023 mYScale = float(mSurfaceHeight) / rawHeight;
3024 mXTranslate = -mSurfaceLeft;
3025 mYTranslate = -mSurfaceTop;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003026 mXPrecision = 1.0f / mXScale;
3027 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003028
Jeff Brownbe1aa822011-07-27 16:04:54 -07003029 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07003030 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003031 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07003032 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08003033
Jeff Brownbe1aa822011-07-27 16:04:54 -07003034 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003035
Jeff Brown8d608662010-08-30 03:02:23 -07003036 // Scale factor for terms that are not oriented in a particular axis.
3037 // If the pixels are square then xScale == yScale otherwise we fake it
3038 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003039 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003040
Jeff Brown8d608662010-08-30 03:02:23 -07003041 // Size of diagonal axis.
Jeff Brown83d616a2012-09-09 20:33:43 -07003042 float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003043
Jeff Browna1f89ce2011-08-11 00:05:01 -07003044 // Size factors.
3045 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
3046 if (mRawPointerAxes.touchMajor.valid
3047 && mRawPointerAxes.touchMajor.maxValue != 0) {
3048 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
3049 } else if (mRawPointerAxes.toolMajor.valid
3050 && mRawPointerAxes.toolMajor.maxValue != 0) {
3051 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
3052 } else {
3053 mSizeScale = 0.0f;
3054 }
3055
Jeff Brownbe1aa822011-07-27 16:04:54 -07003056 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003057 mOrientedRanges.haveToolSize = true;
3058 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08003059
Jeff Brownbe1aa822011-07-27 16:04:54 -07003060 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003061 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003062 mOrientedRanges.touchMajor.min = 0;
3063 mOrientedRanges.touchMajor.max = diagonalSize;
3064 mOrientedRanges.touchMajor.flat = 0;
3065 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003066
Jeff Brownbe1aa822011-07-27 16:04:54 -07003067 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
3068 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08003069
Jeff Brownbe1aa822011-07-27 16:04:54 -07003070 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003071 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003072 mOrientedRanges.toolMajor.min = 0;
3073 mOrientedRanges.toolMajor.max = diagonalSize;
3074 mOrientedRanges.toolMajor.flat = 0;
3075 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003076
Jeff Brownbe1aa822011-07-27 16:04:54 -07003077 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3078 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003079
3080 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003081 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003082 mOrientedRanges.size.min = 0;
3083 mOrientedRanges.size.max = 1.0;
3084 mOrientedRanges.size.flat = 0;
3085 mOrientedRanges.size.fuzz = 0;
3086 } else {
3087 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003088 }
3089
3090 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003091 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003092 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3093 || mCalibration.pressureCalibration
3094 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3095 if (mCalibration.havePressureScale) {
3096 mPressureScale = mCalibration.pressureScale;
3097 } else if (mRawPointerAxes.pressure.valid
3098 && mRawPointerAxes.pressure.maxValue != 0) {
3099 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003100 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003101 }
Jeff Brown8d608662010-08-30 03:02:23 -07003102
Jeff Brown65fd2512011-08-18 11:20:58 -07003103 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3104 mOrientedRanges.pressure.source = mSource;
3105 mOrientedRanges.pressure.min = 0;
3106 mOrientedRanges.pressure.max = 1.0;
3107 mOrientedRanges.pressure.flat = 0;
3108 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003109
Jeff Brown65fd2512011-08-18 11:20:58 -07003110 // Tilt
3111 mTiltXCenter = 0;
3112 mTiltXScale = 0;
3113 mTiltYCenter = 0;
3114 mTiltYScale = 0;
3115 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3116 if (mHaveTilt) {
3117 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3118 mRawPointerAxes.tiltX.maxValue);
3119 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3120 mRawPointerAxes.tiltY.maxValue);
3121 mTiltXScale = M_PI / 180;
3122 mTiltYScale = M_PI / 180;
3123
3124 mOrientedRanges.haveTilt = true;
3125
3126 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3127 mOrientedRanges.tilt.source = mSource;
3128 mOrientedRanges.tilt.min = 0;
3129 mOrientedRanges.tilt.max = M_PI_2;
3130 mOrientedRanges.tilt.flat = 0;
3131 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003132 }
3133
Jeff Brown8d608662010-08-30 03:02:23 -07003134 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07003135 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003136 if (mHaveTilt) {
3137 mOrientedRanges.haveOrientation = true;
3138
3139 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3140 mOrientedRanges.orientation.source = mSource;
3141 mOrientedRanges.orientation.min = -M_PI;
3142 mOrientedRanges.orientation.max = M_PI;
3143 mOrientedRanges.orientation.flat = 0;
3144 mOrientedRanges.orientation.fuzz = 0;
3145 } else if (mCalibration.orientationCalibration !=
3146 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003147 if (mCalibration.orientationCalibration
3148 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003149 if (mRawPointerAxes.orientation.valid) {
Jeff Brown037f7272012-06-25 17:31:23 -07003150 if (mRawPointerAxes.orientation.maxValue > 0) {
3151 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
3152 } else if (mRawPointerAxes.orientation.minValue < 0) {
3153 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
3154 } else {
3155 mOrientationScale = 0;
3156 }
Jeff Brown8d608662010-08-30 03:02:23 -07003157 }
3158 }
3159
Jeff Brownbe1aa822011-07-27 16:04:54 -07003160 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003161
Jeff Brownbe1aa822011-07-27 16:04:54 -07003162 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003163 mOrientedRanges.orientation.source = mSource;
3164 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003165 mOrientedRanges.orientation.max = M_PI_2;
3166 mOrientedRanges.orientation.flat = 0;
3167 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003168 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003169
3170 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003171 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003172 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3173 if (mCalibration.distanceCalibration
3174 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3175 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003176 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003177 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003178 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003179 }
3180 }
3181
Jeff Brownbe1aa822011-07-27 16:04:54 -07003182 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003183
Jeff Brownbe1aa822011-07-27 16:04:54 -07003184 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003185 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003186 mOrientedRanges.distance.min =
3187 mRawPointerAxes.distance.minValue * mDistanceScale;
3188 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003189 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003190 mOrientedRanges.distance.flat = 0;
3191 mOrientedRanges.distance.fuzz =
3192 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003193 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003194
Jeff Brown83d616a2012-09-09 20:33:43 -07003195 // Compute oriented precision, scales and ranges.
Jeff Brown9626b142011-03-03 02:09:54 -08003196 // Note that the maximum value reported is an inclusive maximum value so it is one
3197 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003198 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003199 case DISPLAY_ORIENTATION_90:
3200 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003201 mOrientedXPrecision = mYPrecision;
3202 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003203
Jeff Brown83d616a2012-09-09 20:33:43 -07003204 mOrientedRanges.x.min = mYTranslate;
3205 mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003206 mOrientedRanges.x.flat = 0;
3207 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003208
Jeff Brown83d616a2012-09-09 20:33:43 -07003209 mOrientedRanges.y.min = mXTranslate;
3210 mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003211 mOrientedRanges.y.flat = 0;
3212 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003213 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003214
Jeff Brown6d0fec22010-07-23 21:28:06 -07003215 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003216 mOrientedXPrecision = mXPrecision;
3217 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003218
Jeff Brown83d616a2012-09-09 20:33:43 -07003219 mOrientedRanges.x.min = mXTranslate;
3220 mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003221 mOrientedRanges.x.flat = 0;
3222 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003223
Jeff Brown83d616a2012-09-09 20:33:43 -07003224 mOrientedRanges.y.min = mYTranslate;
3225 mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003226 mOrientedRanges.y.flat = 0;
3227 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003228 break;
3229 }
Jeff Brownace13b12011-03-09 17:39:48 -08003230
3231 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003232 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown2352b972011-04-12 22:39:53 -07003233 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003234 float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003235
Jeff Brown2352b972011-04-12 22:39:53 -07003236 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003237 // given area relative to the diagonal size of the display when no acceleration
3238 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003239 // Assume that the touch pad has a square aspect ratio such that movements in
3240 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003241 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003242 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003243 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003244
3245 // Scale zooms to cover a smaller range of the display than movements do.
3246 // This value determines the area around the pointer that is affected by freeform
3247 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003248 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003249 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003250 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003251
Jeff Brown2352b972011-04-12 22:39:53 -07003252 // Max width between pointers to detect a swipe gesture is more than some fraction
3253 // of the diagonal axis of the touch pad. Touches that are wider than this are
3254 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003255 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003256 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003257 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003258
Jeff Brown65fd2512011-08-18 11:20:58 -07003259 // Abort current pointer usages because the state has changed.
3260 abortPointerUsage(when, 0 /*policyFlags*/);
3261
3262 // Inform the dispatcher about the changes.
3263 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003264 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003265 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003266}
3267
Jeff Brownbe1aa822011-07-27 16:04:54 -07003268void TouchInputMapper::dumpSurface(String8& dump) {
Jeff Brown83d616a2012-09-09 20:33:43 -07003269 dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, "
3270 "logicalFrame=[%d, %d, %d, %d], "
3271 "physicalFrame=[%d, %d, %d, %d], "
3272 "deviceSize=[%d, %d]\n",
3273 mViewport.displayId, mViewport.orientation,
3274 mViewport.logicalLeft, mViewport.logicalTop,
3275 mViewport.logicalRight, mViewport.logicalBottom,
3276 mViewport.physicalLeft, mViewport.physicalTop,
3277 mViewport.physicalRight, mViewport.physicalBottom,
3278 mViewport.deviceWidth, mViewport.deviceHeight);
3279
Jeff Brownbe1aa822011-07-27 16:04:54 -07003280 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3281 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003282 dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft);
3283 dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003284 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003285}
3286
Jeff Brownbe1aa822011-07-27 16:04:54 -07003287void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003288 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003289 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003290
Jeff Brownbe1aa822011-07-27 16:04:54 -07003291 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003292
Jeff Brown6328cdc2010-07-29 18:18:33 -07003293 if (virtualKeyDefinitions.size() == 0) {
3294 return;
3295 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003296
Jeff Brownbe1aa822011-07-27 16:04:54 -07003297 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003298
Jeff Brownbe1aa822011-07-27 16:04:54 -07003299 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3300 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3301 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3302 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003303
3304 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003305 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003306 virtualKeyDefinitions[i];
3307
Jeff Brownbe1aa822011-07-27 16:04:54 -07003308 mVirtualKeys.add();
3309 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003310
3311 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3312 int32_t keyCode;
3313 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003314 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003315 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003316 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003317 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003318 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003319 }
3320
Jeff Brown6328cdc2010-07-29 18:18:33 -07003321 virtualKey.keyCode = keyCode;
3322 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003323
Jeff Brown6328cdc2010-07-29 18:18:33 -07003324 // convert the key definition's display coordinates into touch coordinates for a hit box
3325 int32_t halfWidth = virtualKeyDefinition.width / 2;
3326 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003327
Jeff Brown6328cdc2010-07-29 18:18:33 -07003328 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003329 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003330 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003331 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003332 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003333 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003334 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003335 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003336 }
3337}
3338
Jeff Brownbe1aa822011-07-27 16:04:54 -07003339void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3340 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003341 dump.append(INDENT3 "Virtual Keys:\n");
3342
Jeff Brownbe1aa822011-07-27 16:04:54 -07003343 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3344 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003345 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3346 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3347 i, virtualKey.scanCode, virtualKey.keyCode,
3348 virtualKey.hitLeft, virtualKey.hitRight,
3349 virtualKey.hitTop, virtualKey.hitBottom);
3350 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003351 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003352}
3353
Jeff Brown8d608662010-08-30 03:02:23 -07003354void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003355 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003356 Calibration& out = mCalibration;
3357
Jeff Browna1f89ce2011-08-11 00:05:01 -07003358 // Size
3359 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3360 String8 sizeCalibrationString;
3361 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3362 if (sizeCalibrationString == "none") {
3363 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3364 } else if (sizeCalibrationString == "geometric") {
3365 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3366 } else if (sizeCalibrationString == "diameter") {
3367 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
Jeff Brown037f7272012-06-25 17:31:23 -07003368 } else if (sizeCalibrationString == "box") {
3369 out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003370 } else if (sizeCalibrationString == "area") {
3371 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3372 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003373 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003374 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003375 }
3376 }
3377
Jeff Browna1f89ce2011-08-11 00:05:01 -07003378 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3379 out.sizeScale);
3380 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3381 out.sizeBias);
3382 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3383 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003384
3385 // Pressure
3386 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3387 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003388 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003389 if (pressureCalibrationString == "none") {
3390 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3391 } else if (pressureCalibrationString == "physical") {
3392 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3393 } else if (pressureCalibrationString == "amplitude") {
3394 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3395 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003396 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003397 pressureCalibrationString.string());
3398 }
3399 }
3400
Jeff Brown8d608662010-08-30 03:02:23 -07003401 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3402 out.pressureScale);
3403
Jeff Brown8d608662010-08-30 03:02:23 -07003404 // Orientation
3405 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3406 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003407 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003408 if (orientationCalibrationString == "none") {
3409 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3410 } else if (orientationCalibrationString == "interpolated") {
3411 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003412 } else if (orientationCalibrationString == "vector") {
3413 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003414 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003415 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003416 orientationCalibrationString.string());
3417 }
3418 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003419
3420 // Distance
3421 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3422 String8 distanceCalibrationString;
3423 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3424 if (distanceCalibrationString == "none") {
3425 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3426 } else if (distanceCalibrationString == "scaled") {
3427 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3428 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003429 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003430 distanceCalibrationString.string());
3431 }
3432 }
3433
3434 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3435 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003436}
3437
3438void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003439 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003440 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3441 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3442 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003443 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003444 } else {
3445 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3446 }
Jeff Brown8d608662010-08-30 03:02:23 -07003447
Jeff Browna1f89ce2011-08-11 00:05:01 -07003448 // Pressure
3449 if (mRawPointerAxes.pressure.valid) {
3450 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3451 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3452 }
3453 } else {
3454 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003455 }
3456
3457 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003458 if (mRawPointerAxes.orientation.valid) {
3459 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003460 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003461 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003462 } else {
3463 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003464 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003465
3466 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003467 if (mRawPointerAxes.distance.valid) {
3468 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003469 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003470 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003471 } else {
3472 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003473 }
Jeff Brown8d608662010-08-30 03:02:23 -07003474}
3475
Jeff Brownef3d7e82010-09-30 14:33:04 -07003476void TouchInputMapper::dumpCalibration(String8& dump) {
3477 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003478
Jeff Browna1f89ce2011-08-11 00:05:01 -07003479 // Size
3480 switch (mCalibration.sizeCalibration) {
3481 case Calibration::SIZE_CALIBRATION_NONE:
3482 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003483 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003484 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3485 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003486 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003487 case Calibration::SIZE_CALIBRATION_DIAMETER:
3488 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3489 break;
Jeff Brown037f7272012-06-25 17:31:23 -07003490 case Calibration::SIZE_CALIBRATION_BOX:
3491 dump.append(INDENT4 "touch.size.calibration: box\n");
3492 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003493 case Calibration::SIZE_CALIBRATION_AREA:
3494 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003495 break;
3496 default:
Steve Blockec193de2012-01-09 18:35:44 +00003497 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003498 }
3499
Jeff Browna1f89ce2011-08-11 00:05:01 -07003500 if (mCalibration.haveSizeScale) {
3501 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3502 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003503 }
3504
Jeff Browna1f89ce2011-08-11 00:05:01 -07003505 if (mCalibration.haveSizeBias) {
3506 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3507 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003508 }
3509
Jeff Browna1f89ce2011-08-11 00:05:01 -07003510 if (mCalibration.haveSizeIsSummed) {
3511 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3512 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003513 }
3514
3515 // Pressure
3516 switch (mCalibration.pressureCalibration) {
3517 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003518 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003519 break;
3520 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003521 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003522 break;
3523 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003524 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003525 break;
3526 default:
Steve Blockec193de2012-01-09 18:35:44 +00003527 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003528 }
3529
Jeff Brown8d608662010-08-30 03:02:23 -07003530 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003531 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3532 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003533 }
3534
Jeff Brown8d608662010-08-30 03:02:23 -07003535 // Orientation
3536 switch (mCalibration.orientationCalibration) {
3537 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003538 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003539 break;
3540 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003541 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003542 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003543 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3544 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3545 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003546 default:
Steve Blockec193de2012-01-09 18:35:44 +00003547 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003548 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003549
3550 // Distance
3551 switch (mCalibration.distanceCalibration) {
3552 case Calibration::DISTANCE_CALIBRATION_NONE:
3553 dump.append(INDENT4 "touch.distance.calibration: none\n");
3554 break;
3555 case Calibration::DISTANCE_CALIBRATION_SCALED:
3556 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3557 break;
3558 default:
Steve Blockec193de2012-01-09 18:35:44 +00003559 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003560 }
3561
3562 if (mCalibration.haveDistanceScale) {
3563 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3564 mCalibration.distanceScale);
3565 }
Jeff Brown8d608662010-08-30 03:02:23 -07003566}
3567
Jeff Brown65fd2512011-08-18 11:20:58 -07003568void TouchInputMapper::reset(nsecs_t when) {
3569 mCursorButtonAccumulator.reset(getDevice());
3570 mCursorScrollAccumulator.reset(getDevice());
3571 mTouchButtonAccumulator.reset(getDevice());
3572
3573 mPointerVelocityControl.reset();
3574 mWheelXVelocityControl.reset();
3575 mWheelYVelocityControl.reset();
3576
Jeff Brownbe1aa822011-07-27 16:04:54 -07003577 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003578 mLastRawPointerData.clear();
3579 mCurrentCookedPointerData.clear();
3580 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003581 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003582 mLastButtonState = 0;
3583 mCurrentRawVScroll = 0;
3584 mCurrentRawHScroll = 0;
3585 mCurrentFingerIdBits.clear();
3586 mLastFingerIdBits.clear();
3587 mCurrentStylusIdBits.clear();
3588 mLastStylusIdBits.clear();
3589 mCurrentMouseIdBits.clear();
3590 mLastMouseIdBits.clear();
3591 mPointerUsage = POINTER_USAGE_NONE;
3592 mSentHoverEnter = false;
3593 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003594
Jeff Brown65fd2512011-08-18 11:20:58 -07003595 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003596
Jeff Brown65fd2512011-08-18 11:20:58 -07003597 mPointerGesture.reset();
3598 mPointerSimple.reset();
3599
3600 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003601 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3602 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003603 }
3604
Jeff Brown65fd2512011-08-18 11:20:58 -07003605 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003606}
3607
Jeff Brown65fd2512011-08-18 11:20:58 -07003608void TouchInputMapper::process(const RawEvent* rawEvent) {
3609 mCursorButtonAccumulator.process(rawEvent);
3610 mCursorScrollAccumulator.process(rawEvent);
3611 mTouchButtonAccumulator.process(rawEvent);
3612
Jeff Brown49ccac52012-04-11 18:27:33 -07003613 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003614 sync(rawEvent->when);
3615 }
3616}
3617
3618void TouchInputMapper::sync(nsecs_t when) {
3619 // Sync button state.
3620 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3621 | mCursorButtonAccumulator.getButtonState();
3622
3623 // Sync scroll state.
3624 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3625 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3626 mCursorScrollAccumulator.finishSync();
3627
3628 // Sync touch state.
3629 bool havePointerIds = true;
3630 mCurrentRawPointerData.clear();
3631 syncTouch(when, &havePointerIds);
3632
Jeff Brownaa3855d2011-03-17 01:34:19 -07003633#if DEBUG_RAW_EVENTS
3634 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003635 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003636 mLastRawPointerData.pointerCount,
3637 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003638 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003639 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003640 "hovering ids 0x%08x -> 0x%08x",
3641 mLastRawPointerData.pointerCount,
3642 mCurrentRawPointerData.pointerCount,
3643 mLastRawPointerData.touchingIdBits.value,
3644 mCurrentRawPointerData.touchingIdBits.value,
3645 mLastRawPointerData.hoveringIdBits.value,
3646 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003647 }
3648#endif
3649
Jeff Brown65fd2512011-08-18 11:20:58 -07003650 // Reset state that we will compute below.
3651 mCurrentFingerIdBits.clear();
3652 mCurrentStylusIdBits.clear();
3653 mCurrentMouseIdBits.clear();
3654 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003655
Jeff Brown65fd2512011-08-18 11:20:58 -07003656 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3657 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003658 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003659 mCurrentButtonState = 0;
3660 } else {
3661 // Preprocess pointer data.
3662 if (!havePointerIds) {
3663 assignPointerIds();
3664 }
3665
3666 // Handle policy on initial down or hover events.
3667 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003668 bool initialDown = mLastRawPointerData.pointerCount == 0
3669 && mCurrentRawPointerData.pointerCount != 0;
3670 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3671 if (initialDown || buttonsPressed) {
3672 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003673 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003674 getContext()->fadePointer();
3675 }
3676
3677 // Initial downs on external touch devices should wake the device.
3678 // We don't do this for internal touch screens to prevent them from waking
3679 // up in your pocket.
3680 // TODO: Use the input device configuration to control this behavior more finely.
3681 if (getDevice()->isExternal()) {
3682 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3683 }
3684 }
3685
3686 // Synthesize key down from raw buttons if needed.
3687 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3688 policyFlags, mLastButtonState, mCurrentButtonState);
3689
3690 // Consume raw off-screen touches before cooking pointer data.
3691 // If touches are consumed, subsequent code will not receive any pointer data.
3692 if (consumeRawTouches(when, policyFlags)) {
3693 mCurrentRawPointerData.clear();
3694 }
3695
3696 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3697 // with cooked pointer data that has the same ids and indices as the raw data.
3698 // The following code can use either the raw or cooked data, as needed.
3699 cookPointerData();
3700
3701 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003702 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003703 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3704 uint32_t id = idBits.clearFirstMarkedBit();
3705 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3706 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3707 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3708 mCurrentStylusIdBits.markBit(id);
3709 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3710 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3711 mCurrentFingerIdBits.markBit(id);
3712 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3713 mCurrentMouseIdBits.markBit(id);
3714 }
3715 }
3716 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3717 uint32_t id = idBits.clearFirstMarkedBit();
3718 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3719 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3720 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3721 mCurrentStylusIdBits.markBit(id);
3722 }
3723 }
3724
3725 // Stylus takes precedence over all tools, then mouse, then finger.
3726 PointerUsage pointerUsage = mPointerUsage;
3727 if (!mCurrentStylusIdBits.isEmpty()) {
3728 mCurrentMouseIdBits.clear();
3729 mCurrentFingerIdBits.clear();
3730 pointerUsage = POINTER_USAGE_STYLUS;
3731 } else if (!mCurrentMouseIdBits.isEmpty()) {
3732 mCurrentFingerIdBits.clear();
3733 pointerUsage = POINTER_USAGE_MOUSE;
3734 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3735 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003736 }
3737
3738 dispatchPointerUsage(when, policyFlags, pointerUsage);
3739 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003740 if (mDeviceMode == DEVICE_MODE_DIRECT
3741 && mConfig.showTouches && mPointerController != NULL) {
3742 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3743 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3744
3745 mPointerController->setButtonState(mCurrentButtonState);
3746 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3747 mCurrentCookedPointerData.idToIndex,
3748 mCurrentCookedPointerData.touchingIdBits);
3749 }
3750
Jeff Brown65fd2512011-08-18 11:20:58 -07003751 dispatchHoverExit(when, policyFlags);
3752 dispatchTouches(when, policyFlags);
3753 dispatchHoverEnterAndMove(when, policyFlags);
3754 }
3755
3756 // Synthesize key up from raw buttons if needed.
3757 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3758 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003759 }
3760
Jeff Brown6328cdc2010-07-29 18:18:33 -07003761 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003762 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3763 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3764 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003765 mLastFingerIdBits = mCurrentFingerIdBits;
3766 mLastStylusIdBits = mCurrentStylusIdBits;
3767 mLastMouseIdBits = mCurrentMouseIdBits;
3768
3769 // Clear some transient state.
3770 mCurrentRawVScroll = 0;
3771 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003772}
3773
Jeff Brown79ac9692011-04-19 21:20:10 -07003774void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003775 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003776 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3777 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3778 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003779 }
3780}
3781
Jeff Brownbe1aa822011-07-27 16:04:54 -07003782bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3783 // Check for release of a virtual key.
3784 if (mCurrentVirtualKey.down) {
3785 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3786 // Pointer went up while virtual key was down.
3787 mCurrentVirtualKey.down = false;
3788 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003789#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003790 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003791 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003792#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003793 dispatchVirtualKey(when, policyFlags,
3794 AKEY_EVENT_ACTION_UP,
3795 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003796 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003797 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003798 }
3799
Jeff Brownbe1aa822011-07-27 16:04:54 -07003800 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3801 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3802 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3803 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3804 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3805 // Pointer is still within the space of the virtual key.
3806 return true;
3807 }
3808 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003809
Jeff Brownbe1aa822011-07-27 16:04:54 -07003810 // Pointer left virtual key area or another pointer also went down.
3811 // Send key cancellation but do not consume the touch yet.
3812 // This is useful when the user swipes through from the virtual key area
3813 // into the main display surface.
3814 mCurrentVirtualKey.down = false;
3815 if (!mCurrentVirtualKey.ignored) {
3816#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003817 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003818 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3819#endif
3820 dispatchVirtualKey(when, policyFlags,
3821 AKEY_EVENT_ACTION_UP,
3822 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3823 | AKEY_EVENT_FLAG_CANCELED);
3824 }
3825 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003826
Jeff Brownbe1aa822011-07-27 16:04:54 -07003827 if (mLastRawPointerData.touchingIdBits.isEmpty()
3828 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3829 // Pointer just went down. Check for virtual key press or off-screen touches.
3830 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3831 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3832 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3833 // If exactly one pointer went down, check for virtual key hit.
3834 // Otherwise we will drop the entire stroke.
3835 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3836 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3837 if (virtualKey) {
3838 mCurrentVirtualKey.down = true;
3839 mCurrentVirtualKey.downTime = when;
3840 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3841 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3842 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3843 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3844
3845 if (!mCurrentVirtualKey.ignored) {
3846#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003847 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003848 mCurrentVirtualKey.keyCode,
3849 mCurrentVirtualKey.scanCode);
3850#endif
3851 dispatchVirtualKey(when, policyFlags,
3852 AKEY_EVENT_ACTION_DOWN,
3853 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3854 }
3855 }
3856 }
3857 return true;
3858 }
3859 }
3860
Jeff Brownfe508922011-01-18 15:10:10 -08003861 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003862 // most recent touch within the screen area. The idea is to filter out stray
3863 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003864 //
3865 // Problems we're trying to solve:
3866 //
3867 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3868 // virtual key area that is implemented by a separate touch panel and accidentally
3869 // triggers a virtual key.
3870 //
3871 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3872 // area and accidentally triggers a virtual key. This often happens when virtual keys
3873 // are layed out below the screen near to where the on screen keyboard's space bar
3874 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003875 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003876 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003877 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003878 return false;
3879}
3880
3881void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3882 int32_t keyEventAction, int32_t keyEventFlags) {
3883 int32_t keyCode = mCurrentVirtualKey.keyCode;
3884 int32_t scanCode = mCurrentVirtualKey.scanCode;
3885 nsecs_t downTime = mCurrentVirtualKey.downTime;
3886 int32_t metaState = mContext->getGlobalMetaState();
3887 policyFlags |= POLICY_FLAG_VIRTUAL;
3888
3889 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3890 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3891 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003892}
3893
Jeff Brown6d0fec22010-07-23 21:28:06 -07003894void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003895 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3896 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003897 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003898 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003899
3900 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003901 if (!currentIdBits.isEmpty()) {
3902 // No pointer id changes so this is a move event.
3903 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003904 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003905 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3906 AMOTION_EVENT_EDGE_FLAG_NONE,
3907 mCurrentCookedPointerData.pointerProperties,
3908 mCurrentCookedPointerData.pointerCoords,
3909 mCurrentCookedPointerData.idToIndex,
3910 currentIdBits, -1,
3911 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3912 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003913 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003914 // There may be pointers going up and pointers going down and pointers moving
3915 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003916 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3917 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003918 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003919 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003920
Jeff Brownace13b12011-03-09 17:39:48 -08003921 // Update last coordinates of pointers that have moved so that we observe the new
3922 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003923 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003924 mCurrentCookedPointerData.pointerProperties,
3925 mCurrentCookedPointerData.pointerCoords,
3926 mCurrentCookedPointerData.idToIndex,
3927 mLastCookedPointerData.pointerProperties,
3928 mLastCookedPointerData.pointerCoords,
3929 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003930 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003931 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003932 moveNeeded = true;
3933 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003934
Jeff Brownace13b12011-03-09 17:39:48 -08003935 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003936 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003937 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003938
Jeff Brown65fd2512011-08-18 11:20:58 -07003939 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003940 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003941 mLastCookedPointerData.pointerProperties,
3942 mLastCookedPointerData.pointerCoords,
3943 mLastCookedPointerData.idToIndex,
3944 dispatchedIdBits, upId,
3945 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003946 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003947 }
3948
Jeff Brownc3db8582010-10-20 15:33:38 -07003949 // Dispatch move events if any of the remaining pointers moved from their old locations.
3950 // Although applications receive new locations as part of individual pointer up
3951 // events, they do not generally handle them except when presented in a move event.
3952 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003953 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003954 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003955 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003956 mCurrentCookedPointerData.pointerProperties,
3957 mCurrentCookedPointerData.pointerCoords,
3958 mCurrentCookedPointerData.idToIndex,
3959 dispatchedIdBits, -1,
3960 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003961 }
3962
3963 // Dispatch pointer down events using the new pointer locations.
3964 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003965 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003966 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003967
Jeff Brownace13b12011-03-09 17:39:48 -08003968 if (dispatchedIdBits.count() == 1) {
3969 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003970 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003971 }
3972
Jeff Brown65fd2512011-08-18 11:20:58 -07003973 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003974 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003975 mCurrentCookedPointerData.pointerProperties,
3976 mCurrentCookedPointerData.pointerCoords,
3977 mCurrentCookedPointerData.idToIndex,
3978 dispatchedIdBits, downId,
3979 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003980 }
3981 }
Jeff Brownace13b12011-03-09 17:39:48 -08003982}
3983
Jeff Brownbe1aa822011-07-27 16:04:54 -07003984void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3985 if (mSentHoverEnter &&
3986 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3987 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3988 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003989 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003990 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3991 mLastCookedPointerData.pointerProperties,
3992 mLastCookedPointerData.pointerCoords,
3993 mLastCookedPointerData.idToIndex,
3994 mLastCookedPointerData.hoveringIdBits, -1,
3995 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3996 mSentHoverEnter = false;
3997 }
3998}
Jeff Brownace13b12011-03-09 17:39:48 -08003999
Jeff Brownbe1aa822011-07-27 16:04:54 -07004000void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
4001 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
4002 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
4003 int32_t metaState = getContext()->getGlobalMetaState();
4004 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004005 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004006 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
4007 mCurrentCookedPointerData.pointerProperties,
4008 mCurrentCookedPointerData.pointerCoords,
4009 mCurrentCookedPointerData.idToIndex,
4010 mCurrentCookedPointerData.hoveringIdBits, -1,
4011 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4012 mSentHoverEnter = true;
4013 }
Jeff Brownace13b12011-03-09 17:39:48 -08004014
Jeff Brown65fd2512011-08-18 11:20:58 -07004015 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004016 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
4017 mCurrentCookedPointerData.pointerProperties,
4018 mCurrentCookedPointerData.pointerCoords,
4019 mCurrentCookedPointerData.idToIndex,
4020 mCurrentCookedPointerData.hoveringIdBits, -1,
4021 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4022 }
4023}
4024
4025void TouchInputMapper::cookPointerData() {
4026 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4027
4028 mCurrentCookedPointerData.clear();
4029 mCurrentCookedPointerData.pointerCount = currentPointerCount;
4030 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
4031 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
4032
4033 // Walk through the the active pointers and map device coordinates onto
4034 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08004035 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004036 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004037
Jeff Browna1f89ce2011-08-11 00:05:01 -07004038 // Size
4039 float touchMajor, touchMinor, toolMajor, toolMinor, size;
4040 switch (mCalibration.sizeCalibration) {
4041 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
4042 case Calibration::SIZE_CALIBRATION_DIAMETER:
Jeff Brown037f7272012-06-25 17:31:23 -07004043 case Calibration::SIZE_CALIBRATION_BOX:
Jeff Browna1f89ce2011-08-11 00:05:01 -07004044 case Calibration::SIZE_CALIBRATION_AREA:
4045 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
4046 touchMajor = in.touchMajor;
4047 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
4048 toolMajor = in.toolMajor;
4049 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
4050 size = mRawPointerAxes.touchMinor.valid
4051 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4052 } else if (mRawPointerAxes.touchMajor.valid) {
4053 toolMajor = touchMajor = in.touchMajor;
4054 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
4055 ? in.touchMinor : in.touchMajor;
4056 size = mRawPointerAxes.touchMinor.valid
4057 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4058 } else if (mRawPointerAxes.toolMajor.valid) {
4059 touchMajor = toolMajor = in.toolMajor;
4060 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
4061 ? in.toolMinor : in.toolMajor;
4062 size = mRawPointerAxes.toolMinor.valid
4063 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004064 } else {
Steve Blockec193de2012-01-09 18:35:44 +00004065 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07004066 "Size calibration should have been resolved to NONE.");
4067 touchMajor = 0;
4068 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004069 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004070 toolMinor = 0;
4071 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004072 }
Jeff Brownace13b12011-03-09 17:39:48 -08004073
Jeff Browna1f89ce2011-08-11 00:05:01 -07004074 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
4075 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
4076 if (touchingCount > 1) {
4077 touchMajor /= touchingCount;
4078 touchMinor /= touchingCount;
4079 toolMajor /= touchingCount;
4080 toolMinor /= touchingCount;
4081 size /= touchingCount;
4082 }
4083 }
Jeff Brownace13b12011-03-09 17:39:48 -08004084
Jeff Browna1f89ce2011-08-11 00:05:01 -07004085 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4086 touchMajor *= mGeometricScale;
4087 touchMinor *= mGeometricScale;
4088 toolMajor *= mGeometricScale;
4089 toolMinor *= mGeometricScale;
4090 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4091 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004092 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004093 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4094 toolMinor = toolMajor;
4095 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4096 touchMinor = touchMajor;
4097 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004098 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004099
4100 mCalibration.applySizeScaleAndBias(&touchMajor);
4101 mCalibration.applySizeScaleAndBias(&touchMinor);
4102 mCalibration.applySizeScaleAndBias(&toolMajor);
4103 mCalibration.applySizeScaleAndBias(&toolMinor);
4104 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004105 break;
4106 default:
4107 touchMajor = 0;
4108 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004109 toolMajor = 0;
4110 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004111 size = 0;
4112 break;
4113 }
4114
Jeff Browna1f89ce2011-08-11 00:05:01 -07004115 // Pressure
4116 float pressure;
4117 switch (mCalibration.pressureCalibration) {
4118 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4119 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4120 pressure = in.pressure * mPressureScale;
4121 break;
4122 default:
4123 pressure = in.isHovering ? 0 : 1;
4124 break;
4125 }
4126
Jeff Brown65fd2512011-08-18 11:20:58 -07004127 // Tilt and Orientation
4128 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004129 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004130 if (mHaveTilt) {
4131 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4132 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4133 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4134 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4135 } else {
4136 tilt = 0;
4137
4138 switch (mCalibration.orientationCalibration) {
4139 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown037f7272012-06-25 17:31:23 -07004140 orientation = in.orientation * mOrientationScale;
Jeff Brown65fd2512011-08-18 11:20:58 -07004141 break;
4142 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4143 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4144 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4145 if (c1 != 0 || c2 != 0) {
4146 orientation = atan2f(c1, c2) * 0.5f;
4147 float confidence = hypotf(c1, c2);
4148 float scale = 1.0f + confidence / 16.0f;
4149 touchMajor *= scale;
4150 touchMinor /= scale;
4151 toolMajor *= scale;
4152 toolMinor /= scale;
4153 } else {
4154 orientation = 0;
4155 }
4156 break;
4157 }
4158 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004159 orientation = 0;
4160 }
Jeff Brownace13b12011-03-09 17:39:48 -08004161 }
4162
Jeff Brown80fd47c2011-05-24 01:07:44 -07004163 // Distance
4164 float distance;
4165 switch (mCalibration.distanceCalibration) {
4166 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004167 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004168 break;
4169 default:
4170 distance = 0;
4171 }
4172
Jeff Brownace13b12011-03-09 17:39:48 -08004173 // X and Y
4174 // Adjust coords for surface orientation.
4175 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004176 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004177 case DISPLAY_ORIENTATION_90:
Jeff Brown83d616a2012-09-09 20:33:43 -07004178 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4179 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004180 orientation -= M_PI_2;
4181 if (orientation < - M_PI_2) {
4182 orientation += M_PI;
4183 }
4184 break;
4185 case DISPLAY_ORIENTATION_180:
Jeff Brown83d616a2012-09-09 20:33:43 -07004186 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
4187 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004188 break;
4189 case DISPLAY_ORIENTATION_270:
Jeff Brown83d616a2012-09-09 20:33:43 -07004190 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
4191 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004192 orientation += M_PI_2;
4193 if (orientation > M_PI_2) {
4194 orientation -= M_PI;
4195 }
4196 break;
4197 default:
Jeff Brown83d616a2012-09-09 20:33:43 -07004198 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4199 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004200 break;
4201 }
4202
4203 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004204 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004205 out.clear();
4206 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4207 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4208 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4209 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4210 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4211 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4212 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4213 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4214 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004215 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004216 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004217
4218 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004219 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4220 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004221 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004222 properties.id = id;
4223 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004224
Jeff Brownbe1aa822011-07-27 16:04:54 -07004225 // Write id index.
4226 mCurrentCookedPointerData.idToIndex[id] = i;
4227 }
Jeff Brownace13b12011-03-09 17:39:48 -08004228}
4229
Jeff Brown65fd2512011-08-18 11:20:58 -07004230void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4231 PointerUsage pointerUsage) {
4232 if (pointerUsage != mPointerUsage) {
4233 abortPointerUsage(when, policyFlags);
4234 mPointerUsage = pointerUsage;
4235 }
4236
4237 switch (mPointerUsage) {
4238 case POINTER_USAGE_GESTURES:
4239 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4240 break;
4241 case POINTER_USAGE_STYLUS:
4242 dispatchPointerStylus(when, policyFlags);
4243 break;
4244 case POINTER_USAGE_MOUSE:
4245 dispatchPointerMouse(when, policyFlags);
4246 break;
4247 default:
4248 break;
4249 }
4250}
4251
4252void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4253 switch (mPointerUsage) {
4254 case POINTER_USAGE_GESTURES:
4255 abortPointerGestures(when, policyFlags);
4256 break;
4257 case POINTER_USAGE_STYLUS:
4258 abortPointerStylus(when, policyFlags);
4259 break;
4260 case POINTER_USAGE_MOUSE:
4261 abortPointerMouse(when, policyFlags);
4262 break;
4263 default:
4264 break;
4265 }
4266
4267 mPointerUsage = POINTER_USAGE_NONE;
4268}
4269
Jeff Brown79ac9692011-04-19 21:20:10 -07004270void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4271 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004272 // Update current gesture coordinates.
4273 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004274 bool sendEvents = preparePointerGestures(when,
4275 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4276 if (!sendEvents) {
4277 return;
4278 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004279 if (finishPreviousGesture) {
4280 cancelPreviousGesture = false;
4281 }
Jeff Brownace13b12011-03-09 17:39:48 -08004282
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004283 // Update the pointer presentation and spots.
4284 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4285 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4286 if (finishPreviousGesture || cancelPreviousGesture) {
4287 mPointerController->clearSpots();
4288 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004289 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4290 mPointerGesture.currentGestureIdToIndex,
4291 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004292 } else {
4293 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4294 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004295
Jeff Brown538881e2011-05-25 18:23:38 -07004296 // Show or hide the pointer if needed.
4297 switch (mPointerGesture.currentGestureMode) {
4298 case PointerGesture::NEUTRAL:
4299 case PointerGesture::QUIET:
4300 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4301 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4302 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4303 // Remind the user of where the pointer is after finishing a gesture with spots.
4304 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4305 }
4306 break;
4307 case PointerGesture::TAP:
4308 case PointerGesture::TAP_DRAG:
4309 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4310 case PointerGesture::HOVER:
4311 case PointerGesture::PRESS:
4312 // Unfade the pointer when the current gesture manipulates the
4313 // area directly under the pointer.
4314 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4315 break;
4316 case PointerGesture::SWIPE:
4317 case PointerGesture::FREEFORM:
4318 // Fade the pointer when the current gesture manipulates a different
4319 // area and there are spots to guide the user experience.
4320 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4321 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4322 } else {
4323 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4324 }
4325 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004326 }
4327
Jeff Brownace13b12011-03-09 17:39:48 -08004328 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004329 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004330 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004331
4332 // Update last coordinates of pointers that have moved so that we observe the new
4333 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004334 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4335 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4336 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004337 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004338 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4339 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4340 bool moveNeeded = false;
4341 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004342 && !mPointerGesture.lastGestureIdBits.isEmpty()
4343 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004344 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4345 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004346 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004347 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004348 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004349 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4350 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004351 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004352 moveNeeded = true;
4353 }
Jeff Brownace13b12011-03-09 17:39:48 -08004354 }
4355
4356 // Send motion events for all pointers that went up or were canceled.
4357 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4358 if (!dispatchedGestureIdBits.isEmpty()) {
4359 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004360 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004361 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4362 AMOTION_EVENT_EDGE_FLAG_NONE,
4363 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004364 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4365 dispatchedGestureIdBits, -1,
4366 0, 0, mPointerGesture.downTime);
4367
4368 dispatchedGestureIdBits.clear();
4369 } else {
4370 BitSet32 upGestureIdBits;
4371 if (finishPreviousGesture) {
4372 upGestureIdBits = dispatchedGestureIdBits;
4373 } else {
4374 upGestureIdBits.value = dispatchedGestureIdBits.value
4375 & ~mPointerGesture.currentGestureIdBits.value;
4376 }
4377 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004378 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004379
Jeff Brown65fd2512011-08-18 11:20:58 -07004380 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004381 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004382 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4383 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004384 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4385 dispatchedGestureIdBits, id,
4386 0, 0, mPointerGesture.downTime);
4387
4388 dispatchedGestureIdBits.clearBit(id);
4389 }
4390 }
4391 }
4392
4393 // Send motion events for all pointers that moved.
4394 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004395 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004396 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4397 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004398 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4399 dispatchedGestureIdBits, -1,
4400 0, 0, mPointerGesture.downTime);
4401 }
4402
4403 // Send motion events for all pointers that went down.
4404 if (down) {
4405 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4406 & ~dispatchedGestureIdBits.value);
4407 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004408 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004409 dispatchedGestureIdBits.markBit(id);
4410
Jeff Brownace13b12011-03-09 17:39:48 -08004411 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004412 mPointerGesture.downTime = when;
4413 }
4414
Jeff Brown65fd2512011-08-18 11:20:58 -07004415 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004416 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004417 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004418 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4419 dispatchedGestureIdBits, id,
4420 0, 0, mPointerGesture.downTime);
4421 }
4422 }
4423
Jeff Brownace13b12011-03-09 17:39:48 -08004424 // Send motion events for hover.
4425 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004426 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004427 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4428 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4429 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004430 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4431 mPointerGesture.currentGestureIdBits, -1,
4432 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004433 } else if (dispatchedGestureIdBits.isEmpty()
4434 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4435 // Synthesize a hover move event after all pointers go up to indicate that
4436 // the pointer is hovering again even if the user is not currently touching
4437 // the touch pad. This ensures that a view will receive a fresh hover enter
4438 // event after a tap.
4439 float x, y;
4440 mPointerController->getPosition(&x, &y);
4441
4442 PointerProperties pointerProperties;
4443 pointerProperties.clear();
4444 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004445 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004446
4447 PointerCoords pointerCoords;
4448 pointerCoords.clear();
4449 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4450 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4451
Jeff Brown65fd2512011-08-18 11:20:58 -07004452 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004453 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4454 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07004455 mViewport.displayId, 1, &pointerProperties, &pointerCoords,
4456 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004457 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004458 }
4459
4460 // Update state.
4461 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4462 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004463 mPointerGesture.lastGestureIdBits.clear();
4464 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004465 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4466 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004467 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004468 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004469 mPointerGesture.lastGestureProperties[index].copyFrom(
4470 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004471 mPointerGesture.lastGestureCoords[index].copyFrom(
4472 mPointerGesture.currentGestureCoords[index]);
4473 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004474 }
4475 }
4476}
4477
Jeff Brown65fd2512011-08-18 11:20:58 -07004478void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4479 // Cancel previously dispatches pointers.
4480 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4481 int32_t metaState = getContext()->getGlobalMetaState();
4482 int32_t buttonState = mCurrentButtonState;
4483 dispatchMotion(when, policyFlags, mSource,
4484 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4485 AMOTION_EVENT_EDGE_FLAG_NONE,
4486 mPointerGesture.lastGestureProperties,
4487 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4488 mPointerGesture.lastGestureIdBits, -1,
4489 0, 0, mPointerGesture.downTime);
4490 }
4491
4492 // Reset the current pointer gesture.
4493 mPointerGesture.reset();
4494 mPointerVelocityControl.reset();
4495
4496 // Remove any current spots.
4497 if (mPointerController != NULL) {
4498 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4499 mPointerController->clearSpots();
4500 }
4501}
4502
Jeff Brown79ac9692011-04-19 21:20:10 -07004503bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4504 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004505 *outCancelPreviousGesture = false;
4506 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004507
Jeff Brown79ac9692011-04-19 21:20:10 -07004508 // Handle TAP timeout.
4509 if (isTimeout) {
4510#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004511 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004512#endif
4513
4514 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004515 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004516 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004517 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004518 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004519 } else {
4520 // The tap is finished.
4521#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004522 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004523#endif
4524 *outFinishPreviousGesture = true;
4525
4526 mPointerGesture.activeGestureId = -1;
4527 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4528 mPointerGesture.currentGestureIdBits.clear();
4529
Jeff Brown65fd2512011-08-18 11:20:58 -07004530 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004531 return true;
4532 }
4533 }
4534
4535 // We did not handle this timeout.
4536 return false;
4537 }
4538
Jeff Brown65fd2512011-08-18 11:20:58 -07004539 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4540 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4541
Jeff Brownace13b12011-03-09 17:39:48 -08004542 // Update the velocity tracker.
4543 {
4544 VelocityTracker::Position positions[MAX_POINTERS];
4545 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004546 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004547 uint32_t id = idBits.clearFirstMarkedBit();
4548 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004549 positions[count].x = pointer.x * mPointerXMovementScale;
4550 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004551 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004552 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004553 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004554 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004555
Jeff Brownace13b12011-03-09 17:39:48 -08004556 // Pick a new active touch id if needed.
4557 // Choose an arbitrary pointer that just went down, if there is one.
4558 // Otherwise choose an arbitrary remaining pointer.
4559 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004560 // We keep the same active touch id for as long as possible.
4561 bool activeTouchChanged = false;
4562 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4563 int32_t activeTouchId = lastActiveTouchId;
4564 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004565 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004566 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004567 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004568 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004569 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004570 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004571 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004572 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004573 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004574 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004575 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004576 } else {
4577 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004578 }
4579 }
4580
4581 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004582 bool isQuietTime = false;
4583 if (activeTouchId < 0) {
4584 mPointerGesture.resetQuietTime();
4585 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004586 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004587 if (!isQuietTime) {
4588 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4589 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4590 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004591 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004592 // Enter quiet time when exiting swipe or freeform state.
4593 // This is to prevent accidentally entering the hover state and flinging the
4594 // pointer when finishing a swipe and there is still one pointer left onscreen.
4595 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004596 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004597 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004598 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004599 // Enter quiet time when releasing the button and there are still two or more
4600 // fingers down. This may indicate that one finger was used to press the button
4601 // but it has not gone up yet.
4602 isQuietTime = true;
4603 }
4604 if (isQuietTime) {
4605 mPointerGesture.quietTime = when;
4606 }
Jeff Brownace13b12011-03-09 17:39:48 -08004607 }
4608 }
4609
4610 // Switch states based on button and pointer state.
4611 if (isQuietTime) {
4612 // Case 1: Quiet time. (QUIET)
4613#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004614 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004615 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004616#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004617 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4618 *outFinishPreviousGesture = true;
4619 }
Jeff Brownace13b12011-03-09 17:39:48 -08004620
4621 mPointerGesture.activeGestureId = -1;
4622 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004623 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004624
Jeff Brown65fd2512011-08-18 11:20:58 -07004625 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004626 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004627 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004628 // The pointer follows the active touch point.
4629 // Emit DOWN, MOVE, UP events at the pointer location.
4630 //
4631 // Only the active touch matters; other fingers are ignored. This policy helps
4632 // to handle the case where the user places a second finger on the touch pad
4633 // to apply the necessary force to depress an integrated button below the surface.
4634 // We don't want the second finger to be delivered to applications.
4635 //
4636 // For this to work well, we need to make sure to track the pointer that is really
4637 // active. If the user first puts one finger down to click then adds another
4638 // finger to drag then the active pointer should switch to the finger that is
4639 // being dragged.
4640#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004641 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004642 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004643#endif
4644 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004645 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004646 *outFinishPreviousGesture = true;
4647 mPointerGesture.activeGestureId = 0;
4648 }
4649
4650 // Switch pointers if needed.
4651 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004652 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004653 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004654 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004655 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004656 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004657 float vx, vy;
4658 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4659 float speed = hypotf(vx, vy);
4660 if (speed > bestSpeed) {
4661 bestId = id;
4662 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004663 }
Jeff Brown8d608662010-08-30 03:02:23 -07004664 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004665 }
4666 if (bestId >= 0 && bestId != activeTouchId) {
4667 mPointerGesture.activeTouchId = activeTouchId = bestId;
4668 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004669#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004670 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004671 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004672#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004673 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004674 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004675
Jeff Brown65fd2512011-08-18 11:20:58 -07004676 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004677 const RawPointerData::Pointer& currentPointer =
4678 mCurrentRawPointerData.pointerForId(activeTouchId);
4679 const RawPointerData::Pointer& lastPointer =
4680 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004681 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4682 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004683
Jeff Brownbe1aa822011-07-27 16:04:54 -07004684 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004685 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004686
4687 // Move the pointer using a relative motion.
4688 // When using spots, the click will occur at the position of the anchor
4689 // spot and all other spots will move there.
4690 mPointerController->move(deltaX, deltaY);
4691 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004692 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004693 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004694
Jeff Brownace13b12011-03-09 17:39:48 -08004695 float x, y;
4696 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004697
Jeff Brown79ac9692011-04-19 21:20:10 -07004698 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004699 mPointerGesture.currentGestureIdBits.clear();
4700 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4701 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004702 mPointerGesture.currentGestureProperties[0].clear();
4703 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004704 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004705 mPointerGesture.currentGestureCoords[0].clear();
4706 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4707 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4708 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004709 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004710 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004711 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4712 *outFinishPreviousGesture = true;
4713 }
Jeff Brownace13b12011-03-09 17:39:48 -08004714
Jeff Brown79ac9692011-04-19 21:20:10 -07004715 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004716 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004717 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004718 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4719 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004720 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004721 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004722 float x, y;
4723 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004724 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4725 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004726#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004727 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004728#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004729
4730 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004731 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004732 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004733
Jeff Brownace13b12011-03-09 17:39:48 -08004734 mPointerGesture.activeGestureId = 0;
4735 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004736 mPointerGesture.currentGestureIdBits.clear();
4737 mPointerGesture.currentGestureIdBits.markBit(
4738 mPointerGesture.activeGestureId);
4739 mPointerGesture.currentGestureIdToIndex[
4740 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004741 mPointerGesture.currentGestureProperties[0].clear();
4742 mPointerGesture.currentGestureProperties[0].id =
4743 mPointerGesture.activeGestureId;
4744 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004745 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004746 mPointerGesture.currentGestureCoords[0].clear();
4747 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004748 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004749 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004750 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004751 mPointerGesture.currentGestureCoords[0].setAxisValue(
4752 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004753
Jeff Brownace13b12011-03-09 17:39:48 -08004754 tapped = true;
4755 } else {
4756#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004757 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004758 x - mPointerGesture.tapX,
4759 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004760#endif
4761 }
4762 } else {
4763#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004764 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004765 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004766#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004767 }
Jeff Brownace13b12011-03-09 17:39:48 -08004768 }
Jeff Brown2352b972011-04-12 22:39:53 -07004769
Jeff Brown65fd2512011-08-18 11:20:58 -07004770 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004771
Jeff Brownace13b12011-03-09 17:39:48 -08004772 if (!tapped) {
4773#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004774 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004775#endif
4776 mPointerGesture.activeGestureId = -1;
4777 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004778 mPointerGesture.currentGestureIdBits.clear();
4779 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004780 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004781 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004782 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004783 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4784 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004785 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004786
Jeff Brown79ac9692011-04-19 21:20:10 -07004787 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4788 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004789 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004790 float x, y;
4791 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004792 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4793 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004794 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4795 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004796#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004797 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004798 x - mPointerGesture.tapX,
4799 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004800#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004801 }
4802 } else {
4803#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004804 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004805 (when - mPointerGesture.tapUpTime) * 0.000001f);
4806#endif
4807 }
4808 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4809 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4810 }
Jeff Brownace13b12011-03-09 17:39:48 -08004811
Jeff Brown65fd2512011-08-18 11:20:58 -07004812 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004813 const RawPointerData::Pointer& currentPointer =
4814 mCurrentRawPointerData.pointerForId(activeTouchId);
4815 const RawPointerData::Pointer& lastPointer =
4816 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004817 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004818 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004819 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004820 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004821
Jeff Brownbe1aa822011-07-27 16:04:54 -07004822 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004823 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004824
Jeff Brown2352b972011-04-12 22:39:53 -07004825 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004826 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004827 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004828 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004829 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004830 }
4831
Jeff Brown79ac9692011-04-19 21:20:10 -07004832 bool down;
4833 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4834#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004835 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004836#endif
4837 down = true;
4838 } else {
4839#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004840 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004841#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004842 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4843 *outFinishPreviousGesture = true;
4844 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004845 mPointerGesture.activeGestureId = 0;
4846 down = false;
4847 }
Jeff Brownace13b12011-03-09 17:39:48 -08004848
4849 float x, y;
4850 mPointerController->getPosition(&x, &y);
4851
Jeff Brownace13b12011-03-09 17:39:48 -08004852 mPointerGesture.currentGestureIdBits.clear();
4853 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4854 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004855 mPointerGesture.currentGestureProperties[0].clear();
4856 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4857 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004858 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004859 mPointerGesture.currentGestureCoords[0].clear();
4860 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4861 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004862 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4863 down ? 1.0f : 0.0f);
4864
Jeff Brown65fd2512011-08-18 11:20:58 -07004865 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004866 mPointerGesture.resetTap();
4867 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004868 mPointerGesture.tapX = x;
4869 mPointerGesture.tapY = y;
4870 }
Jeff Brownace13b12011-03-09 17:39:48 -08004871 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004872 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4873 // We need to provide feedback for each finger that goes down so we cannot wait
4874 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004875 //
Jeff Brown2352b972011-04-12 22:39:53 -07004876 // The ambiguous case is deciding what to do when there are two fingers down but they
4877 // have not moved enough to determine whether they are part of a drag or part of a
4878 // freeform gesture, or just a press or long-press at the pointer location.
4879 //
4880 // When there are two fingers we start with the PRESS hypothesis and we generate a
4881 // down at the pointer location.
4882 //
4883 // When the two fingers move enough or when additional fingers are added, we make
4884 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004885 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004886
Jeff Brown214eaf42011-05-26 19:17:02 -07004887 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004888 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004889 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004890 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4891 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004892 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004893 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004894 // Additional pointers have gone down but not yet settled.
4895 // Reset the gesture.
4896#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004897 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004898 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004899 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004900 * 0.000001f);
4901#endif
4902 *outCancelPreviousGesture = true;
4903 } else {
4904 // Continue previous gesture.
4905 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4906 }
4907
4908 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004909 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4910 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004911 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004912 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004913
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004914 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004915#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004916 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004917 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004918 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004919 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004920#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004921 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4922 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004923 &mPointerGesture.referenceTouchY);
4924 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4925 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004926 }
Jeff Brownace13b12011-03-09 17:39:48 -08004927
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004928 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004929 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004930 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4931 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004932 mPointerGesture.referenceDeltas[id].dx = 0;
4933 mPointerGesture.referenceDeltas[id].dy = 0;
4934 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004935 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004936
4937 // Add delta for all fingers and calculate a common movement delta.
4938 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004939 BitSet32 commonIdBits(mLastFingerIdBits.value
4940 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004941 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4942 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004943 uint32_t id = idBits.clearFirstMarkedBit();
4944 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4945 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004946 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4947 delta.dx += cpd.x - lpd.x;
4948 delta.dy += cpd.y - lpd.y;
4949
4950 if (first) {
4951 commonDeltaX = delta.dx;
4952 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004953 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004954 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4955 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4956 }
4957 }
Jeff Brownace13b12011-03-09 17:39:48 -08004958
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004959 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4960 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4961 float dist[MAX_POINTER_ID + 1];
4962 int32_t distOverThreshold = 0;
4963 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004964 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004965 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004966 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4967 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004968 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004969 distOverThreshold += 1;
4970 }
4971 }
4972
4973 // Only transition when at least two pointers have moved further than
4974 // the minimum distance threshold.
4975 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004976 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004977 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004978#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004979 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004980 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004981#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004982 *outCancelPreviousGesture = true;
4983 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4984 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004985 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004986 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004987 uint32_t id1 = idBits.clearFirstMarkedBit();
4988 uint32_t id2 = idBits.firstMarkedBit();
4989 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4990 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4991 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4992 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4993 // There are two pointers but they are too far apart for a SWIPE,
4994 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004995#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004996 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004997 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004998#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004999 *outCancelPreviousGesture = true;
5000 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5001 } else {
5002 // There are two pointers. Wait for both pointers to start moving
5003 // before deciding whether this is a SWIPE or FREEFORM gesture.
5004 float dist1 = dist[id1];
5005 float dist2 = dist[id2];
5006 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
5007 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
5008 // Calculate the dot product of the displacement vectors.
5009 // When the vectors are oriented in approximately the same direction,
5010 // the angle betweeen them is near zero and the cosine of the angle
5011 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
5012 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
5013 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07005014 float dx1 = delta1.dx * mPointerXZoomScale;
5015 float dy1 = delta1.dy * mPointerYZoomScale;
5016 float dx2 = delta2.dx * mPointerXZoomScale;
5017 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005018 float dot = dx1 * dx2 + dy1 * dy2;
5019 float cosine = dot / (dist1 * dist2); // denominator always > 0
5020 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
5021 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005022#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005023 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005024 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5025 "cosine %0.3f >= %0.3f",
5026 dist1, mConfig.pointerGestureMultitouchMinDistance,
5027 dist2, mConfig.pointerGestureMultitouchMinDistance,
5028 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005029#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005030 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
5031 } else {
5032 // Pointers are moving in different directions. Switch to FREEFORM.
5033#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005034 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005035 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5036 "cosine %0.3f < %0.3f",
5037 dist1, mConfig.pointerGestureMultitouchMinDistance,
5038 dist2, mConfig.pointerGestureMultitouchMinDistance,
5039 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
5040#endif
5041 *outCancelPreviousGesture = true;
5042 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5043 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005044 }
Jeff Brownace13b12011-03-09 17:39:48 -08005045 }
5046 }
Jeff Brownace13b12011-03-09 17:39:48 -08005047 }
5048 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07005049 // Switch from SWIPE to FREEFORM if additional pointers go down.
5050 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07005051 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07005052#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005053 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005054 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005055#endif
Jeff Brownace13b12011-03-09 17:39:48 -08005056 *outCancelPreviousGesture = true;
5057 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07005058 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005059 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005060
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005061 // Move the reference points based on the overall group motion of the fingers
5062 // except in PRESS mode while waiting for a transition to occur.
5063 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
5064 && (commonDeltaX || commonDeltaY)) {
5065 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005066 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07005067 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005068 delta.dx = 0;
5069 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07005070 }
5071
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005072 mPointerGesture.referenceTouchX += commonDeltaX;
5073 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07005074
Jeff Brown65fd2512011-08-18 11:20:58 -07005075 commonDeltaX *= mPointerXMovementScale;
5076 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07005077
Jeff Brownbe1aa822011-07-27 16:04:54 -07005078 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07005079 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07005080
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005081 mPointerGesture.referenceGestureX += commonDeltaX;
5082 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005083 }
5084
5085 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005086 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5087 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5088 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005089#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005090 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005091 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005092 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005093#endif
Steve Blockec193de2012-01-09 18:35:44 +00005094 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005095
5096 mPointerGesture.currentGestureIdBits.clear();
5097 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5098 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005099 mPointerGesture.currentGestureProperties[0].clear();
5100 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5101 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005102 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005103 mPointerGesture.currentGestureCoords[0].clear();
5104 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5105 mPointerGesture.referenceGestureX);
5106 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5107 mPointerGesture.referenceGestureY);
5108 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005109 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5110 // FREEFORM mode.
5111#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005112 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005113 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005114 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005115#endif
Steve Blockec193de2012-01-09 18:35:44 +00005116 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005117
Jeff Brownace13b12011-03-09 17:39:48 -08005118 mPointerGesture.currentGestureIdBits.clear();
5119
5120 BitSet32 mappedTouchIdBits;
5121 BitSet32 usedGestureIdBits;
5122 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5123 // Initially, assign the active gesture id to the active touch point
5124 // if there is one. No other touch id bits are mapped yet.
5125 if (!*outCancelPreviousGesture) {
5126 mappedTouchIdBits.markBit(activeTouchId);
5127 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5128 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5129 mPointerGesture.activeGestureId;
5130 } else {
5131 mPointerGesture.activeGestureId = -1;
5132 }
5133 } else {
5134 // Otherwise, assume we mapped all touches from the previous frame.
5135 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005136 mappedTouchIdBits.value = mLastFingerIdBits.value
5137 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005138 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5139
5140 // Check whether we need to choose a new active gesture id because the
5141 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005142 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5143 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005144 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005145 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005146 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5147 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5148 mPointerGesture.activeGestureId = -1;
5149 break;
5150 }
5151 }
5152 }
5153
5154#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005155 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005156 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5157 "activeGestureId=%d",
5158 mappedTouchIdBits.value, usedGestureIdBits.value,
5159 mPointerGesture.activeGestureId);
5160#endif
5161
Jeff Brown65fd2512011-08-18 11:20:58 -07005162 BitSet32 idBits(mCurrentFingerIdBits);
5163 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005164 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005165 uint32_t gestureId;
5166 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005167 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005168 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5169#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005170 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005171 "new mapping for touch id %d -> gesture id %d",
5172 touchId, gestureId);
5173#endif
5174 } else {
5175 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5176#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005177 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005178 "existing mapping for touch id %d -> gesture id %d",
5179 touchId, gestureId);
5180#endif
5181 }
5182 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5183 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5184
Jeff Brownbe1aa822011-07-27 16:04:54 -07005185 const RawPointerData::Pointer& pointer =
5186 mCurrentRawPointerData.pointerForId(touchId);
5187 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005188 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005189 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005190 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005191 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005192
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005193 mPointerGesture.currentGestureProperties[i].clear();
5194 mPointerGesture.currentGestureProperties[i].id = gestureId;
5195 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005196 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005197 mPointerGesture.currentGestureCoords[i].clear();
5198 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005199 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005200 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005201 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005202 mPointerGesture.currentGestureCoords[i].setAxisValue(
5203 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5204 }
5205
5206 if (mPointerGesture.activeGestureId < 0) {
5207 mPointerGesture.activeGestureId =
5208 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5209#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005210 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005211 "activeGestureId=%d", mPointerGesture.activeGestureId);
5212#endif
5213 }
Jeff Brown2352b972011-04-12 22:39:53 -07005214 }
Jeff Brownace13b12011-03-09 17:39:48 -08005215 }
5216
Jeff Brownbe1aa822011-07-27 16:04:54 -07005217 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005218
Jeff Brownace13b12011-03-09 17:39:48 -08005219#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005220 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005221 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5222 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005223 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005224 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5225 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005226 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005227 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005228 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005229 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005230 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005231 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005232 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5233 id, index, properties.toolType,
5234 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005235 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5236 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5237 }
5238 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005239 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005240 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005241 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005242 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005243 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005244 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5245 id, index, properties.toolType,
5246 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005247 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5248 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5249 }
5250#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005251 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005252}
5253
Jeff Brown65fd2512011-08-18 11:20:58 -07005254void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5255 mPointerSimple.currentCoords.clear();
5256 mPointerSimple.currentProperties.clear();
5257
5258 bool down, hovering;
5259 if (!mCurrentStylusIdBits.isEmpty()) {
5260 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5261 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5262 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5263 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5264 mPointerController->setPosition(x, y);
5265
5266 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5267 down = !hovering;
5268
5269 mPointerController->getPosition(&x, &y);
5270 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5271 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5272 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5273 mPointerSimple.currentProperties.id = 0;
5274 mPointerSimple.currentProperties.toolType =
5275 mCurrentCookedPointerData.pointerProperties[index].toolType;
5276 } else {
5277 down = false;
5278 hovering = false;
5279 }
5280
5281 dispatchPointerSimple(when, policyFlags, down, hovering);
5282}
5283
5284void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5285 abortPointerSimple(when, policyFlags);
5286}
5287
5288void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5289 mPointerSimple.currentCoords.clear();
5290 mPointerSimple.currentProperties.clear();
5291
5292 bool down, hovering;
5293 if (!mCurrentMouseIdBits.isEmpty()) {
5294 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5295 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5296 if (mLastMouseIdBits.hasBit(id)) {
5297 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5298 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5299 - mLastRawPointerData.pointers[lastIndex].x)
5300 * mPointerXMovementScale;
5301 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5302 - mLastRawPointerData.pointers[lastIndex].y)
5303 * mPointerYMovementScale;
5304
5305 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5306 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5307
5308 mPointerController->move(deltaX, deltaY);
5309 } else {
5310 mPointerVelocityControl.reset();
5311 }
5312
5313 down = isPointerDown(mCurrentButtonState);
5314 hovering = !down;
5315
5316 float x, y;
5317 mPointerController->getPosition(&x, &y);
5318 mPointerSimple.currentCoords.copyFrom(
5319 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5320 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5321 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5322 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5323 hovering ? 0.0f : 1.0f);
5324 mPointerSimple.currentProperties.id = 0;
5325 mPointerSimple.currentProperties.toolType =
5326 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5327 } else {
5328 mPointerVelocityControl.reset();
5329
5330 down = false;
5331 hovering = false;
5332 }
5333
5334 dispatchPointerSimple(when, policyFlags, down, hovering);
5335}
5336
5337void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5338 abortPointerSimple(when, policyFlags);
5339
5340 mPointerVelocityControl.reset();
5341}
5342
5343void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5344 bool down, bool hovering) {
5345 int32_t metaState = getContext()->getGlobalMetaState();
5346
5347 if (mPointerController != NULL) {
5348 if (down || hovering) {
5349 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5350 mPointerController->clearSpots();
5351 mPointerController->setButtonState(mCurrentButtonState);
5352 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5353 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5354 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5355 }
5356 }
5357
5358 if (mPointerSimple.down && !down) {
5359 mPointerSimple.down = false;
5360
5361 // Send up.
5362 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5363 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005364 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005365 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5366 mOrientedXPrecision, mOrientedYPrecision,
5367 mPointerSimple.downTime);
5368 getListener()->notifyMotion(&args);
5369 }
5370
5371 if (mPointerSimple.hovering && !hovering) {
5372 mPointerSimple.hovering = false;
5373
5374 // Send hover exit.
5375 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5376 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005377 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005378 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5379 mOrientedXPrecision, mOrientedYPrecision,
5380 mPointerSimple.downTime);
5381 getListener()->notifyMotion(&args);
5382 }
5383
5384 if (down) {
5385 if (!mPointerSimple.down) {
5386 mPointerSimple.down = true;
5387 mPointerSimple.downTime = when;
5388
5389 // Send down.
5390 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5391 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005392 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005393 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5394 mOrientedXPrecision, mOrientedYPrecision,
5395 mPointerSimple.downTime);
5396 getListener()->notifyMotion(&args);
5397 }
5398
5399 // Send move.
5400 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5401 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005402 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005403 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5404 mOrientedXPrecision, mOrientedYPrecision,
5405 mPointerSimple.downTime);
5406 getListener()->notifyMotion(&args);
5407 }
5408
5409 if (hovering) {
5410 if (!mPointerSimple.hovering) {
5411 mPointerSimple.hovering = true;
5412
5413 // Send hover enter.
5414 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5415 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005416 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005417 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5418 mOrientedXPrecision, mOrientedYPrecision,
5419 mPointerSimple.downTime);
5420 getListener()->notifyMotion(&args);
5421 }
5422
5423 // Send hover move.
5424 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5425 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005426 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005427 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5428 mOrientedXPrecision, mOrientedYPrecision,
5429 mPointerSimple.downTime);
5430 getListener()->notifyMotion(&args);
5431 }
5432
5433 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5434 float vscroll = mCurrentRawVScroll;
5435 float hscroll = mCurrentRawHScroll;
5436 mWheelYVelocityControl.move(when, NULL, &vscroll);
5437 mWheelXVelocityControl.move(when, &hscroll, NULL);
5438
5439 // Send scroll.
5440 PointerCoords pointerCoords;
5441 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5442 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5443 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5444
5445 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5446 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005447 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005448 1, &mPointerSimple.currentProperties, &pointerCoords,
5449 mOrientedXPrecision, mOrientedYPrecision,
5450 mPointerSimple.downTime);
5451 getListener()->notifyMotion(&args);
5452 }
5453
5454 // Save state.
5455 if (down || hovering) {
5456 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5457 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5458 } else {
5459 mPointerSimple.reset();
5460 }
5461}
5462
5463void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5464 mPointerSimple.currentCoords.clear();
5465 mPointerSimple.currentProperties.clear();
5466
5467 dispatchPointerSimple(when, policyFlags, false, false);
5468}
5469
Jeff Brownace13b12011-03-09 17:39:48 -08005470void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005471 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5472 const PointerProperties* properties, const PointerCoords* coords,
5473 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005474 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5475 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005476 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005477 uint32_t pointerCount = 0;
5478 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005479 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005480 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005481 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005482 pointerCoords[pointerCount].copyFrom(coords[index]);
5483
5484 if (changedId >= 0 && id == uint32_t(changedId)) {
5485 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5486 }
5487
5488 pointerCount += 1;
5489 }
5490
Steve Blockec193de2012-01-09 18:35:44 +00005491 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005492
5493 if (changedId >= 0 && pointerCount == 1) {
5494 // Replace initial down and final up action.
5495 // We can compare the action without masking off the changed pointer index
5496 // because we know the index is 0.
5497 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5498 action = AMOTION_EVENT_ACTION_DOWN;
5499 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5500 action = AMOTION_EVENT_ACTION_UP;
5501 } else {
5502 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005503 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005504 }
5505 }
5506
Jeff Brownbe1aa822011-07-27 16:04:54 -07005507 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005508 action, flags, metaState, buttonState, edgeFlags,
Jeff Brown83d616a2012-09-09 20:33:43 -07005509 mViewport.displayId, pointerCount, pointerProperties, pointerCoords,
5510 xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005511 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005512}
5513
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005514bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005515 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005516 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5517 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005518 bool changed = false;
5519 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005520 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005521 uint32_t inIndex = inIdToIndex[id];
5522 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005523
5524 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005525 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005526 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005527 PointerCoords& curOutCoords = outCoords[outIndex];
5528
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005529 if (curInProperties != curOutProperties) {
5530 curOutProperties.copyFrom(curInProperties);
5531 changed = true;
5532 }
5533
Jeff Brownace13b12011-03-09 17:39:48 -08005534 if (curInCoords != curOutCoords) {
5535 curOutCoords.copyFrom(curInCoords);
5536 changed = true;
5537 }
5538 }
5539 return changed;
5540}
5541
5542void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005543 if (mPointerController != NULL) {
5544 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5545 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005546}
5547
Jeff Brownbe1aa822011-07-27 16:04:54 -07005548bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5549 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5550 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005551}
5552
Jeff Brownbe1aa822011-07-27 16:04:54 -07005553const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005554 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005555 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005556 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005557 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005558
5559#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005560 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005561 "left=%d, top=%d, right=%d, bottom=%d",
5562 x, y,
5563 virtualKey.keyCode, virtualKey.scanCode,
5564 virtualKey.hitLeft, virtualKey.hitTop,
5565 virtualKey.hitRight, virtualKey.hitBottom);
5566#endif
5567
5568 if (virtualKey.isHit(x, y)) {
5569 return & virtualKey;
5570 }
5571 }
5572
5573 return NULL;
5574}
5575
Jeff Brownbe1aa822011-07-27 16:04:54 -07005576void TouchInputMapper::assignPointerIds() {
5577 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5578 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5579
5580 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005581
5582 if (currentPointerCount == 0) {
5583 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005584 return;
5585 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005586
Jeff Brownbe1aa822011-07-27 16:04:54 -07005587 if (lastPointerCount == 0) {
5588 // All pointers are new.
5589 for (uint32_t i = 0; i < currentPointerCount; i++) {
5590 uint32_t id = i;
5591 mCurrentRawPointerData.pointers[i].id = id;
5592 mCurrentRawPointerData.idToIndex[id] = i;
5593 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5594 }
5595 return;
5596 }
5597
5598 if (currentPointerCount == 1 && lastPointerCount == 1
5599 && mCurrentRawPointerData.pointers[0].toolType
5600 == mLastRawPointerData.pointers[0].toolType) {
5601 // Only one pointer and no change in count so it must have the same id as before.
5602 uint32_t id = mLastRawPointerData.pointers[0].id;
5603 mCurrentRawPointerData.pointers[0].id = id;
5604 mCurrentRawPointerData.idToIndex[id] = 0;
5605 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5606 return;
5607 }
5608
5609 // General case.
5610 // We build a heap of squared euclidean distances between current and last pointers
5611 // associated with the current and last pointer indices. Then, we find the best
5612 // match (by distance) for each current pointer.
5613 // The pointers must have the same tool type but it is possible for them to
5614 // transition from hovering to touching or vice-versa while retaining the same id.
5615 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5616
5617 uint32_t heapSize = 0;
5618 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5619 currentPointerIndex++) {
5620 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5621 lastPointerIndex++) {
5622 const RawPointerData::Pointer& currentPointer =
5623 mCurrentRawPointerData.pointers[currentPointerIndex];
5624 const RawPointerData::Pointer& lastPointer =
5625 mLastRawPointerData.pointers[lastPointerIndex];
5626 if (currentPointer.toolType == lastPointer.toolType) {
5627 int64_t deltaX = currentPointer.x - lastPointer.x;
5628 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005629
5630 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5631
5632 // Insert new element into the heap (sift up).
5633 heap[heapSize].currentPointerIndex = currentPointerIndex;
5634 heap[heapSize].lastPointerIndex = lastPointerIndex;
5635 heap[heapSize].distance = distance;
5636 heapSize += 1;
5637 }
5638 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005639 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005640
Jeff Brownbe1aa822011-07-27 16:04:54 -07005641 // Heapify
5642 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5643 startIndex -= 1;
5644 for (uint32_t parentIndex = startIndex; ;) {
5645 uint32_t childIndex = parentIndex * 2 + 1;
5646 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005647 break;
5648 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005649
5650 if (childIndex + 1 < heapSize
5651 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5652 childIndex += 1;
5653 }
5654
5655 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5656 break;
5657 }
5658
5659 swap(heap[parentIndex], heap[childIndex]);
5660 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005661 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005662 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005663
5664#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005665 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005666 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005667 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005668 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5669 heap[i].distance);
5670 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005671#endif
5672
Jeff Brownbe1aa822011-07-27 16:04:54 -07005673 // Pull matches out by increasing order of distance.
5674 // To avoid reassigning pointers that have already been matched, the loop keeps track
5675 // of which last and current pointers have been matched using the matchedXXXBits variables.
5676 // It also tracks the used pointer id bits.
5677 BitSet32 matchedLastBits(0);
5678 BitSet32 matchedCurrentBits(0);
5679 BitSet32 usedIdBits(0);
5680 bool first = true;
5681 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5682 while (heapSize > 0) {
5683 if (first) {
5684 // The first time through the loop, we just consume the root element of
5685 // the heap (the one with smallest distance).
5686 first = false;
5687 } else {
5688 // Previous iterations consumed the root element of the heap.
5689 // Pop root element off of the heap (sift down).
5690 heap[0] = heap[heapSize];
5691 for (uint32_t parentIndex = 0; ;) {
5692 uint32_t childIndex = parentIndex * 2 + 1;
5693 if (childIndex >= heapSize) {
5694 break;
5695 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005696
Jeff Brownbe1aa822011-07-27 16:04:54 -07005697 if (childIndex + 1 < heapSize
5698 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5699 childIndex += 1;
5700 }
5701
5702 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5703 break;
5704 }
5705
5706 swap(heap[parentIndex], heap[childIndex]);
5707 parentIndex = childIndex;
5708 }
5709
5710#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005711 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005712 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005713 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005714 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5715 heap[i].distance);
5716 }
5717#endif
5718 }
5719
5720 heapSize -= 1;
5721
5722 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5723 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5724
5725 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5726 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5727
5728 matchedCurrentBits.markBit(currentPointerIndex);
5729 matchedLastBits.markBit(lastPointerIndex);
5730
5731 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5732 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5733 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5734 mCurrentRawPointerData.markIdBit(id,
5735 mCurrentRawPointerData.isHovering(currentPointerIndex));
5736 usedIdBits.markBit(id);
5737
5738#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005739 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005740 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5741#endif
5742 break;
5743 }
5744 }
5745
5746 // Assign fresh ids to pointers that were not matched in the process.
5747 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5748 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5749 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5750
5751 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5752 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5753 mCurrentRawPointerData.markIdBit(id,
5754 mCurrentRawPointerData.isHovering(currentPointerIndex));
5755
5756#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005757 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005758 currentPointerIndex, id);
5759#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005760 }
5761}
5762
Jeff Brown6d0fec22010-07-23 21:28:06 -07005763int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005764 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5765 return AKEY_STATE_VIRTUAL;
5766 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005767
Jeff Brownbe1aa822011-07-27 16:04:54 -07005768 size_t numVirtualKeys = mVirtualKeys.size();
5769 for (size_t i = 0; i < numVirtualKeys; i++) {
5770 const VirtualKey& virtualKey = mVirtualKeys[i];
5771 if (virtualKey.keyCode == keyCode) {
5772 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005773 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005774 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005775
5776 return AKEY_STATE_UNKNOWN;
5777}
5778
5779int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005780 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5781 return AKEY_STATE_VIRTUAL;
5782 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005783
Jeff Brownbe1aa822011-07-27 16:04:54 -07005784 size_t numVirtualKeys = mVirtualKeys.size();
5785 for (size_t i = 0; i < numVirtualKeys; i++) {
5786 const VirtualKey& virtualKey = mVirtualKeys[i];
5787 if (virtualKey.scanCode == scanCode) {
5788 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005789 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005790 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005791
5792 return AKEY_STATE_UNKNOWN;
5793}
5794
5795bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5796 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005797 size_t numVirtualKeys = mVirtualKeys.size();
5798 for (size_t i = 0; i < numVirtualKeys; i++) {
5799 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005800
Jeff Brownbe1aa822011-07-27 16:04:54 -07005801 for (size_t i = 0; i < numCodes; i++) {
5802 if (virtualKey.keyCode == keyCodes[i]) {
5803 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005804 }
5805 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005806 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005807
5808 return true;
5809}
5810
5811
5812// --- SingleTouchInputMapper ---
5813
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005814SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5815 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005816}
5817
5818SingleTouchInputMapper::~SingleTouchInputMapper() {
5819}
5820
Jeff Brown65fd2512011-08-18 11:20:58 -07005821void SingleTouchInputMapper::reset(nsecs_t when) {
5822 mSingleTouchMotionAccumulator.reset(getDevice());
5823
5824 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005825}
5826
Jeff Brown6d0fec22010-07-23 21:28:06 -07005827void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005828 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005829
Jeff Brown65fd2512011-08-18 11:20:58 -07005830 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005831}
5832
Jeff Brown65fd2512011-08-18 11:20:58 -07005833void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005834 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005835 mCurrentRawPointerData.pointerCount = 1;
5836 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005837
Jeff Brown65fd2512011-08-18 11:20:58 -07005838 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5839 && (mTouchButtonAccumulator.isHovering()
5840 || (mRawPointerAxes.pressure.valid
5841 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005842 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005843
Jeff Brownbe1aa822011-07-27 16:04:54 -07005844 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005845 outPointer.id = 0;
5846 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5847 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5848 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5849 outPointer.touchMajor = 0;
5850 outPointer.touchMinor = 0;
5851 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5852 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5853 outPointer.orientation = 0;
5854 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005855 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5856 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005857 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5858 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5859 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5860 }
5861 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005862 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005863}
5864
Jeff Brownbe1aa822011-07-27 16:04:54 -07005865void SingleTouchInputMapper::configureRawPointerAxes() {
5866 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005867
Jeff Brownbe1aa822011-07-27 16:04:54 -07005868 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5869 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5870 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5871 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5872 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005873 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5874 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005875}
5876
Jeff Brown00710e92012-04-19 15:18:26 -07005877bool SingleTouchInputMapper::hasStylus() const {
5878 return mTouchButtonAccumulator.hasStylus();
5879}
5880
Jeff Brown6d0fec22010-07-23 21:28:06 -07005881
5882// --- MultiTouchInputMapper ---
5883
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005884MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005885 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005886}
5887
5888MultiTouchInputMapper::~MultiTouchInputMapper() {
5889}
5890
Jeff Brown65fd2512011-08-18 11:20:58 -07005891void MultiTouchInputMapper::reset(nsecs_t when) {
5892 mMultiTouchMotionAccumulator.reset(getDevice());
5893
Jeff Brown6894a292011-07-01 17:59:27 -07005894 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005895
Jeff Brown65fd2512011-08-18 11:20:58 -07005896 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005897}
5898
5899void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005900 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005901
Jeff Brown65fd2512011-08-18 11:20:58 -07005902 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005903}
5904
Jeff Brown65fd2512011-08-18 11:20:58 -07005905void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005906 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005907 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005908 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005909
Jeff Brown80fd47c2011-05-24 01:07:44 -07005910 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005911 const MultiTouchMotionAccumulator::Slot* inSlot =
5912 mMultiTouchMotionAccumulator.getSlot(inIndex);
5913 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005914 continue;
5915 }
5916
Jeff Brown80fd47c2011-05-24 01:07:44 -07005917 if (outCount >= MAX_POINTERS) {
5918#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005919 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005920 "ignoring the rest.",
5921 getDeviceName().string(), MAX_POINTERS);
5922#endif
5923 break; // too many fingers!
5924 }
5925
Jeff Brownbe1aa822011-07-27 16:04:54 -07005926 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005927 outPointer.x = inSlot->getX();
5928 outPointer.y = inSlot->getY();
5929 outPointer.pressure = inSlot->getPressure();
5930 outPointer.touchMajor = inSlot->getTouchMajor();
5931 outPointer.touchMinor = inSlot->getTouchMinor();
5932 outPointer.toolMajor = inSlot->getToolMajor();
5933 outPointer.toolMinor = inSlot->getToolMinor();
5934 outPointer.orientation = inSlot->getOrientation();
5935 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005936 outPointer.tiltX = 0;
5937 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005938
Jeff Brown49754db2011-07-01 17:37:58 -07005939 outPointer.toolType = inSlot->getToolType();
5940 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5941 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5942 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5943 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5944 }
Jeff Brown8d608662010-08-30 03:02:23 -07005945 }
5946
Jeff Brown65fd2512011-08-18 11:20:58 -07005947 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5948 && (mTouchButtonAccumulator.isHovering()
5949 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005950 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005951
Jeff Brown8d608662010-08-30 03:02:23 -07005952 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005953 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005954 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005955 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005956 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005957 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005958 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005959 if (mPointerTrackingIdMap[n] == trackingId) {
5960 id = n;
5961 }
5962 }
5963
5964 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005965 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005966 mPointerTrackingIdMap[id] = trackingId;
5967 }
5968 }
5969 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005970 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005971 mCurrentRawPointerData.clearIdBits();
5972 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005973 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005974 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005975 mCurrentRawPointerData.idToIndex[id] = outCount;
5976 mCurrentRawPointerData.markIdBit(id, isHovering);
5977 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005978 }
5979 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005980
Jeff Brown6d0fec22010-07-23 21:28:06 -07005981 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005982 }
5983
Jeff Brownbe1aa822011-07-27 16:04:54 -07005984 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005985 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005986
Jeff Brown65fd2512011-08-18 11:20:58 -07005987 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005988}
5989
Jeff Brownbe1aa822011-07-27 16:04:54 -07005990void MultiTouchInputMapper::configureRawPointerAxes() {
5991 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005992
Jeff Brownbe1aa822011-07-27 16:04:54 -07005993 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5994 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5995 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5996 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5997 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5998 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5999 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
6000 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
6001 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
6002 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
6003 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006004
Jeff Brownbe1aa822011-07-27 16:04:54 -07006005 if (mRawPointerAxes.trackingId.valid
6006 && mRawPointerAxes.slot.valid
6007 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
6008 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07006009 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00006010 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006011 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07006012 getDeviceName().string(), slotCount, MAX_SLOTS);
6013 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07006014 }
Jeff Brown00710e92012-04-19 15:18:26 -07006015 mMultiTouchMotionAccumulator.configure(getDevice(),
6016 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006017 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07006018 mMultiTouchMotionAccumulator.configure(getDevice(),
6019 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006020 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07006021}
6022
Jeff Brown00710e92012-04-19 15:18:26 -07006023bool MultiTouchInputMapper::hasStylus() const {
6024 return mMultiTouchMotionAccumulator.hasStylus()
6025 || mTouchButtonAccumulator.hasStylus();
6026}
6027
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006028
Jeff Browncb1404e2011-01-15 18:14:15 -08006029// --- JoystickInputMapper ---
6030
6031JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
6032 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006033}
6034
6035JoystickInputMapper::~JoystickInputMapper() {
6036}
6037
6038uint32_t JoystickInputMapper::getSources() {
6039 return AINPUT_SOURCE_JOYSTICK;
6040}
6041
6042void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
6043 InputMapper::populateDeviceInfo(info);
6044
Jeff Brown6f2fba42011-02-19 01:08:02 -08006045 for (size_t i = 0; i < mAxes.size(); i++) {
6046 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08006047 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
6048 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08006049 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08006050 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
6051 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08006052 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006053 }
6054}
6055
6056void JoystickInputMapper::dump(String8& dump) {
6057 dump.append(INDENT2 "Joystick Input Mapper:\n");
6058
Jeff Brown6f2fba42011-02-19 01:08:02 -08006059 dump.append(INDENT3 "Axes:\n");
6060 size_t numAxes = mAxes.size();
6061 for (size_t i = 0; i < numAxes; i++) {
6062 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006063 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006064 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08006065 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006066 } else {
Jeff Brown85297452011-03-04 13:07:49 -08006067 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006068 }
Jeff Brown85297452011-03-04 13:07:49 -08006069 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6070 label = getAxisLabel(axis.axisInfo.highAxis);
6071 if (label) {
6072 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
6073 } else {
6074 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
6075 axis.axisInfo.splitValue);
6076 }
6077 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
6078 dump.append(" (invert)");
6079 }
6080
6081 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
6082 axis.min, axis.max, axis.flat, axis.fuzz);
6083 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
6084 "highScale=%0.5f, highOffset=%0.5f\n",
6085 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07006086 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
6087 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006088 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006089 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006090 }
6091}
6092
Jeff Brown65fd2512011-08-18 11:20:58 -07006093void JoystickInputMapper::configure(nsecs_t when,
6094 const InputReaderConfiguration* config, uint32_t changes) {
6095 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006096
Jeff Brown474dcb52011-06-14 20:22:50 -07006097 if (!changes) { // first time only
6098 // Collect all axes.
6099 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07006100 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6101 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6102 continue; // axis must be claimed by a different device
6103 }
6104
Jeff Brown474dcb52011-06-14 20:22:50 -07006105 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006106 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006107 if (rawAxisInfo.valid) {
6108 // Map axis.
6109 AxisInfo axisInfo;
6110 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6111 if (!explicitlyMapped) {
6112 // Axis is not explicitly mapped, will choose a generic axis later.
6113 axisInfo.mode = AxisInfo::MODE_NORMAL;
6114 axisInfo.axis = -1;
6115 }
6116
6117 // Apply flat override.
6118 int32_t rawFlat = axisInfo.flatOverride < 0
6119 ? rawAxisInfo.flat : axisInfo.flatOverride;
6120
6121 // Calculate scaling factors and limits.
6122 Axis axis;
6123 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6124 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6125 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6126 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6127 scale, 0.0f, highScale, 0.0f,
6128 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6129 } else if (isCenteredAxis(axisInfo.axis)) {
6130 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6131 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6132 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6133 scale, offset, scale, offset,
6134 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6135 } else {
6136 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6137 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6138 scale, 0.0f, scale, 0.0f,
6139 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6140 }
6141
6142 // To eliminate noise while the joystick is at rest, filter out small variations
6143 // in axis values up front.
6144 axis.filter = axis.flat * 0.25f;
6145
6146 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006147 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006148 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006149
Jeff Brown474dcb52011-06-14 20:22:50 -07006150 // If there are too many axes, start dropping them.
6151 // Prefer to keep explicitly mapped axes.
6152 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006153 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006154 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6155 pruneAxes(true);
6156 pruneAxes(false);
6157 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006158
Jeff Brown474dcb52011-06-14 20:22:50 -07006159 // Assign generic axis ids to remaining axes.
6160 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6161 size_t numAxes = mAxes.size();
6162 for (size_t i = 0; i < numAxes; i++) {
6163 Axis& axis = mAxes.editValueAt(i);
6164 if (axis.axisInfo.axis < 0) {
6165 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6166 && haveAxis(nextGenericAxisId)) {
6167 nextGenericAxisId += 1;
6168 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006169
Jeff Brown474dcb52011-06-14 20:22:50 -07006170 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6171 axis.axisInfo.axis = nextGenericAxisId;
6172 nextGenericAxisId += 1;
6173 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006174 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006175 "have already been assigned to other axes.",
6176 getDeviceName().string(), mAxes.keyAt(i));
6177 mAxes.removeItemsAt(i--);
6178 numAxes -= 1;
6179 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006180 }
6181 }
6182 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006183}
6184
Jeff Brown85297452011-03-04 13:07:49 -08006185bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006186 size_t numAxes = mAxes.size();
6187 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006188 const Axis& axis = mAxes.valueAt(i);
6189 if (axis.axisInfo.axis == axisId
6190 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6191 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006192 return true;
6193 }
6194 }
6195 return false;
6196}
Jeff Browncb1404e2011-01-15 18:14:15 -08006197
Jeff Brown6f2fba42011-02-19 01:08:02 -08006198void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6199 size_t i = mAxes.size();
6200 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6201 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6202 continue;
6203 }
Steve Block6215d3f2012-01-04 20:05:49 +00006204 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006205 getDeviceName().string(), mAxes.keyAt(i));
6206 mAxes.removeItemsAt(i);
6207 }
6208}
6209
6210bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6211 switch (axis) {
6212 case AMOTION_EVENT_AXIS_X:
6213 case AMOTION_EVENT_AXIS_Y:
6214 case AMOTION_EVENT_AXIS_Z:
6215 case AMOTION_EVENT_AXIS_RX:
6216 case AMOTION_EVENT_AXIS_RY:
6217 case AMOTION_EVENT_AXIS_RZ:
6218 case AMOTION_EVENT_AXIS_HAT_X:
6219 case AMOTION_EVENT_AXIS_HAT_Y:
6220 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006221 case AMOTION_EVENT_AXIS_RUDDER:
6222 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006223 return true;
6224 default:
6225 return false;
6226 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006227}
6228
Jeff Brown65fd2512011-08-18 11:20:58 -07006229void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006230 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006231 size_t numAxes = mAxes.size();
6232 for (size_t i = 0; i < numAxes; i++) {
6233 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006234 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006235 }
6236
Jeff Brown65fd2512011-08-18 11:20:58 -07006237 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006238}
6239
6240void JoystickInputMapper::process(const RawEvent* rawEvent) {
6241 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006242 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006243 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006244 if (index >= 0) {
6245 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006246 float newValue, highNewValue;
6247 switch (axis.axisInfo.mode) {
6248 case AxisInfo::MODE_INVERT:
6249 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6250 * axis.scale + axis.offset;
6251 highNewValue = 0.0f;
6252 break;
6253 case AxisInfo::MODE_SPLIT:
6254 if (rawEvent->value < axis.axisInfo.splitValue) {
6255 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6256 * axis.scale + axis.offset;
6257 highNewValue = 0.0f;
6258 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6259 newValue = 0.0f;
6260 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6261 * axis.highScale + axis.highOffset;
6262 } else {
6263 newValue = 0.0f;
6264 highNewValue = 0.0f;
6265 }
6266 break;
6267 default:
6268 newValue = rawEvent->value * axis.scale + axis.offset;
6269 highNewValue = 0.0f;
6270 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006271 }
Jeff Brown85297452011-03-04 13:07:49 -08006272 axis.newValue = newValue;
6273 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006274 }
6275 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006276 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006277
6278 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006279 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006280 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006281 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006282 break;
6283 }
6284 break;
6285 }
6286}
6287
Jeff Brown6f2fba42011-02-19 01:08:02 -08006288void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006289 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006290 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006291 }
6292
6293 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006294 int32_t buttonState = 0;
6295
6296 PointerProperties pointerProperties;
6297 pointerProperties.clear();
6298 pointerProperties.id = 0;
6299 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006300
Jeff Brown6f2fba42011-02-19 01:08:02 -08006301 PointerCoords pointerCoords;
6302 pointerCoords.clear();
6303
6304 size_t numAxes = mAxes.size();
6305 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006306 const Axis& axis = mAxes.valueAt(i);
6307 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6308 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6309 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6310 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006311 }
6312
Jeff Brown83d616a2012-09-09 20:33:43 -07006313 // Moving a joystick axis should not wake the device because joysticks can
Jeff Brown56194eb2011-03-02 19:23:13 -08006314 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6315 // button will likely wake the device.
6316 // TODO: Use the input device configuration to control this behavior more finely.
6317 uint32_t policyFlags = 0;
6318
Jeff Brownbe1aa822011-07-27 16:04:54 -07006319 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006320 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07006321 ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006322 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006323}
6324
Jeff Brown85297452011-03-04 13:07:49 -08006325bool JoystickInputMapper::filterAxes(bool force) {
6326 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006327 size_t numAxes = mAxes.size();
6328 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006329 Axis& axis = mAxes.editValueAt(i);
6330 if (force || hasValueChangedSignificantly(axis.filter,
6331 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6332 axis.currentValue = axis.newValue;
6333 atLeastOneSignificantChange = true;
6334 }
6335 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6336 if (force || hasValueChangedSignificantly(axis.filter,
6337 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6338 axis.highCurrentValue = axis.highNewValue;
6339 atLeastOneSignificantChange = true;
6340 }
6341 }
6342 }
6343 return atLeastOneSignificantChange;
6344}
6345
6346bool JoystickInputMapper::hasValueChangedSignificantly(
6347 float filter, float newValue, float currentValue, float min, float max) {
6348 if (newValue != currentValue) {
6349 // Filter out small changes in value unless the value is converging on the axis
6350 // bounds or center point. This is intended to reduce the amount of information
6351 // sent to applications by particularly noisy joysticks (such as PS3).
6352 if (fabs(newValue - currentValue) > filter
6353 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6354 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6355 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6356 return true;
6357 }
6358 }
6359 return false;
6360}
6361
6362bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6363 float filter, float newValue, float currentValue, float thresholdValue) {
6364 float newDistance = fabs(newValue - thresholdValue);
6365 if (newDistance < filter) {
6366 float oldDistance = fabs(currentValue - thresholdValue);
6367 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006368 return true;
6369 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006370 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006371 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006372}
6373
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006374} // namespace android