blob: e41cce2026d6ce85f508d39161f14ab6c766b3c7 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff 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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070047
48#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070049#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070050#include <unistd.h>
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700122};
Jeff Brown9626b142011-03-03 02:09:54 -0800123static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brownf2f48712010-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 Brown9ee285a2011-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -07002403 }
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -07002451
Jeff Brownbe1aa822011-07-27 16:04:54 -07002452 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-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;
Jeff Brownace13b12011-03-09 17:39:48 -08002791 } else if (deviceTypeString == "pointer") {
2792 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002793 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002794 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002795 }
2796 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002797
Jeff Brownefd32662011-03-08 15:13:06 -08002798 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002799 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2800 mParameters.orientationAware);
2801
Jeff Brownd728bf52012-09-08 18:05:28 -07002802 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002803 mParameters.associatedDisplayIsExternal = false;
2804 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002805 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002806 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002807 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002808 mParameters.associatedDisplayIsExternal =
2809 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2810 && getDevice()->isExternal();
Jeff Brownbc68a592011-07-25 12:58:12 -07002811 }
Jeff Brown8d608662010-08-30 03:02:23 -07002812}
2813
Jeff Brownef3d7e82010-09-30 14:33:04 -07002814void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002815 dump.append(INDENT3 "Parameters:\n");
2816
Jeff Brown538881e2011-05-25 18:23:38 -07002817 switch (mParameters.gestureMode) {
2818 case Parameters::GESTURE_MODE_POINTER:
2819 dump.append(INDENT4 "GestureMode: pointer\n");
2820 break;
2821 case Parameters::GESTURE_MODE_SPOTS:
2822 dump.append(INDENT4 "GestureMode: spots\n");
2823 break;
2824 default:
2825 assert(false);
2826 }
2827
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002828 switch (mParameters.deviceType) {
2829 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2830 dump.append(INDENT4 "DeviceType: touchScreen\n");
2831 break;
2832 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2833 dump.append(INDENT4 "DeviceType: touchPad\n");
2834 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002835 case Parameters::DEVICE_TYPE_POINTER:
2836 dump.append(INDENT4 "DeviceType: pointer\n");
2837 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002838 default:
Steve Blockec193de2012-01-09 18:35:44 +00002839 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002840 }
2841
Jeff Brown83d616a2012-09-09 20:33:43 -07002842 dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n",
Jeff Brownd728bf52012-09-08 18:05:28 -07002843 toString(mParameters.hasAssociatedDisplay),
2844 toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002845 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2846 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002847}
2848
Jeff Brownbe1aa822011-07-27 16:04:54 -07002849void TouchInputMapper::configureRawPointerAxes() {
2850 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002851}
2852
Jeff Brownbe1aa822011-07-27 16:04:54 -07002853void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2854 dump.append(INDENT3 "Raw Touch Axes:\n");
2855 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2856 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2857 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2858 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2859 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2860 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2861 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2862 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2863 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002864 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2865 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002866 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2867 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002868}
2869
Jeff Brown65fd2512011-08-18 11:20:58 -07002870void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2871 int32_t oldDeviceMode = mDeviceMode;
2872
2873 // Determine device mode.
2874 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2875 && mConfig.pointerGesturesEnabled) {
2876 mSource = AINPUT_SOURCE_MOUSE;
2877 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002878 if (hasStylus()) {
2879 mSource |= AINPUT_SOURCE_STYLUS;
2880 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002881 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownd728bf52012-09-08 18:05:28 -07002882 && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002883 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2884 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002885 if (hasStylus()) {
2886 mSource |= AINPUT_SOURCE_STYLUS;
2887 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002888 } else {
2889 mSource = AINPUT_SOURCE_TOUCHPAD;
2890 mDeviceMode = DEVICE_MODE_UNSCALED;
2891 }
2892
Jeff Brown9626b142011-03-03 02:09:54 -08002893 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002894 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002895 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002896 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002897 mDeviceMode = DEVICE_MODE_DISABLED;
2898 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002899 }
2900
Jeff Brown83d616a2012-09-09 20:33:43 -07002901 // Raw width and height in the natural orientation.
2902 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2903 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2904
Jeff Brown65fd2512011-08-18 11:20:58 -07002905 // Get associated display dimensions.
Jeff Brown83d616a2012-09-09 20:33:43 -07002906 bool viewportChanged = false;
2907 DisplayViewport newViewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07002908 if (mParameters.hasAssociatedDisplay) {
Jeff Brown83d616a2012-09-09 20:33:43 -07002909 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002910 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brownd728bf52012-09-08 18:05:28 -07002911 "display. The device will be inoperable until the display size "
Jeff Brown65fd2512011-08-18 11:20:58 -07002912 "becomes available.",
Jeff Brownd728bf52012-09-08 18:05:28 -07002913 getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002914 mDeviceMode = DEVICE_MODE_DISABLED;
2915 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002916 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002917 } else {
Jeff Brown83d616a2012-09-09 20:33:43 -07002918 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
2919 }
2920 if (mViewport != newViewport) {
2921 mViewport = newViewport;
2922 viewportChanged = true;
2923
2924 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2925 // Convert rotated viewport to natural surface coordinates.
2926 int32_t naturalLogicalWidth, naturalLogicalHeight;
2927 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
2928 int32_t naturalPhysicalLeft, naturalPhysicalTop;
2929 int32_t naturalDeviceWidth, naturalDeviceHeight;
2930 switch (mViewport.orientation) {
2931 case DISPLAY_ORIENTATION_90:
2932 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2933 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2934 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2935 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2936 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
2937 naturalPhysicalTop = mViewport.physicalLeft;
2938 naturalDeviceWidth = mViewport.deviceHeight;
2939 naturalDeviceHeight = mViewport.deviceWidth;
2940 break;
2941 case DISPLAY_ORIENTATION_180:
2942 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2943 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2944 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2945 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2946 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
2947 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
2948 naturalDeviceWidth = mViewport.deviceWidth;
2949 naturalDeviceHeight = mViewport.deviceHeight;
2950 break;
2951 case DISPLAY_ORIENTATION_270:
2952 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2953 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2954 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2955 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2956 naturalPhysicalLeft = mViewport.physicalTop;
2957 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
2958 naturalDeviceWidth = mViewport.deviceHeight;
2959 naturalDeviceHeight = mViewport.deviceWidth;
2960 break;
2961 case DISPLAY_ORIENTATION_0:
2962 default:
2963 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2964 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2965 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2966 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2967 naturalPhysicalLeft = mViewport.physicalLeft;
2968 naturalPhysicalTop = mViewport.physicalTop;
2969 naturalDeviceWidth = mViewport.deviceWidth;
2970 naturalDeviceHeight = mViewport.deviceHeight;
2971 break;
2972 }
2973
2974 mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
2975 mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
2976 mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
2977 mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
2978
2979 mSurfaceOrientation = mParameters.orientationAware ?
2980 mViewport.orientation : DISPLAY_ORIENTATION_0;
2981 } else {
2982 mSurfaceWidth = rawWidth;
2983 mSurfaceHeight = rawHeight;
2984 mSurfaceLeft = 0;
2985 mSurfaceTop = 0;
2986 mSurfaceOrientation = DISPLAY_ORIENTATION_0;
2987 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002988 }
2989
2990 // If moving between pointer modes, need to reset some state.
2991 bool deviceModeChanged;
2992 if (mDeviceMode != oldDeviceMode) {
2993 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002994 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002995 }
2996
Jeff Browndaf4a122011-08-26 17:14:14 -07002997 // Create pointer controller if needed.
2998 if (mDeviceMode == DEVICE_MODE_POINTER ||
2999 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
3000 if (mPointerController == NULL) {
3001 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
3002 }
3003 } else {
3004 mPointerController.clear();
3005 }
3006
Jeff Brown83d616a2012-09-09 20:33:43 -07003007 if (viewportChanged || deviceModeChanged) {
3008 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
3009 "display id %d",
3010 getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight,
3011 mSurfaceOrientation, mDeviceMode, mViewport.displayId);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003012
Jeff Brown8d608662010-08-30 03:02:23 -07003013 // Configure X and Y factors.
Jeff Brown83d616a2012-09-09 20:33:43 -07003014 mXScale = float(mSurfaceWidth) / rawWidth;
3015 mYScale = float(mSurfaceHeight) / rawHeight;
3016 mXTranslate = -mSurfaceLeft;
3017 mYTranslate = -mSurfaceTop;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003018 mXPrecision = 1.0f / mXScale;
3019 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003020
Jeff Brownbe1aa822011-07-27 16:04:54 -07003021 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07003022 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003023 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07003024 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08003025
Jeff Brownbe1aa822011-07-27 16:04:54 -07003026 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003027
Jeff Brown8d608662010-08-30 03:02:23 -07003028 // Scale factor for terms that are not oriented in a particular axis.
3029 // If the pixels are square then xScale == yScale otherwise we fake it
3030 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003031 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003032
Jeff Brown8d608662010-08-30 03:02:23 -07003033 // Size of diagonal axis.
Jeff Brown83d616a2012-09-09 20:33:43 -07003034 float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003035
Jeff Browna1f89ce2011-08-11 00:05:01 -07003036 // Size factors.
3037 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
3038 if (mRawPointerAxes.touchMajor.valid
3039 && mRawPointerAxes.touchMajor.maxValue != 0) {
3040 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
3041 } else if (mRawPointerAxes.toolMajor.valid
3042 && mRawPointerAxes.toolMajor.maxValue != 0) {
3043 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
3044 } else {
3045 mSizeScale = 0.0f;
3046 }
3047
Jeff Brownbe1aa822011-07-27 16:04:54 -07003048 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003049 mOrientedRanges.haveToolSize = true;
3050 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08003051
Jeff Brownbe1aa822011-07-27 16:04:54 -07003052 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003053 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003054 mOrientedRanges.touchMajor.min = 0;
3055 mOrientedRanges.touchMajor.max = diagonalSize;
3056 mOrientedRanges.touchMajor.flat = 0;
3057 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003058
Jeff Brownbe1aa822011-07-27 16:04:54 -07003059 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
3060 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08003061
Jeff Brownbe1aa822011-07-27 16:04:54 -07003062 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003063 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003064 mOrientedRanges.toolMajor.min = 0;
3065 mOrientedRanges.toolMajor.max = diagonalSize;
3066 mOrientedRanges.toolMajor.flat = 0;
3067 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003068
Jeff Brownbe1aa822011-07-27 16:04:54 -07003069 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3070 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003071
3072 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003073 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003074 mOrientedRanges.size.min = 0;
3075 mOrientedRanges.size.max = 1.0;
3076 mOrientedRanges.size.flat = 0;
3077 mOrientedRanges.size.fuzz = 0;
3078 } else {
3079 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003080 }
3081
3082 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003083 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003084 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3085 || mCalibration.pressureCalibration
3086 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3087 if (mCalibration.havePressureScale) {
3088 mPressureScale = mCalibration.pressureScale;
3089 } else if (mRawPointerAxes.pressure.valid
3090 && mRawPointerAxes.pressure.maxValue != 0) {
3091 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003092 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003093 }
Jeff Brown8d608662010-08-30 03:02:23 -07003094
Jeff Brown65fd2512011-08-18 11:20:58 -07003095 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3096 mOrientedRanges.pressure.source = mSource;
3097 mOrientedRanges.pressure.min = 0;
3098 mOrientedRanges.pressure.max = 1.0;
3099 mOrientedRanges.pressure.flat = 0;
3100 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003101
Jeff Brown65fd2512011-08-18 11:20:58 -07003102 // Tilt
3103 mTiltXCenter = 0;
3104 mTiltXScale = 0;
3105 mTiltYCenter = 0;
3106 mTiltYScale = 0;
3107 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3108 if (mHaveTilt) {
3109 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3110 mRawPointerAxes.tiltX.maxValue);
3111 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3112 mRawPointerAxes.tiltY.maxValue);
3113 mTiltXScale = M_PI / 180;
3114 mTiltYScale = M_PI / 180;
3115
3116 mOrientedRanges.haveTilt = true;
3117
3118 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3119 mOrientedRanges.tilt.source = mSource;
3120 mOrientedRanges.tilt.min = 0;
3121 mOrientedRanges.tilt.max = M_PI_2;
3122 mOrientedRanges.tilt.flat = 0;
3123 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003124 }
3125
Jeff Brown8d608662010-08-30 03:02:23 -07003126 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07003127 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003128 if (mHaveTilt) {
3129 mOrientedRanges.haveOrientation = true;
3130
3131 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3132 mOrientedRanges.orientation.source = mSource;
3133 mOrientedRanges.orientation.min = -M_PI;
3134 mOrientedRanges.orientation.max = M_PI;
3135 mOrientedRanges.orientation.flat = 0;
3136 mOrientedRanges.orientation.fuzz = 0;
3137 } else if (mCalibration.orientationCalibration !=
3138 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003139 if (mCalibration.orientationCalibration
3140 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003141 if (mRawPointerAxes.orientation.valid) {
Jeff Brown037f7272012-06-25 17:31:23 -07003142 if (mRawPointerAxes.orientation.maxValue > 0) {
3143 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
3144 } else if (mRawPointerAxes.orientation.minValue < 0) {
3145 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
3146 } else {
3147 mOrientationScale = 0;
3148 }
Jeff Brown8d608662010-08-30 03:02:23 -07003149 }
3150 }
3151
Jeff Brownbe1aa822011-07-27 16:04:54 -07003152 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003153
Jeff Brownbe1aa822011-07-27 16:04:54 -07003154 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003155 mOrientedRanges.orientation.source = mSource;
3156 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003157 mOrientedRanges.orientation.max = M_PI_2;
3158 mOrientedRanges.orientation.flat = 0;
3159 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003160 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003161
3162 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003163 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003164 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3165 if (mCalibration.distanceCalibration
3166 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3167 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003168 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003169 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003170 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003171 }
3172 }
3173
Jeff Brownbe1aa822011-07-27 16:04:54 -07003174 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003175
Jeff Brownbe1aa822011-07-27 16:04:54 -07003176 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003177 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003178 mOrientedRanges.distance.min =
3179 mRawPointerAxes.distance.minValue * mDistanceScale;
3180 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003181 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003182 mOrientedRanges.distance.flat = 0;
3183 mOrientedRanges.distance.fuzz =
3184 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003185 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003186
Jeff Brown83d616a2012-09-09 20:33:43 -07003187 // Compute oriented precision, scales and ranges.
Jeff Brown9626b142011-03-03 02:09:54 -08003188 // Note that the maximum value reported is an inclusive maximum value so it is one
3189 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003190 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003191 case DISPLAY_ORIENTATION_90:
3192 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003193 mOrientedXPrecision = mYPrecision;
3194 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003195
Jeff Brown83d616a2012-09-09 20:33:43 -07003196 mOrientedRanges.x.min = mYTranslate;
3197 mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003198 mOrientedRanges.x.flat = 0;
3199 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003200
Jeff Brown83d616a2012-09-09 20:33:43 -07003201 mOrientedRanges.y.min = mXTranslate;
3202 mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003203 mOrientedRanges.y.flat = 0;
3204 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003205 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003206
Jeff Brown6d0fec22010-07-23 21:28:06 -07003207 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003208 mOrientedXPrecision = mXPrecision;
3209 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003210
Jeff Brown83d616a2012-09-09 20:33:43 -07003211 mOrientedRanges.x.min = mXTranslate;
3212 mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003213 mOrientedRanges.x.flat = 0;
3214 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003215
Jeff Brown83d616a2012-09-09 20:33:43 -07003216 mOrientedRanges.y.min = mYTranslate;
3217 mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003218 mOrientedRanges.y.flat = 0;
3219 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003220 break;
3221 }
Jeff Brownace13b12011-03-09 17:39:48 -08003222
3223 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003224 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown2352b972011-04-12 22:39:53 -07003225 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003226 float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003227
Jeff Brown2352b972011-04-12 22:39:53 -07003228 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003229 // given area relative to the diagonal size of the display when no acceleration
3230 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003231 // Assume that the touch pad has a square aspect ratio such that movements in
3232 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003233 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003234 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003235 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003236
3237 // Scale zooms to cover a smaller range of the display than movements do.
3238 // This value determines the area around the pointer that is affected by freeform
3239 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003240 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003241 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003242 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003243
Jeff Brown2352b972011-04-12 22:39:53 -07003244 // Max width between pointers to detect a swipe gesture is more than some fraction
3245 // of the diagonal axis of the touch pad. Touches that are wider than this are
3246 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003247 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003248 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003249 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003250
Jeff Brown65fd2512011-08-18 11:20:58 -07003251 // Abort current pointer usages because the state has changed.
3252 abortPointerUsage(when, 0 /*policyFlags*/);
3253
3254 // Inform the dispatcher about the changes.
3255 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003256 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003257 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003258}
3259
Jeff Brownbe1aa822011-07-27 16:04:54 -07003260void TouchInputMapper::dumpSurface(String8& dump) {
Jeff Brown83d616a2012-09-09 20:33:43 -07003261 dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, "
3262 "logicalFrame=[%d, %d, %d, %d], "
3263 "physicalFrame=[%d, %d, %d, %d], "
3264 "deviceSize=[%d, %d]\n",
3265 mViewport.displayId, mViewport.orientation,
3266 mViewport.logicalLeft, mViewport.logicalTop,
3267 mViewport.logicalRight, mViewport.logicalBottom,
3268 mViewport.physicalLeft, mViewport.physicalTop,
3269 mViewport.physicalRight, mViewport.physicalBottom,
3270 mViewport.deviceWidth, mViewport.deviceHeight);
3271
Jeff Brownbe1aa822011-07-27 16:04:54 -07003272 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3273 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003274 dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft);
3275 dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003276 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003277}
3278
Jeff Brownbe1aa822011-07-27 16:04:54 -07003279void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003280 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003281 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003282
Jeff Brownbe1aa822011-07-27 16:04:54 -07003283 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003284
Jeff Brown6328cdc2010-07-29 18:18:33 -07003285 if (virtualKeyDefinitions.size() == 0) {
3286 return;
3287 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003288
Jeff Brownbe1aa822011-07-27 16:04:54 -07003289 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003290
Jeff Brownbe1aa822011-07-27 16:04:54 -07003291 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3292 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3293 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3294 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003295
3296 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003297 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003298 virtualKeyDefinitions[i];
3299
Jeff Brownbe1aa822011-07-27 16:04:54 -07003300 mVirtualKeys.add();
3301 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003302
3303 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3304 int32_t keyCode;
3305 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003306 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003307 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003308 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003309 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003310 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003311 }
3312
Jeff Brown6328cdc2010-07-29 18:18:33 -07003313 virtualKey.keyCode = keyCode;
3314 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003315
Jeff Brown6328cdc2010-07-29 18:18:33 -07003316 // convert the key definition's display coordinates into touch coordinates for a hit box
3317 int32_t halfWidth = virtualKeyDefinition.width / 2;
3318 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003319
Jeff Brown6328cdc2010-07-29 18:18:33 -07003320 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003321 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003322 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003323 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003324 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003325 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003326 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003327 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003328 }
3329}
3330
Jeff Brownbe1aa822011-07-27 16:04:54 -07003331void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3332 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003333 dump.append(INDENT3 "Virtual Keys:\n");
3334
Jeff Brownbe1aa822011-07-27 16:04:54 -07003335 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3336 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003337 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3338 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3339 i, virtualKey.scanCode, virtualKey.keyCode,
3340 virtualKey.hitLeft, virtualKey.hitRight,
3341 virtualKey.hitTop, virtualKey.hitBottom);
3342 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003343 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003344}
3345
Jeff Brown8d608662010-08-30 03:02:23 -07003346void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003347 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003348 Calibration& out = mCalibration;
3349
Jeff Browna1f89ce2011-08-11 00:05:01 -07003350 // Size
3351 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3352 String8 sizeCalibrationString;
3353 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3354 if (sizeCalibrationString == "none") {
3355 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3356 } else if (sizeCalibrationString == "geometric") {
3357 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3358 } else if (sizeCalibrationString == "diameter") {
3359 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
Jeff Brown037f7272012-06-25 17:31:23 -07003360 } else if (sizeCalibrationString == "box") {
3361 out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003362 } else if (sizeCalibrationString == "area") {
3363 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3364 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003365 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003366 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003367 }
3368 }
3369
Jeff Browna1f89ce2011-08-11 00:05:01 -07003370 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3371 out.sizeScale);
3372 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3373 out.sizeBias);
3374 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3375 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003376
3377 // Pressure
3378 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3379 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003380 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003381 if (pressureCalibrationString == "none") {
3382 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3383 } else if (pressureCalibrationString == "physical") {
3384 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3385 } else if (pressureCalibrationString == "amplitude") {
3386 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3387 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003388 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003389 pressureCalibrationString.string());
3390 }
3391 }
3392
Jeff Brown8d608662010-08-30 03:02:23 -07003393 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3394 out.pressureScale);
3395
Jeff Brown8d608662010-08-30 03:02:23 -07003396 // Orientation
3397 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3398 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003399 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003400 if (orientationCalibrationString == "none") {
3401 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3402 } else if (orientationCalibrationString == "interpolated") {
3403 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003404 } else if (orientationCalibrationString == "vector") {
3405 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003406 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003407 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003408 orientationCalibrationString.string());
3409 }
3410 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003411
3412 // Distance
3413 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3414 String8 distanceCalibrationString;
3415 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3416 if (distanceCalibrationString == "none") {
3417 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3418 } else if (distanceCalibrationString == "scaled") {
3419 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3420 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003421 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003422 distanceCalibrationString.string());
3423 }
3424 }
3425
3426 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3427 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003428}
3429
3430void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003431 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003432 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3433 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3434 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003435 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003436 } else {
3437 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3438 }
Jeff Brown8d608662010-08-30 03:02:23 -07003439
Jeff Browna1f89ce2011-08-11 00:05:01 -07003440 // Pressure
3441 if (mRawPointerAxes.pressure.valid) {
3442 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3443 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3444 }
3445 } else {
3446 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003447 }
3448
3449 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003450 if (mRawPointerAxes.orientation.valid) {
3451 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003452 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003453 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003454 } else {
3455 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003456 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003457
3458 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003459 if (mRawPointerAxes.distance.valid) {
3460 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003461 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003462 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003463 } else {
3464 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003465 }
Jeff Brown8d608662010-08-30 03:02:23 -07003466}
3467
Jeff Brownef3d7e82010-09-30 14:33:04 -07003468void TouchInputMapper::dumpCalibration(String8& dump) {
3469 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003470
Jeff Browna1f89ce2011-08-11 00:05:01 -07003471 // Size
3472 switch (mCalibration.sizeCalibration) {
3473 case Calibration::SIZE_CALIBRATION_NONE:
3474 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003475 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003476 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3477 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003478 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003479 case Calibration::SIZE_CALIBRATION_DIAMETER:
3480 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3481 break;
Jeff Brown037f7272012-06-25 17:31:23 -07003482 case Calibration::SIZE_CALIBRATION_BOX:
3483 dump.append(INDENT4 "touch.size.calibration: box\n");
3484 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003485 case Calibration::SIZE_CALIBRATION_AREA:
3486 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003487 break;
3488 default:
Steve Blockec193de2012-01-09 18:35:44 +00003489 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003490 }
3491
Jeff Browna1f89ce2011-08-11 00:05:01 -07003492 if (mCalibration.haveSizeScale) {
3493 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3494 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003495 }
3496
Jeff Browna1f89ce2011-08-11 00:05:01 -07003497 if (mCalibration.haveSizeBias) {
3498 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3499 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003500 }
3501
Jeff Browna1f89ce2011-08-11 00:05:01 -07003502 if (mCalibration.haveSizeIsSummed) {
3503 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3504 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003505 }
3506
3507 // Pressure
3508 switch (mCalibration.pressureCalibration) {
3509 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003510 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003511 break;
3512 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003513 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003514 break;
3515 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003516 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003517 break;
3518 default:
Steve Blockec193de2012-01-09 18:35:44 +00003519 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003520 }
3521
Jeff Brown8d608662010-08-30 03:02:23 -07003522 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003523 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3524 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003525 }
3526
Jeff Brown8d608662010-08-30 03:02:23 -07003527 // Orientation
3528 switch (mCalibration.orientationCalibration) {
3529 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003530 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003531 break;
3532 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003533 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003534 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003535 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3536 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3537 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003538 default:
Steve Blockec193de2012-01-09 18:35:44 +00003539 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003540 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003541
3542 // Distance
3543 switch (mCalibration.distanceCalibration) {
3544 case Calibration::DISTANCE_CALIBRATION_NONE:
3545 dump.append(INDENT4 "touch.distance.calibration: none\n");
3546 break;
3547 case Calibration::DISTANCE_CALIBRATION_SCALED:
3548 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3549 break;
3550 default:
Steve Blockec193de2012-01-09 18:35:44 +00003551 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003552 }
3553
3554 if (mCalibration.haveDistanceScale) {
3555 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3556 mCalibration.distanceScale);
3557 }
Jeff Brown8d608662010-08-30 03:02:23 -07003558}
3559
Jeff Brown65fd2512011-08-18 11:20:58 -07003560void TouchInputMapper::reset(nsecs_t when) {
3561 mCursorButtonAccumulator.reset(getDevice());
3562 mCursorScrollAccumulator.reset(getDevice());
3563 mTouchButtonAccumulator.reset(getDevice());
3564
3565 mPointerVelocityControl.reset();
3566 mWheelXVelocityControl.reset();
3567 mWheelYVelocityControl.reset();
3568
Jeff Brownbe1aa822011-07-27 16:04:54 -07003569 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003570 mLastRawPointerData.clear();
3571 mCurrentCookedPointerData.clear();
3572 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003573 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003574 mLastButtonState = 0;
3575 mCurrentRawVScroll = 0;
3576 mCurrentRawHScroll = 0;
3577 mCurrentFingerIdBits.clear();
3578 mLastFingerIdBits.clear();
3579 mCurrentStylusIdBits.clear();
3580 mLastStylusIdBits.clear();
3581 mCurrentMouseIdBits.clear();
3582 mLastMouseIdBits.clear();
3583 mPointerUsage = POINTER_USAGE_NONE;
3584 mSentHoverEnter = false;
3585 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003586
Jeff Brown65fd2512011-08-18 11:20:58 -07003587 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003588
Jeff Brown65fd2512011-08-18 11:20:58 -07003589 mPointerGesture.reset();
3590 mPointerSimple.reset();
3591
3592 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003593 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3594 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003595 }
3596
Jeff Brown65fd2512011-08-18 11:20:58 -07003597 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003598}
3599
Jeff Brown65fd2512011-08-18 11:20:58 -07003600void TouchInputMapper::process(const RawEvent* rawEvent) {
3601 mCursorButtonAccumulator.process(rawEvent);
3602 mCursorScrollAccumulator.process(rawEvent);
3603 mTouchButtonAccumulator.process(rawEvent);
3604
Jeff Brown49ccac52012-04-11 18:27:33 -07003605 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003606 sync(rawEvent->when);
3607 }
3608}
3609
3610void TouchInputMapper::sync(nsecs_t when) {
3611 // Sync button state.
3612 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3613 | mCursorButtonAccumulator.getButtonState();
3614
3615 // Sync scroll state.
3616 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3617 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3618 mCursorScrollAccumulator.finishSync();
3619
3620 // Sync touch state.
3621 bool havePointerIds = true;
3622 mCurrentRawPointerData.clear();
3623 syncTouch(when, &havePointerIds);
3624
Jeff Brownaa3855d2011-03-17 01:34:19 -07003625#if DEBUG_RAW_EVENTS
3626 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003627 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003628 mLastRawPointerData.pointerCount,
3629 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003630 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003631 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003632 "hovering ids 0x%08x -> 0x%08x",
3633 mLastRawPointerData.pointerCount,
3634 mCurrentRawPointerData.pointerCount,
3635 mLastRawPointerData.touchingIdBits.value,
3636 mCurrentRawPointerData.touchingIdBits.value,
3637 mLastRawPointerData.hoveringIdBits.value,
3638 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003639 }
3640#endif
3641
Jeff Brown65fd2512011-08-18 11:20:58 -07003642 // Reset state that we will compute below.
3643 mCurrentFingerIdBits.clear();
3644 mCurrentStylusIdBits.clear();
3645 mCurrentMouseIdBits.clear();
3646 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003647
Jeff Brown65fd2512011-08-18 11:20:58 -07003648 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3649 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003650 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003651 mCurrentButtonState = 0;
3652 } else {
3653 // Preprocess pointer data.
3654 if (!havePointerIds) {
3655 assignPointerIds();
3656 }
3657
3658 // Handle policy on initial down or hover events.
3659 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003660 bool initialDown = mLastRawPointerData.pointerCount == 0
3661 && mCurrentRawPointerData.pointerCount != 0;
3662 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3663 if (initialDown || buttonsPressed) {
3664 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003665 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003666 getContext()->fadePointer();
3667 }
3668
3669 // Initial downs on external touch devices should wake the device.
3670 // We don't do this for internal touch screens to prevent them from waking
3671 // up in your pocket.
3672 // TODO: Use the input device configuration to control this behavior more finely.
3673 if (getDevice()->isExternal()) {
3674 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3675 }
3676 }
3677
3678 // Synthesize key down from raw buttons if needed.
3679 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3680 policyFlags, mLastButtonState, mCurrentButtonState);
3681
3682 // Consume raw off-screen touches before cooking pointer data.
3683 // If touches are consumed, subsequent code will not receive any pointer data.
3684 if (consumeRawTouches(when, policyFlags)) {
3685 mCurrentRawPointerData.clear();
3686 }
3687
3688 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3689 // with cooked pointer data that has the same ids and indices as the raw data.
3690 // The following code can use either the raw or cooked data, as needed.
3691 cookPointerData();
3692
3693 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003694 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003695 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3696 uint32_t id = idBits.clearFirstMarkedBit();
3697 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3698 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3699 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3700 mCurrentStylusIdBits.markBit(id);
3701 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3702 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3703 mCurrentFingerIdBits.markBit(id);
3704 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3705 mCurrentMouseIdBits.markBit(id);
3706 }
3707 }
3708 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3709 uint32_t id = idBits.clearFirstMarkedBit();
3710 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3711 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3712 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3713 mCurrentStylusIdBits.markBit(id);
3714 }
3715 }
3716
3717 // Stylus takes precedence over all tools, then mouse, then finger.
3718 PointerUsage pointerUsage = mPointerUsage;
3719 if (!mCurrentStylusIdBits.isEmpty()) {
3720 mCurrentMouseIdBits.clear();
3721 mCurrentFingerIdBits.clear();
3722 pointerUsage = POINTER_USAGE_STYLUS;
3723 } else if (!mCurrentMouseIdBits.isEmpty()) {
3724 mCurrentFingerIdBits.clear();
3725 pointerUsage = POINTER_USAGE_MOUSE;
3726 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3727 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003728 }
3729
3730 dispatchPointerUsage(when, policyFlags, pointerUsage);
3731 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003732 if (mDeviceMode == DEVICE_MODE_DIRECT
3733 && mConfig.showTouches && mPointerController != NULL) {
3734 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3735 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3736
3737 mPointerController->setButtonState(mCurrentButtonState);
3738 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3739 mCurrentCookedPointerData.idToIndex,
3740 mCurrentCookedPointerData.touchingIdBits);
3741 }
3742
Jeff Brown65fd2512011-08-18 11:20:58 -07003743 dispatchHoverExit(when, policyFlags);
3744 dispatchTouches(when, policyFlags);
3745 dispatchHoverEnterAndMove(when, policyFlags);
3746 }
3747
3748 // Synthesize key up from raw buttons if needed.
3749 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3750 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003751 }
3752
Jeff Brown6328cdc2010-07-29 18:18:33 -07003753 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003754 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3755 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3756 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003757 mLastFingerIdBits = mCurrentFingerIdBits;
3758 mLastStylusIdBits = mCurrentStylusIdBits;
3759 mLastMouseIdBits = mCurrentMouseIdBits;
3760
3761 // Clear some transient state.
3762 mCurrentRawVScroll = 0;
3763 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003764}
3765
Jeff Brown79ac9692011-04-19 21:20:10 -07003766void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003767 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003768 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3769 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3770 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003771 }
3772}
3773
Jeff Brownbe1aa822011-07-27 16:04:54 -07003774bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3775 // Check for release of a virtual key.
3776 if (mCurrentVirtualKey.down) {
3777 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3778 // Pointer went up while virtual key was down.
3779 mCurrentVirtualKey.down = false;
3780 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003781#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003782 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003783 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003784#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003785 dispatchVirtualKey(when, policyFlags,
3786 AKEY_EVENT_ACTION_UP,
3787 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003788 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003789 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003790 }
3791
Jeff Brownbe1aa822011-07-27 16:04:54 -07003792 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3793 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3794 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3795 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3796 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3797 // Pointer is still within the space of the virtual key.
3798 return true;
3799 }
3800 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003801
Jeff Brownbe1aa822011-07-27 16:04:54 -07003802 // Pointer left virtual key area or another pointer also went down.
3803 // Send key cancellation but do not consume the touch yet.
3804 // This is useful when the user swipes through from the virtual key area
3805 // into the main display surface.
3806 mCurrentVirtualKey.down = false;
3807 if (!mCurrentVirtualKey.ignored) {
3808#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003809 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003810 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3811#endif
3812 dispatchVirtualKey(when, policyFlags,
3813 AKEY_EVENT_ACTION_UP,
3814 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3815 | AKEY_EVENT_FLAG_CANCELED);
3816 }
3817 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003818
Jeff Brownbe1aa822011-07-27 16:04:54 -07003819 if (mLastRawPointerData.touchingIdBits.isEmpty()
3820 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3821 // Pointer just went down. Check for virtual key press or off-screen touches.
3822 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3823 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3824 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3825 // If exactly one pointer went down, check for virtual key hit.
3826 // Otherwise we will drop the entire stroke.
3827 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3828 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3829 if (virtualKey) {
3830 mCurrentVirtualKey.down = true;
3831 mCurrentVirtualKey.downTime = when;
3832 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3833 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3834 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3835 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3836
3837 if (!mCurrentVirtualKey.ignored) {
3838#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003839 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003840 mCurrentVirtualKey.keyCode,
3841 mCurrentVirtualKey.scanCode);
3842#endif
3843 dispatchVirtualKey(when, policyFlags,
3844 AKEY_EVENT_ACTION_DOWN,
3845 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3846 }
3847 }
3848 }
3849 return true;
3850 }
3851 }
3852
Jeff Brownfe508922011-01-18 15:10:10 -08003853 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003854 // most recent touch within the screen area. The idea is to filter out stray
3855 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003856 //
3857 // Problems we're trying to solve:
3858 //
3859 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3860 // virtual key area that is implemented by a separate touch panel and accidentally
3861 // triggers a virtual key.
3862 //
3863 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3864 // area and accidentally triggers a virtual key. This often happens when virtual keys
3865 // are layed out below the screen near to where the on screen keyboard's space bar
3866 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003867 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003868 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003869 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003870 return false;
3871}
3872
3873void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3874 int32_t keyEventAction, int32_t keyEventFlags) {
3875 int32_t keyCode = mCurrentVirtualKey.keyCode;
3876 int32_t scanCode = mCurrentVirtualKey.scanCode;
3877 nsecs_t downTime = mCurrentVirtualKey.downTime;
3878 int32_t metaState = mContext->getGlobalMetaState();
3879 policyFlags |= POLICY_FLAG_VIRTUAL;
3880
3881 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3882 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3883 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003884}
3885
Jeff Brown6d0fec22010-07-23 21:28:06 -07003886void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003887 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3888 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003889 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003890 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003891
3892 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003893 if (!currentIdBits.isEmpty()) {
3894 // No pointer id changes so this is a move event.
3895 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003896 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003897 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3898 AMOTION_EVENT_EDGE_FLAG_NONE,
3899 mCurrentCookedPointerData.pointerProperties,
3900 mCurrentCookedPointerData.pointerCoords,
3901 mCurrentCookedPointerData.idToIndex,
3902 currentIdBits, -1,
3903 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3904 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003905 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003906 // There may be pointers going up and pointers going down and pointers moving
3907 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003908 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3909 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003910 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003911 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003912
Jeff Brownace13b12011-03-09 17:39:48 -08003913 // Update last coordinates of pointers that have moved so that we observe the new
3914 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003915 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003916 mCurrentCookedPointerData.pointerProperties,
3917 mCurrentCookedPointerData.pointerCoords,
3918 mCurrentCookedPointerData.idToIndex,
3919 mLastCookedPointerData.pointerProperties,
3920 mLastCookedPointerData.pointerCoords,
3921 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003922 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003923 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003924 moveNeeded = true;
3925 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003926
Jeff Brownace13b12011-03-09 17:39:48 -08003927 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003928 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003929 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003930
Jeff Brown65fd2512011-08-18 11:20:58 -07003931 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003932 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003933 mLastCookedPointerData.pointerProperties,
3934 mLastCookedPointerData.pointerCoords,
3935 mLastCookedPointerData.idToIndex,
3936 dispatchedIdBits, upId,
3937 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003938 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003939 }
3940
Jeff Brownc3db8582010-10-20 15:33:38 -07003941 // Dispatch move events if any of the remaining pointers moved from their old locations.
3942 // Although applications receive new locations as part of individual pointer up
3943 // events, they do not generally handle them except when presented in a move event.
3944 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003945 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003946 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003947 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003948 mCurrentCookedPointerData.pointerProperties,
3949 mCurrentCookedPointerData.pointerCoords,
3950 mCurrentCookedPointerData.idToIndex,
3951 dispatchedIdBits, -1,
3952 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003953 }
3954
3955 // Dispatch pointer down events using the new pointer locations.
3956 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003957 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003958 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003959
Jeff Brownace13b12011-03-09 17:39:48 -08003960 if (dispatchedIdBits.count() == 1) {
3961 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003962 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003963 }
3964
Jeff Brown65fd2512011-08-18 11:20:58 -07003965 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003966 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003967 mCurrentCookedPointerData.pointerProperties,
3968 mCurrentCookedPointerData.pointerCoords,
3969 mCurrentCookedPointerData.idToIndex,
3970 dispatchedIdBits, downId,
3971 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003972 }
3973 }
Jeff Brownace13b12011-03-09 17:39:48 -08003974}
3975
Jeff Brownbe1aa822011-07-27 16:04:54 -07003976void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3977 if (mSentHoverEnter &&
3978 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3979 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3980 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003981 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003982 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3983 mLastCookedPointerData.pointerProperties,
3984 mLastCookedPointerData.pointerCoords,
3985 mLastCookedPointerData.idToIndex,
3986 mLastCookedPointerData.hoveringIdBits, -1,
3987 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3988 mSentHoverEnter = false;
3989 }
3990}
Jeff Brownace13b12011-03-09 17:39:48 -08003991
Jeff Brownbe1aa822011-07-27 16:04:54 -07003992void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3993 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3994 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3995 int32_t metaState = getContext()->getGlobalMetaState();
3996 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003997 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003998 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3999 mCurrentCookedPointerData.pointerProperties,
4000 mCurrentCookedPointerData.pointerCoords,
4001 mCurrentCookedPointerData.idToIndex,
4002 mCurrentCookedPointerData.hoveringIdBits, -1,
4003 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4004 mSentHoverEnter = true;
4005 }
Jeff Brownace13b12011-03-09 17:39:48 -08004006
Jeff Brown65fd2512011-08-18 11:20:58 -07004007 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004008 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
4009 mCurrentCookedPointerData.pointerProperties,
4010 mCurrentCookedPointerData.pointerCoords,
4011 mCurrentCookedPointerData.idToIndex,
4012 mCurrentCookedPointerData.hoveringIdBits, -1,
4013 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4014 }
4015}
4016
4017void TouchInputMapper::cookPointerData() {
4018 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4019
4020 mCurrentCookedPointerData.clear();
4021 mCurrentCookedPointerData.pointerCount = currentPointerCount;
4022 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
4023 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
4024
4025 // Walk through the the active pointers and map device coordinates onto
4026 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08004027 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004028 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004029
Jeff Browna1f89ce2011-08-11 00:05:01 -07004030 // Size
4031 float touchMajor, touchMinor, toolMajor, toolMinor, size;
4032 switch (mCalibration.sizeCalibration) {
4033 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
4034 case Calibration::SIZE_CALIBRATION_DIAMETER:
Jeff Brown037f7272012-06-25 17:31:23 -07004035 case Calibration::SIZE_CALIBRATION_BOX:
Jeff Browna1f89ce2011-08-11 00:05:01 -07004036 case Calibration::SIZE_CALIBRATION_AREA:
4037 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
4038 touchMajor = in.touchMajor;
4039 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
4040 toolMajor = in.toolMajor;
4041 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
4042 size = mRawPointerAxes.touchMinor.valid
4043 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4044 } else if (mRawPointerAxes.touchMajor.valid) {
4045 toolMajor = touchMajor = in.touchMajor;
4046 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
4047 ? in.touchMinor : in.touchMajor;
4048 size = mRawPointerAxes.touchMinor.valid
4049 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4050 } else if (mRawPointerAxes.toolMajor.valid) {
4051 touchMajor = toolMajor = in.toolMajor;
4052 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
4053 ? in.toolMinor : in.toolMajor;
4054 size = mRawPointerAxes.toolMinor.valid
4055 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004056 } else {
Steve Blockec193de2012-01-09 18:35:44 +00004057 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07004058 "Size calibration should have been resolved to NONE.");
4059 touchMajor = 0;
4060 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004061 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004062 toolMinor = 0;
4063 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004064 }
Jeff Brownace13b12011-03-09 17:39:48 -08004065
Jeff Browna1f89ce2011-08-11 00:05:01 -07004066 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
4067 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
4068 if (touchingCount > 1) {
4069 touchMajor /= touchingCount;
4070 touchMinor /= touchingCount;
4071 toolMajor /= touchingCount;
4072 toolMinor /= touchingCount;
4073 size /= touchingCount;
4074 }
4075 }
Jeff Brownace13b12011-03-09 17:39:48 -08004076
Jeff Browna1f89ce2011-08-11 00:05:01 -07004077 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4078 touchMajor *= mGeometricScale;
4079 touchMinor *= mGeometricScale;
4080 toolMajor *= mGeometricScale;
4081 toolMinor *= mGeometricScale;
4082 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4083 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004084 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004085 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4086 toolMinor = toolMajor;
4087 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4088 touchMinor = touchMajor;
4089 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004090 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004091
4092 mCalibration.applySizeScaleAndBias(&touchMajor);
4093 mCalibration.applySizeScaleAndBias(&touchMinor);
4094 mCalibration.applySizeScaleAndBias(&toolMajor);
4095 mCalibration.applySizeScaleAndBias(&toolMinor);
4096 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004097 break;
4098 default:
4099 touchMajor = 0;
4100 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004101 toolMajor = 0;
4102 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004103 size = 0;
4104 break;
4105 }
4106
Jeff Browna1f89ce2011-08-11 00:05:01 -07004107 // Pressure
4108 float pressure;
4109 switch (mCalibration.pressureCalibration) {
4110 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4111 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4112 pressure = in.pressure * mPressureScale;
4113 break;
4114 default:
4115 pressure = in.isHovering ? 0 : 1;
4116 break;
4117 }
4118
Jeff Brown65fd2512011-08-18 11:20:58 -07004119 // Tilt and Orientation
4120 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004121 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004122 if (mHaveTilt) {
4123 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4124 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4125 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4126 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4127 } else {
4128 tilt = 0;
4129
4130 switch (mCalibration.orientationCalibration) {
4131 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown037f7272012-06-25 17:31:23 -07004132 orientation = in.orientation * mOrientationScale;
Jeff Brown65fd2512011-08-18 11:20:58 -07004133 break;
4134 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4135 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4136 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4137 if (c1 != 0 || c2 != 0) {
4138 orientation = atan2f(c1, c2) * 0.5f;
4139 float confidence = hypotf(c1, c2);
4140 float scale = 1.0f + confidence / 16.0f;
4141 touchMajor *= scale;
4142 touchMinor /= scale;
4143 toolMajor *= scale;
4144 toolMinor /= scale;
4145 } else {
4146 orientation = 0;
4147 }
4148 break;
4149 }
4150 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004151 orientation = 0;
4152 }
Jeff Brownace13b12011-03-09 17:39:48 -08004153 }
4154
Jeff Brown80fd47c2011-05-24 01:07:44 -07004155 // Distance
4156 float distance;
4157 switch (mCalibration.distanceCalibration) {
4158 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004159 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004160 break;
4161 default:
4162 distance = 0;
4163 }
4164
Jeff Brownace13b12011-03-09 17:39:48 -08004165 // X and Y
4166 // Adjust coords for surface orientation.
4167 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004168 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004169 case DISPLAY_ORIENTATION_90:
Jeff Brown83d616a2012-09-09 20:33:43 -07004170 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4171 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004172 orientation -= M_PI_2;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004173 if (orientation < mOrientedRanges.orientation.min) {
4174 orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
Jeff Brownace13b12011-03-09 17:39:48 -08004175 }
4176 break;
4177 case DISPLAY_ORIENTATION_180:
Jeff Brown83d616a2012-09-09 20:33:43 -07004178 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
4179 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004180 orientation -= M_PI;
4181 if (orientation < mOrientedRanges.orientation.min) {
4182 orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
4183 }
Jeff Brownace13b12011-03-09 17:39:48 -08004184 break;
4185 case DISPLAY_ORIENTATION_270:
Jeff Brown83d616a2012-09-09 20:33:43 -07004186 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
4187 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004188 orientation += M_PI_2;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004189 if (orientation > mOrientedRanges.orientation.max) {
4190 orientation -= (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
Jeff Brownace13b12011-03-09 17:39:48 -08004191 }
4192 break;
4193 default:
Jeff Brown83d616a2012-09-09 20:33:43 -07004194 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4195 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004196 break;
4197 }
4198
4199 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004200 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004201 out.clear();
4202 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4203 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4204 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4205 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4206 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4207 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4208 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4209 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4210 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004211 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004212 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004213
4214 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004215 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4216 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004217 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004218 properties.id = id;
4219 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004220
Jeff Brownbe1aa822011-07-27 16:04:54 -07004221 // Write id index.
4222 mCurrentCookedPointerData.idToIndex[id] = i;
4223 }
Jeff Brownace13b12011-03-09 17:39:48 -08004224}
4225
Jeff Brown65fd2512011-08-18 11:20:58 -07004226void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4227 PointerUsage pointerUsage) {
4228 if (pointerUsage != mPointerUsage) {
4229 abortPointerUsage(when, policyFlags);
4230 mPointerUsage = pointerUsage;
4231 }
4232
4233 switch (mPointerUsage) {
4234 case POINTER_USAGE_GESTURES:
4235 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4236 break;
4237 case POINTER_USAGE_STYLUS:
4238 dispatchPointerStylus(when, policyFlags);
4239 break;
4240 case POINTER_USAGE_MOUSE:
4241 dispatchPointerMouse(when, policyFlags);
4242 break;
4243 default:
4244 break;
4245 }
4246}
4247
4248void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4249 switch (mPointerUsage) {
4250 case POINTER_USAGE_GESTURES:
4251 abortPointerGestures(when, policyFlags);
4252 break;
4253 case POINTER_USAGE_STYLUS:
4254 abortPointerStylus(when, policyFlags);
4255 break;
4256 case POINTER_USAGE_MOUSE:
4257 abortPointerMouse(when, policyFlags);
4258 break;
4259 default:
4260 break;
4261 }
4262
4263 mPointerUsage = POINTER_USAGE_NONE;
4264}
4265
Jeff Brown79ac9692011-04-19 21:20:10 -07004266void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4267 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004268 // Update current gesture coordinates.
4269 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004270 bool sendEvents = preparePointerGestures(when,
4271 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4272 if (!sendEvents) {
4273 return;
4274 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004275 if (finishPreviousGesture) {
4276 cancelPreviousGesture = false;
4277 }
Jeff Brownace13b12011-03-09 17:39:48 -08004278
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004279 // Update the pointer presentation and spots.
4280 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4281 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4282 if (finishPreviousGesture || cancelPreviousGesture) {
4283 mPointerController->clearSpots();
4284 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004285 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4286 mPointerGesture.currentGestureIdToIndex,
4287 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004288 } else {
4289 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4290 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004291
Jeff Brown538881e2011-05-25 18:23:38 -07004292 // Show or hide the pointer if needed.
4293 switch (mPointerGesture.currentGestureMode) {
4294 case PointerGesture::NEUTRAL:
4295 case PointerGesture::QUIET:
4296 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4297 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4298 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4299 // Remind the user of where the pointer is after finishing a gesture with spots.
4300 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4301 }
4302 break;
4303 case PointerGesture::TAP:
4304 case PointerGesture::TAP_DRAG:
4305 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4306 case PointerGesture::HOVER:
4307 case PointerGesture::PRESS:
4308 // Unfade the pointer when the current gesture manipulates the
4309 // area directly under the pointer.
4310 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4311 break;
4312 case PointerGesture::SWIPE:
4313 case PointerGesture::FREEFORM:
4314 // Fade the pointer when the current gesture manipulates a different
4315 // area and there are spots to guide the user experience.
4316 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4317 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4318 } else {
4319 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4320 }
4321 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004322 }
4323
Jeff Brownace13b12011-03-09 17:39:48 -08004324 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004325 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004326 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004327
4328 // Update last coordinates of pointers that have moved so that we observe the new
4329 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004330 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4331 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4332 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004333 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004334 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4335 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4336 bool moveNeeded = false;
4337 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004338 && !mPointerGesture.lastGestureIdBits.isEmpty()
4339 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004340 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4341 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004342 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004343 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004344 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004345 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4346 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004347 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004348 moveNeeded = true;
4349 }
Jeff Brownace13b12011-03-09 17:39:48 -08004350 }
4351
4352 // Send motion events for all pointers that went up or were canceled.
4353 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4354 if (!dispatchedGestureIdBits.isEmpty()) {
4355 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004356 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004357 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4358 AMOTION_EVENT_EDGE_FLAG_NONE,
4359 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004360 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4361 dispatchedGestureIdBits, -1,
4362 0, 0, mPointerGesture.downTime);
4363
4364 dispatchedGestureIdBits.clear();
4365 } else {
4366 BitSet32 upGestureIdBits;
4367 if (finishPreviousGesture) {
4368 upGestureIdBits = dispatchedGestureIdBits;
4369 } else {
4370 upGestureIdBits.value = dispatchedGestureIdBits.value
4371 & ~mPointerGesture.currentGestureIdBits.value;
4372 }
4373 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004374 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004375
Jeff Brown65fd2512011-08-18 11:20:58 -07004376 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004377 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004378 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4379 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004380 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4381 dispatchedGestureIdBits, id,
4382 0, 0, mPointerGesture.downTime);
4383
4384 dispatchedGestureIdBits.clearBit(id);
4385 }
4386 }
4387 }
4388
4389 // Send motion events for all pointers that moved.
4390 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004391 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004392 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4393 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004394 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4395 dispatchedGestureIdBits, -1,
4396 0, 0, mPointerGesture.downTime);
4397 }
4398
4399 // Send motion events for all pointers that went down.
4400 if (down) {
4401 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4402 & ~dispatchedGestureIdBits.value);
4403 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004404 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004405 dispatchedGestureIdBits.markBit(id);
4406
Jeff Brownace13b12011-03-09 17:39:48 -08004407 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004408 mPointerGesture.downTime = when;
4409 }
4410
Jeff Brown65fd2512011-08-18 11:20:58 -07004411 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004412 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004413 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004414 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4415 dispatchedGestureIdBits, id,
4416 0, 0, mPointerGesture.downTime);
4417 }
4418 }
4419
Jeff Brownace13b12011-03-09 17:39:48 -08004420 // Send motion events for hover.
4421 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004422 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004423 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4424 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4425 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004426 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4427 mPointerGesture.currentGestureIdBits, -1,
4428 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004429 } else if (dispatchedGestureIdBits.isEmpty()
4430 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4431 // Synthesize a hover move event after all pointers go up to indicate that
4432 // the pointer is hovering again even if the user is not currently touching
4433 // the touch pad. This ensures that a view will receive a fresh hover enter
4434 // event after a tap.
4435 float x, y;
4436 mPointerController->getPosition(&x, &y);
4437
4438 PointerProperties pointerProperties;
4439 pointerProperties.clear();
4440 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004441 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004442
4443 PointerCoords pointerCoords;
4444 pointerCoords.clear();
4445 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4446 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4447
Jeff Brown65fd2512011-08-18 11:20:58 -07004448 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004449 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4450 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07004451 mViewport.displayId, 1, &pointerProperties, &pointerCoords,
4452 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004453 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004454 }
4455
4456 // Update state.
4457 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4458 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004459 mPointerGesture.lastGestureIdBits.clear();
4460 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004461 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4462 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004463 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004464 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004465 mPointerGesture.lastGestureProperties[index].copyFrom(
4466 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004467 mPointerGesture.lastGestureCoords[index].copyFrom(
4468 mPointerGesture.currentGestureCoords[index]);
4469 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004470 }
4471 }
4472}
4473
Jeff Brown65fd2512011-08-18 11:20:58 -07004474void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4475 // Cancel previously dispatches pointers.
4476 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4477 int32_t metaState = getContext()->getGlobalMetaState();
4478 int32_t buttonState = mCurrentButtonState;
4479 dispatchMotion(when, policyFlags, mSource,
4480 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4481 AMOTION_EVENT_EDGE_FLAG_NONE,
4482 mPointerGesture.lastGestureProperties,
4483 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4484 mPointerGesture.lastGestureIdBits, -1,
4485 0, 0, mPointerGesture.downTime);
4486 }
4487
4488 // Reset the current pointer gesture.
4489 mPointerGesture.reset();
4490 mPointerVelocityControl.reset();
4491
4492 // Remove any current spots.
4493 if (mPointerController != NULL) {
4494 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4495 mPointerController->clearSpots();
4496 }
4497}
4498
Jeff Brown79ac9692011-04-19 21:20:10 -07004499bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4500 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004501 *outCancelPreviousGesture = false;
4502 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004503
Jeff Brown79ac9692011-04-19 21:20:10 -07004504 // Handle TAP timeout.
4505 if (isTimeout) {
4506#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004507 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004508#endif
4509
4510 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004511 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004512 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004513 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004514 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004515 } else {
4516 // The tap is finished.
4517#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004518 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004519#endif
4520 *outFinishPreviousGesture = true;
4521
4522 mPointerGesture.activeGestureId = -1;
4523 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4524 mPointerGesture.currentGestureIdBits.clear();
4525
Jeff Brown65fd2512011-08-18 11:20:58 -07004526 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004527 return true;
4528 }
4529 }
4530
4531 // We did not handle this timeout.
4532 return false;
4533 }
4534
Jeff Brown65fd2512011-08-18 11:20:58 -07004535 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4536 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4537
Jeff Brownace13b12011-03-09 17:39:48 -08004538 // Update the velocity tracker.
4539 {
4540 VelocityTracker::Position positions[MAX_POINTERS];
4541 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004542 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004543 uint32_t id = idBits.clearFirstMarkedBit();
4544 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004545 positions[count].x = pointer.x * mPointerXMovementScale;
4546 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004547 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004548 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004549 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004550 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004551
Jeff Brownace13b12011-03-09 17:39:48 -08004552 // Pick a new active touch id if needed.
4553 // Choose an arbitrary pointer that just went down, if there is one.
4554 // Otherwise choose an arbitrary remaining pointer.
4555 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004556 // We keep the same active touch id for as long as possible.
4557 bool activeTouchChanged = false;
4558 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4559 int32_t activeTouchId = lastActiveTouchId;
4560 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004561 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004562 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004563 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004564 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004565 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004566 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004567 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004568 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004569 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004570 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004571 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004572 } else {
4573 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004574 }
4575 }
4576
4577 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004578 bool isQuietTime = false;
4579 if (activeTouchId < 0) {
4580 mPointerGesture.resetQuietTime();
4581 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004582 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004583 if (!isQuietTime) {
4584 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4585 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4586 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004587 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004588 // Enter quiet time when exiting swipe or freeform state.
4589 // This is to prevent accidentally entering the hover state and flinging the
4590 // pointer when finishing a swipe and there is still one pointer left onscreen.
4591 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004592 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004593 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004594 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004595 // Enter quiet time when releasing the button and there are still two or more
4596 // fingers down. This may indicate that one finger was used to press the button
4597 // but it has not gone up yet.
4598 isQuietTime = true;
4599 }
4600 if (isQuietTime) {
4601 mPointerGesture.quietTime = when;
4602 }
Jeff Brownace13b12011-03-09 17:39:48 -08004603 }
4604 }
4605
4606 // Switch states based on button and pointer state.
4607 if (isQuietTime) {
4608 // Case 1: Quiet time. (QUIET)
4609#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004610 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004611 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004612#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004613 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4614 *outFinishPreviousGesture = true;
4615 }
Jeff Brownace13b12011-03-09 17:39:48 -08004616
4617 mPointerGesture.activeGestureId = -1;
4618 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004619 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004620
Jeff Brown65fd2512011-08-18 11:20:58 -07004621 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004622 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004623 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004624 // The pointer follows the active touch point.
4625 // Emit DOWN, MOVE, UP events at the pointer location.
4626 //
4627 // Only the active touch matters; other fingers are ignored. This policy helps
4628 // to handle the case where the user places a second finger on the touch pad
4629 // to apply the necessary force to depress an integrated button below the surface.
4630 // We don't want the second finger to be delivered to applications.
4631 //
4632 // For this to work well, we need to make sure to track the pointer that is really
4633 // active. If the user first puts one finger down to click then adds another
4634 // finger to drag then the active pointer should switch to the finger that is
4635 // being dragged.
4636#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004637 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004638 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004639#endif
4640 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004641 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004642 *outFinishPreviousGesture = true;
4643 mPointerGesture.activeGestureId = 0;
4644 }
4645
4646 // Switch pointers if needed.
4647 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004648 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004649 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004650 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004651 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004652 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004653 float vx, vy;
4654 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4655 float speed = hypotf(vx, vy);
4656 if (speed > bestSpeed) {
4657 bestId = id;
4658 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004659 }
Jeff Brown8d608662010-08-30 03:02:23 -07004660 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004661 }
4662 if (bestId >= 0 && bestId != activeTouchId) {
4663 mPointerGesture.activeTouchId = activeTouchId = bestId;
4664 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004665#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004666 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004667 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004668#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004669 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004670 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004671
Jeff Brown65fd2512011-08-18 11:20:58 -07004672 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004673 const RawPointerData::Pointer& currentPointer =
4674 mCurrentRawPointerData.pointerForId(activeTouchId);
4675 const RawPointerData::Pointer& lastPointer =
4676 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004677 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4678 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004679
Jeff Brownbe1aa822011-07-27 16:04:54 -07004680 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004681 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004682
4683 // Move the pointer using a relative motion.
4684 // When using spots, the click will occur at the position of the anchor
4685 // spot and all other spots will move there.
4686 mPointerController->move(deltaX, deltaY);
4687 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004688 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004689 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004690
Jeff Brownace13b12011-03-09 17:39:48 -08004691 float x, y;
4692 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004693
Jeff Brown79ac9692011-04-19 21:20:10 -07004694 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004695 mPointerGesture.currentGestureIdBits.clear();
4696 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4697 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004698 mPointerGesture.currentGestureProperties[0].clear();
4699 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004700 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004701 mPointerGesture.currentGestureCoords[0].clear();
4702 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4703 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4704 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004705 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004706 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004707 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4708 *outFinishPreviousGesture = true;
4709 }
Jeff Brownace13b12011-03-09 17:39:48 -08004710
Jeff Brown79ac9692011-04-19 21:20:10 -07004711 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004712 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004713 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004714 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4715 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004716 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004717 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004718 float x, y;
4719 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004720 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4721 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004722#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004723 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004724#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004725
4726 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004727 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004728 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004729
Jeff Brownace13b12011-03-09 17:39:48 -08004730 mPointerGesture.activeGestureId = 0;
4731 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004732 mPointerGesture.currentGestureIdBits.clear();
4733 mPointerGesture.currentGestureIdBits.markBit(
4734 mPointerGesture.activeGestureId);
4735 mPointerGesture.currentGestureIdToIndex[
4736 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004737 mPointerGesture.currentGestureProperties[0].clear();
4738 mPointerGesture.currentGestureProperties[0].id =
4739 mPointerGesture.activeGestureId;
4740 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004741 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004742 mPointerGesture.currentGestureCoords[0].clear();
4743 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004744 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004745 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004746 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004747 mPointerGesture.currentGestureCoords[0].setAxisValue(
4748 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004749
Jeff Brownace13b12011-03-09 17:39:48 -08004750 tapped = true;
4751 } else {
4752#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004753 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004754 x - mPointerGesture.tapX,
4755 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004756#endif
4757 }
4758 } else {
4759#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004760 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004761 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004762#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004763 }
Jeff Brownace13b12011-03-09 17:39:48 -08004764 }
Jeff Brown2352b972011-04-12 22:39:53 -07004765
Jeff Brown65fd2512011-08-18 11:20:58 -07004766 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004767
Jeff Brownace13b12011-03-09 17:39:48 -08004768 if (!tapped) {
4769#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004770 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004771#endif
4772 mPointerGesture.activeGestureId = -1;
4773 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004774 mPointerGesture.currentGestureIdBits.clear();
4775 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004776 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004777 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004778 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004779 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4780 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004781 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004782
Jeff Brown79ac9692011-04-19 21:20:10 -07004783 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4784 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004785 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004786 float x, y;
4787 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004788 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4789 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004790 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4791 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004792#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004793 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004794 x - mPointerGesture.tapX,
4795 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004796#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004797 }
4798 } else {
4799#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004800 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004801 (when - mPointerGesture.tapUpTime) * 0.000001f);
4802#endif
4803 }
4804 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4805 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4806 }
Jeff Brownace13b12011-03-09 17:39:48 -08004807
Jeff Brown65fd2512011-08-18 11:20:58 -07004808 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004809 const RawPointerData::Pointer& currentPointer =
4810 mCurrentRawPointerData.pointerForId(activeTouchId);
4811 const RawPointerData::Pointer& lastPointer =
4812 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004813 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004814 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004815 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004816 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004817
Jeff Brownbe1aa822011-07-27 16:04:54 -07004818 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004819 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004820
Jeff Brown2352b972011-04-12 22:39:53 -07004821 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004822 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004823 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004824 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004825 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004826 }
4827
Jeff Brown79ac9692011-04-19 21:20:10 -07004828 bool down;
4829 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4830#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004831 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004832#endif
4833 down = true;
4834 } else {
4835#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004836 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004837#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004838 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4839 *outFinishPreviousGesture = true;
4840 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004841 mPointerGesture.activeGestureId = 0;
4842 down = false;
4843 }
Jeff Brownace13b12011-03-09 17:39:48 -08004844
4845 float x, y;
4846 mPointerController->getPosition(&x, &y);
4847
Jeff Brownace13b12011-03-09 17:39:48 -08004848 mPointerGesture.currentGestureIdBits.clear();
4849 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4850 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004851 mPointerGesture.currentGestureProperties[0].clear();
4852 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4853 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004854 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004855 mPointerGesture.currentGestureCoords[0].clear();
4856 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4857 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004858 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4859 down ? 1.0f : 0.0f);
4860
Jeff Brown65fd2512011-08-18 11:20:58 -07004861 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004862 mPointerGesture.resetTap();
4863 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004864 mPointerGesture.tapX = x;
4865 mPointerGesture.tapY = y;
4866 }
Jeff Brownace13b12011-03-09 17:39:48 -08004867 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004868 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4869 // We need to provide feedback for each finger that goes down so we cannot wait
4870 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004871 //
Jeff Brown2352b972011-04-12 22:39:53 -07004872 // The ambiguous case is deciding what to do when there are two fingers down but they
4873 // have not moved enough to determine whether they are part of a drag or part of a
4874 // freeform gesture, or just a press or long-press at the pointer location.
4875 //
4876 // When there are two fingers we start with the PRESS hypothesis and we generate a
4877 // down at the pointer location.
4878 //
4879 // When the two fingers move enough or when additional fingers are added, we make
4880 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004881 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004882
Jeff Brown214eaf42011-05-26 19:17:02 -07004883 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004884 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004885 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004886 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4887 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004888 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004889 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004890 // Additional pointers have gone down but not yet settled.
4891 // Reset the gesture.
4892#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004893 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004894 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004895 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004896 * 0.000001f);
4897#endif
4898 *outCancelPreviousGesture = true;
4899 } else {
4900 // Continue previous gesture.
4901 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4902 }
4903
4904 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004905 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4906 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004907 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004908 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004909
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004910 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004911#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004912 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004913 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004914 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004915 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004916#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004917 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4918 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004919 &mPointerGesture.referenceTouchY);
4920 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4921 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004922 }
Jeff Brownace13b12011-03-09 17:39:48 -08004923
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004924 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004925 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004926 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4927 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004928 mPointerGesture.referenceDeltas[id].dx = 0;
4929 mPointerGesture.referenceDeltas[id].dy = 0;
4930 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004931 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004932
4933 // Add delta for all fingers and calculate a common movement delta.
4934 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004935 BitSet32 commonIdBits(mLastFingerIdBits.value
4936 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004937 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4938 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004939 uint32_t id = idBits.clearFirstMarkedBit();
4940 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4941 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004942 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4943 delta.dx += cpd.x - lpd.x;
4944 delta.dy += cpd.y - lpd.y;
4945
4946 if (first) {
4947 commonDeltaX = delta.dx;
4948 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004949 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004950 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4951 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4952 }
4953 }
Jeff Brownace13b12011-03-09 17:39:48 -08004954
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004955 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4956 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4957 float dist[MAX_POINTER_ID + 1];
4958 int32_t distOverThreshold = 0;
4959 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004960 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004961 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004962 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4963 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004964 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004965 distOverThreshold += 1;
4966 }
4967 }
4968
4969 // Only transition when at least two pointers have moved further than
4970 // the minimum distance threshold.
4971 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004972 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004973 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004974#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004975 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004976 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004977#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004978 *outCancelPreviousGesture = true;
4979 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4980 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004981 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004982 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004983 uint32_t id1 = idBits.clearFirstMarkedBit();
4984 uint32_t id2 = idBits.firstMarkedBit();
4985 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4986 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4987 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4988 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4989 // There are two pointers but they are too far apart for a SWIPE,
4990 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004991#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004992 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004993 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004994#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004995 *outCancelPreviousGesture = true;
4996 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4997 } else {
4998 // There are two pointers. Wait for both pointers to start moving
4999 // before deciding whether this is a SWIPE or FREEFORM gesture.
5000 float dist1 = dist[id1];
5001 float dist2 = dist[id2];
5002 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
5003 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
5004 // Calculate the dot product of the displacement vectors.
5005 // When the vectors are oriented in approximately the same direction,
5006 // the angle betweeen them is near zero and the cosine of the angle
5007 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
5008 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
5009 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07005010 float dx1 = delta1.dx * mPointerXZoomScale;
5011 float dy1 = delta1.dy * mPointerYZoomScale;
5012 float dx2 = delta2.dx * mPointerXZoomScale;
5013 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005014 float dot = dx1 * dx2 + dy1 * dy2;
5015 float cosine = dot / (dist1 * dist2); // denominator always > 0
5016 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
5017 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005018#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005019 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005020 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5021 "cosine %0.3f >= %0.3f",
5022 dist1, mConfig.pointerGestureMultitouchMinDistance,
5023 dist2, mConfig.pointerGestureMultitouchMinDistance,
5024 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005025#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005026 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
5027 } else {
5028 // Pointers are moving in different directions. Switch to FREEFORM.
5029#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005030 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005031 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5032 "cosine %0.3f < %0.3f",
5033 dist1, mConfig.pointerGestureMultitouchMinDistance,
5034 dist2, mConfig.pointerGestureMultitouchMinDistance,
5035 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
5036#endif
5037 *outCancelPreviousGesture = true;
5038 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5039 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005040 }
Jeff Brownace13b12011-03-09 17:39:48 -08005041 }
5042 }
Jeff Brownace13b12011-03-09 17:39:48 -08005043 }
5044 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07005045 // Switch from SWIPE to FREEFORM if additional pointers go down.
5046 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07005047 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07005048#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005049 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005050 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005051#endif
Jeff Brownace13b12011-03-09 17:39:48 -08005052 *outCancelPreviousGesture = true;
5053 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07005054 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005055 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005056
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005057 // Move the reference points based on the overall group motion of the fingers
5058 // except in PRESS mode while waiting for a transition to occur.
5059 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
5060 && (commonDeltaX || commonDeltaY)) {
5061 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005062 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07005063 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005064 delta.dx = 0;
5065 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07005066 }
5067
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005068 mPointerGesture.referenceTouchX += commonDeltaX;
5069 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07005070
Jeff Brown65fd2512011-08-18 11:20:58 -07005071 commonDeltaX *= mPointerXMovementScale;
5072 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07005073
Jeff Brownbe1aa822011-07-27 16:04:54 -07005074 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07005075 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07005076
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005077 mPointerGesture.referenceGestureX += commonDeltaX;
5078 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005079 }
5080
5081 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005082 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5083 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5084 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005085#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005086 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005087 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005088 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005089#endif
Steve Blockec193de2012-01-09 18:35:44 +00005090 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005091
5092 mPointerGesture.currentGestureIdBits.clear();
5093 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5094 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005095 mPointerGesture.currentGestureProperties[0].clear();
5096 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5097 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005098 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005099 mPointerGesture.currentGestureCoords[0].clear();
5100 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5101 mPointerGesture.referenceGestureX);
5102 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5103 mPointerGesture.referenceGestureY);
5104 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005105 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5106 // FREEFORM mode.
5107#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005108 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005109 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005110 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005111#endif
Steve Blockec193de2012-01-09 18:35:44 +00005112 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005113
Jeff Brownace13b12011-03-09 17:39:48 -08005114 mPointerGesture.currentGestureIdBits.clear();
5115
5116 BitSet32 mappedTouchIdBits;
5117 BitSet32 usedGestureIdBits;
5118 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5119 // Initially, assign the active gesture id to the active touch point
5120 // if there is one. No other touch id bits are mapped yet.
5121 if (!*outCancelPreviousGesture) {
5122 mappedTouchIdBits.markBit(activeTouchId);
5123 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5124 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5125 mPointerGesture.activeGestureId;
5126 } else {
5127 mPointerGesture.activeGestureId = -1;
5128 }
5129 } else {
5130 // Otherwise, assume we mapped all touches from the previous frame.
5131 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005132 mappedTouchIdBits.value = mLastFingerIdBits.value
5133 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005134 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5135
5136 // Check whether we need to choose a new active gesture id because the
5137 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005138 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5139 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005140 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005141 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005142 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5143 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5144 mPointerGesture.activeGestureId = -1;
5145 break;
5146 }
5147 }
5148 }
5149
5150#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005151 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005152 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5153 "activeGestureId=%d",
5154 mappedTouchIdBits.value, usedGestureIdBits.value,
5155 mPointerGesture.activeGestureId);
5156#endif
5157
Jeff Brown65fd2512011-08-18 11:20:58 -07005158 BitSet32 idBits(mCurrentFingerIdBits);
5159 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005160 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005161 uint32_t gestureId;
5162 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005163 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005164 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5165#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005166 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005167 "new mapping for touch id %d -> gesture id %d",
5168 touchId, gestureId);
5169#endif
5170 } else {
5171 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5172#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005173 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005174 "existing mapping for touch id %d -> gesture id %d",
5175 touchId, gestureId);
5176#endif
5177 }
5178 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5179 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5180
Jeff Brownbe1aa822011-07-27 16:04:54 -07005181 const RawPointerData::Pointer& pointer =
5182 mCurrentRawPointerData.pointerForId(touchId);
5183 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005184 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005185 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005186 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005187 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005188
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005189 mPointerGesture.currentGestureProperties[i].clear();
5190 mPointerGesture.currentGestureProperties[i].id = gestureId;
5191 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005192 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005193 mPointerGesture.currentGestureCoords[i].clear();
5194 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005195 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005196 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005197 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005198 mPointerGesture.currentGestureCoords[i].setAxisValue(
5199 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5200 }
5201
5202 if (mPointerGesture.activeGestureId < 0) {
5203 mPointerGesture.activeGestureId =
5204 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5205#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005206 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005207 "activeGestureId=%d", mPointerGesture.activeGestureId);
5208#endif
5209 }
Jeff Brown2352b972011-04-12 22:39:53 -07005210 }
Jeff Brownace13b12011-03-09 17:39:48 -08005211 }
5212
Jeff Brownbe1aa822011-07-27 16:04:54 -07005213 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005214
Jeff Brownace13b12011-03-09 17:39:48 -08005215#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005216 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005217 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5218 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005219 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005220 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5221 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005222 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005223 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005224 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005225 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005226 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005227 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005228 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5229 id, index, properties.toolType,
5230 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005231 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5232 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5233 }
5234 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005235 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005236 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005237 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005238 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005239 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005240 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5241 id, index, properties.toolType,
5242 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005243 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5244 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5245 }
5246#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005247 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005248}
5249
Jeff Brown65fd2512011-08-18 11:20:58 -07005250void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5251 mPointerSimple.currentCoords.clear();
5252 mPointerSimple.currentProperties.clear();
5253
5254 bool down, hovering;
5255 if (!mCurrentStylusIdBits.isEmpty()) {
5256 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5257 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5258 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5259 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5260 mPointerController->setPosition(x, y);
5261
5262 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5263 down = !hovering;
5264
5265 mPointerController->getPosition(&x, &y);
5266 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5267 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5268 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5269 mPointerSimple.currentProperties.id = 0;
5270 mPointerSimple.currentProperties.toolType =
5271 mCurrentCookedPointerData.pointerProperties[index].toolType;
5272 } else {
5273 down = false;
5274 hovering = false;
5275 }
5276
5277 dispatchPointerSimple(when, policyFlags, down, hovering);
5278}
5279
5280void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5281 abortPointerSimple(when, policyFlags);
5282}
5283
5284void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5285 mPointerSimple.currentCoords.clear();
5286 mPointerSimple.currentProperties.clear();
5287
5288 bool down, hovering;
5289 if (!mCurrentMouseIdBits.isEmpty()) {
5290 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5291 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5292 if (mLastMouseIdBits.hasBit(id)) {
5293 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5294 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5295 - mLastRawPointerData.pointers[lastIndex].x)
5296 * mPointerXMovementScale;
5297 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5298 - mLastRawPointerData.pointers[lastIndex].y)
5299 * mPointerYMovementScale;
5300
5301 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5302 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5303
5304 mPointerController->move(deltaX, deltaY);
5305 } else {
5306 mPointerVelocityControl.reset();
5307 }
5308
5309 down = isPointerDown(mCurrentButtonState);
5310 hovering = !down;
5311
5312 float x, y;
5313 mPointerController->getPosition(&x, &y);
5314 mPointerSimple.currentCoords.copyFrom(
5315 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5316 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5317 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5318 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5319 hovering ? 0.0f : 1.0f);
5320 mPointerSimple.currentProperties.id = 0;
5321 mPointerSimple.currentProperties.toolType =
5322 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5323 } else {
5324 mPointerVelocityControl.reset();
5325
5326 down = false;
5327 hovering = false;
5328 }
5329
5330 dispatchPointerSimple(when, policyFlags, down, hovering);
5331}
5332
5333void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5334 abortPointerSimple(when, policyFlags);
5335
5336 mPointerVelocityControl.reset();
5337}
5338
5339void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5340 bool down, bool hovering) {
5341 int32_t metaState = getContext()->getGlobalMetaState();
5342
5343 if (mPointerController != NULL) {
5344 if (down || hovering) {
5345 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5346 mPointerController->clearSpots();
5347 mPointerController->setButtonState(mCurrentButtonState);
5348 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5349 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5350 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5351 }
5352 }
5353
5354 if (mPointerSimple.down && !down) {
5355 mPointerSimple.down = false;
5356
5357 // Send up.
5358 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5359 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005360 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005361 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5362 mOrientedXPrecision, mOrientedYPrecision,
5363 mPointerSimple.downTime);
5364 getListener()->notifyMotion(&args);
5365 }
5366
5367 if (mPointerSimple.hovering && !hovering) {
5368 mPointerSimple.hovering = false;
5369
5370 // Send hover exit.
5371 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5372 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005373 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005374 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5375 mOrientedXPrecision, mOrientedYPrecision,
5376 mPointerSimple.downTime);
5377 getListener()->notifyMotion(&args);
5378 }
5379
5380 if (down) {
5381 if (!mPointerSimple.down) {
5382 mPointerSimple.down = true;
5383 mPointerSimple.downTime = when;
5384
5385 // Send down.
5386 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5387 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005388 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005389 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5390 mOrientedXPrecision, mOrientedYPrecision,
5391 mPointerSimple.downTime);
5392 getListener()->notifyMotion(&args);
5393 }
5394
5395 // Send move.
5396 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5397 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005398 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005399 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5400 mOrientedXPrecision, mOrientedYPrecision,
5401 mPointerSimple.downTime);
5402 getListener()->notifyMotion(&args);
5403 }
5404
5405 if (hovering) {
5406 if (!mPointerSimple.hovering) {
5407 mPointerSimple.hovering = true;
5408
5409 // Send hover enter.
5410 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5411 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005412 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005413 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5414 mOrientedXPrecision, mOrientedYPrecision,
5415 mPointerSimple.downTime);
5416 getListener()->notifyMotion(&args);
5417 }
5418
5419 // Send hover move.
5420 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5421 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005422 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005423 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5424 mOrientedXPrecision, mOrientedYPrecision,
5425 mPointerSimple.downTime);
5426 getListener()->notifyMotion(&args);
5427 }
5428
5429 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5430 float vscroll = mCurrentRawVScroll;
5431 float hscroll = mCurrentRawHScroll;
5432 mWheelYVelocityControl.move(when, NULL, &vscroll);
5433 mWheelXVelocityControl.move(when, &hscroll, NULL);
5434
5435 // Send scroll.
5436 PointerCoords pointerCoords;
5437 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5438 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5439 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5440
5441 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5442 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005443 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005444 1, &mPointerSimple.currentProperties, &pointerCoords,
5445 mOrientedXPrecision, mOrientedYPrecision,
5446 mPointerSimple.downTime);
5447 getListener()->notifyMotion(&args);
5448 }
5449
5450 // Save state.
5451 if (down || hovering) {
5452 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5453 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5454 } else {
5455 mPointerSimple.reset();
5456 }
5457}
5458
5459void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5460 mPointerSimple.currentCoords.clear();
5461 mPointerSimple.currentProperties.clear();
5462
5463 dispatchPointerSimple(when, policyFlags, false, false);
5464}
5465
Jeff Brownace13b12011-03-09 17:39:48 -08005466void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005467 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5468 const PointerProperties* properties, const PointerCoords* coords,
5469 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005470 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5471 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005472 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005473 uint32_t pointerCount = 0;
5474 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005475 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005476 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005477 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005478 pointerCoords[pointerCount].copyFrom(coords[index]);
5479
5480 if (changedId >= 0 && id == uint32_t(changedId)) {
5481 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5482 }
5483
5484 pointerCount += 1;
5485 }
5486
Steve Blockec193de2012-01-09 18:35:44 +00005487 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005488
5489 if (changedId >= 0 && pointerCount == 1) {
5490 // Replace initial down and final up action.
5491 // We can compare the action without masking off the changed pointer index
5492 // because we know the index is 0.
5493 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5494 action = AMOTION_EVENT_ACTION_DOWN;
5495 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5496 action = AMOTION_EVENT_ACTION_UP;
5497 } else {
5498 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005499 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005500 }
5501 }
5502
Jeff Brownbe1aa822011-07-27 16:04:54 -07005503 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005504 action, flags, metaState, buttonState, edgeFlags,
Jeff Brown83d616a2012-09-09 20:33:43 -07005505 mViewport.displayId, pointerCount, pointerProperties, pointerCoords,
5506 xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005507 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005508}
5509
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005510bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005511 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005512 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5513 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005514 bool changed = false;
5515 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005516 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005517 uint32_t inIndex = inIdToIndex[id];
5518 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005519
5520 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005521 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005522 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005523 PointerCoords& curOutCoords = outCoords[outIndex];
5524
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005525 if (curInProperties != curOutProperties) {
5526 curOutProperties.copyFrom(curInProperties);
5527 changed = true;
5528 }
5529
Jeff Brownace13b12011-03-09 17:39:48 -08005530 if (curInCoords != curOutCoords) {
5531 curOutCoords.copyFrom(curInCoords);
5532 changed = true;
5533 }
5534 }
5535 return changed;
5536}
5537
5538void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005539 if (mPointerController != NULL) {
5540 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5541 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005542}
5543
Jeff Brownbe1aa822011-07-27 16:04:54 -07005544bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5545 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5546 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005547}
5548
Jeff Brownbe1aa822011-07-27 16:04:54 -07005549const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005550 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005551 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005552 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005553 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005554
5555#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005556 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005557 "left=%d, top=%d, right=%d, bottom=%d",
5558 x, y,
5559 virtualKey.keyCode, virtualKey.scanCode,
5560 virtualKey.hitLeft, virtualKey.hitTop,
5561 virtualKey.hitRight, virtualKey.hitBottom);
5562#endif
5563
5564 if (virtualKey.isHit(x, y)) {
5565 return & virtualKey;
5566 }
5567 }
5568
5569 return NULL;
5570}
5571
Jeff Brownbe1aa822011-07-27 16:04:54 -07005572void TouchInputMapper::assignPointerIds() {
5573 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5574 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5575
5576 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005577
5578 if (currentPointerCount == 0) {
5579 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005580 return;
5581 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005582
Jeff Brownbe1aa822011-07-27 16:04:54 -07005583 if (lastPointerCount == 0) {
5584 // All pointers are new.
5585 for (uint32_t i = 0; i < currentPointerCount; i++) {
5586 uint32_t id = i;
5587 mCurrentRawPointerData.pointers[i].id = id;
5588 mCurrentRawPointerData.idToIndex[id] = i;
5589 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5590 }
5591 return;
5592 }
5593
5594 if (currentPointerCount == 1 && lastPointerCount == 1
5595 && mCurrentRawPointerData.pointers[0].toolType
5596 == mLastRawPointerData.pointers[0].toolType) {
5597 // Only one pointer and no change in count so it must have the same id as before.
5598 uint32_t id = mLastRawPointerData.pointers[0].id;
5599 mCurrentRawPointerData.pointers[0].id = id;
5600 mCurrentRawPointerData.idToIndex[id] = 0;
5601 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5602 return;
5603 }
5604
5605 // General case.
5606 // We build a heap of squared euclidean distances between current and last pointers
5607 // associated with the current and last pointer indices. Then, we find the best
5608 // match (by distance) for each current pointer.
5609 // The pointers must have the same tool type but it is possible for them to
5610 // transition from hovering to touching or vice-versa while retaining the same id.
5611 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5612
5613 uint32_t heapSize = 0;
5614 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5615 currentPointerIndex++) {
5616 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5617 lastPointerIndex++) {
5618 const RawPointerData::Pointer& currentPointer =
5619 mCurrentRawPointerData.pointers[currentPointerIndex];
5620 const RawPointerData::Pointer& lastPointer =
5621 mLastRawPointerData.pointers[lastPointerIndex];
5622 if (currentPointer.toolType == lastPointer.toolType) {
5623 int64_t deltaX = currentPointer.x - lastPointer.x;
5624 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005625
5626 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5627
5628 // Insert new element into the heap (sift up).
5629 heap[heapSize].currentPointerIndex = currentPointerIndex;
5630 heap[heapSize].lastPointerIndex = lastPointerIndex;
5631 heap[heapSize].distance = distance;
5632 heapSize += 1;
5633 }
5634 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005635 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005636
Jeff Brownbe1aa822011-07-27 16:04:54 -07005637 // Heapify
5638 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5639 startIndex -= 1;
5640 for (uint32_t parentIndex = startIndex; ;) {
5641 uint32_t childIndex = parentIndex * 2 + 1;
5642 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005643 break;
5644 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005645
5646 if (childIndex + 1 < heapSize
5647 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5648 childIndex += 1;
5649 }
5650
5651 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5652 break;
5653 }
5654
5655 swap(heap[parentIndex], heap[childIndex]);
5656 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005657 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005658 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005659
5660#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005661 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005662 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005663 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005664 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5665 heap[i].distance);
5666 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005667#endif
5668
Jeff Brownbe1aa822011-07-27 16:04:54 -07005669 // Pull matches out by increasing order of distance.
5670 // To avoid reassigning pointers that have already been matched, the loop keeps track
5671 // of which last and current pointers have been matched using the matchedXXXBits variables.
5672 // It also tracks the used pointer id bits.
5673 BitSet32 matchedLastBits(0);
5674 BitSet32 matchedCurrentBits(0);
5675 BitSet32 usedIdBits(0);
5676 bool first = true;
5677 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5678 while (heapSize > 0) {
5679 if (first) {
5680 // The first time through the loop, we just consume the root element of
5681 // the heap (the one with smallest distance).
5682 first = false;
5683 } else {
5684 // Previous iterations consumed the root element of the heap.
5685 // Pop root element off of the heap (sift down).
5686 heap[0] = heap[heapSize];
5687 for (uint32_t parentIndex = 0; ;) {
5688 uint32_t childIndex = parentIndex * 2 + 1;
5689 if (childIndex >= heapSize) {
5690 break;
5691 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005692
Jeff Brownbe1aa822011-07-27 16:04:54 -07005693 if (childIndex + 1 < heapSize
5694 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5695 childIndex += 1;
5696 }
5697
5698 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5699 break;
5700 }
5701
5702 swap(heap[parentIndex], heap[childIndex]);
5703 parentIndex = childIndex;
5704 }
5705
5706#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005707 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005708 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005709 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005710 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5711 heap[i].distance);
5712 }
5713#endif
5714 }
5715
5716 heapSize -= 1;
5717
5718 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5719 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5720
5721 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5722 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5723
5724 matchedCurrentBits.markBit(currentPointerIndex);
5725 matchedLastBits.markBit(lastPointerIndex);
5726
5727 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5728 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5729 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5730 mCurrentRawPointerData.markIdBit(id,
5731 mCurrentRawPointerData.isHovering(currentPointerIndex));
5732 usedIdBits.markBit(id);
5733
5734#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005735 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005736 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5737#endif
5738 break;
5739 }
5740 }
5741
5742 // Assign fresh ids to pointers that were not matched in the process.
5743 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5744 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5745 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5746
5747 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5748 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5749 mCurrentRawPointerData.markIdBit(id,
5750 mCurrentRawPointerData.isHovering(currentPointerIndex));
5751
5752#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005753 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005754 currentPointerIndex, id);
5755#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005756 }
5757}
5758
Jeff Brown6d0fec22010-07-23 21:28:06 -07005759int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005760 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5761 return AKEY_STATE_VIRTUAL;
5762 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005763
Jeff Brownbe1aa822011-07-27 16:04:54 -07005764 size_t numVirtualKeys = mVirtualKeys.size();
5765 for (size_t i = 0; i < numVirtualKeys; i++) {
5766 const VirtualKey& virtualKey = mVirtualKeys[i];
5767 if (virtualKey.keyCode == keyCode) {
5768 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005769 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005770 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005771
5772 return AKEY_STATE_UNKNOWN;
5773}
5774
5775int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005776 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5777 return AKEY_STATE_VIRTUAL;
5778 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005779
Jeff Brownbe1aa822011-07-27 16:04:54 -07005780 size_t numVirtualKeys = mVirtualKeys.size();
5781 for (size_t i = 0; i < numVirtualKeys; i++) {
5782 const VirtualKey& virtualKey = mVirtualKeys[i];
5783 if (virtualKey.scanCode == scanCode) {
5784 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005785 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005786 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005787
5788 return AKEY_STATE_UNKNOWN;
5789}
5790
5791bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5792 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005793 size_t numVirtualKeys = mVirtualKeys.size();
5794 for (size_t i = 0; i < numVirtualKeys; i++) {
5795 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005796
Jeff Brownbe1aa822011-07-27 16:04:54 -07005797 for (size_t i = 0; i < numCodes; i++) {
5798 if (virtualKey.keyCode == keyCodes[i]) {
5799 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005800 }
5801 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005802 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005803
5804 return true;
5805}
5806
5807
5808// --- SingleTouchInputMapper ---
5809
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005810SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5811 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005812}
5813
5814SingleTouchInputMapper::~SingleTouchInputMapper() {
5815}
5816
Jeff Brown65fd2512011-08-18 11:20:58 -07005817void SingleTouchInputMapper::reset(nsecs_t when) {
5818 mSingleTouchMotionAccumulator.reset(getDevice());
5819
5820 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005821}
5822
Jeff Brown6d0fec22010-07-23 21:28:06 -07005823void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005824 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005825
Jeff Brown65fd2512011-08-18 11:20:58 -07005826 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005827}
5828
Jeff Brown65fd2512011-08-18 11:20:58 -07005829void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005830 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005831 mCurrentRawPointerData.pointerCount = 1;
5832 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005833
Jeff Brown65fd2512011-08-18 11:20:58 -07005834 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5835 && (mTouchButtonAccumulator.isHovering()
5836 || (mRawPointerAxes.pressure.valid
5837 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005838 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005839
Jeff Brownbe1aa822011-07-27 16:04:54 -07005840 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005841 outPointer.id = 0;
5842 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5843 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5844 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5845 outPointer.touchMajor = 0;
5846 outPointer.touchMinor = 0;
5847 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5848 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5849 outPointer.orientation = 0;
5850 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005851 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5852 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005853 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5854 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5855 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5856 }
5857 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005858 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005859}
5860
Jeff Brownbe1aa822011-07-27 16:04:54 -07005861void SingleTouchInputMapper::configureRawPointerAxes() {
5862 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005863
Jeff Brownbe1aa822011-07-27 16:04:54 -07005864 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5865 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5866 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5867 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5868 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005869 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5870 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005871}
5872
Jeff Brown00710e92012-04-19 15:18:26 -07005873bool SingleTouchInputMapper::hasStylus() const {
5874 return mTouchButtonAccumulator.hasStylus();
5875}
5876
Jeff Brown6d0fec22010-07-23 21:28:06 -07005877
5878// --- MultiTouchInputMapper ---
5879
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005880MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005881 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005882}
5883
5884MultiTouchInputMapper::~MultiTouchInputMapper() {
5885}
5886
Jeff Brown65fd2512011-08-18 11:20:58 -07005887void MultiTouchInputMapper::reset(nsecs_t when) {
5888 mMultiTouchMotionAccumulator.reset(getDevice());
5889
Jeff Brown6894a292011-07-01 17:59:27 -07005890 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005891
Jeff Brown65fd2512011-08-18 11:20:58 -07005892 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005893}
5894
5895void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005896 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005897
Jeff Brown65fd2512011-08-18 11:20:58 -07005898 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005899}
5900
Jeff Brown65fd2512011-08-18 11:20:58 -07005901void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005902 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005903 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005904 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005905
Jeff Brown80fd47c2011-05-24 01:07:44 -07005906 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005907 const MultiTouchMotionAccumulator::Slot* inSlot =
5908 mMultiTouchMotionAccumulator.getSlot(inIndex);
5909 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005910 continue;
5911 }
5912
Jeff Brown80fd47c2011-05-24 01:07:44 -07005913 if (outCount >= MAX_POINTERS) {
5914#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005915 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005916 "ignoring the rest.",
5917 getDeviceName().string(), MAX_POINTERS);
5918#endif
5919 break; // too many fingers!
5920 }
5921
Jeff Brownbe1aa822011-07-27 16:04:54 -07005922 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005923 outPointer.x = inSlot->getX();
5924 outPointer.y = inSlot->getY();
5925 outPointer.pressure = inSlot->getPressure();
5926 outPointer.touchMajor = inSlot->getTouchMajor();
5927 outPointer.touchMinor = inSlot->getTouchMinor();
5928 outPointer.toolMajor = inSlot->getToolMajor();
5929 outPointer.toolMinor = inSlot->getToolMinor();
5930 outPointer.orientation = inSlot->getOrientation();
5931 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005932 outPointer.tiltX = 0;
5933 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005934
Jeff Brown49754db2011-07-01 17:37:58 -07005935 outPointer.toolType = inSlot->getToolType();
5936 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5937 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5938 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5939 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5940 }
Jeff Brown8d608662010-08-30 03:02:23 -07005941 }
5942
Jeff Brown65fd2512011-08-18 11:20:58 -07005943 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5944 && (mTouchButtonAccumulator.isHovering()
5945 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005946 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005947
Jeff Brown8d608662010-08-30 03:02:23 -07005948 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005949 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005950 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005951 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005952 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005953 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005954 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005955 if (mPointerTrackingIdMap[n] == trackingId) {
5956 id = n;
5957 }
5958 }
5959
5960 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005961 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005962 mPointerTrackingIdMap[id] = trackingId;
5963 }
5964 }
5965 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005966 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005967 mCurrentRawPointerData.clearIdBits();
5968 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005969 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005970 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005971 mCurrentRawPointerData.idToIndex[id] = outCount;
5972 mCurrentRawPointerData.markIdBit(id, isHovering);
5973 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005974 }
5975 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005976
Jeff Brown6d0fec22010-07-23 21:28:06 -07005977 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005978 }
5979
Jeff Brownbe1aa822011-07-27 16:04:54 -07005980 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005981 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005982
Jeff Brown65fd2512011-08-18 11:20:58 -07005983 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005984}
5985
Jeff Brownbe1aa822011-07-27 16:04:54 -07005986void MultiTouchInputMapper::configureRawPointerAxes() {
5987 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005988
Jeff Brownbe1aa822011-07-27 16:04:54 -07005989 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5990 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5991 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5992 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5993 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5994 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5995 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5996 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5997 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5998 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5999 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006000
Jeff Brownbe1aa822011-07-27 16:04:54 -07006001 if (mRawPointerAxes.trackingId.valid
6002 && mRawPointerAxes.slot.valid
6003 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
6004 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07006005 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00006006 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006007 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07006008 getDeviceName().string(), slotCount, MAX_SLOTS);
6009 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07006010 }
Jeff Brown00710e92012-04-19 15:18:26 -07006011 mMultiTouchMotionAccumulator.configure(getDevice(),
6012 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006013 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07006014 mMultiTouchMotionAccumulator.configure(getDevice(),
6015 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006016 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07006017}
6018
Jeff Brown00710e92012-04-19 15:18:26 -07006019bool MultiTouchInputMapper::hasStylus() const {
6020 return mMultiTouchMotionAccumulator.hasStylus()
6021 || mTouchButtonAccumulator.hasStylus();
6022}
6023
Jeff Brown46b9ac02010-04-22 18:58:52 -07006024
Jeff Browncb1404e2011-01-15 18:14:15 -08006025// --- JoystickInputMapper ---
6026
6027JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
6028 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006029}
6030
6031JoystickInputMapper::~JoystickInputMapper() {
6032}
6033
6034uint32_t JoystickInputMapper::getSources() {
6035 return AINPUT_SOURCE_JOYSTICK;
6036}
6037
6038void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
6039 InputMapper::populateDeviceInfo(info);
6040
Jeff Brown6f2fba42011-02-19 01:08:02 -08006041 for (size_t i = 0; i < mAxes.size(); i++) {
6042 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08006043 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
6044 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08006045 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08006046 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
6047 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08006048 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006049 }
6050}
6051
6052void JoystickInputMapper::dump(String8& dump) {
6053 dump.append(INDENT2 "Joystick Input Mapper:\n");
6054
Jeff Brown6f2fba42011-02-19 01:08:02 -08006055 dump.append(INDENT3 "Axes:\n");
6056 size_t numAxes = mAxes.size();
6057 for (size_t i = 0; i < numAxes; i++) {
6058 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006059 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006060 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08006061 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006062 } else {
Jeff Brown85297452011-03-04 13:07:49 -08006063 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006064 }
Jeff Brown85297452011-03-04 13:07:49 -08006065 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6066 label = getAxisLabel(axis.axisInfo.highAxis);
6067 if (label) {
6068 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
6069 } else {
6070 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
6071 axis.axisInfo.splitValue);
6072 }
6073 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
6074 dump.append(" (invert)");
6075 }
6076
6077 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
6078 axis.min, axis.max, axis.flat, axis.fuzz);
6079 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
6080 "highScale=%0.5f, highOffset=%0.5f\n",
6081 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07006082 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
6083 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006084 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006085 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006086 }
6087}
6088
Jeff Brown65fd2512011-08-18 11:20:58 -07006089void JoystickInputMapper::configure(nsecs_t when,
6090 const InputReaderConfiguration* config, uint32_t changes) {
6091 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006092
Jeff Brown474dcb52011-06-14 20:22:50 -07006093 if (!changes) { // first time only
6094 // Collect all axes.
6095 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285a2011-08-31 12:56:34 -07006096 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6097 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6098 continue; // axis must be claimed by a different device
6099 }
6100
Jeff Brown474dcb52011-06-14 20:22:50 -07006101 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006102 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006103 if (rawAxisInfo.valid) {
6104 // Map axis.
6105 AxisInfo axisInfo;
6106 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6107 if (!explicitlyMapped) {
6108 // Axis is not explicitly mapped, will choose a generic axis later.
6109 axisInfo.mode = AxisInfo::MODE_NORMAL;
6110 axisInfo.axis = -1;
6111 }
6112
6113 // Apply flat override.
6114 int32_t rawFlat = axisInfo.flatOverride < 0
6115 ? rawAxisInfo.flat : axisInfo.flatOverride;
6116
6117 // Calculate scaling factors and limits.
6118 Axis axis;
6119 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6120 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6121 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6122 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6123 scale, 0.0f, highScale, 0.0f,
6124 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6125 } else if (isCenteredAxis(axisInfo.axis)) {
6126 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6127 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6128 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6129 scale, offset, scale, offset,
6130 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6131 } else {
6132 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6133 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6134 scale, 0.0f, scale, 0.0f,
6135 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6136 }
6137
6138 // To eliminate noise while the joystick is at rest, filter out small variations
6139 // in axis values up front.
6140 axis.filter = axis.flat * 0.25f;
6141
6142 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006143 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006144 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006145
Jeff Brown474dcb52011-06-14 20:22:50 -07006146 // If there are too many axes, start dropping them.
6147 // Prefer to keep explicitly mapped axes.
6148 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006149 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006150 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6151 pruneAxes(true);
6152 pruneAxes(false);
6153 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006154
Jeff Brown474dcb52011-06-14 20:22:50 -07006155 // Assign generic axis ids to remaining axes.
6156 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6157 size_t numAxes = mAxes.size();
6158 for (size_t i = 0; i < numAxes; i++) {
6159 Axis& axis = mAxes.editValueAt(i);
6160 if (axis.axisInfo.axis < 0) {
6161 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6162 && haveAxis(nextGenericAxisId)) {
6163 nextGenericAxisId += 1;
6164 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006165
Jeff Brown474dcb52011-06-14 20:22:50 -07006166 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6167 axis.axisInfo.axis = nextGenericAxisId;
6168 nextGenericAxisId += 1;
6169 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006170 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006171 "have already been assigned to other axes.",
6172 getDeviceName().string(), mAxes.keyAt(i));
6173 mAxes.removeItemsAt(i--);
6174 numAxes -= 1;
6175 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006176 }
6177 }
6178 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006179}
6180
Jeff Brown85297452011-03-04 13:07:49 -08006181bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006182 size_t numAxes = mAxes.size();
6183 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006184 const Axis& axis = mAxes.valueAt(i);
6185 if (axis.axisInfo.axis == axisId
6186 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6187 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006188 return true;
6189 }
6190 }
6191 return false;
6192}
Jeff Browncb1404e2011-01-15 18:14:15 -08006193
Jeff Brown6f2fba42011-02-19 01:08:02 -08006194void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6195 size_t i = mAxes.size();
6196 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6197 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6198 continue;
6199 }
Steve Block6215d3f2012-01-04 20:05:49 +00006200 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006201 getDeviceName().string(), mAxes.keyAt(i));
6202 mAxes.removeItemsAt(i);
6203 }
6204}
6205
6206bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6207 switch (axis) {
6208 case AMOTION_EVENT_AXIS_X:
6209 case AMOTION_EVENT_AXIS_Y:
6210 case AMOTION_EVENT_AXIS_Z:
6211 case AMOTION_EVENT_AXIS_RX:
6212 case AMOTION_EVENT_AXIS_RY:
6213 case AMOTION_EVENT_AXIS_RZ:
6214 case AMOTION_EVENT_AXIS_HAT_X:
6215 case AMOTION_EVENT_AXIS_HAT_Y:
6216 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006217 case AMOTION_EVENT_AXIS_RUDDER:
6218 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006219 return true;
6220 default:
6221 return false;
6222 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006223}
6224
Jeff Brown65fd2512011-08-18 11:20:58 -07006225void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006226 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006227 size_t numAxes = mAxes.size();
6228 for (size_t i = 0; i < numAxes; i++) {
6229 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006230 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006231 }
6232
Jeff Brown65fd2512011-08-18 11:20:58 -07006233 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006234}
6235
6236void JoystickInputMapper::process(const RawEvent* rawEvent) {
6237 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006238 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006239 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006240 if (index >= 0) {
6241 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006242 float newValue, highNewValue;
6243 switch (axis.axisInfo.mode) {
6244 case AxisInfo::MODE_INVERT:
6245 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6246 * axis.scale + axis.offset;
6247 highNewValue = 0.0f;
6248 break;
6249 case AxisInfo::MODE_SPLIT:
6250 if (rawEvent->value < axis.axisInfo.splitValue) {
6251 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6252 * axis.scale + axis.offset;
6253 highNewValue = 0.0f;
6254 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6255 newValue = 0.0f;
6256 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6257 * axis.highScale + axis.highOffset;
6258 } else {
6259 newValue = 0.0f;
6260 highNewValue = 0.0f;
6261 }
6262 break;
6263 default:
6264 newValue = rawEvent->value * axis.scale + axis.offset;
6265 highNewValue = 0.0f;
6266 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006267 }
Jeff Brown85297452011-03-04 13:07:49 -08006268 axis.newValue = newValue;
6269 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006270 }
6271 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006272 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006273
6274 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006275 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006276 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006277 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006278 break;
6279 }
6280 break;
6281 }
6282}
6283
Jeff Brown6f2fba42011-02-19 01:08:02 -08006284void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006285 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006286 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006287 }
6288
6289 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006290 int32_t buttonState = 0;
6291
6292 PointerProperties pointerProperties;
6293 pointerProperties.clear();
6294 pointerProperties.id = 0;
6295 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006296
Jeff Brown6f2fba42011-02-19 01:08:02 -08006297 PointerCoords pointerCoords;
6298 pointerCoords.clear();
6299
6300 size_t numAxes = mAxes.size();
6301 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006302 const Axis& axis = mAxes.valueAt(i);
6303 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6304 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6305 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6306 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006307 }
6308
Jeff Brown83d616a2012-09-09 20:33:43 -07006309 // Moving a joystick axis should not wake the device because joysticks can
Jeff Brown56194eb2011-03-02 19:23:13 -08006310 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6311 // button will likely wake the device.
6312 // TODO: Use the input device configuration to control this behavior more finely.
6313 uint32_t policyFlags = 0;
6314
Jeff Brownbe1aa822011-07-27 16:04:54 -07006315 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006316 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07006317 ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006318 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006319}
6320
Jeff Brown85297452011-03-04 13:07:49 -08006321bool JoystickInputMapper::filterAxes(bool force) {
6322 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006323 size_t numAxes = mAxes.size();
6324 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006325 Axis& axis = mAxes.editValueAt(i);
6326 if (force || hasValueChangedSignificantly(axis.filter,
6327 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6328 axis.currentValue = axis.newValue;
6329 atLeastOneSignificantChange = true;
6330 }
6331 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6332 if (force || hasValueChangedSignificantly(axis.filter,
6333 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6334 axis.highCurrentValue = axis.highNewValue;
6335 atLeastOneSignificantChange = true;
6336 }
6337 }
6338 }
6339 return atLeastOneSignificantChange;
6340}
6341
6342bool JoystickInputMapper::hasValueChangedSignificantly(
6343 float filter, float newValue, float currentValue, float min, float max) {
6344 if (newValue != currentValue) {
6345 // Filter out small changes in value unless the value is converging on the axis
6346 // bounds or center point. This is intended to reduce the amount of information
6347 // sent to applications by particularly noisy joysticks (such as PS3).
6348 if (fabs(newValue - currentValue) > filter
6349 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6350 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6351 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6352 return true;
6353 }
6354 }
6355 return false;
6356}
6357
6358bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6359 float filter, float newValue, float currentValue, float thresholdValue) {
6360 float newDistance = fabs(newValue - thresholdValue);
6361 if (newDistance < filter) {
6362 float oldDistance = fabs(currentValue - thresholdValue);
6363 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006364 return true;
6365 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006366 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006367 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006368}
6369
Jeff Brown46b9ac02010-04-22 18:58:52 -07006370} // namespace android