blob: cebfeb4dc389805ef294c4cac0a142ba2083c1a9 [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 Brown49ccac52012-04-11 18:27:33 -0700959 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
960 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
Jeff Brownb7198742011-03-18 18:14:26 -0700961#endif
962
Jeff Brown80fd47c2011-05-24 01:07:44 -0700963 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700964 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700965 mDropUntilNextSync = false;
966#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000967 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700968#endif
969 } else {
970#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000971 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700972#endif
973 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700974 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700975 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700976 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700977 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700978 } else {
979 for (size_t i = 0; i < numMappers; i++) {
980 InputMapper* mapper = mMappers[i];
981 mapper->process(rawEvent);
982 }
Jeff Brownb7198742011-03-18 18:14:26 -0700983 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700984 }
985}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700986
Jeff Brownaa3855d2011-03-17 01:34:19 -0700987void InputDevice::timeoutExpired(nsecs_t when) {
988 size_t numMappers = mMappers.size();
989 for (size_t i = 0; i < numMappers; i++) {
990 InputMapper* mapper = mMappers[i];
991 mapper->timeoutExpired(when);
992 }
993}
994
Jeff Brown6d0fec22010-07-23 21:28:06 -0700995void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Browndaa37532012-05-01 15:54:03 -0700996 outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700997
998 size_t numMappers = mMappers.size();
999 for (size_t i = 0; i < numMappers; i++) {
1000 InputMapper* mapper = mMappers[i];
1001 mapper->populateDeviceInfo(outDeviceInfo);
1002 }
1003}
1004
1005int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1006 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1007}
1008
1009int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1010 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1011}
1012
1013int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1014 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1015}
1016
1017int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1018 int32_t result = AKEY_STATE_UNKNOWN;
1019 size_t numMappers = mMappers.size();
1020 for (size_t i = 0; i < numMappers; i++) {
1021 InputMapper* mapper = mMappers[i];
1022 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001023 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1024 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1025 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1026 if (currentResult >= AKEY_STATE_DOWN) {
1027 return currentResult;
1028 } else if (currentResult == AKEY_STATE_UP) {
1029 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001030 }
1031 }
1032 }
1033 return result;
1034}
1035
1036bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1037 const int32_t* keyCodes, uint8_t* outFlags) {
1038 bool result = false;
1039 size_t numMappers = mMappers.size();
1040 for (size_t i = 0; i < numMappers; i++) {
1041 InputMapper* mapper = mMappers[i];
1042 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1043 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1044 }
1045 }
1046 return result;
1047}
1048
Jeff Browna47425a2012-04-13 04:09:27 -07001049void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1050 int32_t token) {
1051 size_t numMappers = mMappers.size();
1052 for (size_t i = 0; i < numMappers; i++) {
1053 InputMapper* mapper = mMappers[i];
1054 mapper->vibrate(pattern, patternSize, repeat, token);
1055 }
1056}
1057
1058void InputDevice::cancelVibrate(int32_t token) {
1059 size_t numMappers = mMappers.size();
1060 for (size_t i = 0; i < numMappers; i++) {
1061 InputMapper* mapper = mMappers[i];
1062 mapper->cancelVibrate(token);
1063 }
1064}
1065
Jeff Brown6d0fec22010-07-23 21:28:06 -07001066int32_t InputDevice::getMetaState() {
1067 int32_t result = 0;
1068 size_t numMappers = mMappers.size();
1069 for (size_t i = 0; i < numMappers; i++) {
1070 InputMapper* mapper = mMappers[i];
1071 result |= mapper->getMetaState();
1072 }
1073 return result;
1074}
1075
Jeff Brown05dc66a2011-03-02 14:41:58 -08001076void InputDevice::fadePointer() {
1077 size_t numMappers = mMappers.size();
1078 for (size_t i = 0; i < numMappers; i++) {
1079 InputMapper* mapper = mMappers[i];
1080 mapper->fadePointer();
1081 }
1082}
1083
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001084void InputDevice::bumpGeneration() {
1085 mGeneration = mContext->bumpGeneration();
1086}
1087
Jeff Brown65fd2512011-08-18 11:20:58 -07001088void InputDevice::notifyReset(nsecs_t when) {
1089 NotifyDeviceResetArgs args(when, mId);
1090 mContext->getListener()->notifyDeviceReset(&args);
1091}
1092
Jeff Brown6d0fec22010-07-23 21:28:06 -07001093
Jeff Brown49754db2011-07-01 17:37:58 -07001094// --- CursorButtonAccumulator ---
1095
1096CursorButtonAccumulator::CursorButtonAccumulator() {
1097 clearButtons();
1098}
1099
Jeff Brown65fd2512011-08-18 11:20:58 -07001100void CursorButtonAccumulator::reset(InputDevice* device) {
1101 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1102 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1103 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1104 mBtnBack = device->isKeyPressed(BTN_BACK);
1105 mBtnSide = device->isKeyPressed(BTN_SIDE);
1106 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1107 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1108 mBtnTask = device->isKeyPressed(BTN_TASK);
1109}
1110
Jeff Brown49754db2011-07-01 17:37:58 -07001111void CursorButtonAccumulator::clearButtons() {
1112 mBtnLeft = 0;
1113 mBtnRight = 0;
1114 mBtnMiddle = 0;
1115 mBtnBack = 0;
1116 mBtnSide = 0;
1117 mBtnForward = 0;
1118 mBtnExtra = 0;
1119 mBtnTask = 0;
1120}
1121
1122void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1123 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001124 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001125 case BTN_LEFT:
1126 mBtnLeft = rawEvent->value;
1127 break;
1128 case BTN_RIGHT:
1129 mBtnRight = rawEvent->value;
1130 break;
1131 case BTN_MIDDLE:
1132 mBtnMiddle = rawEvent->value;
1133 break;
1134 case BTN_BACK:
1135 mBtnBack = rawEvent->value;
1136 break;
1137 case BTN_SIDE:
1138 mBtnSide = rawEvent->value;
1139 break;
1140 case BTN_FORWARD:
1141 mBtnForward = rawEvent->value;
1142 break;
1143 case BTN_EXTRA:
1144 mBtnExtra = rawEvent->value;
1145 break;
1146 case BTN_TASK:
1147 mBtnTask = rawEvent->value;
1148 break;
1149 }
1150 }
1151}
1152
1153uint32_t CursorButtonAccumulator::getButtonState() const {
1154 uint32_t result = 0;
1155 if (mBtnLeft) {
1156 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1157 }
1158 if (mBtnRight) {
1159 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1160 }
1161 if (mBtnMiddle) {
1162 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1163 }
1164 if (mBtnBack || mBtnSide) {
1165 result |= AMOTION_EVENT_BUTTON_BACK;
1166 }
1167 if (mBtnForward || mBtnExtra) {
1168 result |= AMOTION_EVENT_BUTTON_FORWARD;
1169 }
1170 return result;
1171}
1172
1173
1174// --- CursorMotionAccumulator ---
1175
Jeff Brown65fd2512011-08-18 11:20:58 -07001176CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001177 clearRelativeAxes();
1178}
1179
Jeff Brown65fd2512011-08-18 11:20:58 -07001180void CursorMotionAccumulator::reset(InputDevice* device) {
1181 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001182}
1183
1184void CursorMotionAccumulator::clearRelativeAxes() {
1185 mRelX = 0;
1186 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001187}
1188
1189void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1190 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001191 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001192 case REL_X:
1193 mRelX = rawEvent->value;
1194 break;
1195 case REL_Y:
1196 mRelY = rawEvent->value;
1197 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001198 }
1199 }
1200}
1201
1202void CursorMotionAccumulator::finishSync() {
1203 clearRelativeAxes();
1204}
1205
1206
1207// --- CursorScrollAccumulator ---
1208
1209CursorScrollAccumulator::CursorScrollAccumulator() :
1210 mHaveRelWheel(false), mHaveRelHWheel(false) {
1211 clearRelativeAxes();
1212}
1213
1214void CursorScrollAccumulator::configure(InputDevice* device) {
1215 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1216 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1217}
1218
1219void CursorScrollAccumulator::reset(InputDevice* device) {
1220 clearRelativeAxes();
1221}
1222
1223void CursorScrollAccumulator::clearRelativeAxes() {
1224 mRelWheel = 0;
1225 mRelHWheel = 0;
1226}
1227
1228void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1229 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001230 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001231 case REL_WHEEL:
1232 mRelWheel = rawEvent->value;
1233 break;
1234 case REL_HWHEEL:
1235 mRelHWheel = rawEvent->value;
1236 break;
1237 }
1238 }
1239}
1240
Jeff Brown65fd2512011-08-18 11:20:58 -07001241void CursorScrollAccumulator::finishSync() {
1242 clearRelativeAxes();
1243}
1244
Jeff Brown49754db2011-07-01 17:37:58 -07001245
1246// --- TouchButtonAccumulator ---
1247
1248TouchButtonAccumulator::TouchButtonAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001249 mHaveBtnTouch(false), mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001250 clearButtons();
1251}
1252
1253void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001254 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
Jeff Brown00710e92012-04-19 15:18:26 -07001255 mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1256 || device->hasKey(BTN_TOOL_RUBBER)
1257 || device->hasKey(BTN_TOOL_BRUSH)
1258 || device->hasKey(BTN_TOOL_PENCIL)
1259 || device->hasKey(BTN_TOOL_AIRBRUSH);
Jeff Brown65fd2512011-08-18 11:20:58 -07001260}
1261
1262void TouchButtonAccumulator::reset(InputDevice* device) {
1263 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1264 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1265 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1266 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1267 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1268 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1269 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1270 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1271 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1272 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1273 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001274 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1275 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1276 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001277}
1278
1279void TouchButtonAccumulator::clearButtons() {
1280 mBtnTouch = 0;
1281 mBtnStylus = 0;
1282 mBtnStylus2 = 0;
1283 mBtnToolFinger = 0;
1284 mBtnToolPen = 0;
1285 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001286 mBtnToolBrush = 0;
1287 mBtnToolPencil = 0;
1288 mBtnToolAirbrush = 0;
1289 mBtnToolMouse = 0;
1290 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001291 mBtnToolDoubleTap = 0;
1292 mBtnToolTripleTap = 0;
1293 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001294}
1295
1296void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1297 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001298 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001299 case BTN_TOUCH:
1300 mBtnTouch = rawEvent->value;
1301 break;
1302 case BTN_STYLUS:
1303 mBtnStylus = rawEvent->value;
1304 break;
1305 case BTN_STYLUS2:
1306 mBtnStylus2 = rawEvent->value;
1307 break;
1308 case BTN_TOOL_FINGER:
1309 mBtnToolFinger = rawEvent->value;
1310 break;
1311 case BTN_TOOL_PEN:
1312 mBtnToolPen = rawEvent->value;
1313 break;
1314 case BTN_TOOL_RUBBER:
1315 mBtnToolRubber = rawEvent->value;
1316 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001317 case BTN_TOOL_BRUSH:
1318 mBtnToolBrush = rawEvent->value;
1319 break;
1320 case BTN_TOOL_PENCIL:
1321 mBtnToolPencil = rawEvent->value;
1322 break;
1323 case BTN_TOOL_AIRBRUSH:
1324 mBtnToolAirbrush = rawEvent->value;
1325 break;
1326 case BTN_TOOL_MOUSE:
1327 mBtnToolMouse = rawEvent->value;
1328 break;
1329 case BTN_TOOL_LENS:
1330 mBtnToolLens = rawEvent->value;
1331 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001332 case BTN_TOOL_DOUBLETAP:
1333 mBtnToolDoubleTap = rawEvent->value;
1334 break;
1335 case BTN_TOOL_TRIPLETAP:
1336 mBtnToolTripleTap = rawEvent->value;
1337 break;
1338 case BTN_TOOL_QUADTAP:
1339 mBtnToolQuadTap = rawEvent->value;
1340 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001341 }
1342 }
1343}
1344
1345uint32_t TouchButtonAccumulator::getButtonState() const {
1346 uint32_t result = 0;
1347 if (mBtnStylus) {
1348 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1349 }
1350 if (mBtnStylus2) {
1351 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1352 }
1353 return result;
1354}
1355
1356int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001357 if (mBtnToolMouse || mBtnToolLens) {
1358 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1359 }
Jeff Brown49754db2011-07-01 17:37:58 -07001360 if (mBtnToolRubber) {
1361 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1362 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001363 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001364 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1365 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001366 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001367 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1368 }
1369 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1370}
1371
Jeff Brownd87c6d52011-08-10 14:55:59 -07001372bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001373 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1374 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001375 || mBtnToolMouse || mBtnToolLens
1376 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001377}
1378
1379bool TouchButtonAccumulator::isHovering() const {
1380 return mHaveBtnTouch && !mBtnTouch;
1381}
1382
Jeff Brown00710e92012-04-19 15:18:26 -07001383bool TouchButtonAccumulator::hasStylus() const {
1384 return mHaveStylus;
1385}
1386
Jeff Brown49754db2011-07-01 17:37:58 -07001387
Jeff Brownbe1aa822011-07-27 16:04:54 -07001388// --- RawPointerAxes ---
1389
1390RawPointerAxes::RawPointerAxes() {
1391 clear();
1392}
1393
1394void RawPointerAxes::clear() {
1395 x.clear();
1396 y.clear();
1397 pressure.clear();
1398 touchMajor.clear();
1399 touchMinor.clear();
1400 toolMajor.clear();
1401 toolMinor.clear();
1402 orientation.clear();
1403 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001404 tiltX.clear();
1405 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001406 trackingId.clear();
1407 slot.clear();
1408}
1409
1410
1411// --- RawPointerData ---
1412
1413RawPointerData::RawPointerData() {
1414 clear();
1415}
1416
1417void RawPointerData::clear() {
1418 pointerCount = 0;
1419 clearIdBits();
1420}
1421
1422void RawPointerData::copyFrom(const RawPointerData& other) {
1423 pointerCount = other.pointerCount;
1424 hoveringIdBits = other.hoveringIdBits;
1425 touchingIdBits = other.touchingIdBits;
1426
1427 for (uint32_t i = 0; i < pointerCount; i++) {
1428 pointers[i] = other.pointers[i];
1429
1430 int id = pointers[i].id;
1431 idToIndex[id] = other.idToIndex[id];
1432 }
1433}
1434
1435void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1436 float x = 0, y = 0;
1437 uint32_t count = touchingIdBits.count();
1438 if (count) {
1439 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1440 uint32_t id = idBits.clearFirstMarkedBit();
1441 const Pointer& pointer = pointerForId(id);
1442 x += pointer.x;
1443 y += pointer.y;
1444 }
1445 x /= count;
1446 y /= count;
1447 }
1448 *outX = x;
1449 *outY = y;
1450}
1451
1452
1453// --- CookedPointerData ---
1454
1455CookedPointerData::CookedPointerData() {
1456 clear();
1457}
1458
1459void CookedPointerData::clear() {
1460 pointerCount = 0;
1461 hoveringIdBits.clear();
1462 touchingIdBits.clear();
1463}
1464
1465void CookedPointerData::copyFrom(const CookedPointerData& other) {
1466 pointerCount = other.pointerCount;
1467 hoveringIdBits = other.hoveringIdBits;
1468 touchingIdBits = other.touchingIdBits;
1469
1470 for (uint32_t i = 0; i < pointerCount; i++) {
1471 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1472 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1473
1474 int id = pointerProperties[i].id;
1475 idToIndex[id] = other.idToIndex[id];
1476 }
1477}
1478
1479
Jeff Brown49754db2011-07-01 17:37:58 -07001480// --- SingleTouchMotionAccumulator ---
1481
1482SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1483 clearAbsoluteAxes();
1484}
1485
Jeff Brown65fd2512011-08-18 11:20:58 -07001486void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1487 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1488 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1489 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1490 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1491 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1492 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1493 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1494}
1495
Jeff Brown49754db2011-07-01 17:37:58 -07001496void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1497 mAbsX = 0;
1498 mAbsY = 0;
1499 mAbsPressure = 0;
1500 mAbsToolWidth = 0;
1501 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001502 mAbsTiltX = 0;
1503 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001504}
1505
1506void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1507 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001508 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001509 case ABS_X:
1510 mAbsX = rawEvent->value;
1511 break;
1512 case ABS_Y:
1513 mAbsY = rawEvent->value;
1514 break;
1515 case ABS_PRESSURE:
1516 mAbsPressure = rawEvent->value;
1517 break;
1518 case ABS_TOOL_WIDTH:
1519 mAbsToolWidth = rawEvent->value;
1520 break;
1521 case ABS_DISTANCE:
1522 mAbsDistance = rawEvent->value;
1523 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001524 case ABS_TILT_X:
1525 mAbsTiltX = rawEvent->value;
1526 break;
1527 case ABS_TILT_Y:
1528 mAbsTiltY = rawEvent->value;
1529 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001530 }
1531 }
1532}
1533
1534
1535// --- MultiTouchMotionAccumulator ---
1536
1537MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001538 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1539 mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001540}
1541
1542MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1543 delete[] mSlots;
1544}
1545
Jeff Brown00710e92012-04-19 15:18:26 -07001546void MultiTouchMotionAccumulator::configure(InputDevice* device,
1547 size_t slotCount, bool usingSlotsProtocol) {
Jeff Brown49754db2011-07-01 17:37:58 -07001548 mSlotCount = slotCount;
1549 mUsingSlotsProtocol = usingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -07001550 mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Jeff Brown49754db2011-07-01 17:37:58 -07001551
1552 delete[] mSlots;
1553 mSlots = new Slot[slotCount];
1554}
1555
Jeff Brown65fd2512011-08-18 11:20:58 -07001556void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1557 // Unfortunately there is no way to read the initial contents of the slots.
1558 // So when we reset the accumulator, we must assume they are all zeroes.
1559 if (mUsingSlotsProtocol) {
1560 // Query the driver for the current slot index and use it as the initial slot
1561 // before we start reading events from the device. It is possible that the
1562 // current slot index will not be the same as it was when the first event was
1563 // written into the evdev buffer, which means the input mapper could start
1564 // out of sync with the initial state of the events in the evdev buffer.
1565 // In the extremely unlikely case that this happens, the data from
1566 // two slots will be confused until the next ABS_MT_SLOT event is received.
1567 // This can cause the touch point to "jump", but at least there will be
1568 // no stuck touches.
1569 int32_t initialSlot;
1570 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1571 ABS_MT_SLOT, &initialSlot);
1572 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001573 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001574 initialSlot = -1;
1575 }
1576 clearSlots(initialSlot);
1577 } else {
1578 clearSlots(-1);
1579 }
1580}
1581
Jeff Brown49754db2011-07-01 17:37:58 -07001582void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001583 if (mSlots) {
1584 for (size_t i = 0; i < mSlotCount; i++) {
1585 mSlots[i].clear();
1586 }
Jeff Brown49754db2011-07-01 17:37:58 -07001587 }
1588 mCurrentSlot = initialSlot;
1589}
1590
1591void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1592 if (rawEvent->type == EV_ABS) {
1593 bool newSlot = false;
1594 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001595 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001596 mCurrentSlot = rawEvent->value;
1597 newSlot = true;
1598 }
1599 } else if (mCurrentSlot < 0) {
1600 mCurrentSlot = 0;
1601 }
1602
1603 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1604#if DEBUG_POINTERS
1605 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001606 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001607 "should be between 0 and %d; ignoring this slot.",
1608 mCurrentSlot, mSlotCount - 1);
1609 }
1610#endif
1611 } else {
1612 Slot* slot = &mSlots[mCurrentSlot];
1613
Jeff Brown49ccac52012-04-11 18:27:33 -07001614 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001615 case ABS_MT_POSITION_X:
1616 slot->mInUse = true;
1617 slot->mAbsMTPositionX = rawEvent->value;
1618 break;
1619 case ABS_MT_POSITION_Y:
1620 slot->mInUse = true;
1621 slot->mAbsMTPositionY = rawEvent->value;
1622 break;
1623 case ABS_MT_TOUCH_MAJOR:
1624 slot->mInUse = true;
1625 slot->mAbsMTTouchMajor = rawEvent->value;
1626 break;
1627 case ABS_MT_TOUCH_MINOR:
1628 slot->mInUse = true;
1629 slot->mAbsMTTouchMinor = rawEvent->value;
1630 slot->mHaveAbsMTTouchMinor = true;
1631 break;
1632 case ABS_MT_WIDTH_MAJOR:
1633 slot->mInUse = true;
1634 slot->mAbsMTWidthMajor = rawEvent->value;
1635 break;
1636 case ABS_MT_WIDTH_MINOR:
1637 slot->mInUse = true;
1638 slot->mAbsMTWidthMinor = rawEvent->value;
1639 slot->mHaveAbsMTWidthMinor = true;
1640 break;
1641 case ABS_MT_ORIENTATION:
1642 slot->mInUse = true;
1643 slot->mAbsMTOrientation = rawEvent->value;
1644 break;
1645 case ABS_MT_TRACKING_ID:
1646 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001647 // The slot is no longer in use but it retains its previous contents,
1648 // which may be reused for subsequent touches.
1649 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001650 } else {
1651 slot->mInUse = true;
1652 slot->mAbsMTTrackingId = rawEvent->value;
1653 }
1654 break;
1655 case ABS_MT_PRESSURE:
1656 slot->mInUse = true;
1657 slot->mAbsMTPressure = rawEvent->value;
1658 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001659 case ABS_MT_DISTANCE:
1660 slot->mInUse = true;
1661 slot->mAbsMTDistance = rawEvent->value;
1662 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001663 case ABS_MT_TOOL_TYPE:
1664 slot->mInUse = true;
1665 slot->mAbsMTToolType = rawEvent->value;
1666 slot->mHaveAbsMTToolType = true;
1667 break;
1668 }
1669 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001670 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001671 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1672 mCurrentSlot += 1;
1673 }
1674}
1675
Jeff Brown65fd2512011-08-18 11:20:58 -07001676void MultiTouchMotionAccumulator::finishSync() {
1677 if (!mUsingSlotsProtocol) {
1678 clearSlots(-1);
1679 }
1680}
1681
Jeff Brown00710e92012-04-19 15:18:26 -07001682bool MultiTouchMotionAccumulator::hasStylus() const {
1683 return mHaveStylus;
1684}
1685
Jeff Brown49754db2011-07-01 17:37:58 -07001686
1687// --- MultiTouchMotionAccumulator::Slot ---
1688
1689MultiTouchMotionAccumulator::Slot::Slot() {
1690 clear();
1691}
1692
Jeff Brown49754db2011-07-01 17:37:58 -07001693void MultiTouchMotionAccumulator::Slot::clear() {
1694 mInUse = false;
1695 mHaveAbsMTTouchMinor = false;
1696 mHaveAbsMTWidthMinor = false;
1697 mHaveAbsMTToolType = false;
1698 mAbsMTPositionX = 0;
1699 mAbsMTPositionY = 0;
1700 mAbsMTTouchMajor = 0;
1701 mAbsMTTouchMinor = 0;
1702 mAbsMTWidthMajor = 0;
1703 mAbsMTWidthMinor = 0;
1704 mAbsMTOrientation = 0;
1705 mAbsMTTrackingId = -1;
1706 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001707 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001708 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001709}
1710
1711int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1712 if (mHaveAbsMTToolType) {
1713 switch (mAbsMTToolType) {
1714 case MT_TOOL_FINGER:
1715 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1716 case MT_TOOL_PEN:
1717 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1718 }
1719 }
1720 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1721}
1722
1723
Jeff Brown6d0fec22010-07-23 21:28:06 -07001724// --- InputMapper ---
1725
1726InputMapper::InputMapper(InputDevice* device) :
1727 mDevice(device), mContext(device->getContext()) {
1728}
1729
1730InputMapper::~InputMapper() {
1731}
1732
1733void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1734 info->addSource(getSources());
1735}
1736
Jeff Brownef3d7e82010-09-30 14:33:04 -07001737void InputMapper::dump(String8& dump) {
1738}
1739
Jeff Brown65fd2512011-08-18 11:20:58 -07001740void InputMapper::configure(nsecs_t when,
1741 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001742}
1743
Jeff Brown65fd2512011-08-18 11:20:58 -07001744void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001745}
1746
Jeff Brownaa3855d2011-03-17 01:34:19 -07001747void InputMapper::timeoutExpired(nsecs_t when) {
1748}
1749
Jeff Brown6d0fec22010-07-23 21:28:06 -07001750int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1751 return AKEY_STATE_UNKNOWN;
1752}
1753
1754int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1755 return AKEY_STATE_UNKNOWN;
1756}
1757
1758int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1759 return AKEY_STATE_UNKNOWN;
1760}
1761
1762bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1763 const int32_t* keyCodes, uint8_t* outFlags) {
1764 return false;
1765}
1766
Jeff Browna47425a2012-04-13 04:09:27 -07001767void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1768 int32_t token) {
1769}
1770
1771void InputMapper::cancelVibrate(int32_t token) {
1772}
1773
Jeff Brown6d0fec22010-07-23 21:28:06 -07001774int32_t InputMapper::getMetaState() {
1775 return 0;
1776}
1777
Jeff Brown05dc66a2011-03-02 14:41:58 -08001778void InputMapper::fadePointer() {
1779}
1780
Jeff Brownbe1aa822011-07-27 16:04:54 -07001781status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1782 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1783}
1784
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001785void InputMapper::bumpGeneration() {
1786 mDevice->bumpGeneration();
1787}
1788
Jeff Browncb1404e2011-01-15 18:14:15 -08001789void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1790 const RawAbsoluteAxisInfo& axis, const char* name) {
1791 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001792 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1793 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001794 } else {
1795 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1796 }
1797}
1798
Jeff Brown6d0fec22010-07-23 21:28:06 -07001799
1800// --- SwitchInputMapper ---
1801
1802SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
Jeff Brownbcc046a2012-09-27 20:46:43 -07001803 InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001804}
1805
1806SwitchInputMapper::~SwitchInputMapper() {
1807}
1808
1809uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001810 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001811}
1812
1813void SwitchInputMapper::process(const RawEvent* rawEvent) {
1814 switch (rawEvent->type) {
1815 case EV_SW:
Jeff Brownbcc046a2012-09-27 20:46:43 -07001816 processSwitch(rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001817 break;
Jeff Brownbcc046a2012-09-27 20:46:43 -07001818
1819 case EV_SYN:
1820 if (rawEvent->code == SYN_REPORT) {
1821 sync(rawEvent->when);
1822 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001823 }
1824}
1825
Jeff Brownbcc046a2012-09-27 20:46:43 -07001826void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
1827 if (switchCode >= 0 && switchCode < 32) {
1828 if (switchValue) {
1829 mUpdatedSwitchValues |= 1 << switchCode;
1830 }
1831 mUpdatedSwitchMask |= 1 << switchCode;
1832 }
1833}
1834
1835void SwitchInputMapper::sync(nsecs_t when) {
1836 if (mUpdatedSwitchMask) {
1837 NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask);
1838 getListener()->notifySwitch(&args);
1839
1840 mUpdatedSwitchValues = 0;
1841 mUpdatedSwitchMask = 0;
1842 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001843}
1844
1845int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1846 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1847}
1848
1849
Jeff Browna47425a2012-04-13 04:09:27 -07001850// --- VibratorInputMapper ---
1851
1852VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1853 InputMapper(device), mVibrating(false) {
1854}
1855
1856VibratorInputMapper::~VibratorInputMapper() {
1857}
1858
1859uint32_t VibratorInputMapper::getSources() {
1860 return 0;
1861}
1862
1863void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1864 InputMapper::populateDeviceInfo(info);
1865
1866 info->setVibrator(true);
1867}
1868
1869void VibratorInputMapper::process(const RawEvent* rawEvent) {
1870 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1871}
1872
1873void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1874 int32_t token) {
1875#if DEBUG_VIBRATOR
1876 String8 patternStr;
1877 for (size_t i = 0; i < patternSize; i++) {
1878 if (i != 0) {
1879 patternStr.append(", ");
1880 }
1881 patternStr.appendFormat("%lld", pattern[i]);
1882 }
1883 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1884 getDeviceId(), patternStr.string(), repeat, token);
1885#endif
1886
1887 mVibrating = true;
1888 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1889 mPatternSize = patternSize;
1890 mRepeat = repeat;
1891 mToken = token;
1892 mIndex = -1;
1893
1894 nextStep();
1895}
1896
1897void VibratorInputMapper::cancelVibrate(int32_t token) {
1898#if DEBUG_VIBRATOR
1899 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1900#endif
1901
1902 if (mVibrating && mToken == token) {
1903 stopVibrating();
1904 }
1905}
1906
1907void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1908 if (mVibrating) {
1909 if (when >= mNextStepTime) {
1910 nextStep();
1911 } else {
1912 getContext()->requestTimeoutAtTime(mNextStepTime);
1913 }
1914 }
1915}
1916
1917void VibratorInputMapper::nextStep() {
1918 mIndex += 1;
1919 if (size_t(mIndex) >= mPatternSize) {
1920 if (mRepeat < 0) {
1921 // We are done.
1922 stopVibrating();
1923 return;
1924 }
1925 mIndex = mRepeat;
1926 }
1927
1928 bool vibratorOn = mIndex & 1;
1929 nsecs_t duration = mPattern[mIndex];
1930 if (vibratorOn) {
1931#if DEBUG_VIBRATOR
1932 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1933 getDeviceId(), duration);
1934#endif
1935 getEventHub()->vibrate(getDeviceId(), duration);
1936 } else {
1937#if DEBUG_VIBRATOR
1938 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1939#endif
1940 getEventHub()->cancelVibrate(getDeviceId());
1941 }
1942 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1943 mNextStepTime = now + duration;
1944 getContext()->requestTimeoutAtTime(mNextStepTime);
1945#if DEBUG_VIBRATOR
1946 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1947#endif
1948}
1949
1950void VibratorInputMapper::stopVibrating() {
1951 mVibrating = false;
1952#if DEBUG_VIBRATOR
1953 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1954#endif
1955 getEventHub()->cancelVibrate(getDeviceId());
1956}
1957
1958void VibratorInputMapper::dump(String8& dump) {
1959 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1960 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1961}
1962
1963
Jeff Brown6d0fec22010-07-23 21:28:06 -07001964// --- KeyboardInputMapper ---
1965
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001966KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001967 uint32_t source, int32_t keyboardType) :
1968 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001969 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970}
1971
1972KeyboardInputMapper::~KeyboardInputMapper() {
1973}
1974
Jeff Brown6d0fec22010-07-23 21:28:06 -07001975uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001976 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001977}
1978
1979void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1980 InputMapper::populateDeviceInfo(info);
1981
1982 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001983 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001984}
1985
Jeff Brownef3d7e82010-09-30 14:33:04 -07001986void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001987 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1988 dumpParameters(dump);
1989 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001990 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001991 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1992 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1993 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001994}
1995
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001996
Jeff Brown65fd2512011-08-18 11:20:58 -07001997void KeyboardInputMapper::configure(nsecs_t when,
1998 const InputReaderConfiguration* config, uint32_t changes) {
1999 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002000
Jeff Brown474dcb52011-06-14 20:22:50 -07002001 if (!changes) { // first time only
2002 // Configure basic parameters.
2003 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002004 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002005
Jeff Brown65fd2512011-08-18 11:20:58 -07002006 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002007 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2008 DisplayViewport v;
2009 if (config->getDisplayInfo(false /*external*/, &v)) {
2010 mOrientation = v.orientation;
2011 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002012 mOrientation = DISPLAY_ORIENTATION_0;
2013 }
2014 } else {
2015 mOrientation = DISPLAY_ORIENTATION_0;
2016 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002017 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002018}
2019
2020void KeyboardInputMapper::configureParameters() {
2021 mParameters.orientationAware = false;
2022 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2023 mParameters.orientationAware);
2024
Jeff Brownd728bf52012-09-08 18:05:28 -07002025 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002026 if (mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002027 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002028 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002029}
2030
2031void KeyboardInputMapper::dumpParameters(String8& dump) {
2032 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002033 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2034 toString(mParameters.hasAssociatedDisplay));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002035 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2036 toString(mParameters.orientationAware));
2037}
2038
Jeff Brown65fd2512011-08-18 11:20:58 -07002039void KeyboardInputMapper::reset(nsecs_t when) {
2040 mMetaState = AMETA_NONE;
2041 mDownTime = 0;
2042 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002043 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002044
Jeff Brownbe1aa822011-07-27 16:04:54 -07002045 resetLedState();
2046
Jeff Brown65fd2512011-08-18 11:20:58 -07002047 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002048}
2049
2050void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2051 switch (rawEvent->type) {
2052 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002053 int32_t scanCode = rawEvent->code;
2054 int32_t usageCode = mCurrentHidUsage;
2055 mCurrentHidUsage = 0;
2056
Jeff Brown6d0fec22010-07-23 21:28:06 -07002057 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002058 int32_t keyCode;
2059 uint32_t flags;
2060 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2061 keyCode = AKEYCODE_UNKNOWN;
2062 flags = 0;
2063 }
2064 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002065 }
2066 break;
2067 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002068 case EV_MSC: {
2069 if (rawEvent->code == MSC_SCAN) {
2070 mCurrentHidUsage = rawEvent->value;
2071 }
2072 break;
2073 }
2074 case EV_SYN: {
2075 if (rawEvent->code == SYN_REPORT) {
2076 mCurrentHidUsage = 0;
2077 }
2078 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002079 }
2080}
2081
2082bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2083 return scanCode < BTN_MOUSE
2084 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002085 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002086 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002087}
2088
Jeff Brown6328cdc2010-07-29 18:18:33 -07002089void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2090 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002091
Jeff Brownbe1aa822011-07-27 16:04:54 -07002092 if (down) {
2093 // Rotate key codes according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002094 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002095 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002096 }
Jeff Brownfe508922011-01-18 15:10:10 -08002097
Jeff Brownbe1aa822011-07-27 16:04:54 -07002098 // Add key down.
2099 ssize_t keyDownIndex = findKeyDown(scanCode);
2100 if (keyDownIndex >= 0) {
2101 // key repeat, be sure to use same keycode as before in case of rotation
2102 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002103 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002104 // key down
2105 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2106 && mContext->shouldDropVirtualKey(when,
2107 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002108 return;
2109 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002110
2111 mKeyDowns.push();
2112 KeyDown& keyDown = mKeyDowns.editTop();
2113 keyDown.keyCode = keyCode;
2114 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002115 }
2116
Jeff Brownbe1aa822011-07-27 16:04:54 -07002117 mDownTime = when;
2118 } else {
2119 // Remove key down.
2120 ssize_t keyDownIndex = findKeyDown(scanCode);
2121 if (keyDownIndex >= 0) {
2122 // key up, be sure to use same keycode as before in case of rotation
2123 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2124 mKeyDowns.removeAt(size_t(keyDownIndex));
2125 } else {
2126 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002127 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002128 "keyCode=%d, scanCode=%d",
2129 getDeviceName().string(), keyCode, scanCode);
2130 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002131 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002132 }
Jeff Brownfd035822010-06-30 16:10:35 -07002133
Jeff Brownbe1aa822011-07-27 16:04:54 -07002134 bool metaStateChanged = false;
2135 int32_t oldMetaState = mMetaState;
2136 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2137 if (oldMetaState != newMetaState) {
2138 mMetaState = newMetaState;
2139 metaStateChanged = true;
2140 updateLedState(false);
2141 }
2142
2143 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002144
Jeff Brown56194eb2011-03-02 19:23:13 -08002145 // Key down on external an keyboard should wake the device.
2146 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2147 // For internal keyboards, the key layout file should specify the policy flags for
2148 // each wake key individually.
2149 // TODO: Use the input device configuration to control this behavior more finely.
2150 if (down && getDevice()->isExternal()
2151 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2152 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2153 }
2154
Jeff Brown6328cdc2010-07-29 18:18:33 -07002155 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002156 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002157 }
2158
Jeff Brown05dc66a2011-03-02 14:41:58 -08002159 if (down && !isMetaKey(keyCode)) {
2160 getContext()->fadePointer();
2161 }
2162
Jeff Brownbe1aa822011-07-27 16:04:54 -07002163 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002164 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2165 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002166 getListener()->notifyKey(&args);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002167}
2168
Jeff Brownbe1aa822011-07-27 16:04:54 -07002169ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2170 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002171 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002172 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002173 return i;
2174 }
2175 }
2176 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002177}
2178
Jeff Brown6d0fec22010-07-23 21:28:06 -07002179int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2180 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2181}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002182
Jeff Brown6d0fec22010-07-23 21:28:06 -07002183int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2184 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2185}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002186
Jeff Brown6d0fec22010-07-23 21:28:06 -07002187bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2188 const int32_t* keyCodes, uint8_t* outFlags) {
2189 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2190}
2191
2192int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002193 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002194}
2195
Jeff Brownbe1aa822011-07-27 16:04:54 -07002196void KeyboardInputMapper::resetLedState() {
2197 initializeLedState(mCapsLockLedState, LED_CAPSL);
2198 initializeLedState(mNumLockLedState, LED_NUML);
2199 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002200
Jeff Brownbe1aa822011-07-27 16:04:54 -07002201 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002202}
2203
Jeff Brownbe1aa822011-07-27 16:04:54 -07002204void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002205 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2206 ledState.on = false;
2207}
2208
Jeff Brownbe1aa822011-07-27 16:04:54 -07002209void KeyboardInputMapper::updateLedState(bool reset) {
2210 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002211 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002212 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002213 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002215 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002216}
2217
Jeff Brownbe1aa822011-07-27 16:04:54 -07002218void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002219 int32_t led, int32_t modifier, bool reset) {
2220 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002221 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002222 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002223 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2224 ledState.on = desiredState;
2225 }
2226 }
2227}
2228
Jeff Brown6d0fec22010-07-23 21:28:06 -07002229
Jeff Brown83c09682010-12-23 17:50:18 -08002230// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002231
Jeff Brown83c09682010-12-23 17:50:18 -08002232CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002233 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002234}
2235
Jeff Brown83c09682010-12-23 17:50:18 -08002236CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002237}
2238
Jeff Brown83c09682010-12-23 17:50:18 -08002239uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002240 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002241}
2242
Jeff Brown83c09682010-12-23 17:50:18 -08002243void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002244 InputMapper::populateDeviceInfo(info);
2245
Jeff Brown83c09682010-12-23 17:50:18 -08002246 if (mParameters.mode == Parameters::MODE_POINTER) {
2247 float minX, minY, maxX, maxY;
2248 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002249 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2250 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002251 }
2252 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002253 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2254 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002255 }
Jeff Brownefd32662011-03-08 15:13:06 -08002256 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002257
Jeff Brown65fd2512011-08-18 11:20:58 -07002258 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002259 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002260 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002261 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002262 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002263 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002264}
2265
Jeff Brown83c09682010-12-23 17:50:18 -08002266void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002267 dump.append(INDENT2 "Cursor Input Mapper:\n");
2268 dumpParameters(dump);
2269 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2270 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2271 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2272 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2273 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002274 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002275 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002276 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002277 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2278 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002279 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002280 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2281 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2282 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002283}
2284
Jeff Brown65fd2512011-08-18 11:20:58 -07002285void CursorInputMapper::configure(nsecs_t when,
2286 const InputReaderConfiguration* config, uint32_t changes) {
2287 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002288
Jeff Brown474dcb52011-06-14 20:22:50 -07002289 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002290 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002291
Jeff Brown474dcb52011-06-14 20:22:50 -07002292 // Configure basic parameters.
2293 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002294
Jeff Brown474dcb52011-06-14 20:22:50 -07002295 // Configure device mode.
2296 switch (mParameters.mode) {
2297 case Parameters::MODE_POINTER:
2298 mSource = AINPUT_SOURCE_MOUSE;
2299 mXPrecision = 1.0f;
2300 mYPrecision = 1.0f;
2301 mXScale = 1.0f;
2302 mYScale = 1.0f;
2303 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2304 break;
2305 case Parameters::MODE_NAVIGATION:
2306 mSource = AINPUT_SOURCE_TRACKBALL;
2307 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2308 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2309 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2310 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2311 break;
2312 }
2313
2314 mVWheelScale = 1.0f;
2315 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002316 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002317
Jeff Brown474dcb52011-06-14 20:22:50 -07002318 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2319 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2320 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2321 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2322 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002323
2324 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002325 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2326 DisplayViewport v;
2327 if (config->getDisplayInfo(false /*external*/, &v)) {
2328 mOrientation = v.orientation;
2329 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002330 mOrientation = DISPLAY_ORIENTATION_0;
2331 }
2332 } else {
2333 mOrientation = DISPLAY_ORIENTATION_0;
2334 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002335 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002336 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002337}
2338
Jeff Brown83c09682010-12-23 17:50:18 -08002339void CursorInputMapper::configureParameters() {
2340 mParameters.mode = Parameters::MODE_POINTER;
2341 String8 cursorModeString;
2342 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2343 if (cursorModeString == "navigation") {
2344 mParameters.mode = Parameters::MODE_NAVIGATION;
2345 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002346 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002347 }
2348 }
2349
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002350 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002351 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002352 mParameters.orientationAware);
2353
Jeff Brownd728bf52012-09-08 18:05:28 -07002354 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002355 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002356 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002357 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002358}
2359
Jeff Brown83c09682010-12-23 17:50:18 -08002360void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002361 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002362 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2363 toString(mParameters.hasAssociatedDisplay));
Jeff Brown83c09682010-12-23 17:50:18 -08002364
2365 switch (mParameters.mode) {
2366 case Parameters::MODE_POINTER:
2367 dump.append(INDENT4 "Mode: pointer\n");
2368 break;
2369 case Parameters::MODE_NAVIGATION:
2370 dump.append(INDENT4 "Mode: navigation\n");
2371 break;
2372 default:
Steve Blockec193de2012-01-09 18:35:44 +00002373 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002374 }
2375
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002376 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2377 toString(mParameters.orientationAware));
2378}
2379
Jeff Brown65fd2512011-08-18 11:20:58 -07002380void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002381 mButtonState = 0;
2382 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002383
Jeff Brownbe1aa822011-07-27 16:04:54 -07002384 mPointerVelocityControl.reset();
2385 mWheelXVelocityControl.reset();
2386 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002387
Jeff Brown65fd2512011-08-18 11:20:58 -07002388 mCursorButtonAccumulator.reset(getDevice());
2389 mCursorMotionAccumulator.reset(getDevice());
2390 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002391
Jeff Brown65fd2512011-08-18 11:20:58 -07002392 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002393}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002394
Jeff Brown83c09682010-12-23 17:50:18 -08002395void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002396 mCursorButtonAccumulator.process(rawEvent);
2397 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002398 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002399
Jeff Brown49ccac52012-04-11 18:27:33 -07002400 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002401 sync(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002402 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002403}
2404
Jeff Brown83c09682010-12-23 17:50:18 -08002405void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002406 int32_t lastButtonState = mButtonState;
2407 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2408 mButtonState = currentButtonState;
2409
2410 bool wasDown = isPointerDown(lastButtonState);
2411 bool down = isPointerDown(currentButtonState);
2412 bool downChanged;
2413 if (!wasDown && down) {
2414 mDownTime = when;
2415 downChanged = true;
2416 } else if (wasDown && !down) {
2417 downChanged = true;
2418 } else {
2419 downChanged = false;
2420 }
2421 nsecs_t downTime = mDownTime;
2422 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002423 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002424
2425 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2426 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2427 bool moved = deltaX != 0 || deltaY != 0;
2428
Jeff Brown65fd2512011-08-18 11:20:58 -07002429 // Rotate delta according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002430 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay
Jeff Brownbe1aa822011-07-27 16:04:54 -07002431 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002432 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002433 }
2434
Jeff Brown65fd2512011-08-18 11:20:58 -07002435 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002436 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002437 pointerProperties.clear();
2438 pointerProperties.id = 0;
2439 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2440
Jeff Brown6328cdc2010-07-29 18:18:33 -07002441 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002442 pointerCoords.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002443
Jeff Brown65fd2512011-08-18 11:20:58 -07002444 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2445 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002446 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002447
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448 mWheelYVelocityControl.move(when, NULL, &vscroll);
2449 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002450
Jeff Brownbe1aa822011-07-27 16:04:54 -07002451 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002452
Jeff Brown83d616a2012-09-09 20:33:43 -07002453 int32_t displayId;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002454 if (mPointerController != NULL) {
2455 if (moved || scrolled || buttonsChanged) {
2456 mPointerController->setPresentation(
2457 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002458
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 if (moved) {
2460 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002461 }
2462
Jeff Brownbe1aa822011-07-27 16:04:54 -07002463 if (buttonsChanged) {
2464 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002465 }
Jeff Brownefd32662011-03-08 15:13:06 -08002466
Jeff Brownbe1aa822011-07-27 16:04:54 -07002467 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002468 }
2469
Jeff Brownbe1aa822011-07-27 16:04:54 -07002470 float x, y;
2471 mPointerController->getPosition(&x, &y);
2472 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2473 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83d616a2012-09-09 20:33:43 -07002474 displayId = ADISPLAY_ID_DEFAULT;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002475 } else {
2476 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2477 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83d616a2012-09-09 20:33:43 -07002478 displayId = ADISPLAY_ID_NONE;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002479 }
2480
2481 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002482
Jeff Brown56194eb2011-03-02 19:23:13 -08002483 // Moving an external trackball or mouse should wake the device.
2484 // We don't do this for internal cursor devices to prevent them from waking up
2485 // the device in your pocket.
2486 // TODO: Use the input device configuration to control this behavior more finely.
2487 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002488 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002489 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2490 }
2491
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002492 // Synthesize key down from buttons if needed.
2493 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2494 policyFlags, lastButtonState, currentButtonState);
2495
2496 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002497 if (downChanged || moved || scrolled || buttonsChanged) {
2498 int32_t metaState = mContext->getGlobalMetaState();
2499 int32_t motionEventAction;
2500 if (downChanged) {
2501 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2502 } else if (down || mPointerController == NULL) {
2503 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2504 } else {
2505 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2506 }
Jeff Brownb6997262010-10-08 22:31:17 -07002507
Jeff Brownbe1aa822011-07-27 16:04:54 -07002508 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2509 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07002510 displayId, 1, &pointerProperties, &pointerCoords,
2511 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002512 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002513
Jeff Brownbe1aa822011-07-27 16:04:54 -07002514 // Send hover move after UP to tell the application that the mouse is hovering now.
2515 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2516 && mPointerController != NULL) {
2517 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2518 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2519 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002520 displayId, 1, &pointerProperties, &pointerCoords,
2521 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002522 getListener()->notifyMotion(&hoverArgs);
2523 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002524
Jeff Brownbe1aa822011-07-27 16:04:54 -07002525 // Send scroll events.
2526 if (scrolled) {
2527 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2528 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2529
2530 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2531 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2532 AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002533 displayId, 1, &pointerProperties, &pointerCoords,
2534 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002535 getListener()->notifyMotion(&scrollArgs);
2536 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002537 }
Jeff Browna032cc02011-03-07 16:56:21 -08002538
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002539 // Synthesize key up from buttons if needed.
2540 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2541 policyFlags, lastButtonState, currentButtonState);
2542
Jeff Brown65fd2512011-08-18 11:20:58 -07002543 mCursorMotionAccumulator.finishSync();
2544 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002545}
2546
Jeff Brown83c09682010-12-23 17:50:18 -08002547int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002548 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2549 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2550 } else {
2551 return AKEY_STATE_UNKNOWN;
2552 }
2553}
2554
Jeff Brown05dc66a2011-03-02 14:41:58 -08002555void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002556 if (mPointerController != NULL) {
2557 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2558 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002559}
2560
Jeff Brown6d0fec22010-07-23 21:28:06 -07002561
2562// --- TouchInputMapper ---
2563
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002564TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002565 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002566 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brown83d616a2012-09-09 20:33:43 -07002567 mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0),
2568 mSurfaceOrientation(DISPLAY_ORIENTATION_0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002569}
2570
2571TouchInputMapper::~TouchInputMapper() {
2572}
2573
2574uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002575 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002576}
2577
2578void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2579 InputMapper::populateDeviceInfo(info);
2580
Jeff Brown65fd2512011-08-18 11:20:58 -07002581 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2582 info->addMotionRange(mOrientedRanges.x);
2583 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002584 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002585
Jeff Brown65fd2512011-08-18 11:20:58 -07002586 if (mOrientedRanges.haveSize) {
2587 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002588 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002589
2590 if (mOrientedRanges.haveTouchSize) {
2591 info->addMotionRange(mOrientedRanges.touchMajor);
2592 info->addMotionRange(mOrientedRanges.touchMinor);
2593 }
2594
2595 if (mOrientedRanges.haveToolSize) {
2596 info->addMotionRange(mOrientedRanges.toolMajor);
2597 info->addMotionRange(mOrientedRanges.toolMinor);
2598 }
2599
2600 if (mOrientedRanges.haveOrientation) {
2601 info->addMotionRange(mOrientedRanges.orientation);
2602 }
2603
2604 if (mOrientedRanges.haveDistance) {
2605 info->addMotionRange(mOrientedRanges.distance);
2606 }
2607
2608 if (mOrientedRanges.haveTilt) {
2609 info->addMotionRange(mOrientedRanges.tilt);
2610 }
2611
2612 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2613 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2614 }
2615 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2616 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2617 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002618 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002619}
2620
Jeff Brownef3d7e82010-09-30 14:33:04 -07002621void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002622 dump.append(INDENT2 "Touch Input Mapper:\n");
2623 dumpParameters(dump);
2624 dumpVirtualKeys(dump);
2625 dumpRawPointerAxes(dump);
2626 dumpCalibration(dump);
2627 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002628
Jeff Brownbe1aa822011-07-27 16:04:54 -07002629 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown83d616a2012-09-09 20:33:43 -07002630 dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate);
2631 dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002632 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2633 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2634 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2635 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2636 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002637 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2638 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2639 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2640 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002641 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2642 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2643 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2644 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2645 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002646
Jeff Brownbe1aa822011-07-27 16:04:54 -07002647 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002648
Jeff Brownbe1aa822011-07-27 16:04:54 -07002649 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2650 mLastRawPointerData.pointerCount);
2651 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2652 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2653 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2654 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002655 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2656 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002657 pointer.id, pointer.x, pointer.y, pointer.pressure,
2658 pointer.touchMajor, pointer.touchMinor,
2659 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002660 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002661 pointer.toolType, toString(pointer.isHovering));
2662 }
2663
2664 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2665 mLastCookedPointerData.pointerCount);
2666 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2667 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2668 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2669 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2670 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002671 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2672 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002673 pointerProperties.id,
2674 pointerCoords.getX(),
2675 pointerCoords.getY(),
2676 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2677 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2678 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2679 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2680 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2681 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002682 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002683 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2684 pointerProperties.toolType,
2685 toString(mLastCookedPointerData.isHovering(i)));
2686 }
2687
Jeff Brown65fd2512011-08-18 11:20:58 -07002688 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002689 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2690 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002691 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002692 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002693 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002694 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002695 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002696 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002697 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002698 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2699 mPointerGestureMaxSwipeWidth);
2700 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002701}
2702
Jeff Brown65fd2512011-08-18 11:20:58 -07002703void TouchInputMapper::configure(nsecs_t when,
2704 const InputReaderConfiguration* config, uint32_t changes) {
2705 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002706
Jeff Brown474dcb52011-06-14 20:22:50 -07002707 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002708
Jeff Brown474dcb52011-06-14 20:22:50 -07002709 if (!changes) { // first time only
2710 // Configure basic parameters.
2711 configureParameters();
2712
Jeff Brown65fd2512011-08-18 11:20:58 -07002713 // Configure common accumulators.
2714 mCursorScrollAccumulator.configure(getDevice());
2715 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002716
2717 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002718 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002719
2720 // Prepare input device calibration.
2721 parseCalibration();
2722 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002723 }
2724
Jeff Brown474dcb52011-06-14 20:22:50 -07002725 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002726 // Update pointer speed.
2727 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2728 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2729 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002730 }
Jeff Brown8d608662010-08-30 03:02:23 -07002731
Jeff Brown65fd2512011-08-18 11:20:58 -07002732 bool resetNeeded = false;
2733 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002734 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2735 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002736 // Configure device sources, surface dimensions, orientation and
2737 // scaling factors.
2738 configureSurface(when, &resetNeeded);
2739 }
2740
2741 if (changes && resetNeeded) {
2742 // Send reset, unless this is the first time the device has been configured,
2743 // in which case the reader will call reset itself after all mappers are ready.
2744 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002745 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002746}
2747
Jeff Brown8d608662010-08-30 03:02:23 -07002748void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002749 // Use the pointer presentation mode for devices that do not support distinct
2750 // multitouch. The spot-based presentation relies on being able to accurately
2751 // locate two or more fingers on the touch pad.
2752 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2753 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002754
Jeff Brown538881e2011-05-25 18:23:38 -07002755 String8 gestureModeString;
2756 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2757 gestureModeString)) {
2758 if (gestureModeString == "pointer") {
2759 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2760 } else if (gestureModeString == "spots") {
2761 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2762 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002763 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002764 }
2765 }
2766
Jeff Browndeffe072011-08-26 18:38:46 -07002767 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2768 // The device is a touch screen.
2769 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2770 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2771 // The device is a pointing device like a track pad.
2772 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2773 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002774 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2775 // The device is a cursor device with a touch pad attached.
2776 // By default don't use the touch pad to move the pointer.
2777 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2778 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002779 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002780 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2781 }
2782
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002783 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002784 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2785 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002786 if (deviceTypeString == "touchScreen") {
2787 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002788 } else if (deviceTypeString == "touchPad") {
2789 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002790 } else if (deviceTypeString == "pointer") {
2791 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002792 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002793 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002794 }
2795 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002796
Jeff Brownefd32662011-03-08 15:13:06 -08002797 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002798 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2799 mParameters.orientationAware);
2800
Jeff Brownd728bf52012-09-08 18:05:28 -07002801 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002802 mParameters.associatedDisplayIsExternal = false;
2803 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002804 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002805 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002806 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002807 mParameters.associatedDisplayIsExternal =
2808 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2809 && getDevice()->isExternal();
Jeff Brownbc68a592011-07-25 12:58:12 -07002810 }
Jeff Brown8d608662010-08-30 03:02:23 -07002811}
2812
Jeff Brownef3d7e82010-09-30 14:33:04 -07002813void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002814 dump.append(INDENT3 "Parameters:\n");
2815
Jeff Brown538881e2011-05-25 18:23:38 -07002816 switch (mParameters.gestureMode) {
2817 case Parameters::GESTURE_MODE_POINTER:
2818 dump.append(INDENT4 "GestureMode: pointer\n");
2819 break;
2820 case Parameters::GESTURE_MODE_SPOTS:
2821 dump.append(INDENT4 "GestureMode: spots\n");
2822 break;
2823 default:
2824 assert(false);
2825 }
2826
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002827 switch (mParameters.deviceType) {
2828 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2829 dump.append(INDENT4 "DeviceType: touchScreen\n");
2830 break;
2831 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2832 dump.append(INDENT4 "DeviceType: touchPad\n");
2833 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002834 case Parameters::DEVICE_TYPE_POINTER:
2835 dump.append(INDENT4 "DeviceType: pointer\n");
2836 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002837 default:
Steve Blockec193de2012-01-09 18:35:44 +00002838 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002839 }
2840
Jeff Brown83d616a2012-09-09 20:33:43 -07002841 dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n",
Jeff Brownd728bf52012-09-08 18:05:28 -07002842 toString(mParameters.hasAssociatedDisplay),
2843 toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002844 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2845 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002846}
2847
Jeff Brownbe1aa822011-07-27 16:04:54 -07002848void TouchInputMapper::configureRawPointerAxes() {
2849 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002850}
2851
Jeff Brownbe1aa822011-07-27 16:04:54 -07002852void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2853 dump.append(INDENT3 "Raw Touch Axes:\n");
2854 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2855 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2856 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2857 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2858 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2859 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2860 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2861 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2862 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002863 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2864 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002865 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2866 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002867}
2868
Jeff Brown65fd2512011-08-18 11:20:58 -07002869void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2870 int32_t oldDeviceMode = mDeviceMode;
2871
2872 // Determine device mode.
2873 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2874 && mConfig.pointerGesturesEnabled) {
2875 mSource = AINPUT_SOURCE_MOUSE;
2876 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002877 if (hasStylus()) {
2878 mSource |= AINPUT_SOURCE_STYLUS;
2879 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002880 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownd728bf52012-09-08 18:05:28 -07002881 && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002882 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2883 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002884 if (hasStylus()) {
2885 mSource |= AINPUT_SOURCE_STYLUS;
2886 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002887 } else {
2888 mSource = AINPUT_SOURCE_TOUCHPAD;
2889 mDeviceMode = DEVICE_MODE_UNSCALED;
2890 }
2891
Jeff Brown9626b142011-03-03 02:09:54 -08002892 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002893 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002894 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002895 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002896 mDeviceMode = DEVICE_MODE_DISABLED;
2897 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002898 }
2899
Jeff Brown83d616a2012-09-09 20:33:43 -07002900 // Raw width and height in the natural orientation.
2901 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2902 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2903
Jeff Brown65fd2512011-08-18 11:20:58 -07002904 // Get associated display dimensions.
Jeff Brown83d616a2012-09-09 20:33:43 -07002905 bool viewportChanged = false;
2906 DisplayViewport newViewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07002907 if (mParameters.hasAssociatedDisplay) {
Jeff Brown83d616a2012-09-09 20:33:43 -07002908 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002909 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brownd728bf52012-09-08 18:05:28 -07002910 "display. The device will be inoperable until the display size "
Jeff Brown65fd2512011-08-18 11:20:58 -07002911 "becomes available.",
Jeff Brownd728bf52012-09-08 18:05:28 -07002912 getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002913 mDeviceMode = DEVICE_MODE_DISABLED;
2914 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002915 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002916 } else {
Jeff Brown83d616a2012-09-09 20:33:43 -07002917 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
2918 }
2919 if (mViewport != newViewport) {
2920 mViewport = newViewport;
2921 viewportChanged = true;
2922
2923 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2924 // Convert rotated viewport to natural surface coordinates.
2925 int32_t naturalLogicalWidth, naturalLogicalHeight;
2926 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
2927 int32_t naturalPhysicalLeft, naturalPhysicalTop;
2928 int32_t naturalDeviceWidth, naturalDeviceHeight;
2929 switch (mViewport.orientation) {
2930 case DISPLAY_ORIENTATION_90:
2931 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2932 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2933 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2934 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2935 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
2936 naturalPhysicalTop = mViewport.physicalLeft;
2937 naturalDeviceWidth = mViewport.deviceHeight;
2938 naturalDeviceHeight = mViewport.deviceWidth;
2939 break;
2940 case DISPLAY_ORIENTATION_180:
2941 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2942 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2943 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2944 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2945 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
2946 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
2947 naturalDeviceWidth = mViewport.deviceWidth;
2948 naturalDeviceHeight = mViewport.deviceHeight;
2949 break;
2950 case DISPLAY_ORIENTATION_270:
2951 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2952 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2953 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2954 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2955 naturalPhysicalLeft = mViewport.physicalTop;
2956 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
2957 naturalDeviceWidth = mViewport.deviceHeight;
2958 naturalDeviceHeight = mViewport.deviceWidth;
2959 break;
2960 case DISPLAY_ORIENTATION_0:
2961 default:
2962 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2963 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2964 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2965 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2966 naturalPhysicalLeft = mViewport.physicalLeft;
2967 naturalPhysicalTop = mViewport.physicalTop;
2968 naturalDeviceWidth = mViewport.deviceWidth;
2969 naturalDeviceHeight = mViewport.deviceHeight;
2970 break;
2971 }
2972
2973 mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
2974 mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
2975 mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
2976 mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
2977
2978 mSurfaceOrientation = mParameters.orientationAware ?
2979 mViewport.orientation : DISPLAY_ORIENTATION_0;
2980 } else {
2981 mSurfaceWidth = rawWidth;
2982 mSurfaceHeight = rawHeight;
2983 mSurfaceLeft = 0;
2984 mSurfaceTop = 0;
2985 mSurfaceOrientation = DISPLAY_ORIENTATION_0;
2986 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002987 }
2988
2989 // If moving between pointer modes, need to reset some state.
2990 bool deviceModeChanged;
2991 if (mDeviceMode != oldDeviceMode) {
2992 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002993 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002994 }
2995
Jeff Browndaf4a122011-08-26 17:14:14 -07002996 // Create pointer controller if needed.
2997 if (mDeviceMode == DEVICE_MODE_POINTER ||
2998 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2999 if (mPointerController == NULL) {
3000 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
3001 }
3002 } else {
3003 mPointerController.clear();
3004 }
3005
Jeff Brown83d616a2012-09-09 20:33:43 -07003006 if (viewportChanged || deviceModeChanged) {
3007 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
3008 "display id %d",
3009 getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight,
3010 mSurfaceOrientation, mDeviceMode, mViewport.displayId);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003011
Jeff Brown8d608662010-08-30 03:02:23 -07003012 // Configure X and Y factors.
Jeff Brown83d616a2012-09-09 20:33:43 -07003013 mXScale = float(mSurfaceWidth) / rawWidth;
3014 mYScale = float(mSurfaceHeight) / rawHeight;
3015 mXTranslate = -mSurfaceLeft;
3016 mYTranslate = -mSurfaceTop;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003017 mXPrecision = 1.0f / mXScale;
3018 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003019
Jeff Brownbe1aa822011-07-27 16:04:54 -07003020 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07003021 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003022 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07003023 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08003024
Jeff Brownbe1aa822011-07-27 16:04:54 -07003025 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003026
Jeff Brown8d608662010-08-30 03:02:23 -07003027 // Scale factor for terms that are not oriented in a particular axis.
3028 // If the pixels are square then xScale == yScale otherwise we fake it
3029 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003030 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003031
Jeff Brown8d608662010-08-30 03:02:23 -07003032 // Size of diagonal axis.
Jeff Brown83d616a2012-09-09 20:33:43 -07003033 float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003034
Jeff Browna1f89ce2011-08-11 00:05:01 -07003035 // Size factors.
3036 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
3037 if (mRawPointerAxes.touchMajor.valid
3038 && mRawPointerAxes.touchMajor.maxValue != 0) {
3039 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
3040 } else if (mRawPointerAxes.toolMajor.valid
3041 && mRawPointerAxes.toolMajor.maxValue != 0) {
3042 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
3043 } else {
3044 mSizeScale = 0.0f;
3045 }
3046
Jeff Brownbe1aa822011-07-27 16:04:54 -07003047 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003048 mOrientedRanges.haveToolSize = true;
3049 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08003050
Jeff Brownbe1aa822011-07-27 16:04:54 -07003051 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003052 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003053 mOrientedRanges.touchMajor.min = 0;
3054 mOrientedRanges.touchMajor.max = diagonalSize;
3055 mOrientedRanges.touchMajor.flat = 0;
3056 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003057
Jeff Brownbe1aa822011-07-27 16:04:54 -07003058 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
3059 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08003060
Jeff Brownbe1aa822011-07-27 16:04:54 -07003061 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003062 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003063 mOrientedRanges.toolMajor.min = 0;
3064 mOrientedRanges.toolMajor.max = diagonalSize;
3065 mOrientedRanges.toolMajor.flat = 0;
3066 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003067
Jeff Brownbe1aa822011-07-27 16:04:54 -07003068 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3069 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003070
3071 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003072 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003073 mOrientedRanges.size.min = 0;
3074 mOrientedRanges.size.max = 1.0;
3075 mOrientedRanges.size.flat = 0;
3076 mOrientedRanges.size.fuzz = 0;
3077 } else {
3078 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003079 }
3080
3081 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003082 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003083 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3084 || mCalibration.pressureCalibration
3085 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3086 if (mCalibration.havePressureScale) {
3087 mPressureScale = mCalibration.pressureScale;
3088 } else if (mRawPointerAxes.pressure.valid
3089 && mRawPointerAxes.pressure.maxValue != 0) {
3090 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003091 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003092 }
Jeff Brown8d608662010-08-30 03:02:23 -07003093
Jeff Brown65fd2512011-08-18 11:20:58 -07003094 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3095 mOrientedRanges.pressure.source = mSource;
3096 mOrientedRanges.pressure.min = 0;
3097 mOrientedRanges.pressure.max = 1.0;
3098 mOrientedRanges.pressure.flat = 0;
3099 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003100
Jeff Brown65fd2512011-08-18 11:20:58 -07003101 // Tilt
3102 mTiltXCenter = 0;
3103 mTiltXScale = 0;
3104 mTiltYCenter = 0;
3105 mTiltYScale = 0;
3106 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3107 if (mHaveTilt) {
3108 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3109 mRawPointerAxes.tiltX.maxValue);
3110 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3111 mRawPointerAxes.tiltY.maxValue);
3112 mTiltXScale = M_PI / 180;
3113 mTiltYScale = M_PI / 180;
3114
3115 mOrientedRanges.haveTilt = true;
3116
3117 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3118 mOrientedRanges.tilt.source = mSource;
3119 mOrientedRanges.tilt.min = 0;
3120 mOrientedRanges.tilt.max = M_PI_2;
3121 mOrientedRanges.tilt.flat = 0;
3122 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003123 }
3124
Jeff Brown8d608662010-08-30 03:02:23 -07003125 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07003126 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003127 if (mHaveTilt) {
3128 mOrientedRanges.haveOrientation = true;
3129
3130 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3131 mOrientedRanges.orientation.source = mSource;
3132 mOrientedRanges.orientation.min = -M_PI;
3133 mOrientedRanges.orientation.max = M_PI;
3134 mOrientedRanges.orientation.flat = 0;
3135 mOrientedRanges.orientation.fuzz = 0;
3136 } else if (mCalibration.orientationCalibration !=
3137 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003138 if (mCalibration.orientationCalibration
3139 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003140 if (mRawPointerAxes.orientation.valid) {
Jeff Brown037f7272012-06-25 17:31:23 -07003141 if (mRawPointerAxes.orientation.maxValue > 0) {
3142 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
3143 } else if (mRawPointerAxes.orientation.minValue < 0) {
3144 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
3145 } else {
3146 mOrientationScale = 0;
3147 }
Jeff Brown8d608662010-08-30 03:02:23 -07003148 }
3149 }
3150
Jeff Brownbe1aa822011-07-27 16:04:54 -07003151 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003152
Jeff Brownbe1aa822011-07-27 16:04:54 -07003153 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003154 mOrientedRanges.orientation.source = mSource;
3155 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003156 mOrientedRanges.orientation.max = M_PI_2;
3157 mOrientedRanges.orientation.flat = 0;
3158 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003159 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003160
3161 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003162 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003163 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3164 if (mCalibration.distanceCalibration
3165 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3166 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003167 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003168 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003169 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003170 }
3171 }
3172
Jeff Brownbe1aa822011-07-27 16:04:54 -07003173 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003174
Jeff Brownbe1aa822011-07-27 16:04:54 -07003175 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003176 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003177 mOrientedRanges.distance.min =
3178 mRawPointerAxes.distance.minValue * mDistanceScale;
3179 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003180 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003181 mOrientedRanges.distance.flat = 0;
3182 mOrientedRanges.distance.fuzz =
3183 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003184 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003185
Jeff Brown83d616a2012-09-09 20:33:43 -07003186 // Compute oriented precision, scales and ranges.
Jeff Brown9626b142011-03-03 02:09:54 -08003187 // Note that the maximum value reported is an inclusive maximum value so it is one
3188 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003189 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003190 case DISPLAY_ORIENTATION_90:
3191 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003192 mOrientedXPrecision = mYPrecision;
3193 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003194
Jeff Brown83d616a2012-09-09 20:33:43 -07003195 mOrientedRanges.x.min = mYTranslate;
3196 mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003197 mOrientedRanges.x.flat = 0;
3198 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003199
Jeff Brown83d616a2012-09-09 20:33:43 -07003200 mOrientedRanges.y.min = mXTranslate;
3201 mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003202 mOrientedRanges.y.flat = 0;
3203 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003204 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003205
Jeff Brown6d0fec22010-07-23 21:28:06 -07003206 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003207 mOrientedXPrecision = mXPrecision;
3208 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003209
Jeff Brown83d616a2012-09-09 20:33:43 -07003210 mOrientedRanges.x.min = mXTranslate;
3211 mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003212 mOrientedRanges.x.flat = 0;
3213 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003214
Jeff Brown83d616a2012-09-09 20:33:43 -07003215 mOrientedRanges.y.min = mYTranslate;
3216 mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003217 mOrientedRanges.y.flat = 0;
3218 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003219 break;
3220 }
Jeff Brownace13b12011-03-09 17:39:48 -08003221
3222 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003223 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown2352b972011-04-12 22:39:53 -07003224 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003225 float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003226
Jeff Brown2352b972011-04-12 22:39:53 -07003227 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003228 // given area relative to the diagonal size of the display when no acceleration
3229 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003230 // Assume that the touch pad has a square aspect ratio such that movements in
3231 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003232 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003233 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003234 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003235
3236 // Scale zooms to cover a smaller range of the display than movements do.
3237 // This value determines the area around the pointer that is affected by freeform
3238 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003239 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003240 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003241 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003242
Jeff Brown2352b972011-04-12 22:39:53 -07003243 // Max width between pointers to detect a swipe gesture is more than some fraction
3244 // of the diagonal axis of the touch pad. Touches that are wider than this are
3245 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003246 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003247 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003248 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003249
Jeff Brown65fd2512011-08-18 11:20:58 -07003250 // Abort current pointer usages because the state has changed.
3251 abortPointerUsage(when, 0 /*policyFlags*/);
3252
3253 // Inform the dispatcher about the changes.
3254 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003255 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003256 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003257}
3258
Jeff Brownbe1aa822011-07-27 16:04:54 -07003259void TouchInputMapper::dumpSurface(String8& dump) {
Jeff Brown83d616a2012-09-09 20:33:43 -07003260 dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, "
3261 "logicalFrame=[%d, %d, %d, %d], "
3262 "physicalFrame=[%d, %d, %d, %d], "
3263 "deviceSize=[%d, %d]\n",
3264 mViewport.displayId, mViewport.orientation,
3265 mViewport.logicalLeft, mViewport.logicalTop,
3266 mViewport.logicalRight, mViewport.logicalBottom,
3267 mViewport.physicalLeft, mViewport.physicalTop,
3268 mViewport.physicalRight, mViewport.physicalBottom,
3269 mViewport.deviceWidth, mViewport.deviceHeight);
3270
Jeff Brownbe1aa822011-07-27 16:04:54 -07003271 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3272 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003273 dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft);
3274 dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003275 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003276}
3277
Jeff Brownbe1aa822011-07-27 16:04:54 -07003278void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003279 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003280 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003281
Jeff Brownbe1aa822011-07-27 16:04:54 -07003282 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003283
Jeff Brown6328cdc2010-07-29 18:18:33 -07003284 if (virtualKeyDefinitions.size() == 0) {
3285 return;
3286 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003287
Jeff Brownbe1aa822011-07-27 16:04:54 -07003288 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003289
Jeff Brownbe1aa822011-07-27 16:04:54 -07003290 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3291 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3292 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3293 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003294
3295 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003296 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003297 virtualKeyDefinitions[i];
3298
Jeff Brownbe1aa822011-07-27 16:04:54 -07003299 mVirtualKeys.add();
3300 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003301
3302 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3303 int32_t keyCode;
3304 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003305 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003306 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003307 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003308 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003309 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003310 }
3311
Jeff Brown6328cdc2010-07-29 18:18:33 -07003312 virtualKey.keyCode = keyCode;
3313 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003314
Jeff Brown6328cdc2010-07-29 18:18:33 -07003315 // convert the key definition's display coordinates into touch coordinates for a hit box
3316 int32_t halfWidth = virtualKeyDefinition.width / 2;
3317 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003318
Jeff Brown6328cdc2010-07-29 18:18:33 -07003319 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003320 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003321 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003322 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003323 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003324 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003325 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003326 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003327 }
3328}
3329
Jeff Brownbe1aa822011-07-27 16:04:54 -07003330void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3331 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003332 dump.append(INDENT3 "Virtual Keys:\n");
3333
Jeff Brownbe1aa822011-07-27 16:04:54 -07003334 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3335 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003336 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3337 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3338 i, virtualKey.scanCode, virtualKey.keyCode,
3339 virtualKey.hitLeft, virtualKey.hitRight,
3340 virtualKey.hitTop, virtualKey.hitBottom);
3341 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003342 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003343}
3344
Jeff Brown8d608662010-08-30 03:02:23 -07003345void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003346 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003347 Calibration& out = mCalibration;
3348
Jeff Browna1f89ce2011-08-11 00:05:01 -07003349 // Size
3350 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3351 String8 sizeCalibrationString;
3352 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3353 if (sizeCalibrationString == "none") {
3354 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3355 } else if (sizeCalibrationString == "geometric") {
3356 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3357 } else if (sizeCalibrationString == "diameter") {
3358 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
Jeff Brown037f7272012-06-25 17:31:23 -07003359 } else if (sizeCalibrationString == "box") {
3360 out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003361 } else if (sizeCalibrationString == "area") {
3362 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3363 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003364 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003365 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003366 }
3367 }
3368
Jeff Browna1f89ce2011-08-11 00:05:01 -07003369 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3370 out.sizeScale);
3371 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3372 out.sizeBias);
3373 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3374 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003375
3376 // Pressure
3377 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3378 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003379 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003380 if (pressureCalibrationString == "none") {
3381 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3382 } else if (pressureCalibrationString == "physical") {
3383 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3384 } else if (pressureCalibrationString == "amplitude") {
3385 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3386 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003387 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003388 pressureCalibrationString.string());
3389 }
3390 }
3391
Jeff Brown8d608662010-08-30 03:02:23 -07003392 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3393 out.pressureScale);
3394
Jeff Brown8d608662010-08-30 03:02:23 -07003395 // Orientation
3396 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3397 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003398 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003399 if (orientationCalibrationString == "none") {
3400 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3401 } else if (orientationCalibrationString == "interpolated") {
3402 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003403 } else if (orientationCalibrationString == "vector") {
3404 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003405 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003406 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003407 orientationCalibrationString.string());
3408 }
3409 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003410
3411 // Distance
3412 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3413 String8 distanceCalibrationString;
3414 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3415 if (distanceCalibrationString == "none") {
3416 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3417 } else if (distanceCalibrationString == "scaled") {
3418 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3419 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003420 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003421 distanceCalibrationString.string());
3422 }
3423 }
3424
3425 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3426 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003427}
3428
3429void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003430 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003431 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3432 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3433 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003434 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003435 } else {
3436 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3437 }
Jeff Brown8d608662010-08-30 03:02:23 -07003438
Jeff Browna1f89ce2011-08-11 00:05:01 -07003439 // Pressure
3440 if (mRawPointerAxes.pressure.valid) {
3441 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3442 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3443 }
3444 } else {
3445 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003446 }
3447
3448 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003449 if (mRawPointerAxes.orientation.valid) {
3450 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003451 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003452 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003453 } else {
3454 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003455 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003456
3457 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003458 if (mRawPointerAxes.distance.valid) {
3459 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003460 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003461 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003462 } else {
3463 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003464 }
Jeff Brown8d608662010-08-30 03:02:23 -07003465}
3466
Jeff Brownef3d7e82010-09-30 14:33:04 -07003467void TouchInputMapper::dumpCalibration(String8& dump) {
3468 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003469
Jeff Browna1f89ce2011-08-11 00:05:01 -07003470 // Size
3471 switch (mCalibration.sizeCalibration) {
3472 case Calibration::SIZE_CALIBRATION_NONE:
3473 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003474 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003475 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3476 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003477 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003478 case Calibration::SIZE_CALIBRATION_DIAMETER:
3479 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3480 break;
Jeff Brown037f7272012-06-25 17:31:23 -07003481 case Calibration::SIZE_CALIBRATION_BOX:
3482 dump.append(INDENT4 "touch.size.calibration: box\n");
3483 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003484 case Calibration::SIZE_CALIBRATION_AREA:
3485 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003486 break;
3487 default:
Steve Blockec193de2012-01-09 18:35:44 +00003488 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003489 }
3490
Jeff Browna1f89ce2011-08-11 00:05:01 -07003491 if (mCalibration.haveSizeScale) {
3492 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3493 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003494 }
3495
Jeff Browna1f89ce2011-08-11 00:05:01 -07003496 if (mCalibration.haveSizeBias) {
3497 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3498 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003499 }
3500
Jeff Browna1f89ce2011-08-11 00:05:01 -07003501 if (mCalibration.haveSizeIsSummed) {
3502 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3503 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003504 }
3505
3506 // Pressure
3507 switch (mCalibration.pressureCalibration) {
3508 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003509 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003510 break;
3511 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003512 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003513 break;
3514 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003515 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003516 break;
3517 default:
Steve Blockec193de2012-01-09 18:35:44 +00003518 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003519 }
3520
Jeff Brown8d608662010-08-30 03:02:23 -07003521 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003522 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3523 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003524 }
3525
Jeff Brown8d608662010-08-30 03:02:23 -07003526 // Orientation
3527 switch (mCalibration.orientationCalibration) {
3528 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003529 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003530 break;
3531 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003532 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003533 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003534 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3535 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3536 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003537 default:
Steve Blockec193de2012-01-09 18:35:44 +00003538 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003539 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003540
3541 // Distance
3542 switch (mCalibration.distanceCalibration) {
3543 case Calibration::DISTANCE_CALIBRATION_NONE:
3544 dump.append(INDENT4 "touch.distance.calibration: none\n");
3545 break;
3546 case Calibration::DISTANCE_CALIBRATION_SCALED:
3547 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3548 break;
3549 default:
Steve Blockec193de2012-01-09 18:35:44 +00003550 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003551 }
3552
3553 if (mCalibration.haveDistanceScale) {
3554 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3555 mCalibration.distanceScale);
3556 }
Jeff Brown8d608662010-08-30 03:02:23 -07003557}
3558
Jeff Brown65fd2512011-08-18 11:20:58 -07003559void TouchInputMapper::reset(nsecs_t when) {
3560 mCursorButtonAccumulator.reset(getDevice());
3561 mCursorScrollAccumulator.reset(getDevice());
3562 mTouchButtonAccumulator.reset(getDevice());
3563
3564 mPointerVelocityControl.reset();
3565 mWheelXVelocityControl.reset();
3566 mWheelYVelocityControl.reset();
3567
Jeff Brownbe1aa822011-07-27 16:04:54 -07003568 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003569 mLastRawPointerData.clear();
3570 mCurrentCookedPointerData.clear();
3571 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003572 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003573 mLastButtonState = 0;
3574 mCurrentRawVScroll = 0;
3575 mCurrentRawHScroll = 0;
3576 mCurrentFingerIdBits.clear();
3577 mLastFingerIdBits.clear();
3578 mCurrentStylusIdBits.clear();
3579 mLastStylusIdBits.clear();
3580 mCurrentMouseIdBits.clear();
3581 mLastMouseIdBits.clear();
3582 mPointerUsage = POINTER_USAGE_NONE;
3583 mSentHoverEnter = false;
3584 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003585
Jeff Brown65fd2512011-08-18 11:20:58 -07003586 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003587
Jeff Brown65fd2512011-08-18 11:20:58 -07003588 mPointerGesture.reset();
3589 mPointerSimple.reset();
3590
3591 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003592 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3593 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003594 }
3595
Jeff Brown65fd2512011-08-18 11:20:58 -07003596 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003597}
3598
Jeff Brown65fd2512011-08-18 11:20:58 -07003599void TouchInputMapper::process(const RawEvent* rawEvent) {
3600 mCursorButtonAccumulator.process(rawEvent);
3601 mCursorScrollAccumulator.process(rawEvent);
3602 mTouchButtonAccumulator.process(rawEvent);
3603
Jeff Brown49ccac52012-04-11 18:27:33 -07003604 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003605 sync(rawEvent->when);
3606 }
3607}
3608
3609void TouchInputMapper::sync(nsecs_t when) {
3610 // Sync button state.
3611 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3612 | mCursorButtonAccumulator.getButtonState();
3613
3614 // Sync scroll state.
3615 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3616 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3617 mCursorScrollAccumulator.finishSync();
3618
3619 // Sync touch state.
3620 bool havePointerIds = true;
3621 mCurrentRawPointerData.clear();
3622 syncTouch(when, &havePointerIds);
3623
Jeff Brownaa3855d2011-03-17 01:34:19 -07003624#if DEBUG_RAW_EVENTS
3625 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003626 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003627 mLastRawPointerData.pointerCount,
3628 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003629 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003630 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003631 "hovering ids 0x%08x -> 0x%08x",
3632 mLastRawPointerData.pointerCount,
3633 mCurrentRawPointerData.pointerCount,
3634 mLastRawPointerData.touchingIdBits.value,
3635 mCurrentRawPointerData.touchingIdBits.value,
3636 mLastRawPointerData.hoveringIdBits.value,
3637 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003638 }
3639#endif
3640
Jeff Brown65fd2512011-08-18 11:20:58 -07003641 // Reset state that we will compute below.
3642 mCurrentFingerIdBits.clear();
3643 mCurrentStylusIdBits.clear();
3644 mCurrentMouseIdBits.clear();
3645 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003646
Jeff Brown65fd2512011-08-18 11:20:58 -07003647 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3648 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003649 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003650 mCurrentButtonState = 0;
3651 } else {
3652 // Preprocess pointer data.
3653 if (!havePointerIds) {
3654 assignPointerIds();
3655 }
3656
3657 // Handle policy on initial down or hover events.
3658 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003659 bool initialDown = mLastRawPointerData.pointerCount == 0
3660 && mCurrentRawPointerData.pointerCount != 0;
3661 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3662 if (initialDown || buttonsPressed) {
3663 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003664 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003665 getContext()->fadePointer();
3666 }
3667
3668 // Initial downs on external touch devices should wake the device.
3669 // We don't do this for internal touch screens to prevent them from waking
3670 // up in your pocket.
3671 // TODO: Use the input device configuration to control this behavior more finely.
3672 if (getDevice()->isExternal()) {
3673 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3674 }
3675 }
3676
3677 // Synthesize key down from raw buttons if needed.
3678 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3679 policyFlags, mLastButtonState, mCurrentButtonState);
3680
3681 // Consume raw off-screen touches before cooking pointer data.
3682 // If touches are consumed, subsequent code will not receive any pointer data.
3683 if (consumeRawTouches(when, policyFlags)) {
3684 mCurrentRawPointerData.clear();
3685 }
3686
3687 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3688 // with cooked pointer data that has the same ids and indices as the raw data.
3689 // The following code can use either the raw or cooked data, as needed.
3690 cookPointerData();
3691
3692 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003693 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003694 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3695 uint32_t id = idBits.clearFirstMarkedBit();
3696 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3697 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3698 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3699 mCurrentStylusIdBits.markBit(id);
3700 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3701 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3702 mCurrentFingerIdBits.markBit(id);
3703 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3704 mCurrentMouseIdBits.markBit(id);
3705 }
3706 }
3707 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3708 uint32_t id = idBits.clearFirstMarkedBit();
3709 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3710 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3711 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3712 mCurrentStylusIdBits.markBit(id);
3713 }
3714 }
3715
3716 // Stylus takes precedence over all tools, then mouse, then finger.
3717 PointerUsage pointerUsage = mPointerUsage;
3718 if (!mCurrentStylusIdBits.isEmpty()) {
3719 mCurrentMouseIdBits.clear();
3720 mCurrentFingerIdBits.clear();
3721 pointerUsage = POINTER_USAGE_STYLUS;
3722 } else if (!mCurrentMouseIdBits.isEmpty()) {
3723 mCurrentFingerIdBits.clear();
3724 pointerUsage = POINTER_USAGE_MOUSE;
3725 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3726 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003727 }
3728
3729 dispatchPointerUsage(when, policyFlags, pointerUsage);
3730 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003731 if (mDeviceMode == DEVICE_MODE_DIRECT
3732 && mConfig.showTouches && mPointerController != NULL) {
3733 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3734 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3735
3736 mPointerController->setButtonState(mCurrentButtonState);
3737 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3738 mCurrentCookedPointerData.idToIndex,
3739 mCurrentCookedPointerData.touchingIdBits);
3740 }
3741
Jeff Brown65fd2512011-08-18 11:20:58 -07003742 dispatchHoverExit(when, policyFlags);
3743 dispatchTouches(when, policyFlags);
3744 dispatchHoverEnterAndMove(when, policyFlags);
3745 }
3746
3747 // Synthesize key up from raw buttons if needed.
3748 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3749 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003750 }
3751
Jeff Brown6328cdc2010-07-29 18:18:33 -07003752 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003753 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3754 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3755 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003756 mLastFingerIdBits = mCurrentFingerIdBits;
3757 mLastStylusIdBits = mCurrentStylusIdBits;
3758 mLastMouseIdBits = mCurrentMouseIdBits;
3759
3760 // Clear some transient state.
3761 mCurrentRawVScroll = 0;
3762 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003763}
3764
Jeff Brown79ac9692011-04-19 21:20:10 -07003765void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003766 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003767 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3768 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3769 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003770 }
3771}
3772
Jeff Brownbe1aa822011-07-27 16:04:54 -07003773bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3774 // Check for release of a virtual key.
3775 if (mCurrentVirtualKey.down) {
3776 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3777 // Pointer went up while virtual key was down.
3778 mCurrentVirtualKey.down = false;
3779 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003780#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003781 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003782 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003783#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003784 dispatchVirtualKey(when, policyFlags,
3785 AKEY_EVENT_ACTION_UP,
3786 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003787 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003788 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003789 }
3790
Jeff Brownbe1aa822011-07-27 16:04:54 -07003791 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3792 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3793 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3794 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3795 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3796 // Pointer is still within the space of the virtual key.
3797 return true;
3798 }
3799 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003800
Jeff Brownbe1aa822011-07-27 16:04:54 -07003801 // Pointer left virtual key area or another pointer also went down.
3802 // Send key cancellation but do not consume the touch yet.
3803 // This is useful when the user swipes through from the virtual key area
3804 // into the main display surface.
3805 mCurrentVirtualKey.down = false;
3806 if (!mCurrentVirtualKey.ignored) {
3807#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003808 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003809 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3810#endif
3811 dispatchVirtualKey(when, policyFlags,
3812 AKEY_EVENT_ACTION_UP,
3813 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3814 | AKEY_EVENT_FLAG_CANCELED);
3815 }
3816 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003817
Jeff Brownbe1aa822011-07-27 16:04:54 -07003818 if (mLastRawPointerData.touchingIdBits.isEmpty()
3819 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3820 // Pointer just went down. Check for virtual key press or off-screen touches.
3821 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3822 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3823 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3824 // If exactly one pointer went down, check for virtual key hit.
3825 // Otherwise we will drop the entire stroke.
3826 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3827 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3828 if (virtualKey) {
3829 mCurrentVirtualKey.down = true;
3830 mCurrentVirtualKey.downTime = when;
3831 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3832 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3833 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3834 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3835
3836 if (!mCurrentVirtualKey.ignored) {
3837#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003838 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003839 mCurrentVirtualKey.keyCode,
3840 mCurrentVirtualKey.scanCode);
3841#endif
3842 dispatchVirtualKey(when, policyFlags,
3843 AKEY_EVENT_ACTION_DOWN,
3844 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3845 }
3846 }
3847 }
3848 return true;
3849 }
3850 }
3851
Jeff Brownfe508922011-01-18 15:10:10 -08003852 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003853 // most recent touch within the screen area. The idea is to filter out stray
3854 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003855 //
3856 // Problems we're trying to solve:
3857 //
3858 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3859 // virtual key area that is implemented by a separate touch panel and accidentally
3860 // triggers a virtual key.
3861 //
3862 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3863 // area and accidentally triggers a virtual key. This often happens when virtual keys
3864 // are layed out below the screen near to where the on screen keyboard's space bar
3865 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003866 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003867 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003868 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003869 return false;
3870}
3871
3872void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3873 int32_t keyEventAction, int32_t keyEventFlags) {
3874 int32_t keyCode = mCurrentVirtualKey.keyCode;
3875 int32_t scanCode = mCurrentVirtualKey.scanCode;
3876 nsecs_t downTime = mCurrentVirtualKey.downTime;
3877 int32_t metaState = mContext->getGlobalMetaState();
3878 policyFlags |= POLICY_FLAG_VIRTUAL;
3879
3880 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3881 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3882 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003883}
3884
Jeff Brown6d0fec22010-07-23 21:28:06 -07003885void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003886 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3887 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003888 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003889 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003890
3891 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003892 if (!currentIdBits.isEmpty()) {
3893 // No pointer id changes so this is a move event.
3894 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003895 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003896 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3897 AMOTION_EVENT_EDGE_FLAG_NONE,
3898 mCurrentCookedPointerData.pointerProperties,
3899 mCurrentCookedPointerData.pointerCoords,
3900 mCurrentCookedPointerData.idToIndex,
3901 currentIdBits, -1,
3902 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3903 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003904 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003905 // There may be pointers going up and pointers going down and pointers moving
3906 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003907 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3908 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003909 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003910 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003911
Jeff Brownace13b12011-03-09 17:39:48 -08003912 // Update last coordinates of pointers that have moved so that we observe the new
3913 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003914 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003915 mCurrentCookedPointerData.pointerProperties,
3916 mCurrentCookedPointerData.pointerCoords,
3917 mCurrentCookedPointerData.idToIndex,
3918 mLastCookedPointerData.pointerProperties,
3919 mLastCookedPointerData.pointerCoords,
3920 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003921 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003922 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003923 moveNeeded = true;
3924 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003925
Jeff Brownace13b12011-03-09 17:39:48 -08003926 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003927 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003928 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003929
Jeff Brown65fd2512011-08-18 11:20:58 -07003930 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003931 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003932 mLastCookedPointerData.pointerProperties,
3933 mLastCookedPointerData.pointerCoords,
3934 mLastCookedPointerData.idToIndex,
3935 dispatchedIdBits, upId,
3936 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003937 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003938 }
3939
Jeff Brownc3db8582010-10-20 15:33:38 -07003940 // Dispatch move events if any of the remaining pointers moved from their old locations.
3941 // Although applications receive new locations as part of individual pointer up
3942 // events, they do not generally handle them except when presented in a move event.
3943 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003944 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003945 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003946 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003947 mCurrentCookedPointerData.pointerProperties,
3948 mCurrentCookedPointerData.pointerCoords,
3949 mCurrentCookedPointerData.idToIndex,
3950 dispatchedIdBits, -1,
3951 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003952 }
3953
3954 // Dispatch pointer down events using the new pointer locations.
3955 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003956 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003957 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003958
Jeff Brownace13b12011-03-09 17:39:48 -08003959 if (dispatchedIdBits.count() == 1) {
3960 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003961 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003962 }
3963
Jeff Brown65fd2512011-08-18 11:20:58 -07003964 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003965 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003966 mCurrentCookedPointerData.pointerProperties,
3967 mCurrentCookedPointerData.pointerCoords,
3968 mCurrentCookedPointerData.idToIndex,
3969 dispatchedIdBits, downId,
3970 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003971 }
3972 }
Jeff Brownace13b12011-03-09 17:39:48 -08003973}
3974
Jeff Brownbe1aa822011-07-27 16:04:54 -07003975void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3976 if (mSentHoverEnter &&
3977 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3978 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3979 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003980 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003981 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3982 mLastCookedPointerData.pointerProperties,
3983 mLastCookedPointerData.pointerCoords,
3984 mLastCookedPointerData.idToIndex,
3985 mLastCookedPointerData.hoveringIdBits, -1,
3986 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3987 mSentHoverEnter = false;
3988 }
3989}
Jeff Brownace13b12011-03-09 17:39:48 -08003990
Jeff Brownbe1aa822011-07-27 16:04:54 -07003991void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3992 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3993 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3994 int32_t metaState = getContext()->getGlobalMetaState();
3995 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003996 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003997 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3998 mCurrentCookedPointerData.pointerProperties,
3999 mCurrentCookedPointerData.pointerCoords,
4000 mCurrentCookedPointerData.idToIndex,
4001 mCurrentCookedPointerData.hoveringIdBits, -1,
4002 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4003 mSentHoverEnter = true;
4004 }
Jeff Brownace13b12011-03-09 17:39:48 -08004005
Jeff Brown65fd2512011-08-18 11:20:58 -07004006 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004007 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
4008 mCurrentCookedPointerData.pointerProperties,
4009 mCurrentCookedPointerData.pointerCoords,
4010 mCurrentCookedPointerData.idToIndex,
4011 mCurrentCookedPointerData.hoveringIdBits, -1,
4012 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4013 }
4014}
4015
4016void TouchInputMapper::cookPointerData() {
4017 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4018
4019 mCurrentCookedPointerData.clear();
4020 mCurrentCookedPointerData.pointerCount = currentPointerCount;
4021 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
4022 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
4023
4024 // Walk through the the active pointers and map device coordinates onto
4025 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08004026 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004027 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004028
Jeff Browna1f89ce2011-08-11 00:05:01 -07004029 // Size
4030 float touchMajor, touchMinor, toolMajor, toolMinor, size;
4031 switch (mCalibration.sizeCalibration) {
4032 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
4033 case Calibration::SIZE_CALIBRATION_DIAMETER:
Jeff Brown037f7272012-06-25 17:31:23 -07004034 case Calibration::SIZE_CALIBRATION_BOX:
Jeff Browna1f89ce2011-08-11 00:05:01 -07004035 case Calibration::SIZE_CALIBRATION_AREA:
4036 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
4037 touchMajor = in.touchMajor;
4038 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
4039 toolMajor = in.toolMajor;
4040 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
4041 size = mRawPointerAxes.touchMinor.valid
4042 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4043 } else if (mRawPointerAxes.touchMajor.valid) {
4044 toolMajor = touchMajor = in.touchMajor;
4045 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
4046 ? in.touchMinor : in.touchMajor;
4047 size = mRawPointerAxes.touchMinor.valid
4048 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4049 } else if (mRawPointerAxes.toolMajor.valid) {
4050 touchMajor = toolMajor = in.toolMajor;
4051 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
4052 ? in.toolMinor : in.toolMajor;
4053 size = mRawPointerAxes.toolMinor.valid
4054 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004055 } else {
Steve Blockec193de2012-01-09 18:35:44 +00004056 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07004057 "Size calibration should have been resolved to NONE.");
4058 touchMajor = 0;
4059 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004060 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004061 toolMinor = 0;
4062 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004063 }
Jeff Brownace13b12011-03-09 17:39:48 -08004064
Jeff Browna1f89ce2011-08-11 00:05:01 -07004065 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
4066 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
4067 if (touchingCount > 1) {
4068 touchMajor /= touchingCount;
4069 touchMinor /= touchingCount;
4070 toolMajor /= touchingCount;
4071 toolMinor /= touchingCount;
4072 size /= touchingCount;
4073 }
4074 }
Jeff Brownace13b12011-03-09 17:39:48 -08004075
Jeff Browna1f89ce2011-08-11 00:05:01 -07004076 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4077 touchMajor *= mGeometricScale;
4078 touchMinor *= mGeometricScale;
4079 toolMajor *= mGeometricScale;
4080 toolMinor *= mGeometricScale;
4081 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4082 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004083 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004084 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4085 toolMinor = toolMajor;
4086 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4087 touchMinor = touchMajor;
4088 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004089 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004090
4091 mCalibration.applySizeScaleAndBias(&touchMajor);
4092 mCalibration.applySizeScaleAndBias(&touchMinor);
4093 mCalibration.applySizeScaleAndBias(&toolMajor);
4094 mCalibration.applySizeScaleAndBias(&toolMinor);
4095 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004096 break;
4097 default:
4098 touchMajor = 0;
4099 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004100 toolMajor = 0;
4101 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004102 size = 0;
4103 break;
4104 }
4105
Jeff Browna1f89ce2011-08-11 00:05:01 -07004106 // Pressure
4107 float pressure;
4108 switch (mCalibration.pressureCalibration) {
4109 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4110 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4111 pressure = in.pressure * mPressureScale;
4112 break;
4113 default:
4114 pressure = in.isHovering ? 0 : 1;
4115 break;
4116 }
4117
Jeff Brown65fd2512011-08-18 11:20:58 -07004118 // Tilt and Orientation
4119 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004120 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004121 if (mHaveTilt) {
4122 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4123 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4124 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4125 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4126 } else {
4127 tilt = 0;
4128
4129 switch (mCalibration.orientationCalibration) {
4130 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown037f7272012-06-25 17:31:23 -07004131 orientation = in.orientation * mOrientationScale;
Jeff Brown65fd2512011-08-18 11:20:58 -07004132 break;
4133 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4134 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4135 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4136 if (c1 != 0 || c2 != 0) {
4137 orientation = atan2f(c1, c2) * 0.5f;
4138 float confidence = hypotf(c1, c2);
4139 float scale = 1.0f + confidence / 16.0f;
4140 touchMajor *= scale;
4141 touchMinor /= scale;
4142 toolMajor *= scale;
4143 toolMinor /= scale;
4144 } else {
4145 orientation = 0;
4146 }
4147 break;
4148 }
4149 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004150 orientation = 0;
4151 }
Jeff Brownace13b12011-03-09 17:39:48 -08004152 }
4153
Jeff Brown80fd47c2011-05-24 01:07:44 -07004154 // Distance
4155 float distance;
4156 switch (mCalibration.distanceCalibration) {
4157 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004158 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004159 break;
4160 default:
4161 distance = 0;
4162 }
4163
Jeff Brownace13b12011-03-09 17:39:48 -08004164 // X and Y
4165 // Adjust coords for surface orientation.
4166 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004167 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004168 case DISPLAY_ORIENTATION_90:
Jeff Brown83d616a2012-09-09 20:33:43 -07004169 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4170 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004171 orientation -= M_PI_2;
4172 if (orientation < - M_PI_2) {
4173 orientation += M_PI;
4174 }
4175 break;
4176 case DISPLAY_ORIENTATION_180:
Jeff Brown83d616a2012-09-09 20:33:43 -07004177 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
4178 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004179 break;
4180 case DISPLAY_ORIENTATION_270:
Jeff Brown83d616a2012-09-09 20:33:43 -07004181 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
4182 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004183 orientation += M_PI_2;
4184 if (orientation > M_PI_2) {
4185 orientation -= M_PI;
4186 }
4187 break;
4188 default:
Jeff Brown83d616a2012-09-09 20:33:43 -07004189 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4190 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004191 break;
4192 }
4193
4194 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004195 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004196 out.clear();
4197 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4198 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4199 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4200 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4201 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4202 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4203 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4204 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4205 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004206 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004207 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004208
4209 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004210 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4211 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004212 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004213 properties.id = id;
4214 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004215
Jeff Brownbe1aa822011-07-27 16:04:54 -07004216 // Write id index.
4217 mCurrentCookedPointerData.idToIndex[id] = i;
4218 }
Jeff Brownace13b12011-03-09 17:39:48 -08004219}
4220
Jeff Brown65fd2512011-08-18 11:20:58 -07004221void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4222 PointerUsage pointerUsage) {
4223 if (pointerUsage != mPointerUsage) {
4224 abortPointerUsage(when, policyFlags);
4225 mPointerUsage = pointerUsage;
4226 }
4227
4228 switch (mPointerUsage) {
4229 case POINTER_USAGE_GESTURES:
4230 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4231 break;
4232 case POINTER_USAGE_STYLUS:
4233 dispatchPointerStylus(when, policyFlags);
4234 break;
4235 case POINTER_USAGE_MOUSE:
4236 dispatchPointerMouse(when, policyFlags);
4237 break;
4238 default:
4239 break;
4240 }
4241}
4242
4243void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4244 switch (mPointerUsage) {
4245 case POINTER_USAGE_GESTURES:
4246 abortPointerGestures(when, policyFlags);
4247 break;
4248 case POINTER_USAGE_STYLUS:
4249 abortPointerStylus(when, policyFlags);
4250 break;
4251 case POINTER_USAGE_MOUSE:
4252 abortPointerMouse(when, policyFlags);
4253 break;
4254 default:
4255 break;
4256 }
4257
4258 mPointerUsage = POINTER_USAGE_NONE;
4259}
4260
Jeff Brown79ac9692011-04-19 21:20:10 -07004261void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4262 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004263 // Update current gesture coordinates.
4264 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004265 bool sendEvents = preparePointerGestures(when,
4266 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4267 if (!sendEvents) {
4268 return;
4269 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004270 if (finishPreviousGesture) {
4271 cancelPreviousGesture = false;
4272 }
Jeff Brownace13b12011-03-09 17:39:48 -08004273
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004274 // Update the pointer presentation and spots.
4275 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4276 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4277 if (finishPreviousGesture || cancelPreviousGesture) {
4278 mPointerController->clearSpots();
4279 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004280 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4281 mPointerGesture.currentGestureIdToIndex,
4282 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004283 } else {
4284 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4285 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004286
Jeff Brown538881e2011-05-25 18:23:38 -07004287 // Show or hide the pointer if needed.
4288 switch (mPointerGesture.currentGestureMode) {
4289 case PointerGesture::NEUTRAL:
4290 case PointerGesture::QUIET:
4291 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4292 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4293 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4294 // Remind the user of where the pointer is after finishing a gesture with spots.
4295 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4296 }
4297 break;
4298 case PointerGesture::TAP:
4299 case PointerGesture::TAP_DRAG:
4300 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4301 case PointerGesture::HOVER:
4302 case PointerGesture::PRESS:
4303 // Unfade the pointer when the current gesture manipulates the
4304 // area directly under the pointer.
4305 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4306 break;
4307 case PointerGesture::SWIPE:
4308 case PointerGesture::FREEFORM:
4309 // Fade the pointer when the current gesture manipulates a different
4310 // area and there are spots to guide the user experience.
4311 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4312 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4313 } else {
4314 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4315 }
4316 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004317 }
4318
Jeff Brownace13b12011-03-09 17:39:48 -08004319 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004320 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004321 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004322
4323 // Update last coordinates of pointers that have moved so that we observe the new
4324 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004325 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4326 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4327 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004328 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004329 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4330 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4331 bool moveNeeded = false;
4332 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004333 && !mPointerGesture.lastGestureIdBits.isEmpty()
4334 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004335 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4336 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004337 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004338 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004339 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004340 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4341 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004342 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004343 moveNeeded = true;
4344 }
Jeff Brownace13b12011-03-09 17:39:48 -08004345 }
4346
4347 // Send motion events for all pointers that went up or were canceled.
4348 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4349 if (!dispatchedGestureIdBits.isEmpty()) {
4350 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004351 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004352 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4353 AMOTION_EVENT_EDGE_FLAG_NONE,
4354 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004355 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4356 dispatchedGestureIdBits, -1,
4357 0, 0, mPointerGesture.downTime);
4358
4359 dispatchedGestureIdBits.clear();
4360 } else {
4361 BitSet32 upGestureIdBits;
4362 if (finishPreviousGesture) {
4363 upGestureIdBits = dispatchedGestureIdBits;
4364 } else {
4365 upGestureIdBits.value = dispatchedGestureIdBits.value
4366 & ~mPointerGesture.currentGestureIdBits.value;
4367 }
4368 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004369 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004370
Jeff Brown65fd2512011-08-18 11:20:58 -07004371 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004372 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004373 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4374 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004375 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4376 dispatchedGestureIdBits, id,
4377 0, 0, mPointerGesture.downTime);
4378
4379 dispatchedGestureIdBits.clearBit(id);
4380 }
4381 }
4382 }
4383
4384 // Send motion events for all pointers that moved.
4385 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004386 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004387 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4388 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004389 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4390 dispatchedGestureIdBits, -1,
4391 0, 0, mPointerGesture.downTime);
4392 }
4393
4394 // Send motion events for all pointers that went down.
4395 if (down) {
4396 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4397 & ~dispatchedGestureIdBits.value);
4398 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004399 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004400 dispatchedGestureIdBits.markBit(id);
4401
Jeff Brownace13b12011-03-09 17:39:48 -08004402 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004403 mPointerGesture.downTime = when;
4404 }
4405
Jeff Brown65fd2512011-08-18 11:20:58 -07004406 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004407 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004408 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004409 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4410 dispatchedGestureIdBits, id,
4411 0, 0, mPointerGesture.downTime);
4412 }
4413 }
4414
Jeff Brownace13b12011-03-09 17:39:48 -08004415 // Send motion events for hover.
4416 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004417 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004418 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4419 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4420 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004421 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4422 mPointerGesture.currentGestureIdBits, -1,
4423 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004424 } else if (dispatchedGestureIdBits.isEmpty()
4425 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4426 // Synthesize a hover move event after all pointers go up to indicate that
4427 // the pointer is hovering again even if the user is not currently touching
4428 // the touch pad. This ensures that a view will receive a fresh hover enter
4429 // event after a tap.
4430 float x, y;
4431 mPointerController->getPosition(&x, &y);
4432
4433 PointerProperties pointerProperties;
4434 pointerProperties.clear();
4435 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004436 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004437
4438 PointerCoords pointerCoords;
4439 pointerCoords.clear();
4440 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4441 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4442
Jeff Brown65fd2512011-08-18 11:20:58 -07004443 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004444 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4445 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07004446 mViewport.displayId, 1, &pointerProperties, &pointerCoords,
4447 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004448 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004449 }
4450
4451 // Update state.
4452 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4453 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004454 mPointerGesture.lastGestureIdBits.clear();
4455 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004456 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4457 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004458 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004459 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004460 mPointerGesture.lastGestureProperties[index].copyFrom(
4461 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004462 mPointerGesture.lastGestureCoords[index].copyFrom(
4463 mPointerGesture.currentGestureCoords[index]);
4464 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004465 }
4466 }
4467}
4468
Jeff Brown65fd2512011-08-18 11:20:58 -07004469void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4470 // Cancel previously dispatches pointers.
4471 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4472 int32_t metaState = getContext()->getGlobalMetaState();
4473 int32_t buttonState = mCurrentButtonState;
4474 dispatchMotion(when, policyFlags, mSource,
4475 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4476 AMOTION_EVENT_EDGE_FLAG_NONE,
4477 mPointerGesture.lastGestureProperties,
4478 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4479 mPointerGesture.lastGestureIdBits, -1,
4480 0, 0, mPointerGesture.downTime);
4481 }
4482
4483 // Reset the current pointer gesture.
4484 mPointerGesture.reset();
4485 mPointerVelocityControl.reset();
4486
4487 // Remove any current spots.
4488 if (mPointerController != NULL) {
4489 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4490 mPointerController->clearSpots();
4491 }
4492}
4493
Jeff Brown79ac9692011-04-19 21:20:10 -07004494bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4495 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004496 *outCancelPreviousGesture = false;
4497 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004498
Jeff Brown79ac9692011-04-19 21:20:10 -07004499 // Handle TAP timeout.
4500 if (isTimeout) {
4501#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004502 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004503#endif
4504
4505 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004506 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004507 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004508 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004509 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004510 } else {
4511 // The tap is finished.
4512#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004513 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004514#endif
4515 *outFinishPreviousGesture = true;
4516
4517 mPointerGesture.activeGestureId = -1;
4518 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4519 mPointerGesture.currentGestureIdBits.clear();
4520
Jeff Brown65fd2512011-08-18 11:20:58 -07004521 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004522 return true;
4523 }
4524 }
4525
4526 // We did not handle this timeout.
4527 return false;
4528 }
4529
Jeff Brown65fd2512011-08-18 11:20:58 -07004530 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4531 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4532
Jeff Brownace13b12011-03-09 17:39:48 -08004533 // Update the velocity tracker.
4534 {
4535 VelocityTracker::Position positions[MAX_POINTERS];
4536 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004537 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004538 uint32_t id = idBits.clearFirstMarkedBit();
4539 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004540 positions[count].x = pointer.x * mPointerXMovementScale;
4541 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004542 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004543 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004544 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004545 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004546
Jeff Brownace13b12011-03-09 17:39:48 -08004547 // Pick a new active touch id if needed.
4548 // Choose an arbitrary pointer that just went down, if there is one.
4549 // Otherwise choose an arbitrary remaining pointer.
4550 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004551 // We keep the same active touch id for as long as possible.
4552 bool activeTouchChanged = false;
4553 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4554 int32_t activeTouchId = lastActiveTouchId;
4555 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004556 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004557 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004558 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004559 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004560 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004561 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004562 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004563 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004564 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004565 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004566 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004567 } else {
4568 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004569 }
4570 }
4571
4572 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004573 bool isQuietTime = false;
4574 if (activeTouchId < 0) {
4575 mPointerGesture.resetQuietTime();
4576 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004577 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004578 if (!isQuietTime) {
4579 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4580 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4581 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004582 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004583 // Enter quiet time when exiting swipe or freeform state.
4584 // This is to prevent accidentally entering the hover state and flinging the
4585 // pointer when finishing a swipe and there is still one pointer left onscreen.
4586 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004587 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004588 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004589 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004590 // Enter quiet time when releasing the button and there are still two or more
4591 // fingers down. This may indicate that one finger was used to press the button
4592 // but it has not gone up yet.
4593 isQuietTime = true;
4594 }
4595 if (isQuietTime) {
4596 mPointerGesture.quietTime = when;
4597 }
Jeff Brownace13b12011-03-09 17:39:48 -08004598 }
4599 }
4600
4601 // Switch states based on button and pointer state.
4602 if (isQuietTime) {
4603 // Case 1: Quiet time. (QUIET)
4604#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004605 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004606 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004607#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004608 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4609 *outFinishPreviousGesture = true;
4610 }
Jeff Brownace13b12011-03-09 17:39:48 -08004611
4612 mPointerGesture.activeGestureId = -1;
4613 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004614 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004615
Jeff Brown65fd2512011-08-18 11:20:58 -07004616 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004617 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004618 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004619 // The pointer follows the active touch point.
4620 // Emit DOWN, MOVE, UP events at the pointer location.
4621 //
4622 // Only the active touch matters; other fingers are ignored. This policy helps
4623 // to handle the case where the user places a second finger on the touch pad
4624 // to apply the necessary force to depress an integrated button below the surface.
4625 // We don't want the second finger to be delivered to applications.
4626 //
4627 // For this to work well, we need to make sure to track the pointer that is really
4628 // active. If the user first puts one finger down to click then adds another
4629 // finger to drag then the active pointer should switch to the finger that is
4630 // being dragged.
4631#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004632 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004633 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004634#endif
4635 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004636 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004637 *outFinishPreviousGesture = true;
4638 mPointerGesture.activeGestureId = 0;
4639 }
4640
4641 // Switch pointers if needed.
4642 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004643 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004644 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004645 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004646 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004647 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004648 float vx, vy;
4649 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4650 float speed = hypotf(vx, vy);
4651 if (speed > bestSpeed) {
4652 bestId = id;
4653 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004654 }
Jeff Brown8d608662010-08-30 03:02:23 -07004655 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004656 }
4657 if (bestId >= 0 && bestId != activeTouchId) {
4658 mPointerGesture.activeTouchId = activeTouchId = bestId;
4659 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004660#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004661 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004662 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004663#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004664 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004665 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004666
Jeff Brown65fd2512011-08-18 11:20:58 -07004667 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004668 const RawPointerData::Pointer& currentPointer =
4669 mCurrentRawPointerData.pointerForId(activeTouchId);
4670 const RawPointerData::Pointer& lastPointer =
4671 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004672 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4673 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004674
Jeff Brownbe1aa822011-07-27 16:04:54 -07004675 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004676 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004677
4678 // Move the pointer using a relative motion.
4679 // When using spots, the click will occur at the position of the anchor
4680 // spot and all other spots will move there.
4681 mPointerController->move(deltaX, deltaY);
4682 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004683 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004684 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004685
Jeff Brownace13b12011-03-09 17:39:48 -08004686 float x, y;
4687 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004688
Jeff Brown79ac9692011-04-19 21:20:10 -07004689 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004690 mPointerGesture.currentGestureIdBits.clear();
4691 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4692 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004693 mPointerGesture.currentGestureProperties[0].clear();
4694 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004695 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004696 mPointerGesture.currentGestureCoords[0].clear();
4697 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4698 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4699 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004700 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004701 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004702 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4703 *outFinishPreviousGesture = true;
4704 }
Jeff Brownace13b12011-03-09 17:39:48 -08004705
Jeff Brown79ac9692011-04-19 21:20:10 -07004706 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004707 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004708 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004709 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4710 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004711 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004712 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004713 float x, y;
4714 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004715 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4716 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004717#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004718 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004719#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004720
4721 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004722 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004723 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004724
Jeff Brownace13b12011-03-09 17:39:48 -08004725 mPointerGesture.activeGestureId = 0;
4726 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004727 mPointerGesture.currentGestureIdBits.clear();
4728 mPointerGesture.currentGestureIdBits.markBit(
4729 mPointerGesture.activeGestureId);
4730 mPointerGesture.currentGestureIdToIndex[
4731 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004732 mPointerGesture.currentGestureProperties[0].clear();
4733 mPointerGesture.currentGestureProperties[0].id =
4734 mPointerGesture.activeGestureId;
4735 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004736 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004737 mPointerGesture.currentGestureCoords[0].clear();
4738 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004739 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004740 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004741 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004742 mPointerGesture.currentGestureCoords[0].setAxisValue(
4743 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004744
Jeff Brownace13b12011-03-09 17:39:48 -08004745 tapped = true;
4746 } else {
4747#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004748 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004749 x - mPointerGesture.tapX,
4750 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004751#endif
4752 }
4753 } else {
4754#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004755 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004756 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004757#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004758 }
Jeff Brownace13b12011-03-09 17:39:48 -08004759 }
Jeff Brown2352b972011-04-12 22:39:53 -07004760
Jeff Brown65fd2512011-08-18 11:20:58 -07004761 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004762
Jeff Brownace13b12011-03-09 17:39:48 -08004763 if (!tapped) {
4764#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004765 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004766#endif
4767 mPointerGesture.activeGestureId = -1;
4768 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004769 mPointerGesture.currentGestureIdBits.clear();
4770 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004771 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004772 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004773 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004774 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4775 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004776 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004777
Jeff Brown79ac9692011-04-19 21:20:10 -07004778 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4779 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004780 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004781 float x, y;
4782 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004783 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4784 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004785 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4786 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004787#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004788 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004789 x - mPointerGesture.tapX,
4790 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004791#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004792 }
4793 } else {
4794#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004795 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004796 (when - mPointerGesture.tapUpTime) * 0.000001f);
4797#endif
4798 }
4799 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4800 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4801 }
Jeff Brownace13b12011-03-09 17:39:48 -08004802
Jeff Brown65fd2512011-08-18 11:20:58 -07004803 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004804 const RawPointerData::Pointer& currentPointer =
4805 mCurrentRawPointerData.pointerForId(activeTouchId);
4806 const RawPointerData::Pointer& lastPointer =
4807 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004808 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004809 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004810 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004811 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004812
Jeff Brownbe1aa822011-07-27 16:04:54 -07004813 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004814 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004815
Jeff Brown2352b972011-04-12 22:39:53 -07004816 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004817 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004818 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004819 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004820 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004821 }
4822
Jeff Brown79ac9692011-04-19 21:20:10 -07004823 bool down;
4824 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4825#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004826 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004827#endif
4828 down = true;
4829 } else {
4830#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004831 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004832#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004833 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4834 *outFinishPreviousGesture = true;
4835 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004836 mPointerGesture.activeGestureId = 0;
4837 down = false;
4838 }
Jeff Brownace13b12011-03-09 17:39:48 -08004839
4840 float x, y;
4841 mPointerController->getPosition(&x, &y);
4842
Jeff Brownace13b12011-03-09 17:39:48 -08004843 mPointerGesture.currentGestureIdBits.clear();
4844 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4845 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004846 mPointerGesture.currentGestureProperties[0].clear();
4847 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4848 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004849 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004850 mPointerGesture.currentGestureCoords[0].clear();
4851 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4852 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004853 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4854 down ? 1.0f : 0.0f);
4855
Jeff Brown65fd2512011-08-18 11:20:58 -07004856 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004857 mPointerGesture.resetTap();
4858 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004859 mPointerGesture.tapX = x;
4860 mPointerGesture.tapY = y;
4861 }
Jeff Brownace13b12011-03-09 17:39:48 -08004862 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004863 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4864 // We need to provide feedback for each finger that goes down so we cannot wait
4865 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004866 //
Jeff Brown2352b972011-04-12 22:39:53 -07004867 // The ambiguous case is deciding what to do when there are two fingers down but they
4868 // have not moved enough to determine whether they are part of a drag or part of a
4869 // freeform gesture, or just a press or long-press at the pointer location.
4870 //
4871 // When there are two fingers we start with the PRESS hypothesis and we generate a
4872 // down at the pointer location.
4873 //
4874 // When the two fingers move enough or when additional fingers are added, we make
4875 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004876 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004877
Jeff Brown214eaf42011-05-26 19:17:02 -07004878 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004879 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004880 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004881 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4882 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004883 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004884 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004885 // Additional pointers have gone down but not yet settled.
4886 // Reset the gesture.
4887#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004888 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004889 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004890 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004891 * 0.000001f);
4892#endif
4893 *outCancelPreviousGesture = true;
4894 } else {
4895 // Continue previous gesture.
4896 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4897 }
4898
4899 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004900 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4901 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004902 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004903 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004904
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004905 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004906#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004907 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004908 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004909 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004910 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004911#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004912 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4913 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004914 &mPointerGesture.referenceTouchY);
4915 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4916 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004917 }
Jeff Brownace13b12011-03-09 17:39:48 -08004918
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004919 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004920 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004921 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4922 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004923 mPointerGesture.referenceDeltas[id].dx = 0;
4924 mPointerGesture.referenceDeltas[id].dy = 0;
4925 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004926 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004927
4928 // Add delta for all fingers and calculate a common movement delta.
4929 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004930 BitSet32 commonIdBits(mLastFingerIdBits.value
4931 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004932 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4933 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004934 uint32_t id = idBits.clearFirstMarkedBit();
4935 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4936 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004937 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4938 delta.dx += cpd.x - lpd.x;
4939 delta.dy += cpd.y - lpd.y;
4940
4941 if (first) {
4942 commonDeltaX = delta.dx;
4943 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004944 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004945 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4946 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4947 }
4948 }
Jeff Brownace13b12011-03-09 17:39:48 -08004949
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004950 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4951 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4952 float dist[MAX_POINTER_ID + 1];
4953 int32_t distOverThreshold = 0;
4954 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004955 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004956 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004957 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4958 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004959 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004960 distOverThreshold += 1;
4961 }
4962 }
4963
4964 // Only transition when at least two pointers have moved further than
4965 // the minimum distance threshold.
4966 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004967 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004968 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004969#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004970 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004971 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004972#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004973 *outCancelPreviousGesture = true;
4974 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4975 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004976 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004977 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004978 uint32_t id1 = idBits.clearFirstMarkedBit();
4979 uint32_t id2 = idBits.firstMarkedBit();
4980 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4981 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4982 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4983 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4984 // There are two pointers but they are too far apart for a SWIPE,
4985 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004986#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004987 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004988 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004989#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004990 *outCancelPreviousGesture = true;
4991 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4992 } else {
4993 // There are two pointers. Wait for both pointers to start moving
4994 // before deciding whether this is a SWIPE or FREEFORM gesture.
4995 float dist1 = dist[id1];
4996 float dist2 = dist[id2];
4997 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4998 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4999 // Calculate the dot product of the displacement vectors.
5000 // When the vectors are oriented in approximately the same direction,
5001 // the angle betweeen them is near zero and the cosine of the angle
5002 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
5003 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
5004 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07005005 float dx1 = delta1.dx * mPointerXZoomScale;
5006 float dy1 = delta1.dy * mPointerYZoomScale;
5007 float dx2 = delta2.dx * mPointerXZoomScale;
5008 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005009 float dot = dx1 * dx2 + dy1 * dy2;
5010 float cosine = dot / (dist1 * dist2); // denominator always > 0
5011 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
5012 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005013#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005014 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005015 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5016 "cosine %0.3f >= %0.3f",
5017 dist1, mConfig.pointerGestureMultitouchMinDistance,
5018 dist2, mConfig.pointerGestureMultitouchMinDistance,
5019 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005020#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005021 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
5022 } else {
5023 // Pointers are moving in different directions. Switch to FREEFORM.
5024#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005025 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005026 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5027 "cosine %0.3f < %0.3f",
5028 dist1, mConfig.pointerGestureMultitouchMinDistance,
5029 dist2, mConfig.pointerGestureMultitouchMinDistance,
5030 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
5031#endif
5032 *outCancelPreviousGesture = true;
5033 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5034 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005035 }
Jeff Brownace13b12011-03-09 17:39:48 -08005036 }
5037 }
Jeff Brownace13b12011-03-09 17:39:48 -08005038 }
5039 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07005040 // Switch from SWIPE to FREEFORM if additional pointers go down.
5041 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07005042 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07005043#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005044 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005045 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005046#endif
Jeff Brownace13b12011-03-09 17:39:48 -08005047 *outCancelPreviousGesture = true;
5048 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07005049 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005050 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005051
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005052 // Move the reference points based on the overall group motion of the fingers
5053 // except in PRESS mode while waiting for a transition to occur.
5054 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
5055 && (commonDeltaX || commonDeltaY)) {
5056 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005057 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07005058 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005059 delta.dx = 0;
5060 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07005061 }
5062
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005063 mPointerGesture.referenceTouchX += commonDeltaX;
5064 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07005065
Jeff Brown65fd2512011-08-18 11:20:58 -07005066 commonDeltaX *= mPointerXMovementScale;
5067 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07005068
Jeff Brownbe1aa822011-07-27 16:04:54 -07005069 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07005070 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07005071
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005072 mPointerGesture.referenceGestureX += commonDeltaX;
5073 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005074 }
5075
5076 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005077 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5078 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5079 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005080#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005081 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005082 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005083 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005084#endif
Steve Blockec193de2012-01-09 18:35:44 +00005085 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005086
5087 mPointerGesture.currentGestureIdBits.clear();
5088 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5089 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005090 mPointerGesture.currentGestureProperties[0].clear();
5091 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5092 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005093 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005094 mPointerGesture.currentGestureCoords[0].clear();
5095 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5096 mPointerGesture.referenceGestureX);
5097 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5098 mPointerGesture.referenceGestureY);
5099 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005100 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5101 // FREEFORM mode.
5102#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005103 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005104 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005105 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005106#endif
Steve Blockec193de2012-01-09 18:35:44 +00005107 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005108
Jeff Brownace13b12011-03-09 17:39:48 -08005109 mPointerGesture.currentGestureIdBits.clear();
5110
5111 BitSet32 mappedTouchIdBits;
5112 BitSet32 usedGestureIdBits;
5113 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5114 // Initially, assign the active gesture id to the active touch point
5115 // if there is one. No other touch id bits are mapped yet.
5116 if (!*outCancelPreviousGesture) {
5117 mappedTouchIdBits.markBit(activeTouchId);
5118 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5119 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5120 mPointerGesture.activeGestureId;
5121 } else {
5122 mPointerGesture.activeGestureId = -1;
5123 }
5124 } else {
5125 // Otherwise, assume we mapped all touches from the previous frame.
5126 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005127 mappedTouchIdBits.value = mLastFingerIdBits.value
5128 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005129 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5130
5131 // Check whether we need to choose a new active gesture id because the
5132 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005133 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5134 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005135 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005136 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005137 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5138 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5139 mPointerGesture.activeGestureId = -1;
5140 break;
5141 }
5142 }
5143 }
5144
5145#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005146 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005147 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5148 "activeGestureId=%d",
5149 mappedTouchIdBits.value, usedGestureIdBits.value,
5150 mPointerGesture.activeGestureId);
5151#endif
5152
Jeff Brown65fd2512011-08-18 11:20:58 -07005153 BitSet32 idBits(mCurrentFingerIdBits);
5154 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005155 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005156 uint32_t gestureId;
5157 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005158 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005159 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5160#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005161 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005162 "new mapping for touch id %d -> gesture id %d",
5163 touchId, gestureId);
5164#endif
5165 } else {
5166 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5167#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005168 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005169 "existing mapping for touch id %d -> gesture id %d",
5170 touchId, gestureId);
5171#endif
5172 }
5173 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5174 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5175
Jeff Brownbe1aa822011-07-27 16:04:54 -07005176 const RawPointerData::Pointer& pointer =
5177 mCurrentRawPointerData.pointerForId(touchId);
5178 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005179 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005180 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005181 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005182 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005183
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005184 mPointerGesture.currentGestureProperties[i].clear();
5185 mPointerGesture.currentGestureProperties[i].id = gestureId;
5186 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005187 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005188 mPointerGesture.currentGestureCoords[i].clear();
5189 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005190 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005191 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005192 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005193 mPointerGesture.currentGestureCoords[i].setAxisValue(
5194 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5195 }
5196
5197 if (mPointerGesture.activeGestureId < 0) {
5198 mPointerGesture.activeGestureId =
5199 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5200#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005201 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005202 "activeGestureId=%d", mPointerGesture.activeGestureId);
5203#endif
5204 }
Jeff Brown2352b972011-04-12 22:39:53 -07005205 }
Jeff Brownace13b12011-03-09 17:39:48 -08005206 }
5207
Jeff Brownbe1aa822011-07-27 16:04:54 -07005208 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005209
Jeff Brownace13b12011-03-09 17:39:48 -08005210#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005211 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005212 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5213 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005214 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005215 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5216 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005217 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005218 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005219 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005220 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005221 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005222 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005223 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5224 id, index, properties.toolType,
5225 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005226 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5227 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5228 }
5229 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005230 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005231 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005232 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005233 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005234 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005235 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5236 id, index, properties.toolType,
5237 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005238 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5239 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5240 }
5241#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005242 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005243}
5244
Jeff Brown65fd2512011-08-18 11:20:58 -07005245void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5246 mPointerSimple.currentCoords.clear();
5247 mPointerSimple.currentProperties.clear();
5248
5249 bool down, hovering;
5250 if (!mCurrentStylusIdBits.isEmpty()) {
5251 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5252 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5253 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5254 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5255 mPointerController->setPosition(x, y);
5256
5257 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5258 down = !hovering;
5259
5260 mPointerController->getPosition(&x, &y);
5261 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5262 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5263 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5264 mPointerSimple.currentProperties.id = 0;
5265 mPointerSimple.currentProperties.toolType =
5266 mCurrentCookedPointerData.pointerProperties[index].toolType;
5267 } else {
5268 down = false;
5269 hovering = false;
5270 }
5271
5272 dispatchPointerSimple(when, policyFlags, down, hovering);
5273}
5274
5275void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5276 abortPointerSimple(when, policyFlags);
5277}
5278
5279void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5280 mPointerSimple.currentCoords.clear();
5281 mPointerSimple.currentProperties.clear();
5282
5283 bool down, hovering;
5284 if (!mCurrentMouseIdBits.isEmpty()) {
5285 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5286 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5287 if (mLastMouseIdBits.hasBit(id)) {
5288 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5289 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5290 - mLastRawPointerData.pointers[lastIndex].x)
5291 * mPointerXMovementScale;
5292 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5293 - mLastRawPointerData.pointers[lastIndex].y)
5294 * mPointerYMovementScale;
5295
5296 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5297 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5298
5299 mPointerController->move(deltaX, deltaY);
5300 } else {
5301 mPointerVelocityControl.reset();
5302 }
5303
5304 down = isPointerDown(mCurrentButtonState);
5305 hovering = !down;
5306
5307 float x, y;
5308 mPointerController->getPosition(&x, &y);
5309 mPointerSimple.currentCoords.copyFrom(
5310 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5311 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5312 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5313 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5314 hovering ? 0.0f : 1.0f);
5315 mPointerSimple.currentProperties.id = 0;
5316 mPointerSimple.currentProperties.toolType =
5317 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5318 } else {
5319 mPointerVelocityControl.reset();
5320
5321 down = false;
5322 hovering = false;
5323 }
5324
5325 dispatchPointerSimple(when, policyFlags, down, hovering);
5326}
5327
5328void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5329 abortPointerSimple(when, policyFlags);
5330
5331 mPointerVelocityControl.reset();
5332}
5333
5334void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5335 bool down, bool hovering) {
5336 int32_t metaState = getContext()->getGlobalMetaState();
5337
5338 if (mPointerController != NULL) {
5339 if (down || hovering) {
5340 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5341 mPointerController->clearSpots();
5342 mPointerController->setButtonState(mCurrentButtonState);
5343 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5344 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5345 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5346 }
5347 }
5348
5349 if (mPointerSimple.down && !down) {
5350 mPointerSimple.down = false;
5351
5352 // Send up.
5353 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5354 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005355 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005356 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5357 mOrientedXPrecision, mOrientedYPrecision,
5358 mPointerSimple.downTime);
5359 getListener()->notifyMotion(&args);
5360 }
5361
5362 if (mPointerSimple.hovering && !hovering) {
5363 mPointerSimple.hovering = false;
5364
5365 // Send hover exit.
5366 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5367 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005368 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005369 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5370 mOrientedXPrecision, mOrientedYPrecision,
5371 mPointerSimple.downTime);
5372 getListener()->notifyMotion(&args);
5373 }
5374
5375 if (down) {
5376 if (!mPointerSimple.down) {
5377 mPointerSimple.down = true;
5378 mPointerSimple.downTime = when;
5379
5380 // Send down.
5381 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5382 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005383 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005384 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5385 mOrientedXPrecision, mOrientedYPrecision,
5386 mPointerSimple.downTime);
5387 getListener()->notifyMotion(&args);
5388 }
5389
5390 // Send move.
5391 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5392 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005393 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005394 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5395 mOrientedXPrecision, mOrientedYPrecision,
5396 mPointerSimple.downTime);
5397 getListener()->notifyMotion(&args);
5398 }
5399
5400 if (hovering) {
5401 if (!mPointerSimple.hovering) {
5402 mPointerSimple.hovering = true;
5403
5404 // Send hover enter.
5405 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5406 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005407 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005408 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5409 mOrientedXPrecision, mOrientedYPrecision,
5410 mPointerSimple.downTime);
5411 getListener()->notifyMotion(&args);
5412 }
5413
5414 // Send hover move.
5415 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5416 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005417 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005418 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5419 mOrientedXPrecision, mOrientedYPrecision,
5420 mPointerSimple.downTime);
5421 getListener()->notifyMotion(&args);
5422 }
5423
5424 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5425 float vscroll = mCurrentRawVScroll;
5426 float hscroll = mCurrentRawHScroll;
5427 mWheelYVelocityControl.move(when, NULL, &vscroll);
5428 mWheelXVelocityControl.move(when, &hscroll, NULL);
5429
5430 // Send scroll.
5431 PointerCoords pointerCoords;
5432 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5433 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5434 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5435
5436 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5437 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005438 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005439 1, &mPointerSimple.currentProperties, &pointerCoords,
5440 mOrientedXPrecision, mOrientedYPrecision,
5441 mPointerSimple.downTime);
5442 getListener()->notifyMotion(&args);
5443 }
5444
5445 // Save state.
5446 if (down || hovering) {
5447 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5448 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5449 } else {
5450 mPointerSimple.reset();
5451 }
5452}
5453
5454void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5455 mPointerSimple.currentCoords.clear();
5456 mPointerSimple.currentProperties.clear();
5457
5458 dispatchPointerSimple(when, policyFlags, false, false);
5459}
5460
Jeff Brownace13b12011-03-09 17:39:48 -08005461void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005462 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5463 const PointerProperties* properties, const PointerCoords* coords,
5464 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005465 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5466 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005467 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005468 uint32_t pointerCount = 0;
5469 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005470 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005471 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005472 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005473 pointerCoords[pointerCount].copyFrom(coords[index]);
5474
5475 if (changedId >= 0 && id == uint32_t(changedId)) {
5476 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5477 }
5478
5479 pointerCount += 1;
5480 }
5481
Steve Blockec193de2012-01-09 18:35:44 +00005482 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005483
5484 if (changedId >= 0 && pointerCount == 1) {
5485 // Replace initial down and final up action.
5486 // We can compare the action without masking off the changed pointer index
5487 // because we know the index is 0.
5488 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5489 action = AMOTION_EVENT_ACTION_DOWN;
5490 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5491 action = AMOTION_EVENT_ACTION_UP;
5492 } else {
5493 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005494 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005495 }
5496 }
5497
Jeff Brownbe1aa822011-07-27 16:04:54 -07005498 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005499 action, flags, metaState, buttonState, edgeFlags,
Jeff Brown83d616a2012-09-09 20:33:43 -07005500 mViewport.displayId, pointerCount, pointerProperties, pointerCoords,
5501 xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005502 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005503}
5504
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005505bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005506 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005507 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5508 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005509 bool changed = false;
5510 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005511 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005512 uint32_t inIndex = inIdToIndex[id];
5513 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005514
5515 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005516 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005517 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005518 PointerCoords& curOutCoords = outCoords[outIndex];
5519
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005520 if (curInProperties != curOutProperties) {
5521 curOutProperties.copyFrom(curInProperties);
5522 changed = true;
5523 }
5524
Jeff Brownace13b12011-03-09 17:39:48 -08005525 if (curInCoords != curOutCoords) {
5526 curOutCoords.copyFrom(curInCoords);
5527 changed = true;
5528 }
5529 }
5530 return changed;
5531}
5532
5533void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005534 if (mPointerController != NULL) {
5535 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5536 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005537}
5538
Jeff Brownbe1aa822011-07-27 16:04:54 -07005539bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5540 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5541 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005542}
5543
Jeff Brownbe1aa822011-07-27 16:04:54 -07005544const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005545 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005546 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005547 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005548 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005549
5550#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005551 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005552 "left=%d, top=%d, right=%d, bottom=%d",
5553 x, y,
5554 virtualKey.keyCode, virtualKey.scanCode,
5555 virtualKey.hitLeft, virtualKey.hitTop,
5556 virtualKey.hitRight, virtualKey.hitBottom);
5557#endif
5558
5559 if (virtualKey.isHit(x, y)) {
5560 return & virtualKey;
5561 }
5562 }
5563
5564 return NULL;
5565}
5566
Jeff Brownbe1aa822011-07-27 16:04:54 -07005567void TouchInputMapper::assignPointerIds() {
5568 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5569 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5570
5571 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005572
5573 if (currentPointerCount == 0) {
5574 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005575 return;
5576 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005577
Jeff Brownbe1aa822011-07-27 16:04:54 -07005578 if (lastPointerCount == 0) {
5579 // All pointers are new.
5580 for (uint32_t i = 0; i < currentPointerCount; i++) {
5581 uint32_t id = i;
5582 mCurrentRawPointerData.pointers[i].id = id;
5583 mCurrentRawPointerData.idToIndex[id] = i;
5584 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5585 }
5586 return;
5587 }
5588
5589 if (currentPointerCount == 1 && lastPointerCount == 1
5590 && mCurrentRawPointerData.pointers[0].toolType
5591 == mLastRawPointerData.pointers[0].toolType) {
5592 // Only one pointer and no change in count so it must have the same id as before.
5593 uint32_t id = mLastRawPointerData.pointers[0].id;
5594 mCurrentRawPointerData.pointers[0].id = id;
5595 mCurrentRawPointerData.idToIndex[id] = 0;
5596 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5597 return;
5598 }
5599
5600 // General case.
5601 // We build a heap of squared euclidean distances between current and last pointers
5602 // associated with the current and last pointer indices. Then, we find the best
5603 // match (by distance) for each current pointer.
5604 // The pointers must have the same tool type but it is possible for them to
5605 // transition from hovering to touching or vice-versa while retaining the same id.
5606 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5607
5608 uint32_t heapSize = 0;
5609 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5610 currentPointerIndex++) {
5611 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5612 lastPointerIndex++) {
5613 const RawPointerData::Pointer& currentPointer =
5614 mCurrentRawPointerData.pointers[currentPointerIndex];
5615 const RawPointerData::Pointer& lastPointer =
5616 mLastRawPointerData.pointers[lastPointerIndex];
5617 if (currentPointer.toolType == lastPointer.toolType) {
5618 int64_t deltaX = currentPointer.x - lastPointer.x;
5619 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005620
5621 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5622
5623 // Insert new element into the heap (sift up).
5624 heap[heapSize].currentPointerIndex = currentPointerIndex;
5625 heap[heapSize].lastPointerIndex = lastPointerIndex;
5626 heap[heapSize].distance = distance;
5627 heapSize += 1;
5628 }
5629 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005630 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005631
Jeff Brownbe1aa822011-07-27 16:04:54 -07005632 // Heapify
5633 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5634 startIndex -= 1;
5635 for (uint32_t parentIndex = startIndex; ;) {
5636 uint32_t childIndex = parentIndex * 2 + 1;
5637 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005638 break;
5639 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005640
5641 if (childIndex + 1 < heapSize
5642 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5643 childIndex += 1;
5644 }
5645
5646 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5647 break;
5648 }
5649
5650 swap(heap[parentIndex], heap[childIndex]);
5651 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005652 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005653 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005654
5655#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005656 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005657 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005658 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005659 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5660 heap[i].distance);
5661 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005662#endif
5663
Jeff Brownbe1aa822011-07-27 16:04:54 -07005664 // Pull matches out by increasing order of distance.
5665 // To avoid reassigning pointers that have already been matched, the loop keeps track
5666 // of which last and current pointers have been matched using the matchedXXXBits variables.
5667 // It also tracks the used pointer id bits.
5668 BitSet32 matchedLastBits(0);
5669 BitSet32 matchedCurrentBits(0);
5670 BitSet32 usedIdBits(0);
5671 bool first = true;
5672 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5673 while (heapSize > 0) {
5674 if (first) {
5675 // The first time through the loop, we just consume the root element of
5676 // the heap (the one with smallest distance).
5677 first = false;
5678 } else {
5679 // Previous iterations consumed the root element of the heap.
5680 // Pop root element off of the heap (sift down).
5681 heap[0] = heap[heapSize];
5682 for (uint32_t parentIndex = 0; ;) {
5683 uint32_t childIndex = parentIndex * 2 + 1;
5684 if (childIndex >= heapSize) {
5685 break;
5686 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005687
Jeff Brownbe1aa822011-07-27 16:04:54 -07005688 if (childIndex + 1 < heapSize
5689 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5690 childIndex += 1;
5691 }
5692
5693 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5694 break;
5695 }
5696
5697 swap(heap[parentIndex], heap[childIndex]);
5698 parentIndex = childIndex;
5699 }
5700
5701#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005702 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005703 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005704 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005705 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5706 heap[i].distance);
5707 }
5708#endif
5709 }
5710
5711 heapSize -= 1;
5712
5713 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5714 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5715
5716 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5717 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5718
5719 matchedCurrentBits.markBit(currentPointerIndex);
5720 matchedLastBits.markBit(lastPointerIndex);
5721
5722 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5723 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5724 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5725 mCurrentRawPointerData.markIdBit(id,
5726 mCurrentRawPointerData.isHovering(currentPointerIndex));
5727 usedIdBits.markBit(id);
5728
5729#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005730 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005731 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5732#endif
5733 break;
5734 }
5735 }
5736
5737 // Assign fresh ids to pointers that were not matched in the process.
5738 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5739 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5740 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5741
5742 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5743 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5744 mCurrentRawPointerData.markIdBit(id,
5745 mCurrentRawPointerData.isHovering(currentPointerIndex));
5746
5747#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005748 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005749 currentPointerIndex, id);
5750#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005751 }
5752}
5753
Jeff Brown6d0fec22010-07-23 21:28:06 -07005754int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005755 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5756 return AKEY_STATE_VIRTUAL;
5757 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005758
Jeff Brownbe1aa822011-07-27 16:04:54 -07005759 size_t numVirtualKeys = mVirtualKeys.size();
5760 for (size_t i = 0; i < numVirtualKeys; i++) {
5761 const VirtualKey& virtualKey = mVirtualKeys[i];
5762 if (virtualKey.keyCode == keyCode) {
5763 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005764 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005765 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005766
5767 return AKEY_STATE_UNKNOWN;
5768}
5769
5770int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005771 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5772 return AKEY_STATE_VIRTUAL;
5773 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005774
Jeff Brownbe1aa822011-07-27 16:04:54 -07005775 size_t numVirtualKeys = mVirtualKeys.size();
5776 for (size_t i = 0; i < numVirtualKeys; i++) {
5777 const VirtualKey& virtualKey = mVirtualKeys[i];
5778 if (virtualKey.scanCode == scanCode) {
5779 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005780 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005781 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005782
5783 return AKEY_STATE_UNKNOWN;
5784}
5785
5786bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5787 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005788 size_t numVirtualKeys = mVirtualKeys.size();
5789 for (size_t i = 0; i < numVirtualKeys; i++) {
5790 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005791
Jeff Brownbe1aa822011-07-27 16:04:54 -07005792 for (size_t i = 0; i < numCodes; i++) {
5793 if (virtualKey.keyCode == keyCodes[i]) {
5794 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005795 }
5796 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005797 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005798
5799 return true;
5800}
5801
5802
5803// --- SingleTouchInputMapper ---
5804
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005805SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5806 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005807}
5808
5809SingleTouchInputMapper::~SingleTouchInputMapper() {
5810}
5811
Jeff Brown65fd2512011-08-18 11:20:58 -07005812void SingleTouchInputMapper::reset(nsecs_t when) {
5813 mSingleTouchMotionAccumulator.reset(getDevice());
5814
5815 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005816}
5817
Jeff Brown6d0fec22010-07-23 21:28:06 -07005818void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005819 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005820
Jeff Brown65fd2512011-08-18 11:20:58 -07005821 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005822}
5823
Jeff Brown65fd2512011-08-18 11:20:58 -07005824void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005825 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005826 mCurrentRawPointerData.pointerCount = 1;
5827 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005828
Jeff Brown65fd2512011-08-18 11:20:58 -07005829 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5830 && (mTouchButtonAccumulator.isHovering()
5831 || (mRawPointerAxes.pressure.valid
5832 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005833 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005834
Jeff Brownbe1aa822011-07-27 16:04:54 -07005835 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005836 outPointer.id = 0;
5837 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5838 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5839 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5840 outPointer.touchMajor = 0;
5841 outPointer.touchMinor = 0;
5842 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5843 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5844 outPointer.orientation = 0;
5845 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005846 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5847 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005848 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5849 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5850 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5851 }
5852 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005853 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005854}
5855
Jeff Brownbe1aa822011-07-27 16:04:54 -07005856void SingleTouchInputMapper::configureRawPointerAxes() {
5857 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005858
Jeff Brownbe1aa822011-07-27 16:04:54 -07005859 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5860 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5861 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5862 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5863 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005864 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5865 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005866}
5867
Jeff Brown00710e92012-04-19 15:18:26 -07005868bool SingleTouchInputMapper::hasStylus() const {
5869 return mTouchButtonAccumulator.hasStylus();
5870}
5871
Jeff Brown6d0fec22010-07-23 21:28:06 -07005872
5873// --- MultiTouchInputMapper ---
5874
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005875MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005876 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005877}
5878
5879MultiTouchInputMapper::~MultiTouchInputMapper() {
5880}
5881
Jeff Brown65fd2512011-08-18 11:20:58 -07005882void MultiTouchInputMapper::reset(nsecs_t when) {
5883 mMultiTouchMotionAccumulator.reset(getDevice());
5884
Jeff Brown6894a292011-07-01 17:59:27 -07005885 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005886
Jeff Brown65fd2512011-08-18 11:20:58 -07005887 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005888}
5889
5890void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005891 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005892
Jeff Brown65fd2512011-08-18 11:20:58 -07005893 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005894}
5895
Jeff Brown65fd2512011-08-18 11:20:58 -07005896void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005897 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005898 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005899 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005900
Jeff Brown80fd47c2011-05-24 01:07:44 -07005901 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005902 const MultiTouchMotionAccumulator::Slot* inSlot =
5903 mMultiTouchMotionAccumulator.getSlot(inIndex);
5904 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005905 continue;
5906 }
5907
Jeff Brown80fd47c2011-05-24 01:07:44 -07005908 if (outCount >= MAX_POINTERS) {
5909#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005910 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005911 "ignoring the rest.",
5912 getDeviceName().string(), MAX_POINTERS);
5913#endif
5914 break; // too many fingers!
5915 }
5916
Jeff Brownbe1aa822011-07-27 16:04:54 -07005917 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005918 outPointer.x = inSlot->getX();
5919 outPointer.y = inSlot->getY();
5920 outPointer.pressure = inSlot->getPressure();
5921 outPointer.touchMajor = inSlot->getTouchMajor();
5922 outPointer.touchMinor = inSlot->getTouchMinor();
5923 outPointer.toolMajor = inSlot->getToolMajor();
5924 outPointer.toolMinor = inSlot->getToolMinor();
5925 outPointer.orientation = inSlot->getOrientation();
5926 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005927 outPointer.tiltX = 0;
5928 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005929
Jeff Brown49754db2011-07-01 17:37:58 -07005930 outPointer.toolType = inSlot->getToolType();
5931 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5932 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5933 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5934 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5935 }
Jeff Brown8d608662010-08-30 03:02:23 -07005936 }
5937
Jeff Brown65fd2512011-08-18 11:20:58 -07005938 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5939 && (mTouchButtonAccumulator.isHovering()
5940 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005941 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005942
Jeff Brown8d608662010-08-30 03:02:23 -07005943 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005944 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005945 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005946 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005947 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005948 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005949 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005950 if (mPointerTrackingIdMap[n] == trackingId) {
5951 id = n;
5952 }
5953 }
5954
5955 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005956 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005957 mPointerTrackingIdMap[id] = trackingId;
5958 }
5959 }
5960 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005961 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005962 mCurrentRawPointerData.clearIdBits();
5963 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005964 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005965 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005966 mCurrentRawPointerData.idToIndex[id] = outCount;
5967 mCurrentRawPointerData.markIdBit(id, isHovering);
5968 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005969 }
5970 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005971
Jeff Brown6d0fec22010-07-23 21:28:06 -07005972 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005973 }
5974
Jeff Brownbe1aa822011-07-27 16:04:54 -07005975 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005976 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005977
Jeff Brown65fd2512011-08-18 11:20:58 -07005978 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005979}
5980
Jeff Brownbe1aa822011-07-27 16:04:54 -07005981void MultiTouchInputMapper::configureRawPointerAxes() {
5982 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005983
Jeff Brownbe1aa822011-07-27 16:04:54 -07005984 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5985 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5986 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5987 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5988 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5989 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5990 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5991 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5992 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5993 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5994 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005995
Jeff Brownbe1aa822011-07-27 16:04:54 -07005996 if (mRawPointerAxes.trackingId.valid
5997 && mRawPointerAxes.slot.valid
5998 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5999 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07006000 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00006001 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006002 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07006003 getDeviceName().string(), slotCount, MAX_SLOTS);
6004 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07006005 }
Jeff Brown00710e92012-04-19 15:18:26 -07006006 mMultiTouchMotionAccumulator.configure(getDevice(),
6007 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006008 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07006009 mMultiTouchMotionAccumulator.configure(getDevice(),
6010 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006011 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07006012}
6013
Jeff Brown00710e92012-04-19 15:18:26 -07006014bool MultiTouchInputMapper::hasStylus() const {
6015 return mMultiTouchMotionAccumulator.hasStylus()
6016 || mTouchButtonAccumulator.hasStylus();
6017}
6018
Jeff Brown46b9ac02010-04-22 18:58:52 -07006019
Jeff Browncb1404e2011-01-15 18:14:15 -08006020// --- JoystickInputMapper ---
6021
6022JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
6023 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006024}
6025
6026JoystickInputMapper::~JoystickInputMapper() {
6027}
6028
6029uint32_t JoystickInputMapper::getSources() {
6030 return AINPUT_SOURCE_JOYSTICK;
6031}
6032
6033void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
6034 InputMapper::populateDeviceInfo(info);
6035
Jeff Brown6f2fba42011-02-19 01:08:02 -08006036 for (size_t i = 0; i < mAxes.size(); i++) {
6037 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08006038 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
6039 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08006040 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08006041 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
6042 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08006043 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006044 }
6045}
6046
6047void JoystickInputMapper::dump(String8& dump) {
6048 dump.append(INDENT2 "Joystick Input Mapper:\n");
6049
Jeff Brown6f2fba42011-02-19 01:08:02 -08006050 dump.append(INDENT3 "Axes:\n");
6051 size_t numAxes = mAxes.size();
6052 for (size_t i = 0; i < numAxes; i++) {
6053 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006054 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006055 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08006056 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006057 } else {
Jeff Brown85297452011-03-04 13:07:49 -08006058 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006059 }
Jeff Brown85297452011-03-04 13:07:49 -08006060 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6061 label = getAxisLabel(axis.axisInfo.highAxis);
6062 if (label) {
6063 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
6064 } else {
6065 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
6066 axis.axisInfo.splitValue);
6067 }
6068 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
6069 dump.append(" (invert)");
6070 }
6071
6072 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
6073 axis.min, axis.max, axis.flat, axis.fuzz);
6074 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
6075 "highScale=%0.5f, highOffset=%0.5f\n",
6076 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07006077 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
6078 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006079 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006080 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006081 }
6082}
6083
Jeff Brown65fd2512011-08-18 11:20:58 -07006084void JoystickInputMapper::configure(nsecs_t when,
6085 const InputReaderConfiguration* config, uint32_t changes) {
6086 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006087
Jeff Brown474dcb52011-06-14 20:22:50 -07006088 if (!changes) { // first time only
6089 // Collect all axes.
6090 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285a2011-08-31 12:56:34 -07006091 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6092 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6093 continue; // axis must be claimed by a different device
6094 }
6095
Jeff Brown474dcb52011-06-14 20:22:50 -07006096 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006097 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006098 if (rawAxisInfo.valid) {
6099 // Map axis.
6100 AxisInfo axisInfo;
6101 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6102 if (!explicitlyMapped) {
6103 // Axis is not explicitly mapped, will choose a generic axis later.
6104 axisInfo.mode = AxisInfo::MODE_NORMAL;
6105 axisInfo.axis = -1;
6106 }
6107
6108 // Apply flat override.
6109 int32_t rawFlat = axisInfo.flatOverride < 0
6110 ? rawAxisInfo.flat : axisInfo.flatOverride;
6111
6112 // Calculate scaling factors and limits.
6113 Axis axis;
6114 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6115 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6116 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6117 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6118 scale, 0.0f, highScale, 0.0f,
6119 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6120 } else if (isCenteredAxis(axisInfo.axis)) {
6121 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6122 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6123 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6124 scale, offset, scale, offset,
6125 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6126 } else {
6127 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6128 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6129 scale, 0.0f, scale, 0.0f,
6130 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6131 }
6132
6133 // To eliminate noise while the joystick is at rest, filter out small variations
6134 // in axis values up front.
6135 axis.filter = axis.flat * 0.25f;
6136
6137 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006138 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006139 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006140
Jeff Brown474dcb52011-06-14 20:22:50 -07006141 // If there are too many axes, start dropping them.
6142 // Prefer to keep explicitly mapped axes.
6143 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006144 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006145 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6146 pruneAxes(true);
6147 pruneAxes(false);
6148 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006149
Jeff Brown474dcb52011-06-14 20:22:50 -07006150 // Assign generic axis ids to remaining axes.
6151 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6152 size_t numAxes = mAxes.size();
6153 for (size_t i = 0; i < numAxes; i++) {
6154 Axis& axis = mAxes.editValueAt(i);
6155 if (axis.axisInfo.axis < 0) {
6156 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6157 && haveAxis(nextGenericAxisId)) {
6158 nextGenericAxisId += 1;
6159 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006160
Jeff Brown474dcb52011-06-14 20:22:50 -07006161 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6162 axis.axisInfo.axis = nextGenericAxisId;
6163 nextGenericAxisId += 1;
6164 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006165 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006166 "have already been assigned to other axes.",
6167 getDeviceName().string(), mAxes.keyAt(i));
6168 mAxes.removeItemsAt(i--);
6169 numAxes -= 1;
6170 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006171 }
6172 }
6173 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006174}
6175
Jeff Brown85297452011-03-04 13:07:49 -08006176bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006177 size_t numAxes = mAxes.size();
6178 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006179 const Axis& axis = mAxes.valueAt(i);
6180 if (axis.axisInfo.axis == axisId
6181 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6182 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006183 return true;
6184 }
6185 }
6186 return false;
6187}
Jeff Browncb1404e2011-01-15 18:14:15 -08006188
Jeff Brown6f2fba42011-02-19 01:08:02 -08006189void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6190 size_t i = mAxes.size();
6191 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6192 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6193 continue;
6194 }
Steve Block6215d3f2012-01-04 20:05:49 +00006195 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006196 getDeviceName().string(), mAxes.keyAt(i));
6197 mAxes.removeItemsAt(i);
6198 }
6199}
6200
6201bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6202 switch (axis) {
6203 case AMOTION_EVENT_AXIS_X:
6204 case AMOTION_EVENT_AXIS_Y:
6205 case AMOTION_EVENT_AXIS_Z:
6206 case AMOTION_EVENT_AXIS_RX:
6207 case AMOTION_EVENT_AXIS_RY:
6208 case AMOTION_EVENT_AXIS_RZ:
6209 case AMOTION_EVENT_AXIS_HAT_X:
6210 case AMOTION_EVENT_AXIS_HAT_Y:
6211 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006212 case AMOTION_EVENT_AXIS_RUDDER:
6213 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006214 return true;
6215 default:
6216 return false;
6217 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006218}
6219
Jeff Brown65fd2512011-08-18 11:20:58 -07006220void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006221 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006222 size_t numAxes = mAxes.size();
6223 for (size_t i = 0; i < numAxes; i++) {
6224 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006225 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006226 }
6227
Jeff Brown65fd2512011-08-18 11:20:58 -07006228 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006229}
6230
6231void JoystickInputMapper::process(const RawEvent* rawEvent) {
6232 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006233 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006234 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006235 if (index >= 0) {
6236 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006237 float newValue, highNewValue;
6238 switch (axis.axisInfo.mode) {
6239 case AxisInfo::MODE_INVERT:
6240 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6241 * axis.scale + axis.offset;
6242 highNewValue = 0.0f;
6243 break;
6244 case AxisInfo::MODE_SPLIT:
6245 if (rawEvent->value < axis.axisInfo.splitValue) {
6246 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6247 * axis.scale + axis.offset;
6248 highNewValue = 0.0f;
6249 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6250 newValue = 0.0f;
6251 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6252 * axis.highScale + axis.highOffset;
6253 } else {
6254 newValue = 0.0f;
6255 highNewValue = 0.0f;
6256 }
6257 break;
6258 default:
6259 newValue = rawEvent->value * axis.scale + axis.offset;
6260 highNewValue = 0.0f;
6261 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006262 }
Jeff Brown85297452011-03-04 13:07:49 -08006263 axis.newValue = newValue;
6264 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006265 }
6266 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006267 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006268
6269 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006270 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006271 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006272 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006273 break;
6274 }
6275 break;
6276 }
6277}
6278
Jeff Brown6f2fba42011-02-19 01:08:02 -08006279void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006280 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006281 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006282 }
6283
6284 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006285 int32_t buttonState = 0;
6286
6287 PointerProperties pointerProperties;
6288 pointerProperties.clear();
6289 pointerProperties.id = 0;
6290 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006291
Jeff Brown6f2fba42011-02-19 01:08:02 -08006292 PointerCoords pointerCoords;
6293 pointerCoords.clear();
6294
6295 size_t numAxes = mAxes.size();
6296 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006297 const Axis& axis = mAxes.valueAt(i);
6298 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6299 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6300 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6301 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006302 }
6303
Jeff Brown83d616a2012-09-09 20:33:43 -07006304 // Moving a joystick axis should not wake the device because joysticks can
Jeff Brown56194eb2011-03-02 19:23:13 -08006305 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6306 // button will likely wake the device.
6307 // TODO: Use the input device configuration to control this behavior more finely.
6308 uint32_t policyFlags = 0;
6309
Jeff Brownbe1aa822011-07-27 16:04:54 -07006310 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006311 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07006312 ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006313 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006314}
6315
Jeff Brown85297452011-03-04 13:07:49 -08006316bool JoystickInputMapper::filterAxes(bool force) {
6317 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006318 size_t numAxes = mAxes.size();
6319 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006320 Axis& axis = mAxes.editValueAt(i);
6321 if (force || hasValueChangedSignificantly(axis.filter,
6322 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6323 axis.currentValue = axis.newValue;
6324 atLeastOneSignificantChange = true;
6325 }
6326 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6327 if (force || hasValueChangedSignificantly(axis.filter,
6328 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6329 axis.highCurrentValue = axis.highNewValue;
6330 atLeastOneSignificantChange = true;
6331 }
6332 }
6333 }
6334 return atLeastOneSignificantChange;
6335}
6336
6337bool JoystickInputMapper::hasValueChangedSignificantly(
6338 float filter, float newValue, float currentValue, float min, float max) {
6339 if (newValue != currentValue) {
6340 // Filter out small changes in value unless the value is converging on the axis
6341 // bounds or center point. This is intended to reduce the amount of information
6342 // sent to applications by particularly noisy joysticks (such as PS3).
6343 if (fabs(newValue - currentValue) > filter
6344 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6345 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6346 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6347 return true;
6348 }
6349 }
6350 return false;
6351}
6352
6353bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6354 float filter, float newValue, float currentValue, float thresholdValue) {
6355 float newDistance = fabs(newValue - thresholdValue);
6356 if (newDistance < filter) {
6357 float oldDistance = fabs(currentValue - thresholdValue);
6358 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006359 return true;
6360 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006361 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006362 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006363}
6364
Jeff Brown46b9ac02010-04-22 18:58:52 -07006365} // namespace android