blob: 07731fc6842584de8eec7c47067a9027501ea9fb [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Browna47425a2012-04-13 04:09:27 -070039// Log debug messages about the vibrator.
40#define DEBUG_VIBRATOR 0
41
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042#include "InputReader.h"
43
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044#include <cutils/log.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070045#include <input/Keyboard.h>
46#include <input/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047
48#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070049#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070050#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051#include <errno.h>
52#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070053#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070054
Jeff Brown8d608662010-08-30 03:02:23 -070055#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070056#define INDENT2 " "
57#define INDENT3 " "
58#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070059#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070060
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070061namespace android {
62
Jeff Brownace13b12011-03-09 17:39:48 -080063// --- Constants ---
64
Jeff Brown80fd47c2011-05-24 01:07:44 -070065// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
66static const size_t MAX_SLOTS = 32;
67
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070068// --- Static Functions ---
69
70template<typename T>
71inline static T abs(const T& value) {
72 return value < 0 ? - value : value;
73}
74
75template<typename T>
76inline static T min(const T& a, const T& b) {
77 return a < b ? a : b;
78}
79
Jeff Brown5c225b12010-06-16 01:53:36 -070080template<typename T>
81inline static void swap(T& a, T& b) {
82 T temp = a;
83 a = b;
84 b = temp;
85}
86
Jeff Brown8d608662010-08-30 03:02:23 -070087inline static float avg(float x, float y) {
88 return (x + y) / 2;
89}
90
Jeff Brown2352b972011-04-12 22:39:53 -070091inline static float distance(float x1, float y1, float x2, float y2) {
92 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080093}
94
Jeff Brown517bb4c2011-01-14 19:09:23 -080095inline static int32_t signExtendNybble(int32_t value) {
96 return value >= 8 ? value - 16 : value;
97}
98
Jeff Brownef3d7e82010-09-30 14:33:04 -070099static inline const char* toString(bool value) {
100 return value ? "true" : "false";
101}
102
Jeff Brown9626b142011-03-03 02:09:54 -0800103static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
104 const int32_t map[][4], size_t mapSize) {
105 if (orientation != DISPLAY_ORIENTATION_0) {
106 for (size_t i = 0; i < mapSize; i++) {
107 if (value == map[i][0]) {
108 return map[i][orientation];
109 }
110 }
111 }
112 return value;
113}
114
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700115static const int32_t keyCodeRotationMap[][4] = {
116 // key codes enumerated counter-clockwise with the original (unrotated) key first
117 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700118 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
119 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
120 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
121 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700122};
Jeff Brown9626b142011-03-03 02:09:54 -0800123static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700124 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
125
Jeff Brown60691392011-07-15 19:08:26 -0700126static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800127 return rotateValueUsingRotationMap(keyCode, orientation,
128 keyCodeRotationMap, keyCodeRotationMapSize);
129}
130
Jeff Brown612891e2011-07-15 20:44:17 -0700131static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
132 float temp;
133 switch (orientation) {
134 case DISPLAY_ORIENTATION_90:
135 temp = *deltaX;
136 *deltaX = *deltaY;
137 *deltaY = -temp;
138 break;
139
140 case DISPLAY_ORIENTATION_180:
141 *deltaX = -*deltaX;
142 *deltaY = -*deltaY;
143 break;
144
145 case DISPLAY_ORIENTATION_270:
146 temp = *deltaX;
147 *deltaX = -*deltaY;
148 *deltaY = temp;
149 break;
150 }
151}
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
154 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
155}
156
Jeff Brownefd32662011-03-08 15:13:06 -0800157// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700158// button states. This determines whether the event is reported as a touch event.
159static bool isPointerDown(int32_t buttonState) {
160 return buttonState &
161 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700162 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800163}
164
Jeff Brown2352b972011-04-12 22:39:53 -0700165static float calculateCommonVector(float a, float b) {
166 if (a > 0 && b > 0) {
167 return a < b ? a : b;
168 } else if (a < 0 && b < 0) {
169 return a > b ? a : b;
170 } else {
171 return 0;
172 }
173}
174
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700175static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
176 nsecs_t when, int32_t deviceId, uint32_t source,
177 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
178 int32_t buttonState, int32_t keyCode) {
179 if (
180 (action == AKEY_EVENT_ACTION_DOWN
181 && !(lastButtonState & buttonState)
182 && (currentButtonState & buttonState))
183 || (action == AKEY_EVENT_ACTION_UP
184 && (lastButtonState & buttonState)
185 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700186 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700187 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700188 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700189 }
190}
191
192static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
193 nsecs_t when, int32_t deviceId, uint32_t source,
194 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
198 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
199 lastButtonState, currentButtonState,
200 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
201}
202
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700203
Jeff Brown65fd2512011-08-18 11:20:58 -0700204// --- InputReaderConfiguration ---
205
Jeff Brownd728bf52012-09-08 18:05:28 -0700206bool InputReaderConfiguration::getDisplayInfo(bool external, DisplayViewport* outViewport) const {
207 const DisplayViewport& viewport = external ? mExternalDisplay : mInternalDisplay;
208 if (viewport.displayId >= 0) {
209 *outViewport = viewport;
210 return true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700211 }
212 return false;
213}
214
Jeff Brownd728bf52012-09-08 18:05:28 -0700215void InputReaderConfiguration::setDisplayInfo(bool external, const DisplayViewport& viewport) {
216 DisplayViewport& v = external ? mExternalDisplay : mInternalDisplay;
217 v = viewport;
Jeff Brown65fd2512011-08-18 11:20:58 -0700218}
219
220
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700221// --- InputReader ---
222
223InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700224 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700225 const sp<InputListenerInterface>& listener) :
226 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700227 mGlobalMetaState(0), mGeneration(1),
228 mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700229 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700230 mQueuedListener = new QueuedInputListener(listener);
231
232 { // acquire lock
233 AutoMutex _l(mLock);
234
235 refreshConfigurationLocked(0);
236 updateGlobalMetaStateLocked();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700237 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700238}
239
240InputReader::~InputReader() {
241 for (size_t i = 0; i < mDevices.size(); i++) {
242 delete mDevices.valueAt(i);
243 }
244}
245
246void InputReader::loopOnce() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700247 int32_t oldGeneration;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700248 int32_t timeoutMillis;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700249 bool inputDevicesChanged = false;
250 Vector<InputDeviceInfo> inputDevices;
Jeff Brown474dcb52011-06-14 20:22:50 -0700251 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700252 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700253
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700254 oldGeneration = mGeneration;
255 timeoutMillis = -1;
256
Jeff Brownbe1aa822011-07-27 16:04:54 -0700257 uint32_t changes = mConfigurationChangesToRefresh;
258 if (changes) {
259 mConfigurationChangesToRefresh = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700260 timeoutMillis = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700261 refreshConfigurationLocked(changes);
Jeff Browna47425a2012-04-13 04:09:27 -0700262 } else if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700263 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
264 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
265 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700266 } // release lock
267
Jeff Brownb7198742011-03-18 18:14:26 -0700268 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700269
270 { // acquire lock
271 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800272 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700273
274 if (count) {
275 processEventsLocked(mEventBuffer, count);
276 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700277
278 if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700279 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800280 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700281#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800282 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700283#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800284 mNextTimeout = LLONG_MAX;
285 timeoutExpiredLocked(now);
286 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700287 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700288
289 if (oldGeneration != mGeneration) {
290 inputDevicesChanged = true;
291 getInputDevicesLocked(inputDevices);
292 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700293 } // release lock
294
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700295 // Send out a message that the describes the changed input devices.
296 if (inputDevicesChanged) {
297 mPolicy->notifyInputDevicesChanged(inputDevices);
298 }
299
Jeff Brownbe1aa822011-07-27 16:04:54 -0700300 // Flush queued events out to the listener.
301 // This must happen outside of the lock because the listener could potentially call
302 // back into the InputReader's methods, such as getScanCodeState, or become blocked
303 // on another thread similarly waiting to acquire the InputReader lock thereby
304 // resulting in a deadlock. This situation is actually quite plausible because the
305 // listener is actually the input dispatcher, which calls into the window manager,
306 // which occasionally calls into the input reader.
307 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700308}
309
Jeff Brownbe1aa822011-07-27 16:04:54 -0700310void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700311 for (const RawEvent* rawEvent = rawEvents; count;) {
312 int32_t type = rawEvent->type;
313 size_t batchSize = 1;
314 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
315 int32_t deviceId = rawEvent->deviceId;
316 while (batchSize < count) {
317 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
318 || rawEvent[batchSize].deviceId != deviceId) {
319 break;
320 }
321 batchSize += 1;
322 }
323#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000324 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700325#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700326 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700327 } else {
328 switch (rawEvent->type) {
329 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700330 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700331 break;
332 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700333 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700334 break;
335 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700336 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700337 break;
338 default:
Steve Blockec193de2012-01-09 18:35:44 +0000339 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700340 break;
341 }
342 }
343 count -= batchSize;
344 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700345 }
346}
347
Jeff Brown65fd2512011-08-18 11:20:58 -0700348void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700349 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
350 if (deviceIndex >= 0) {
351 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
352 return;
353 }
354
Jeff Browne38fdfa2012-04-06 14:51:01 -0700355 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
357
Jeff Browne38fdfa2012-04-06 14:51:01 -0700358 InputDevice* device = createDeviceLocked(deviceId, identifier, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700359 device->configure(when, &mConfig, 0);
360 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700361
Jeff Brown8d608662010-08-30 03:02:23 -0700362 if (device->isIgnored()) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700363 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
364 identifier.name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700365 } else {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700366 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
367 identifier.name.string(), device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700368 }
369
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700370 mDevices.add(deviceId, device);
371 bumpGenerationLocked();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700372}
373
Jeff Brown65fd2512011-08-18 11:20:58 -0700374void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700376 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700377 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000378 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700379 return;
380 }
381
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700382 device = mDevices.valueAt(deviceIndex);
383 mDevices.removeItemsAt(deviceIndex, 1);
384 bumpGenerationLocked();
385
Jeff Brown6d0fec22010-07-23 21:28:06 -0700386 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000387 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 device->getId(), device->getName().string());
389 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000390 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700391 device->getId(), device->getName().string(), device->getSources());
392 }
393
Jeff Brown65fd2512011-08-18 11:20:58 -0700394 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700396}
397
Jeff Brownbe1aa822011-07-27 16:04:54 -0700398InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700399 const InputDeviceIdentifier& identifier, uint32_t classes) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700400 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
401 identifier, classes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700402
Jeff Brown56194eb2011-03-02 19:23:13 -0800403 // External devices.
404 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
405 device->setExternal(true);
406 }
407
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408 // Switch-like devices.
409 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
410 device->addMapper(new SwitchInputMapper(device));
411 }
412
Jeff Browna47425a2012-04-13 04:09:27 -0700413 // Vibrator-like devices.
414 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
415 device->addMapper(new VibratorInputMapper(device));
416 }
417
Jeff Brown6d0fec22010-07-23 21:28:06 -0700418 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800419 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
421 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800422 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 }
424 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
425 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
426 }
427 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800428 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700429 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800430 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800431 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800432 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433
Jeff Brownefd32662011-03-08 15:13:06 -0800434 if (keyboardSource != 0) {
435 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700436 }
437
Jeff Brown83c09682010-12-23 17:50:18 -0800438 // Cursor-like devices.
439 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
440 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441 }
442
Jeff Brown58a2da82011-01-25 16:02:22 -0800443 // Touchscreens and touchpad devices.
444 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800445 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800446 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800447 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700448 }
449
Jeff Browncb1404e2011-01-15 18:14:15 -0800450 // Joystick-like devices.
451 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
452 device->addMapper(new JoystickInputMapper(device));
453 }
454
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455 return device;
456}
457
Jeff Brownbe1aa822011-07-27 16:04:54 -0700458void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700459 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700460 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
461 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000462 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700463 return;
464 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465
Jeff Brownbe1aa822011-07-27 16:04:54 -0700466 InputDevice* device = mDevices.valueAt(deviceIndex);
467 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000468 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700469 return;
470 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700471
Jeff Brownbe1aa822011-07-27 16:04:54 -0700472 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700473}
474
Jeff Brownbe1aa822011-07-27 16:04:54 -0700475void InputReader::timeoutExpiredLocked(nsecs_t when) {
476 for (size_t i = 0; i < mDevices.size(); i++) {
477 InputDevice* device = mDevices.valueAt(i);
478 if (!device->isIgnored()) {
479 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700480 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700481 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700482}
483
Jeff Brownbe1aa822011-07-27 16:04:54 -0700484void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700485 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700486 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487
Jeff Brown6d0fec22010-07-23 21:28:06 -0700488 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700489 NotifyConfigurationChangedArgs args(when);
490 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700491}
492
Jeff Brownbe1aa822011-07-27 16:04:54 -0700493void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700494 mPolicy->getReaderConfiguration(&mConfig);
495 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
496
Jeff Brown474dcb52011-06-14 20:22:50 -0700497 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000498 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700499 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700500
501 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
502 mEventHub->requestReopenDevices();
503 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700504 for (size_t i = 0; i < mDevices.size(); i++) {
505 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700506 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700507 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700508 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509 }
510}
511
Jeff Brownbe1aa822011-07-27 16:04:54 -0700512void InputReader::updateGlobalMetaStateLocked() {
513 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700514
Jeff Brownbe1aa822011-07-27 16:04:54 -0700515 for (size_t i = 0; i < mDevices.size(); i++) {
516 InputDevice* device = mDevices.valueAt(i);
517 mGlobalMetaState |= device->getMetaState();
518 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700519}
520
Jeff Brownbe1aa822011-07-27 16:04:54 -0700521int32_t InputReader::getGlobalMetaStateLocked() {
522 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700523}
524
Jeff Brownbe1aa822011-07-27 16:04:54 -0700525void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800526 mDisableVirtualKeysTimeout = time;
527}
528
Jeff Brownbe1aa822011-07-27 16:04:54 -0700529bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800530 InputDevice* device, int32_t keyCode, int32_t scanCode) {
531 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000532 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800533 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
534 device->getName().string(),
535 (mDisableVirtualKeysTimeout - now) * 0.000001,
536 keyCode, scanCode);
537 return true;
538 } else {
539 return false;
540 }
541}
542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543void InputReader::fadePointerLocked() {
544 for (size_t i = 0; i < mDevices.size(); i++) {
545 InputDevice* device = mDevices.valueAt(i);
546 device->fadePointer();
547 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800548}
549
Jeff Brownbe1aa822011-07-27 16:04:54 -0700550void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700551 if (when < mNextTimeout) {
552 mNextTimeout = when;
Jeff Browna47425a2012-04-13 04:09:27 -0700553 mEventHub->wake();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700554 }
555}
556
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700557int32_t InputReader::bumpGenerationLocked() {
558 return ++mGeneration;
559}
560
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700561void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700562 AutoMutex _l(mLock);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700563 getInputDevicesLocked(outInputDevices);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700564}
565
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700566void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) {
567 outInputDevices.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700568
Jeff Brownbe1aa822011-07-27 16:04:54 -0700569 size_t numDevices = mDevices.size();
570 for (size_t i = 0; i < numDevices; i++) {
571 InputDevice* device = mDevices.valueAt(i);
572 if (!device->isIgnored()) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700573 outInputDevices.push();
574 device->getDeviceInfo(&outInputDevices.editTop());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700576 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700577}
578
579int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
580 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 AutoMutex _l(mLock);
582
583 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700584}
585
586int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
587 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700588 AutoMutex _l(mLock);
589
590 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700591}
592
593int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700594 AutoMutex _l(mLock);
595
596 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700597}
598
Jeff Brownbe1aa822011-07-27 16:04:54 -0700599int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700601 int32_t result = AKEY_STATE_UNKNOWN;
602 if (deviceId >= 0) {
603 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
604 if (deviceIndex >= 0) {
605 InputDevice* device = mDevices.valueAt(deviceIndex);
606 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
607 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700608 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700609 }
610 } else {
611 size_t numDevices = mDevices.size();
612 for (size_t i = 0; i < numDevices; i++) {
613 InputDevice* device = mDevices.valueAt(i);
614 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800615 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
616 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
617 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
618 if (currentResult >= AKEY_STATE_DOWN) {
619 return currentResult;
620 } else if (currentResult == AKEY_STATE_UP) {
621 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700622 }
623 }
624 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700625 }
626 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700627}
628
629bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
630 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631 AutoMutex _l(mLock);
632
Jeff Brown6d0fec22010-07-23 21:28:06 -0700633 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700634 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700635}
636
Jeff Brownbe1aa822011-07-27 16:04:54 -0700637bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
638 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
639 bool result = false;
640 if (deviceId >= 0) {
641 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
642 if (deviceIndex >= 0) {
643 InputDevice* device = mDevices.valueAt(deviceIndex);
644 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
645 result = device->markSupportedKeyCodes(sourceMask,
646 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700647 }
648 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700649 } else {
650 size_t numDevices = mDevices.size();
651 for (size_t i = 0; i < numDevices; i++) {
652 InputDevice* device = mDevices.valueAt(i);
653 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
654 result |= device->markSupportedKeyCodes(sourceMask,
655 numCodes, keyCodes, outFlags);
656 }
657 }
658 }
659 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700660}
661
Jeff Brown474dcb52011-06-14 20:22:50 -0700662void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700663 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700664
Jeff Brownbe1aa822011-07-27 16:04:54 -0700665 if (changes) {
666 bool needWake = !mConfigurationChangesToRefresh;
667 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700668
669 if (needWake) {
670 mEventHub->wake();
671 }
672 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700673}
674
Jeff Browna47425a2012-04-13 04:09:27 -0700675void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
676 ssize_t repeat, int32_t token) {
677 AutoMutex _l(mLock);
678
679 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
680 if (deviceIndex >= 0) {
681 InputDevice* device = mDevices.valueAt(deviceIndex);
682 device->vibrate(pattern, patternSize, repeat, token);
683 }
684}
685
686void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
687 AutoMutex _l(mLock);
688
689 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
690 if (deviceIndex >= 0) {
691 InputDevice* device = mDevices.valueAt(deviceIndex);
692 device->cancelVibrate(token);
693 }
694}
695
Jeff Brownb88102f2010-09-08 11:49:43 -0700696void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700697 AutoMutex _l(mLock);
698
Jeff Brownf2f487182010-10-01 17:46:21 -0700699 mEventHub->dump(dump);
700 dump.append("\n");
701
702 dump.append("Input Reader State:\n");
703
Jeff Brownbe1aa822011-07-27 16:04:54 -0700704 for (size_t i = 0; i < mDevices.size(); i++) {
705 mDevices.valueAt(i)->dump(dump);
706 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700707
708 dump.append(INDENT "Configuration:\n");
709 dump.append(INDENT2 "ExcludedDeviceNames: [");
710 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
711 if (i != 0) {
712 dump.append(", ");
713 }
714 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
715 }
716 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700717 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
718 mConfig.virtualKeyQuietTime * 0.000001f);
719
Jeff Brown19c97d462011-06-01 12:33:19 -0700720 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
721 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
722 mConfig.pointerVelocityControlParameters.scale,
723 mConfig.pointerVelocityControlParameters.lowThreshold,
724 mConfig.pointerVelocityControlParameters.highThreshold,
725 mConfig.pointerVelocityControlParameters.acceleration);
726
727 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
728 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
729 mConfig.wheelVelocityControlParameters.scale,
730 mConfig.wheelVelocityControlParameters.lowThreshold,
731 mConfig.wheelVelocityControlParameters.highThreshold,
732 mConfig.wheelVelocityControlParameters.acceleration);
733
Jeff Brown214eaf42011-05-26 19:17:02 -0700734 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700735 dump.appendFormat(INDENT3 "Enabled: %s\n",
736 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700737 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
738 mConfig.pointerGestureQuietInterval * 0.000001f);
739 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
740 mConfig.pointerGestureDragMinSwitchSpeed);
741 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
742 mConfig.pointerGestureTapInterval * 0.000001f);
743 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
744 mConfig.pointerGestureTapDragInterval * 0.000001f);
745 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
746 mConfig.pointerGestureTapSlop);
747 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
748 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700749 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
750 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700751 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
752 mConfig.pointerGestureSwipeTransitionAngleCosine);
753 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
754 mConfig.pointerGestureSwipeMaxWidthRatio);
755 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
756 mConfig.pointerGestureMovementSpeedRatio);
757 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
758 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700759}
760
Jeff Brown89ef0722011-08-10 16:25:21 -0700761void InputReader::monitor() {
762 // Acquire and release the lock to ensure that the reader has not deadlocked.
763 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800764 mEventHub->wake();
765 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700766 mLock.unlock();
767
768 // Check the EventHub
769 mEventHub->monitor();
770}
771
Jeff Brown6d0fec22010-07-23 21:28:06 -0700772
Jeff Brownbe1aa822011-07-27 16:04:54 -0700773// --- InputReader::ContextImpl ---
774
775InputReader::ContextImpl::ContextImpl(InputReader* reader) :
776 mReader(reader) {
777}
778
779void InputReader::ContextImpl::updateGlobalMetaState() {
780 // lock is already held by the input loop
781 mReader->updateGlobalMetaStateLocked();
782}
783
784int32_t InputReader::ContextImpl::getGlobalMetaState() {
785 // lock is already held by the input loop
786 return mReader->getGlobalMetaStateLocked();
787}
788
789void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
790 // lock is already held by the input loop
791 mReader->disableVirtualKeysUntilLocked(time);
792}
793
794bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
795 InputDevice* device, int32_t keyCode, int32_t scanCode) {
796 // lock is already held by the input loop
797 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
798}
799
800void InputReader::ContextImpl::fadePointer() {
801 // lock is already held by the input loop
802 mReader->fadePointerLocked();
803}
804
805void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
806 // lock is already held by the input loop
807 mReader->requestTimeoutAtTimeLocked(when);
808}
809
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700810int32_t InputReader::ContextImpl::bumpGeneration() {
811 // lock is already held by the input loop
812 return mReader->bumpGenerationLocked();
813}
814
Jeff Brownbe1aa822011-07-27 16:04:54 -0700815InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
816 return mReader->mPolicy.get();
817}
818
819InputListenerInterface* InputReader::ContextImpl::getListener() {
820 return mReader->mQueuedListener.get();
821}
822
823EventHubInterface* InputReader::ContextImpl::getEventHub() {
824 return mReader->mEventHub.get();
825}
826
827
Jeff Brown6d0fec22010-07-23 21:28:06 -0700828// --- InputReaderThread ---
829
830InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
831 Thread(/*canCallJava*/ true), mReader(reader) {
832}
833
834InputReaderThread::~InputReaderThread() {
835}
836
837bool InputReaderThread::threadLoop() {
838 mReader->loopOnce();
839 return true;
840}
841
842
843// --- InputDevice ---
844
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700845InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700846 const InputDeviceIdentifier& identifier, uint32_t classes) :
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700847 mContext(context), mId(id), mGeneration(generation),
848 mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285af2011-08-31 12:56:34 -0700849 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700850}
851
852InputDevice::~InputDevice() {
853 size_t numMappers = mMappers.size();
854 for (size_t i = 0; i < numMappers; i++) {
855 delete mMappers[i];
856 }
857 mMappers.clear();
858}
859
Jeff Brownef3d7e82010-09-30 14:33:04 -0700860void InputDevice::dump(String8& dump) {
861 InputDeviceInfo deviceInfo;
862 getDeviceInfo(& deviceInfo);
863
Jeff Brown90655042010-12-02 13:50:46 -0800864 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700865 deviceInfo.getDisplayName().string());
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700866 dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration);
Jeff Brown56194eb2011-03-02 19:23:13 -0800867 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700868 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
869 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800870
Jeff Brownefd32662011-03-08 15:13:06 -0800871 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800872 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700873 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800874 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800875 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
876 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800877 char name[32];
878 if (label) {
879 strncpy(name, label, sizeof(name));
880 name[sizeof(name) - 1] = '\0';
881 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800882 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800883 }
Jeff Brownefd32662011-03-08 15:13:06 -0800884 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
Michael Wrightc6091c62013-04-01 20:56:04 -0700885 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
886 name, range.source, range.min, range.max, range.flat, range.fuzz,
887 range.resolution);
Jeff Browncc0c1592011-02-19 05:07:28 -0800888 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700889 }
890
891 size_t numMappers = mMappers.size();
892 for (size_t i = 0; i < numMappers; i++) {
893 InputMapper* mapper = mMappers[i];
894 mapper->dump(dump);
895 }
896}
897
Jeff Brown6d0fec22010-07-23 21:28:06 -0700898void InputDevice::addMapper(InputMapper* mapper) {
899 mMappers.add(mapper);
900}
901
Jeff Brown65fd2512011-08-18 11:20:58 -0700902void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700903 mSources = 0;
904
Jeff Brown474dcb52011-06-14 20:22:50 -0700905 if (!isIgnored()) {
906 if (!changes) { // first time only
907 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
908 }
909
Jeff Brown6ec6f792012-04-17 16:52:41 -0700910 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Jeff Brown61c08242012-04-19 11:14:33 -0700911 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
912 sp<KeyCharacterMap> keyboardLayout =
913 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor);
914 if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
915 bumpGeneration();
916 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700917 }
918 }
919
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700920 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
921 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
922 String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
923 if (mAlias != alias) {
924 mAlias = alias;
925 bumpGeneration();
926 }
927 }
928 }
929
Jeff Brown474dcb52011-06-14 20:22:50 -0700930 size_t numMappers = mMappers.size();
931 for (size_t i = 0; i < numMappers; i++) {
932 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700933 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700934 mSources |= mapper->getSources();
935 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700936 }
937}
938
Jeff Brown65fd2512011-08-18 11:20:58 -0700939void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700940 size_t numMappers = mMappers.size();
941 for (size_t i = 0; i < numMappers; i++) {
942 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700943 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700944 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700945
946 mContext->updateGlobalMetaState();
947
948 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700949}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700950
Jeff Brownb7198742011-03-18 18:14:26 -0700951void InputDevice::process(const RawEvent* rawEvents, size_t count) {
952 // Process all of the events in order for each mapper.
953 // We cannot simply ask each mapper to process them in bulk because mappers may
954 // have side-effects that must be interleaved. For example, joystick movement events and
955 // gamepad button presses are handled by different mappers but they should be dispatched
956 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700957 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700958 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
959#if DEBUG_RAW_EVENTS
Jeff Brownf33b2b22012-10-05 17:59:56 -0700960 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld",
961 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
962 rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700963#endif
964
Jeff Brown80fd47c2011-05-24 01:07:44 -0700965 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700966 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700967 mDropUntilNextSync = false;
968#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000969 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700970#endif
971 } else {
972#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000973 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700974#endif
975 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700976 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700977 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700978 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700979 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700980 } else {
981 for (size_t i = 0; i < numMappers; i++) {
982 InputMapper* mapper = mMappers[i];
983 mapper->process(rawEvent);
984 }
Jeff Brownb7198742011-03-18 18:14:26 -0700985 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700986 }
987}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700988
Jeff Brownaa3855d2011-03-17 01:34:19 -0700989void InputDevice::timeoutExpired(nsecs_t when) {
990 size_t numMappers = mMappers.size();
991 for (size_t i = 0; i < numMappers; i++) {
992 InputMapper* mapper = mMappers[i];
993 mapper->timeoutExpired(when);
994 }
995}
996
Jeff Brown6d0fec22010-07-23 21:28:06 -0700997void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Browndaa37532012-05-01 15:54:03 -0700998 outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700999
1000 size_t numMappers = mMappers.size();
1001 for (size_t i = 0; i < numMappers; i++) {
1002 InputMapper* mapper = mMappers[i];
1003 mapper->populateDeviceInfo(outDeviceInfo);
1004 }
1005}
1006
1007int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1008 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1009}
1010
1011int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1012 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1013}
1014
1015int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1016 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1017}
1018
1019int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1020 int32_t result = AKEY_STATE_UNKNOWN;
1021 size_t numMappers = mMappers.size();
1022 for (size_t i = 0; i < numMappers; i++) {
1023 InputMapper* mapper = mMappers[i];
1024 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001025 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1026 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1027 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1028 if (currentResult >= AKEY_STATE_DOWN) {
1029 return currentResult;
1030 } else if (currentResult == AKEY_STATE_UP) {
1031 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001032 }
1033 }
1034 }
1035 return result;
1036}
1037
1038bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1039 const int32_t* keyCodes, uint8_t* outFlags) {
1040 bool result = false;
1041 size_t numMappers = mMappers.size();
1042 for (size_t i = 0; i < numMappers; i++) {
1043 InputMapper* mapper = mMappers[i];
1044 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1045 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1046 }
1047 }
1048 return result;
1049}
1050
Jeff Browna47425a2012-04-13 04:09:27 -07001051void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1052 int32_t token) {
1053 size_t numMappers = mMappers.size();
1054 for (size_t i = 0; i < numMappers; i++) {
1055 InputMapper* mapper = mMappers[i];
1056 mapper->vibrate(pattern, patternSize, repeat, token);
1057 }
1058}
1059
1060void InputDevice::cancelVibrate(int32_t token) {
1061 size_t numMappers = mMappers.size();
1062 for (size_t i = 0; i < numMappers; i++) {
1063 InputMapper* mapper = mMappers[i];
1064 mapper->cancelVibrate(token);
1065 }
1066}
1067
Jeff Brown6d0fec22010-07-23 21:28:06 -07001068int32_t InputDevice::getMetaState() {
1069 int32_t result = 0;
1070 size_t numMappers = mMappers.size();
1071 for (size_t i = 0; i < numMappers; i++) {
1072 InputMapper* mapper = mMappers[i];
1073 result |= mapper->getMetaState();
1074 }
1075 return result;
1076}
1077
Jeff Brown05dc66a2011-03-02 14:41:58 -08001078void InputDevice::fadePointer() {
1079 size_t numMappers = mMappers.size();
1080 for (size_t i = 0; i < numMappers; i++) {
1081 InputMapper* mapper = mMappers[i];
1082 mapper->fadePointer();
1083 }
1084}
1085
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001086void InputDevice::bumpGeneration() {
1087 mGeneration = mContext->bumpGeneration();
1088}
1089
Jeff Brown65fd2512011-08-18 11:20:58 -07001090void InputDevice::notifyReset(nsecs_t when) {
1091 NotifyDeviceResetArgs args(when, mId);
1092 mContext->getListener()->notifyDeviceReset(&args);
1093}
1094
Jeff Brown6d0fec22010-07-23 21:28:06 -07001095
Jeff Brown49754db2011-07-01 17:37:58 -07001096// --- CursorButtonAccumulator ---
1097
1098CursorButtonAccumulator::CursorButtonAccumulator() {
1099 clearButtons();
1100}
1101
Jeff Brown65fd2512011-08-18 11:20:58 -07001102void CursorButtonAccumulator::reset(InputDevice* device) {
1103 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1104 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1105 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1106 mBtnBack = device->isKeyPressed(BTN_BACK);
1107 mBtnSide = device->isKeyPressed(BTN_SIDE);
1108 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1109 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1110 mBtnTask = device->isKeyPressed(BTN_TASK);
1111}
1112
Jeff Brown49754db2011-07-01 17:37:58 -07001113void CursorButtonAccumulator::clearButtons() {
1114 mBtnLeft = 0;
1115 mBtnRight = 0;
1116 mBtnMiddle = 0;
1117 mBtnBack = 0;
1118 mBtnSide = 0;
1119 mBtnForward = 0;
1120 mBtnExtra = 0;
1121 mBtnTask = 0;
1122}
1123
1124void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1125 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001126 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001127 case BTN_LEFT:
1128 mBtnLeft = rawEvent->value;
1129 break;
1130 case BTN_RIGHT:
1131 mBtnRight = rawEvent->value;
1132 break;
1133 case BTN_MIDDLE:
1134 mBtnMiddle = rawEvent->value;
1135 break;
1136 case BTN_BACK:
1137 mBtnBack = rawEvent->value;
1138 break;
1139 case BTN_SIDE:
1140 mBtnSide = rawEvent->value;
1141 break;
1142 case BTN_FORWARD:
1143 mBtnForward = rawEvent->value;
1144 break;
1145 case BTN_EXTRA:
1146 mBtnExtra = rawEvent->value;
1147 break;
1148 case BTN_TASK:
1149 mBtnTask = rawEvent->value;
1150 break;
1151 }
1152 }
1153}
1154
1155uint32_t CursorButtonAccumulator::getButtonState() const {
1156 uint32_t result = 0;
1157 if (mBtnLeft) {
1158 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1159 }
1160 if (mBtnRight) {
1161 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1162 }
1163 if (mBtnMiddle) {
1164 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1165 }
1166 if (mBtnBack || mBtnSide) {
1167 result |= AMOTION_EVENT_BUTTON_BACK;
1168 }
1169 if (mBtnForward || mBtnExtra) {
1170 result |= AMOTION_EVENT_BUTTON_FORWARD;
1171 }
1172 return result;
1173}
1174
1175
1176// --- CursorMotionAccumulator ---
1177
Jeff Brown65fd2512011-08-18 11:20:58 -07001178CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001179 clearRelativeAxes();
1180}
1181
Jeff Brown65fd2512011-08-18 11:20:58 -07001182void CursorMotionAccumulator::reset(InputDevice* device) {
1183 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001184}
1185
1186void CursorMotionAccumulator::clearRelativeAxes() {
1187 mRelX = 0;
1188 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001189}
1190
1191void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1192 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001193 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001194 case REL_X:
1195 mRelX = rawEvent->value;
1196 break;
1197 case REL_Y:
1198 mRelY = rawEvent->value;
1199 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001200 }
1201 }
1202}
1203
1204void CursorMotionAccumulator::finishSync() {
1205 clearRelativeAxes();
1206}
1207
1208
1209// --- CursorScrollAccumulator ---
1210
1211CursorScrollAccumulator::CursorScrollAccumulator() :
1212 mHaveRelWheel(false), mHaveRelHWheel(false) {
1213 clearRelativeAxes();
1214}
1215
1216void CursorScrollAccumulator::configure(InputDevice* device) {
1217 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1218 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1219}
1220
1221void CursorScrollAccumulator::reset(InputDevice* device) {
1222 clearRelativeAxes();
1223}
1224
1225void CursorScrollAccumulator::clearRelativeAxes() {
1226 mRelWheel = 0;
1227 mRelHWheel = 0;
1228}
1229
1230void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1231 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001232 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001233 case REL_WHEEL:
1234 mRelWheel = rawEvent->value;
1235 break;
1236 case REL_HWHEEL:
1237 mRelHWheel = rawEvent->value;
1238 break;
1239 }
1240 }
1241}
1242
Jeff Brown65fd2512011-08-18 11:20:58 -07001243void CursorScrollAccumulator::finishSync() {
1244 clearRelativeAxes();
1245}
1246
Jeff Brown49754db2011-07-01 17:37:58 -07001247
1248// --- TouchButtonAccumulator ---
1249
1250TouchButtonAccumulator::TouchButtonAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001251 mHaveBtnTouch(false), mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001252 clearButtons();
1253}
1254
1255void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001256 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
Jeff Brown00710e92012-04-19 15:18:26 -07001257 mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1258 || device->hasKey(BTN_TOOL_RUBBER)
1259 || device->hasKey(BTN_TOOL_BRUSH)
1260 || device->hasKey(BTN_TOOL_PENCIL)
1261 || device->hasKey(BTN_TOOL_AIRBRUSH);
Jeff Brown65fd2512011-08-18 11:20:58 -07001262}
1263
1264void TouchButtonAccumulator::reset(InputDevice* device) {
1265 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1266 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1267 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1268 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1269 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1270 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1271 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1272 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1273 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1274 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1275 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001276 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1277 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1278 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001279}
1280
1281void TouchButtonAccumulator::clearButtons() {
1282 mBtnTouch = 0;
1283 mBtnStylus = 0;
1284 mBtnStylus2 = 0;
1285 mBtnToolFinger = 0;
1286 mBtnToolPen = 0;
1287 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001288 mBtnToolBrush = 0;
1289 mBtnToolPencil = 0;
1290 mBtnToolAirbrush = 0;
1291 mBtnToolMouse = 0;
1292 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001293 mBtnToolDoubleTap = 0;
1294 mBtnToolTripleTap = 0;
1295 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001296}
1297
1298void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1299 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001300 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001301 case BTN_TOUCH:
1302 mBtnTouch = rawEvent->value;
1303 break;
1304 case BTN_STYLUS:
1305 mBtnStylus = rawEvent->value;
1306 break;
1307 case BTN_STYLUS2:
1308 mBtnStylus2 = rawEvent->value;
1309 break;
1310 case BTN_TOOL_FINGER:
1311 mBtnToolFinger = rawEvent->value;
1312 break;
1313 case BTN_TOOL_PEN:
1314 mBtnToolPen = rawEvent->value;
1315 break;
1316 case BTN_TOOL_RUBBER:
1317 mBtnToolRubber = rawEvent->value;
1318 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001319 case BTN_TOOL_BRUSH:
1320 mBtnToolBrush = rawEvent->value;
1321 break;
1322 case BTN_TOOL_PENCIL:
1323 mBtnToolPencil = rawEvent->value;
1324 break;
1325 case BTN_TOOL_AIRBRUSH:
1326 mBtnToolAirbrush = rawEvent->value;
1327 break;
1328 case BTN_TOOL_MOUSE:
1329 mBtnToolMouse = rawEvent->value;
1330 break;
1331 case BTN_TOOL_LENS:
1332 mBtnToolLens = rawEvent->value;
1333 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001334 case BTN_TOOL_DOUBLETAP:
1335 mBtnToolDoubleTap = rawEvent->value;
1336 break;
1337 case BTN_TOOL_TRIPLETAP:
1338 mBtnToolTripleTap = rawEvent->value;
1339 break;
1340 case BTN_TOOL_QUADTAP:
1341 mBtnToolQuadTap = rawEvent->value;
1342 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001343 }
1344 }
1345}
1346
1347uint32_t TouchButtonAccumulator::getButtonState() const {
1348 uint32_t result = 0;
1349 if (mBtnStylus) {
1350 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1351 }
1352 if (mBtnStylus2) {
1353 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1354 }
1355 return result;
1356}
1357
1358int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001359 if (mBtnToolMouse || mBtnToolLens) {
1360 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1361 }
Jeff Brown49754db2011-07-01 17:37:58 -07001362 if (mBtnToolRubber) {
1363 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1364 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001365 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001366 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1367 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001368 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001369 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1370 }
1371 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1372}
1373
Jeff Brownd87c6d52011-08-10 14:55:59 -07001374bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001375 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1376 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001377 || mBtnToolMouse || mBtnToolLens
1378 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001379}
1380
1381bool TouchButtonAccumulator::isHovering() const {
1382 return mHaveBtnTouch && !mBtnTouch;
1383}
1384
Jeff Brown00710e92012-04-19 15:18:26 -07001385bool TouchButtonAccumulator::hasStylus() const {
1386 return mHaveStylus;
1387}
1388
Jeff Brown49754db2011-07-01 17:37:58 -07001389
Jeff Brownbe1aa822011-07-27 16:04:54 -07001390// --- RawPointerAxes ---
1391
1392RawPointerAxes::RawPointerAxes() {
1393 clear();
1394}
1395
1396void RawPointerAxes::clear() {
1397 x.clear();
1398 y.clear();
1399 pressure.clear();
1400 touchMajor.clear();
1401 touchMinor.clear();
1402 toolMajor.clear();
1403 toolMinor.clear();
1404 orientation.clear();
1405 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001406 tiltX.clear();
1407 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001408 trackingId.clear();
1409 slot.clear();
1410}
1411
1412
1413// --- RawPointerData ---
1414
1415RawPointerData::RawPointerData() {
1416 clear();
1417}
1418
1419void RawPointerData::clear() {
1420 pointerCount = 0;
1421 clearIdBits();
1422}
1423
1424void RawPointerData::copyFrom(const RawPointerData& other) {
1425 pointerCount = other.pointerCount;
1426 hoveringIdBits = other.hoveringIdBits;
1427 touchingIdBits = other.touchingIdBits;
1428
1429 for (uint32_t i = 0; i < pointerCount; i++) {
1430 pointers[i] = other.pointers[i];
1431
1432 int id = pointers[i].id;
1433 idToIndex[id] = other.idToIndex[id];
1434 }
1435}
1436
1437void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1438 float x = 0, y = 0;
1439 uint32_t count = touchingIdBits.count();
1440 if (count) {
1441 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1442 uint32_t id = idBits.clearFirstMarkedBit();
1443 const Pointer& pointer = pointerForId(id);
1444 x += pointer.x;
1445 y += pointer.y;
1446 }
1447 x /= count;
1448 y /= count;
1449 }
1450 *outX = x;
1451 *outY = y;
1452}
1453
1454
1455// --- CookedPointerData ---
1456
1457CookedPointerData::CookedPointerData() {
1458 clear();
1459}
1460
1461void CookedPointerData::clear() {
1462 pointerCount = 0;
1463 hoveringIdBits.clear();
1464 touchingIdBits.clear();
1465}
1466
1467void CookedPointerData::copyFrom(const CookedPointerData& other) {
1468 pointerCount = other.pointerCount;
1469 hoveringIdBits = other.hoveringIdBits;
1470 touchingIdBits = other.touchingIdBits;
1471
1472 for (uint32_t i = 0; i < pointerCount; i++) {
1473 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1474 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1475
1476 int id = pointerProperties[i].id;
1477 idToIndex[id] = other.idToIndex[id];
1478 }
1479}
1480
1481
Jeff Brown49754db2011-07-01 17:37:58 -07001482// --- SingleTouchMotionAccumulator ---
1483
1484SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1485 clearAbsoluteAxes();
1486}
1487
Jeff Brown65fd2512011-08-18 11:20:58 -07001488void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1489 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1490 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1491 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1492 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1493 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1494 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1495 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1496}
1497
Jeff Brown49754db2011-07-01 17:37:58 -07001498void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1499 mAbsX = 0;
1500 mAbsY = 0;
1501 mAbsPressure = 0;
1502 mAbsToolWidth = 0;
1503 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001504 mAbsTiltX = 0;
1505 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001506}
1507
1508void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1509 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001510 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001511 case ABS_X:
1512 mAbsX = rawEvent->value;
1513 break;
1514 case ABS_Y:
1515 mAbsY = rawEvent->value;
1516 break;
1517 case ABS_PRESSURE:
1518 mAbsPressure = rawEvent->value;
1519 break;
1520 case ABS_TOOL_WIDTH:
1521 mAbsToolWidth = rawEvent->value;
1522 break;
1523 case ABS_DISTANCE:
1524 mAbsDistance = rawEvent->value;
1525 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001526 case ABS_TILT_X:
1527 mAbsTiltX = rawEvent->value;
1528 break;
1529 case ABS_TILT_Y:
1530 mAbsTiltY = rawEvent->value;
1531 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001532 }
1533 }
1534}
1535
1536
1537// --- MultiTouchMotionAccumulator ---
1538
1539MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001540 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1541 mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001542}
1543
1544MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1545 delete[] mSlots;
1546}
1547
Jeff Brown00710e92012-04-19 15:18:26 -07001548void MultiTouchMotionAccumulator::configure(InputDevice* device,
1549 size_t slotCount, bool usingSlotsProtocol) {
Jeff Brown49754db2011-07-01 17:37:58 -07001550 mSlotCount = slotCount;
1551 mUsingSlotsProtocol = usingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -07001552 mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Jeff Brown49754db2011-07-01 17:37:58 -07001553
1554 delete[] mSlots;
1555 mSlots = new Slot[slotCount];
1556}
1557
Jeff Brown65fd2512011-08-18 11:20:58 -07001558void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1559 // Unfortunately there is no way to read the initial contents of the slots.
1560 // So when we reset the accumulator, we must assume they are all zeroes.
1561 if (mUsingSlotsProtocol) {
1562 // Query the driver for the current slot index and use it as the initial slot
1563 // before we start reading events from the device. It is possible that the
1564 // current slot index will not be the same as it was when the first event was
1565 // written into the evdev buffer, which means the input mapper could start
1566 // out of sync with the initial state of the events in the evdev buffer.
1567 // In the extremely unlikely case that this happens, the data from
1568 // two slots will be confused until the next ABS_MT_SLOT event is received.
1569 // This can cause the touch point to "jump", but at least there will be
1570 // no stuck touches.
1571 int32_t initialSlot;
1572 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1573 ABS_MT_SLOT, &initialSlot);
1574 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001575 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001576 initialSlot = -1;
1577 }
1578 clearSlots(initialSlot);
1579 } else {
1580 clearSlots(-1);
1581 }
1582}
1583
Jeff Brown49754db2011-07-01 17:37:58 -07001584void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001585 if (mSlots) {
1586 for (size_t i = 0; i < mSlotCount; i++) {
1587 mSlots[i].clear();
1588 }
Jeff Brown49754db2011-07-01 17:37:58 -07001589 }
1590 mCurrentSlot = initialSlot;
1591}
1592
1593void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1594 if (rawEvent->type == EV_ABS) {
1595 bool newSlot = false;
1596 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001597 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001598 mCurrentSlot = rawEvent->value;
1599 newSlot = true;
1600 }
1601 } else if (mCurrentSlot < 0) {
1602 mCurrentSlot = 0;
1603 }
1604
1605 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1606#if DEBUG_POINTERS
1607 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001608 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001609 "should be between 0 and %d; ignoring this slot.",
1610 mCurrentSlot, mSlotCount - 1);
1611 }
1612#endif
1613 } else {
1614 Slot* slot = &mSlots[mCurrentSlot];
1615
Jeff Brown49ccac52012-04-11 18:27:33 -07001616 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001617 case ABS_MT_POSITION_X:
1618 slot->mInUse = true;
1619 slot->mAbsMTPositionX = rawEvent->value;
1620 break;
1621 case ABS_MT_POSITION_Y:
1622 slot->mInUse = true;
1623 slot->mAbsMTPositionY = rawEvent->value;
1624 break;
1625 case ABS_MT_TOUCH_MAJOR:
1626 slot->mInUse = true;
1627 slot->mAbsMTTouchMajor = rawEvent->value;
1628 break;
1629 case ABS_MT_TOUCH_MINOR:
1630 slot->mInUse = true;
1631 slot->mAbsMTTouchMinor = rawEvent->value;
1632 slot->mHaveAbsMTTouchMinor = true;
1633 break;
1634 case ABS_MT_WIDTH_MAJOR:
1635 slot->mInUse = true;
1636 slot->mAbsMTWidthMajor = rawEvent->value;
1637 break;
1638 case ABS_MT_WIDTH_MINOR:
1639 slot->mInUse = true;
1640 slot->mAbsMTWidthMinor = rawEvent->value;
1641 slot->mHaveAbsMTWidthMinor = true;
1642 break;
1643 case ABS_MT_ORIENTATION:
1644 slot->mInUse = true;
1645 slot->mAbsMTOrientation = rawEvent->value;
1646 break;
1647 case ABS_MT_TRACKING_ID:
1648 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001649 // The slot is no longer in use but it retains its previous contents,
1650 // which may be reused for subsequent touches.
1651 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001652 } else {
1653 slot->mInUse = true;
1654 slot->mAbsMTTrackingId = rawEvent->value;
1655 }
1656 break;
1657 case ABS_MT_PRESSURE:
1658 slot->mInUse = true;
1659 slot->mAbsMTPressure = rawEvent->value;
1660 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001661 case ABS_MT_DISTANCE:
1662 slot->mInUse = true;
1663 slot->mAbsMTDistance = rawEvent->value;
1664 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001665 case ABS_MT_TOOL_TYPE:
1666 slot->mInUse = true;
1667 slot->mAbsMTToolType = rawEvent->value;
1668 slot->mHaveAbsMTToolType = true;
1669 break;
1670 }
1671 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001672 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001673 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1674 mCurrentSlot += 1;
1675 }
1676}
1677
Jeff Brown65fd2512011-08-18 11:20:58 -07001678void MultiTouchMotionAccumulator::finishSync() {
1679 if (!mUsingSlotsProtocol) {
1680 clearSlots(-1);
1681 }
1682}
1683
Jeff Brown00710e92012-04-19 15:18:26 -07001684bool MultiTouchMotionAccumulator::hasStylus() const {
1685 return mHaveStylus;
1686}
1687
Jeff Brown49754db2011-07-01 17:37:58 -07001688
1689// --- MultiTouchMotionAccumulator::Slot ---
1690
1691MultiTouchMotionAccumulator::Slot::Slot() {
1692 clear();
1693}
1694
Jeff Brown49754db2011-07-01 17:37:58 -07001695void MultiTouchMotionAccumulator::Slot::clear() {
1696 mInUse = false;
1697 mHaveAbsMTTouchMinor = false;
1698 mHaveAbsMTWidthMinor = false;
1699 mHaveAbsMTToolType = false;
1700 mAbsMTPositionX = 0;
1701 mAbsMTPositionY = 0;
1702 mAbsMTTouchMajor = 0;
1703 mAbsMTTouchMinor = 0;
1704 mAbsMTWidthMajor = 0;
1705 mAbsMTWidthMinor = 0;
1706 mAbsMTOrientation = 0;
1707 mAbsMTTrackingId = -1;
1708 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001709 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001710 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001711}
1712
1713int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1714 if (mHaveAbsMTToolType) {
1715 switch (mAbsMTToolType) {
1716 case MT_TOOL_FINGER:
1717 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1718 case MT_TOOL_PEN:
1719 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1720 }
1721 }
1722 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1723}
1724
1725
Jeff Brown6d0fec22010-07-23 21:28:06 -07001726// --- InputMapper ---
1727
1728InputMapper::InputMapper(InputDevice* device) :
1729 mDevice(device), mContext(device->getContext()) {
1730}
1731
1732InputMapper::~InputMapper() {
1733}
1734
1735void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1736 info->addSource(getSources());
1737}
1738
Jeff Brownef3d7e82010-09-30 14:33:04 -07001739void InputMapper::dump(String8& dump) {
1740}
1741
Jeff Brown65fd2512011-08-18 11:20:58 -07001742void InputMapper::configure(nsecs_t when,
1743 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001744}
1745
Jeff Brown65fd2512011-08-18 11:20:58 -07001746void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001747}
1748
Jeff Brownaa3855d2011-03-17 01:34:19 -07001749void InputMapper::timeoutExpired(nsecs_t when) {
1750}
1751
Jeff Brown6d0fec22010-07-23 21:28:06 -07001752int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1753 return AKEY_STATE_UNKNOWN;
1754}
1755
1756int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1757 return AKEY_STATE_UNKNOWN;
1758}
1759
1760int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1761 return AKEY_STATE_UNKNOWN;
1762}
1763
1764bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1765 const int32_t* keyCodes, uint8_t* outFlags) {
1766 return false;
1767}
1768
Jeff Browna47425a2012-04-13 04:09:27 -07001769void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1770 int32_t token) {
1771}
1772
1773void InputMapper::cancelVibrate(int32_t token) {
1774}
1775
Jeff Brown6d0fec22010-07-23 21:28:06 -07001776int32_t InputMapper::getMetaState() {
1777 return 0;
1778}
1779
Jeff Brown05dc66a2011-03-02 14:41:58 -08001780void InputMapper::fadePointer() {
1781}
1782
Jeff Brownbe1aa822011-07-27 16:04:54 -07001783status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1784 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1785}
1786
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001787void InputMapper::bumpGeneration() {
1788 mDevice->bumpGeneration();
1789}
1790
Jeff Browncb1404e2011-01-15 18:14:15 -08001791void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1792 const RawAbsoluteAxisInfo& axis, const char* name) {
1793 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001794 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1795 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001796 } else {
1797 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1798 }
1799}
1800
Jeff Brown6d0fec22010-07-23 21:28:06 -07001801
1802// --- SwitchInputMapper ---
1803
1804SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
Jeff Brownbcc046a2012-09-27 20:46:43 -07001805 InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001806}
1807
1808SwitchInputMapper::~SwitchInputMapper() {
1809}
1810
1811uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001812 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001813}
1814
1815void SwitchInputMapper::process(const RawEvent* rawEvent) {
1816 switch (rawEvent->type) {
1817 case EV_SW:
Jeff Brownbcc046a2012-09-27 20:46:43 -07001818 processSwitch(rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001819 break;
Jeff Brownbcc046a2012-09-27 20:46:43 -07001820
1821 case EV_SYN:
1822 if (rawEvent->code == SYN_REPORT) {
1823 sync(rawEvent->when);
1824 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001825 }
1826}
1827
Jeff Brownbcc046a2012-09-27 20:46:43 -07001828void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
1829 if (switchCode >= 0 && switchCode < 32) {
1830 if (switchValue) {
1831 mUpdatedSwitchValues |= 1 << switchCode;
1832 }
1833 mUpdatedSwitchMask |= 1 << switchCode;
1834 }
1835}
1836
1837void SwitchInputMapper::sync(nsecs_t when) {
1838 if (mUpdatedSwitchMask) {
1839 NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask);
1840 getListener()->notifySwitch(&args);
1841
1842 mUpdatedSwitchValues = 0;
1843 mUpdatedSwitchMask = 0;
1844 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001845}
1846
1847int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1848 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1849}
1850
1851
Jeff Browna47425a2012-04-13 04:09:27 -07001852// --- VibratorInputMapper ---
1853
1854VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1855 InputMapper(device), mVibrating(false) {
1856}
1857
1858VibratorInputMapper::~VibratorInputMapper() {
1859}
1860
1861uint32_t VibratorInputMapper::getSources() {
1862 return 0;
1863}
1864
1865void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1866 InputMapper::populateDeviceInfo(info);
1867
1868 info->setVibrator(true);
1869}
1870
1871void VibratorInputMapper::process(const RawEvent* rawEvent) {
1872 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1873}
1874
1875void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1876 int32_t token) {
1877#if DEBUG_VIBRATOR
1878 String8 patternStr;
1879 for (size_t i = 0; i < patternSize; i++) {
1880 if (i != 0) {
1881 patternStr.append(", ");
1882 }
1883 patternStr.appendFormat("%lld", pattern[i]);
1884 }
1885 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1886 getDeviceId(), patternStr.string(), repeat, token);
1887#endif
1888
1889 mVibrating = true;
1890 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1891 mPatternSize = patternSize;
1892 mRepeat = repeat;
1893 mToken = token;
1894 mIndex = -1;
1895
1896 nextStep();
1897}
1898
1899void VibratorInputMapper::cancelVibrate(int32_t token) {
1900#if DEBUG_VIBRATOR
1901 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1902#endif
1903
1904 if (mVibrating && mToken == token) {
1905 stopVibrating();
1906 }
1907}
1908
1909void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1910 if (mVibrating) {
1911 if (when >= mNextStepTime) {
1912 nextStep();
1913 } else {
1914 getContext()->requestTimeoutAtTime(mNextStepTime);
1915 }
1916 }
1917}
1918
1919void VibratorInputMapper::nextStep() {
1920 mIndex += 1;
1921 if (size_t(mIndex) >= mPatternSize) {
1922 if (mRepeat < 0) {
1923 // We are done.
1924 stopVibrating();
1925 return;
1926 }
1927 mIndex = mRepeat;
1928 }
1929
1930 bool vibratorOn = mIndex & 1;
1931 nsecs_t duration = mPattern[mIndex];
1932 if (vibratorOn) {
1933#if DEBUG_VIBRATOR
1934 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1935 getDeviceId(), duration);
1936#endif
1937 getEventHub()->vibrate(getDeviceId(), duration);
1938 } else {
1939#if DEBUG_VIBRATOR
1940 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1941#endif
1942 getEventHub()->cancelVibrate(getDeviceId());
1943 }
1944 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1945 mNextStepTime = now + duration;
1946 getContext()->requestTimeoutAtTime(mNextStepTime);
1947#if DEBUG_VIBRATOR
1948 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1949#endif
1950}
1951
1952void VibratorInputMapper::stopVibrating() {
1953 mVibrating = false;
1954#if DEBUG_VIBRATOR
1955 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1956#endif
1957 getEventHub()->cancelVibrate(getDeviceId());
1958}
1959
1960void VibratorInputMapper::dump(String8& dump) {
1961 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1962 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1963}
1964
1965
Jeff Brown6d0fec22010-07-23 21:28:06 -07001966// --- KeyboardInputMapper ---
1967
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001968KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001969 uint32_t source, int32_t keyboardType) :
1970 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001971 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001972}
1973
1974KeyboardInputMapper::~KeyboardInputMapper() {
1975}
1976
Jeff Brown6d0fec22010-07-23 21:28:06 -07001977uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001978 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001979}
1980
1981void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1982 InputMapper::populateDeviceInfo(info);
1983
1984 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001985 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001986}
1987
Jeff Brownef3d7e82010-09-30 14:33:04 -07001988void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001989 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1990 dumpParameters(dump);
1991 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001992 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001993 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1994 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1995 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001996}
1997
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001998
Jeff Brown65fd2512011-08-18 11:20:58 -07001999void KeyboardInputMapper::configure(nsecs_t when,
2000 const InputReaderConfiguration* config, uint32_t changes) {
2001 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002002
Jeff Brown474dcb52011-06-14 20:22:50 -07002003 if (!changes) { // first time only
2004 // Configure basic parameters.
2005 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002006 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002007
Jeff Brown65fd2512011-08-18 11:20:58 -07002008 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002009 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2010 DisplayViewport v;
2011 if (config->getDisplayInfo(false /*external*/, &v)) {
2012 mOrientation = v.orientation;
2013 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002014 mOrientation = DISPLAY_ORIENTATION_0;
2015 }
2016 } else {
2017 mOrientation = DISPLAY_ORIENTATION_0;
2018 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002019 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002020}
2021
2022void KeyboardInputMapper::configureParameters() {
2023 mParameters.orientationAware = false;
2024 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2025 mParameters.orientationAware);
2026
Jeff Brownd728bf52012-09-08 18:05:28 -07002027 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002028 if (mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002029 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002030 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002031}
2032
2033void KeyboardInputMapper::dumpParameters(String8& dump) {
2034 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002035 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2036 toString(mParameters.hasAssociatedDisplay));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002037 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2038 toString(mParameters.orientationAware));
2039}
2040
Jeff Brown65fd2512011-08-18 11:20:58 -07002041void KeyboardInputMapper::reset(nsecs_t when) {
2042 mMetaState = AMETA_NONE;
2043 mDownTime = 0;
2044 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002045 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002046
Jeff Brownbe1aa822011-07-27 16:04:54 -07002047 resetLedState();
2048
Jeff Brown65fd2512011-08-18 11:20:58 -07002049 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002050}
2051
2052void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2053 switch (rawEvent->type) {
2054 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002055 int32_t scanCode = rawEvent->code;
2056 int32_t usageCode = mCurrentHidUsage;
2057 mCurrentHidUsage = 0;
2058
Jeff Brown6d0fec22010-07-23 21:28:06 -07002059 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002060 int32_t keyCode;
2061 uint32_t flags;
2062 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2063 keyCode = AKEYCODE_UNKNOWN;
2064 flags = 0;
2065 }
2066 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002067 }
2068 break;
2069 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002070 case EV_MSC: {
2071 if (rawEvent->code == MSC_SCAN) {
2072 mCurrentHidUsage = rawEvent->value;
2073 }
2074 break;
2075 }
2076 case EV_SYN: {
2077 if (rawEvent->code == SYN_REPORT) {
2078 mCurrentHidUsage = 0;
2079 }
2080 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002081 }
2082}
2083
2084bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2085 return scanCode < BTN_MOUSE
2086 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002087 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002088 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002089}
2090
Jeff Brown6328cdc2010-07-29 18:18:33 -07002091void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2092 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002093
Jeff Brownbe1aa822011-07-27 16:04:54 -07002094 if (down) {
2095 // Rotate key codes according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002096 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002097 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002098 }
Jeff Brownfe508922011-01-18 15:10:10 -08002099
Jeff Brownbe1aa822011-07-27 16:04:54 -07002100 // Add key down.
2101 ssize_t keyDownIndex = findKeyDown(scanCode);
2102 if (keyDownIndex >= 0) {
2103 // key repeat, be sure to use same keycode as before in case of rotation
2104 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002105 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002106 // key down
2107 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2108 && mContext->shouldDropVirtualKey(when,
2109 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002110 return;
2111 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002112
2113 mKeyDowns.push();
2114 KeyDown& keyDown = mKeyDowns.editTop();
2115 keyDown.keyCode = keyCode;
2116 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002117 }
2118
Jeff Brownbe1aa822011-07-27 16:04:54 -07002119 mDownTime = when;
2120 } else {
2121 // Remove key down.
2122 ssize_t keyDownIndex = findKeyDown(scanCode);
2123 if (keyDownIndex >= 0) {
2124 // key up, be sure to use same keycode as before in case of rotation
2125 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2126 mKeyDowns.removeAt(size_t(keyDownIndex));
2127 } else {
2128 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002129 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002130 "keyCode=%d, scanCode=%d",
2131 getDeviceName().string(), keyCode, scanCode);
2132 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002133 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002134 }
Jeff Brownfd0358292010-06-30 16:10:35 -07002135
Jeff Brownbe1aa822011-07-27 16:04:54 -07002136 bool metaStateChanged = false;
2137 int32_t oldMetaState = mMetaState;
2138 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2139 if (oldMetaState != newMetaState) {
2140 mMetaState = newMetaState;
2141 metaStateChanged = true;
2142 updateLedState(false);
2143 }
2144
2145 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002146
Jeff Brown56194eb2011-03-02 19:23:13 -08002147 // Key down on external an keyboard should wake the device.
2148 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2149 // For internal keyboards, the key layout file should specify the policy flags for
2150 // each wake key individually.
2151 // TODO: Use the input device configuration to control this behavior more finely.
2152 if (down && getDevice()->isExternal()
2153 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2154 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2155 }
2156
Jeff Brown6328cdc2010-07-29 18:18:33 -07002157 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002158 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002159 }
2160
Jeff Brown05dc66a2011-03-02 14:41:58 -08002161 if (down && !isMetaKey(keyCode)) {
2162 getContext()->fadePointer();
2163 }
2164
Jeff Brownbe1aa822011-07-27 16:04:54 -07002165 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002166 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2167 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002168 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002169}
2170
Jeff Brownbe1aa822011-07-27 16:04:54 -07002171ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2172 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002173 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002174 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002175 return i;
2176 }
2177 }
2178 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002179}
2180
Jeff Brown6d0fec22010-07-23 21:28:06 -07002181int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2182 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2183}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002184
Jeff Brown6d0fec22010-07-23 21:28:06 -07002185int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2186 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2187}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002188
Jeff Brown6d0fec22010-07-23 21:28:06 -07002189bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2190 const int32_t* keyCodes, uint8_t* outFlags) {
2191 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2192}
2193
2194int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002195 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002196}
2197
Jeff Brownbe1aa822011-07-27 16:04:54 -07002198void KeyboardInputMapper::resetLedState() {
2199 initializeLedState(mCapsLockLedState, LED_CAPSL);
2200 initializeLedState(mNumLockLedState, LED_NUML);
2201 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002202
Jeff Brownbe1aa822011-07-27 16:04:54 -07002203 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002204}
2205
Jeff Brownbe1aa822011-07-27 16:04:54 -07002206void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002207 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2208 ledState.on = false;
2209}
2210
Jeff Brownbe1aa822011-07-27 16:04:54 -07002211void KeyboardInputMapper::updateLedState(bool reset) {
2212 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002213 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002215 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002217 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002218}
2219
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002221 int32_t led, int32_t modifier, bool reset) {
2222 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002223 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002224 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002225 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2226 ledState.on = desiredState;
2227 }
2228 }
2229}
2230
Jeff Brown6d0fec22010-07-23 21:28:06 -07002231
Jeff Brown83c09682010-12-23 17:50:18 -08002232// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002233
Jeff Brown83c09682010-12-23 17:50:18 -08002234CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002235 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002236}
2237
Jeff Brown83c09682010-12-23 17:50:18 -08002238CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002239}
2240
Jeff Brown83c09682010-12-23 17:50:18 -08002241uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002242 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002243}
2244
Jeff Brown83c09682010-12-23 17:50:18 -08002245void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002246 InputMapper::populateDeviceInfo(info);
2247
Jeff Brown83c09682010-12-23 17:50:18 -08002248 if (mParameters.mode == Parameters::MODE_POINTER) {
2249 float minX, minY, maxX, maxY;
2250 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002251 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f, 0.0f);
2252 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002253 }
2254 } else {
Michael Wrightc6091c62013-04-01 20:56:04 -07002255 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale, 0.0f);
2256 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002257 }
Michael Wrightc6091c62013-04-01 20:56:04 -07002258 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002259
Jeff Brown65fd2512011-08-18 11:20:58 -07002260 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002261 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002262 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002263 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002264 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002265 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002266}
2267
Jeff Brown83c09682010-12-23 17:50:18 -08002268void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002269 dump.append(INDENT2 "Cursor Input Mapper:\n");
2270 dumpParameters(dump);
2271 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2272 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2273 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2274 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2275 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002276 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002277 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002278 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002279 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2280 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002281 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002282 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2283 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2284 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002285}
2286
Jeff Brown65fd2512011-08-18 11:20:58 -07002287void CursorInputMapper::configure(nsecs_t when,
2288 const InputReaderConfiguration* config, uint32_t changes) {
2289 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002290
Jeff Brown474dcb52011-06-14 20:22:50 -07002291 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002292 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002293
Jeff Brown474dcb52011-06-14 20:22:50 -07002294 // Configure basic parameters.
2295 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002296
Jeff Brown474dcb52011-06-14 20:22:50 -07002297 // Configure device mode.
2298 switch (mParameters.mode) {
2299 case Parameters::MODE_POINTER:
2300 mSource = AINPUT_SOURCE_MOUSE;
2301 mXPrecision = 1.0f;
2302 mYPrecision = 1.0f;
2303 mXScale = 1.0f;
2304 mYScale = 1.0f;
2305 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2306 break;
2307 case Parameters::MODE_NAVIGATION:
2308 mSource = AINPUT_SOURCE_TRACKBALL;
2309 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2310 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2311 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2312 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2313 break;
2314 }
2315
2316 mVWheelScale = 1.0f;
2317 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002318 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002319
Jeff Brown474dcb52011-06-14 20:22:50 -07002320 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2321 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2322 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2323 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2324 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002325
2326 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002327 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2328 DisplayViewport v;
2329 if (config->getDisplayInfo(false /*external*/, &v)) {
2330 mOrientation = v.orientation;
2331 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002332 mOrientation = DISPLAY_ORIENTATION_0;
2333 }
2334 } else {
2335 mOrientation = DISPLAY_ORIENTATION_0;
2336 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002337 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002338 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002339}
2340
Jeff Brown83c09682010-12-23 17:50:18 -08002341void CursorInputMapper::configureParameters() {
2342 mParameters.mode = Parameters::MODE_POINTER;
2343 String8 cursorModeString;
2344 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2345 if (cursorModeString == "navigation") {
2346 mParameters.mode = Parameters::MODE_NAVIGATION;
2347 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002348 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002349 }
2350 }
2351
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002352 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002353 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002354 mParameters.orientationAware);
2355
Jeff Brownd728bf52012-09-08 18:05:28 -07002356 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002357 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002358 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002359 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002360}
2361
Jeff Brown83c09682010-12-23 17:50:18 -08002362void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002363 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002364 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2365 toString(mParameters.hasAssociatedDisplay));
Jeff Brown83c09682010-12-23 17:50:18 -08002366
2367 switch (mParameters.mode) {
2368 case Parameters::MODE_POINTER:
2369 dump.append(INDENT4 "Mode: pointer\n");
2370 break;
2371 case Parameters::MODE_NAVIGATION:
2372 dump.append(INDENT4 "Mode: navigation\n");
2373 break;
2374 default:
Steve Blockec193de2012-01-09 18:35:44 +00002375 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002376 }
2377
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002378 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2379 toString(mParameters.orientationAware));
2380}
2381
Jeff Brown65fd2512011-08-18 11:20:58 -07002382void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002383 mButtonState = 0;
2384 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002385
Jeff Brownbe1aa822011-07-27 16:04:54 -07002386 mPointerVelocityControl.reset();
2387 mWheelXVelocityControl.reset();
2388 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002389
Jeff Brown65fd2512011-08-18 11:20:58 -07002390 mCursorButtonAccumulator.reset(getDevice());
2391 mCursorMotionAccumulator.reset(getDevice());
2392 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002393
Jeff Brown65fd2512011-08-18 11:20:58 -07002394 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002395}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002396
Jeff Brown83c09682010-12-23 17:50:18 -08002397void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002398 mCursorButtonAccumulator.process(rawEvent);
2399 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002400 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002401
Jeff Brown49ccac52012-04-11 18:27:33 -07002402 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002403 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002404 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002405}
2406
Jeff Brown83c09682010-12-23 17:50:18 -08002407void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002408 int32_t lastButtonState = mButtonState;
2409 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2410 mButtonState = currentButtonState;
2411
2412 bool wasDown = isPointerDown(lastButtonState);
2413 bool down = isPointerDown(currentButtonState);
2414 bool downChanged;
2415 if (!wasDown && down) {
2416 mDownTime = when;
2417 downChanged = true;
2418 } else if (wasDown && !down) {
2419 downChanged = true;
2420 } else {
2421 downChanged = false;
2422 }
2423 nsecs_t downTime = mDownTime;
2424 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002425 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002426
2427 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2428 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2429 bool moved = deltaX != 0 || deltaY != 0;
2430
Jeff Brown65fd2512011-08-18 11:20:58 -07002431 // Rotate delta according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002432 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay
Jeff Brownbe1aa822011-07-27 16:04:54 -07002433 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002434 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002435 }
2436
Jeff Brown65fd2512011-08-18 11:20:58 -07002437 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002438 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002439 pointerProperties.clear();
2440 pointerProperties.id = 0;
2441 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2442
Jeff Brown6328cdc2010-07-29 18:18:33 -07002443 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002444 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002445
Jeff Brown65fd2512011-08-18 11:20:58 -07002446 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2447 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002449
Jeff Brownbe1aa822011-07-27 16:04:54 -07002450 mWheelYVelocityControl.move(when, NULL, &vscroll);
2451 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002452
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002454
Jeff Brown83d616a2012-09-09 20:33:43 -07002455 int32_t displayId;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002456 if (mPointerController != NULL) {
2457 if (moved || scrolled || buttonsChanged) {
2458 mPointerController->setPresentation(
2459 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002460
Jeff Brownbe1aa822011-07-27 16:04:54 -07002461 if (moved) {
2462 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002463 }
2464
Jeff Brownbe1aa822011-07-27 16:04:54 -07002465 if (buttonsChanged) {
2466 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002467 }
Jeff Brownefd32662011-03-08 15:13:06 -08002468
Jeff Brownbe1aa822011-07-27 16:04:54 -07002469 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002470 }
2471
Jeff Brownbe1aa822011-07-27 16:04:54 -07002472 float x, y;
2473 mPointerController->getPosition(&x, &y);
2474 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2475 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83d616a2012-09-09 20:33:43 -07002476 displayId = ADISPLAY_ID_DEFAULT;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002477 } else {
2478 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2479 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83d616a2012-09-09 20:33:43 -07002480 displayId = ADISPLAY_ID_NONE;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002481 }
2482
2483 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002484
Jeff Brown56194eb2011-03-02 19:23:13 -08002485 // Moving an external trackball or mouse should wake the device.
2486 // We don't do this for internal cursor devices to prevent them from waking up
2487 // the device in your pocket.
2488 // TODO: Use the input device configuration to control this behavior more finely.
2489 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002490 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002491 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2492 }
2493
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002494 // Synthesize key down from buttons if needed.
2495 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2496 policyFlags, lastButtonState, currentButtonState);
2497
2498 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002499 if (downChanged || moved || scrolled || buttonsChanged) {
2500 int32_t metaState = mContext->getGlobalMetaState();
2501 int32_t motionEventAction;
2502 if (downChanged) {
2503 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2504 } else if (down || mPointerController == NULL) {
2505 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2506 } else {
2507 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2508 }
Jeff Brownb6997262010-10-08 22:31:17 -07002509
Jeff Brownbe1aa822011-07-27 16:04:54 -07002510 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2511 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07002512 displayId, 1, &pointerProperties, &pointerCoords,
2513 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002514 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002515
Jeff Brownbe1aa822011-07-27 16:04:54 -07002516 // Send hover move after UP to tell the application that the mouse is hovering now.
2517 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2518 && mPointerController != NULL) {
2519 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2520 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2521 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002522 displayId, 1, &pointerProperties, &pointerCoords,
2523 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002524 getListener()->notifyMotion(&hoverArgs);
2525 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002526
Jeff Brownbe1aa822011-07-27 16:04:54 -07002527 // Send scroll events.
2528 if (scrolled) {
2529 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2530 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2531
2532 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2533 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2534 AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002535 displayId, 1, &pointerProperties, &pointerCoords,
2536 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002537 getListener()->notifyMotion(&scrollArgs);
2538 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002539 }
Jeff Browna032cc02011-03-07 16:56:21 -08002540
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002541 // Synthesize key up from buttons if needed.
2542 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2543 policyFlags, lastButtonState, currentButtonState);
2544
Jeff Brown65fd2512011-08-18 11:20:58 -07002545 mCursorMotionAccumulator.finishSync();
2546 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002547}
2548
Jeff Brown83c09682010-12-23 17:50:18 -08002549int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002550 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2551 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2552 } else {
2553 return AKEY_STATE_UNKNOWN;
2554 }
2555}
2556
Jeff Brown05dc66a2011-03-02 14:41:58 -08002557void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002558 if (mPointerController != NULL) {
2559 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2560 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002561}
2562
Jeff Brown6d0fec22010-07-23 21:28:06 -07002563
2564// --- TouchInputMapper ---
2565
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002566TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002567 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002568 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brown83d616a2012-09-09 20:33:43 -07002569 mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0),
2570 mSurfaceOrientation(DISPLAY_ORIENTATION_0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002571}
2572
2573TouchInputMapper::~TouchInputMapper() {
2574}
2575
2576uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002577 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002578}
2579
2580void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2581 InputMapper::populateDeviceInfo(info);
2582
Jeff Brown65fd2512011-08-18 11:20:58 -07002583 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2584 info->addMotionRange(mOrientedRanges.x);
2585 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002586 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002587
Jeff Brown65fd2512011-08-18 11:20:58 -07002588 if (mOrientedRanges.haveSize) {
2589 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002590 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002591
2592 if (mOrientedRanges.haveTouchSize) {
2593 info->addMotionRange(mOrientedRanges.touchMajor);
2594 info->addMotionRange(mOrientedRanges.touchMinor);
2595 }
2596
2597 if (mOrientedRanges.haveToolSize) {
2598 info->addMotionRange(mOrientedRanges.toolMajor);
2599 info->addMotionRange(mOrientedRanges.toolMinor);
2600 }
2601
2602 if (mOrientedRanges.haveOrientation) {
2603 info->addMotionRange(mOrientedRanges.orientation);
2604 }
2605
2606 if (mOrientedRanges.haveDistance) {
2607 info->addMotionRange(mOrientedRanges.distance);
2608 }
2609
2610 if (mOrientedRanges.haveTilt) {
2611 info->addMotionRange(mOrientedRanges.tilt);
2612 }
2613
2614 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002615 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
2616 0.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07002617 }
2618 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002619 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
2620 0.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07002621 }
Michael Wright5e025eb2013-05-15 23:16:54 -07002622 if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) {
2623 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
2624 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
2625 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
2626 x.fuzz, x.resolution);
2627 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
2628 y.fuzz, y.resolution);
2629 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
2630 x.fuzz, x.resolution);
2631 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
2632 y.fuzz, y.resolution);
2633 }
Michael Wright7ddd1102013-05-20 15:04:55 -07002634 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002635 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002636}
2637
Jeff Brownef3d7e82010-09-30 14:33:04 -07002638void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002639 dump.append(INDENT2 "Touch Input Mapper:\n");
2640 dumpParameters(dump);
2641 dumpVirtualKeys(dump);
2642 dumpRawPointerAxes(dump);
2643 dumpCalibration(dump);
2644 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002645
Jeff Brownbe1aa822011-07-27 16:04:54 -07002646 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown83d616a2012-09-09 20:33:43 -07002647 dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate);
2648 dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002649 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2650 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2651 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2652 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2653 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002654 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2655 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2656 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2657 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002658 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2659 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2660 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2661 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2662 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002663
Jeff Brownbe1aa822011-07-27 16:04:54 -07002664 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002665
Jeff Brownbe1aa822011-07-27 16:04:54 -07002666 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2667 mLastRawPointerData.pointerCount);
2668 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2669 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2670 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2671 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002672 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2673 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002674 pointer.id, pointer.x, pointer.y, pointer.pressure,
2675 pointer.touchMajor, pointer.touchMinor,
2676 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002677 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002678 pointer.toolType, toString(pointer.isHovering));
2679 }
2680
2681 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2682 mLastCookedPointerData.pointerCount);
2683 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2684 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2685 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2686 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2687 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002688 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2689 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002690 pointerProperties.id,
2691 pointerCoords.getX(),
2692 pointerCoords.getY(),
2693 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2694 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2695 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2696 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2697 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2698 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002699 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002700 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2701 pointerProperties.toolType,
2702 toString(mLastCookedPointerData.isHovering(i)));
2703 }
2704
Jeff Brown65fd2512011-08-18 11:20:58 -07002705 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002706 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2707 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002708 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002709 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002710 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002711 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002712 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002713 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002714 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002715 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2716 mPointerGestureMaxSwipeWidth);
2717 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002718}
2719
Jeff Brown65fd2512011-08-18 11:20:58 -07002720void TouchInputMapper::configure(nsecs_t when,
2721 const InputReaderConfiguration* config, uint32_t changes) {
2722 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002723
Jeff Brown474dcb52011-06-14 20:22:50 -07002724 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002725
Jeff Brown474dcb52011-06-14 20:22:50 -07002726 if (!changes) { // first time only
2727 // Configure basic parameters.
2728 configureParameters();
2729
Jeff Brown65fd2512011-08-18 11:20:58 -07002730 // Configure common accumulators.
2731 mCursorScrollAccumulator.configure(getDevice());
2732 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002733
2734 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002735 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002736
2737 // Prepare input device calibration.
2738 parseCalibration();
2739 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002740 }
2741
Jeff Brown474dcb52011-06-14 20:22:50 -07002742 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002743 // Update pointer speed.
2744 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2745 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2746 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002747 }
Jeff Brown8d608662010-08-30 03:02:23 -07002748
Jeff Brown65fd2512011-08-18 11:20:58 -07002749 bool resetNeeded = false;
2750 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002751 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2752 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002753 // Configure device sources, surface dimensions, orientation and
2754 // scaling factors.
2755 configureSurface(when, &resetNeeded);
2756 }
2757
2758 if (changes && resetNeeded) {
2759 // Send reset, unless this is the first time the device has been configured,
2760 // in which case the reader will call reset itself after all mappers are ready.
2761 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002762 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002763}
2764
Jeff Brown8d608662010-08-30 03:02:23 -07002765void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002766 // Use the pointer presentation mode for devices that do not support distinct
2767 // multitouch. The spot-based presentation relies on being able to accurately
2768 // locate two or more fingers on the touch pad.
2769 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2770 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002771
Jeff Brown538881e2011-05-25 18:23:38 -07002772 String8 gestureModeString;
2773 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2774 gestureModeString)) {
2775 if (gestureModeString == "pointer") {
2776 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2777 } else if (gestureModeString == "spots") {
2778 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2779 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002780 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002781 }
2782 }
2783
Jeff Browndeffe072011-08-26 18:38:46 -07002784 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2785 // The device is a touch screen.
2786 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2787 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2788 // The device is a pointing device like a track pad.
2789 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2790 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002791 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2792 // The device is a cursor device with a touch pad attached.
2793 // By default don't use the touch pad to move the pointer.
2794 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2795 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002796 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002797 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2798 }
2799
Michael Wright7ddd1102013-05-20 15:04:55 -07002800 mParameters.hasButtonUnderPad=
2801 getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_BUTTONPAD);
2802
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002803 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002804 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2805 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002806 if (deviceTypeString == "touchScreen") {
2807 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002808 } else if (deviceTypeString == "touchPad") {
2809 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002810 } else if (deviceTypeString == "touchNavigation") {
2811 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION;
Jeff Brownace13b12011-03-09 17:39:48 -08002812 } else if (deviceTypeString == "pointer") {
2813 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002814 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002815 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002816 }
2817 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002818
Jeff Brownefd32662011-03-08 15:13:06 -08002819 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002820 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2821 mParameters.orientationAware);
2822
Jeff Brownd728bf52012-09-08 18:05:28 -07002823 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002824 mParameters.associatedDisplayIsExternal = false;
2825 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002826 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002827 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002828 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002829 mParameters.associatedDisplayIsExternal =
2830 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2831 && getDevice()->isExternal();
Jeff Brownbc68a592011-07-25 12:58:12 -07002832 }
Jeff Brown8d608662010-08-30 03:02:23 -07002833}
2834
Jeff Brownef3d7e82010-09-30 14:33:04 -07002835void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002836 dump.append(INDENT3 "Parameters:\n");
2837
Jeff Brown538881e2011-05-25 18:23:38 -07002838 switch (mParameters.gestureMode) {
2839 case Parameters::GESTURE_MODE_POINTER:
2840 dump.append(INDENT4 "GestureMode: pointer\n");
2841 break;
2842 case Parameters::GESTURE_MODE_SPOTS:
2843 dump.append(INDENT4 "GestureMode: spots\n");
2844 break;
2845 default:
2846 assert(false);
2847 }
2848
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002849 switch (mParameters.deviceType) {
2850 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2851 dump.append(INDENT4 "DeviceType: touchScreen\n");
2852 break;
2853 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2854 dump.append(INDENT4 "DeviceType: touchPad\n");
2855 break;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002856 case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION:
2857 dump.append(INDENT4 "DeviceType: touchNavigation\n");
2858 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002859 case Parameters::DEVICE_TYPE_POINTER:
2860 dump.append(INDENT4 "DeviceType: pointer\n");
2861 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002862 default:
Steve Blockec193de2012-01-09 18:35:44 +00002863 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002864 }
2865
Jeff Brown83d616a2012-09-09 20:33:43 -07002866 dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n",
Jeff Brownd728bf52012-09-08 18:05:28 -07002867 toString(mParameters.hasAssociatedDisplay),
2868 toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002869 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2870 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002871}
2872
Jeff Brownbe1aa822011-07-27 16:04:54 -07002873void TouchInputMapper::configureRawPointerAxes() {
2874 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002875}
2876
Jeff Brownbe1aa822011-07-27 16:04:54 -07002877void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2878 dump.append(INDENT3 "Raw Touch Axes:\n");
2879 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2880 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2881 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2882 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2883 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2884 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2885 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2886 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2887 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002888 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2889 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002890 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2891 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002892}
2893
Jeff Brown65fd2512011-08-18 11:20:58 -07002894void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2895 int32_t oldDeviceMode = mDeviceMode;
2896
2897 // Determine device mode.
2898 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2899 && mConfig.pointerGesturesEnabled) {
2900 mSource = AINPUT_SOURCE_MOUSE;
2901 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002902 if (hasStylus()) {
2903 mSource |= AINPUT_SOURCE_STYLUS;
2904 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002905 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownd728bf52012-09-08 18:05:28 -07002906 && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002907 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2908 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002909 if (hasStylus()) {
2910 mSource |= AINPUT_SOURCE_STYLUS;
2911 }
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002912 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) {
2913 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Jeff Brown4dac9012013-04-10 01:03:19 -07002914 mDeviceMode = DEVICE_MODE_NAVIGATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002915 } else {
2916 mSource = AINPUT_SOURCE_TOUCHPAD;
2917 mDeviceMode = DEVICE_MODE_UNSCALED;
2918 }
2919
Jeff Brown9626b142011-03-03 02:09:54 -08002920 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002921 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002922 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002923 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002924 mDeviceMode = DEVICE_MODE_DISABLED;
2925 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002926 }
2927
Jeff Brown83d616a2012-09-09 20:33:43 -07002928 // Raw width and height in the natural orientation.
2929 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2930 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2931
Jeff Brown65fd2512011-08-18 11:20:58 -07002932 // Get associated display dimensions.
Jeff Brown83d616a2012-09-09 20:33:43 -07002933 bool viewportChanged = false;
2934 DisplayViewport newViewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07002935 if (mParameters.hasAssociatedDisplay) {
Jeff Brown83d616a2012-09-09 20:33:43 -07002936 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002937 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brownd728bf52012-09-08 18:05:28 -07002938 "display. The device will be inoperable until the display size "
Jeff Brown65fd2512011-08-18 11:20:58 -07002939 "becomes available.",
Jeff Brownd728bf52012-09-08 18:05:28 -07002940 getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002941 mDeviceMode = DEVICE_MODE_DISABLED;
2942 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002943 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002944 } else {
Jeff Brown83d616a2012-09-09 20:33:43 -07002945 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
2946 }
2947 if (mViewport != newViewport) {
2948 mViewport = newViewport;
2949 viewportChanged = true;
2950
2951 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2952 // Convert rotated viewport to natural surface coordinates.
2953 int32_t naturalLogicalWidth, naturalLogicalHeight;
2954 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
2955 int32_t naturalPhysicalLeft, naturalPhysicalTop;
2956 int32_t naturalDeviceWidth, naturalDeviceHeight;
2957 switch (mViewport.orientation) {
2958 case DISPLAY_ORIENTATION_90:
2959 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2960 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2961 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2962 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2963 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
2964 naturalPhysicalTop = mViewport.physicalLeft;
2965 naturalDeviceWidth = mViewport.deviceHeight;
2966 naturalDeviceHeight = mViewport.deviceWidth;
2967 break;
2968 case DISPLAY_ORIENTATION_180:
2969 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2970 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2971 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2972 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2973 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
2974 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
2975 naturalDeviceWidth = mViewport.deviceWidth;
2976 naturalDeviceHeight = mViewport.deviceHeight;
2977 break;
2978 case DISPLAY_ORIENTATION_270:
2979 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2980 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2981 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2982 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2983 naturalPhysicalLeft = mViewport.physicalTop;
2984 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
2985 naturalDeviceWidth = mViewport.deviceHeight;
2986 naturalDeviceHeight = mViewport.deviceWidth;
2987 break;
2988 case DISPLAY_ORIENTATION_0:
2989 default:
2990 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2991 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2992 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2993 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2994 naturalPhysicalLeft = mViewport.physicalLeft;
2995 naturalPhysicalTop = mViewport.physicalTop;
2996 naturalDeviceWidth = mViewport.deviceWidth;
2997 naturalDeviceHeight = mViewport.deviceHeight;
2998 break;
2999 }
3000
3001 mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
3002 mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
3003 mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
3004 mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
3005
3006 mSurfaceOrientation = mParameters.orientationAware ?
3007 mViewport.orientation : DISPLAY_ORIENTATION_0;
3008 } else {
3009 mSurfaceWidth = rawWidth;
3010 mSurfaceHeight = rawHeight;
3011 mSurfaceLeft = 0;
3012 mSurfaceTop = 0;
3013 mSurfaceOrientation = DISPLAY_ORIENTATION_0;
3014 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003015 }
3016
3017 // If moving between pointer modes, need to reset some state.
3018 bool deviceModeChanged;
3019 if (mDeviceMode != oldDeviceMode) {
3020 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07003021 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08003022 }
3023
Jeff Browndaf4a122011-08-26 17:14:14 -07003024 // Create pointer controller if needed.
3025 if (mDeviceMode == DEVICE_MODE_POINTER ||
3026 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
3027 if (mPointerController == NULL) {
3028 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
3029 }
3030 } else {
3031 mPointerController.clear();
3032 }
3033
Jeff Brown83d616a2012-09-09 20:33:43 -07003034 if (viewportChanged || deviceModeChanged) {
3035 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
3036 "display id %d",
3037 getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight,
3038 mSurfaceOrientation, mDeviceMode, mViewport.displayId);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003039
Jeff Brown8d608662010-08-30 03:02:23 -07003040 // Configure X and Y factors.
Jeff Brown83d616a2012-09-09 20:33:43 -07003041 mXScale = float(mSurfaceWidth) / rawWidth;
3042 mYScale = float(mSurfaceHeight) / rawHeight;
3043 mXTranslate = -mSurfaceLeft;
3044 mYTranslate = -mSurfaceTop;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003045 mXPrecision = 1.0f / mXScale;
3046 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003047
Jeff Brownbe1aa822011-07-27 16:04:54 -07003048 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07003049 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003050 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07003051 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08003052
Jeff Brownbe1aa822011-07-27 16:04:54 -07003053 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003054
Jeff Brown8d608662010-08-30 03:02:23 -07003055 // Scale factor for terms that are not oriented in a particular axis.
3056 // If the pixels are square then xScale == yScale otherwise we fake it
3057 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003058 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003059
Jeff Brown8d608662010-08-30 03:02:23 -07003060 // Size of diagonal axis.
Jeff Brown83d616a2012-09-09 20:33:43 -07003061 float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003062
Jeff Browna1f89ce2011-08-11 00:05:01 -07003063 // Size factors.
3064 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
3065 if (mRawPointerAxes.touchMajor.valid
3066 && mRawPointerAxes.touchMajor.maxValue != 0) {
3067 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
3068 } else if (mRawPointerAxes.toolMajor.valid
3069 && mRawPointerAxes.toolMajor.maxValue != 0) {
3070 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
3071 } else {
3072 mSizeScale = 0.0f;
3073 }
3074
Jeff Brownbe1aa822011-07-27 16:04:54 -07003075 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003076 mOrientedRanges.haveToolSize = true;
3077 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08003078
Jeff Brownbe1aa822011-07-27 16:04:54 -07003079 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003080 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003081 mOrientedRanges.touchMajor.min = 0;
3082 mOrientedRanges.touchMajor.max = diagonalSize;
3083 mOrientedRanges.touchMajor.flat = 0;
3084 mOrientedRanges.touchMajor.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003085 mOrientedRanges.touchMajor.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003086
Jeff Brownbe1aa822011-07-27 16:04:54 -07003087 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
3088 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08003089
Jeff Brownbe1aa822011-07-27 16:04:54 -07003090 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003091 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003092 mOrientedRanges.toolMajor.min = 0;
3093 mOrientedRanges.toolMajor.max = diagonalSize;
3094 mOrientedRanges.toolMajor.flat = 0;
3095 mOrientedRanges.toolMajor.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003096 mOrientedRanges.toolMajor.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003097
Jeff Brownbe1aa822011-07-27 16:04:54 -07003098 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3099 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003100
3101 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003102 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003103 mOrientedRanges.size.min = 0;
3104 mOrientedRanges.size.max = 1.0;
3105 mOrientedRanges.size.flat = 0;
3106 mOrientedRanges.size.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003107 mOrientedRanges.size.resolution = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003108 } else {
3109 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003110 }
3111
3112 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003113 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003114 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3115 || mCalibration.pressureCalibration
3116 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3117 if (mCalibration.havePressureScale) {
3118 mPressureScale = mCalibration.pressureScale;
3119 } else if (mRawPointerAxes.pressure.valid
3120 && mRawPointerAxes.pressure.maxValue != 0) {
3121 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003122 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003123 }
Jeff Brown8d608662010-08-30 03:02:23 -07003124
Jeff Brown65fd2512011-08-18 11:20:58 -07003125 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3126 mOrientedRanges.pressure.source = mSource;
3127 mOrientedRanges.pressure.min = 0;
3128 mOrientedRanges.pressure.max = 1.0;
3129 mOrientedRanges.pressure.flat = 0;
3130 mOrientedRanges.pressure.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003131 mOrientedRanges.pressure.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003132
Jeff Brown65fd2512011-08-18 11:20:58 -07003133 // Tilt
3134 mTiltXCenter = 0;
3135 mTiltXScale = 0;
3136 mTiltYCenter = 0;
3137 mTiltYScale = 0;
3138 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3139 if (mHaveTilt) {
3140 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3141 mRawPointerAxes.tiltX.maxValue);
3142 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3143 mRawPointerAxes.tiltY.maxValue);
3144 mTiltXScale = M_PI / 180;
3145 mTiltYScale = M_PI / 180;
3146
3147 mOrientedRanges.haveTilt = true;
3148
3149 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3150 mOrientedRanges.tilt.source = mSource;
3151 mOrientedRanges.tilt.min = 0;
3152 mOrientedRanges.tilt.max = M_PI_2;
3153 mOrientedRanges.tilt.flat = 0;
3154 mOrientedRanges.tilt.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003155 mOrientedRanges.tilt.resolution = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003156 }
3157
Jeff Brown8d608662010-08-30 03:02:23 -07003158 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07003159 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003160 if (mHaveTilt) {
3161 mOrientedRanges.haveOrientation = true;
3162
3163 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3164 mOrientedRanges.orientation.source = mSource;
3165 mOrientedRanges.orientation.min = -M_PI;
3166 mOrientedRanges.orientation.max = M_PI;
3167 mOrientedRanges.orientation.flat = 0;
3168 mOrientedRanges.orientation.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003169 mOrientedRanges.orientation.resolution = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003170 } else if (mCalibration.orientationCalibration !=
3171 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003172 if (mCalibration.orientationCalibration
3173 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003174 if (mRawPointerAxes.orientation.valid) {
Jeff Brown037f7272012-06-25 17:31:23 -07003175 if (mRawPointerAxes.orientation.maxValue > 0) {
3176 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
3177 } else if (mRawPointerAxes.orientation.minValue < 0) {
3178 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
3179 } else {
3180 mOrientationScale = 0;
3181 }
Jeff Brown8d608662010-08-30 03:02:23 -07003182 }
3183 }
3184
Jeff Brownbe1aa822011-07-27 16:04:54 -07003185 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003186
Jeff Brownbe1aa822011-07-27 16:04:54 -07003187 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003188 mOrientedRanges.orientation.source = mSource;
3189 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003190 mOrientedRanges.orientation.max = M_PI_2;
3191 mOrientedRanges.orientation.flat = 0;
3192 mOrientedRanges.orientation.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003193 mOrientedRanges.orientation.resolution = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003194 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003195
3196 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003197 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003198 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3199 if (mCalibration.distanceCalibration
3200 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3201 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003202 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003203 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003204 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003205 }
3206 }
3207
Jeff Brownbe1aa822011-07-27 16:04:54 -07003208 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003209
Jeff Brownbe1aa822011-07-27 16:04:54 -07003210 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003211 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003212 mOrientedRanges.distance.min =
3213 mRawPointerAxes.distance.minValue * mDistanceScale;
3214 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003215 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003216 mOrientedRanges.distance.flat = 0;
3217 mOrientedRanges.distance.fuzz =
3218 mRawPointerAxes.distance.fuzz * mDistanceScale;
Michael Wrightc6091c62013-04-01 20:56:04 -07003219 mOrientedRanges.distance.resolution = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003220 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003221
Jeff Brown83d616a2012-09-09 20:33:43 -07003222 // Compute oriented precision, scales and ranges.
Jeff Brown9626b142011-03-03 02:09:54 -08003223 // Note that the maximum value reported is an inclusive maximum value so it is one
3224 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003225 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003226 case DISPLAY_ORIENTATION_90:
3227 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003228 mOrientedXPrecision = mYPrecision;
3229 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003230
Jeff Brown83d616a2012-09-09 20:33:43 -07003231 mOrientedRanges.x.min = mYTranslate;
3232 mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003233 mOrientedRanges.x.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003234 mOrientedRanges.x.fuzz = 0;
3235 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003236
Jeff Brown83d616a2012-09-09 20:33:43 -07003237 mOrientedRanges.y.min = mXTranslate;
3238 mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003239 mOrientedRanges.y.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003240 mOrientedRanges.y.fuzz = 0;
3241 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003242 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003243
Jeff Brown6d0fec22010-07-23 21:28:06 -07003244 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003245 mOrientedXPrecision = mXPrecision;
3246 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003247
Jeff Brown83d616a2012-09-09 20:33:43 -07003248 mOrientedRanges.x.min = mXTranslate;
3249 mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003250 mOrientedRanges.x.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003251 mOrientedRanges.x.fuzz = 0;
3252 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003253
Jeff Brown83d616a2012-09-09 20:33:43 -07003254 mOrientedRanges.y.min = mYTranslate;
3255 mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003256 mOrientedRanges.y.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003257 mOrientedRanges.y.fuzz = 0;
3258 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003259 break;
3260 }
Jeff Brownace13b12011-03-09 17:39:48 -08003261
Jeff Brown65fd2512011-08-18 11:20:58 -07003262 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown4dac9012013-04-10 01:03:19 -07003263 // Compute pointer gesture detection parameters.
Jeff Brown2352b972011-04-12 22:39:53 -07003264 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003265 float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003266
Jeff Brown2352b972011-04-12 22:39:53 -07003267 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003268 // given area relative to the diagonal size of the display when no acceleration
3269 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003270 // Assume that the touch pad has a square aspect ratio such that movements in
3271 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003272 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003273 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003274 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003275
3276 // Scale zooms to cover a smaller range of the display than movements do.
3277 // This value determines the area around the pointer that is affected by freeform
3278 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003279 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003280 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003281 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003282
Jeff Brown2352b972011-04-12 22:39:53 -07003283 // Max width between pointers to detect a swipe gesture is more than some fraction
3284 // of the diagonal axis of the touch pad. Touches that are wider than this are
3285 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003286 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003287 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003288
Jeff Brown4dac9012013-04-10 01:03:19 -07003289 // Abort current pointer usages because the state has changed.
3290 abortPointerUsage(when, 0 /*policyFlags*/);
Jeff Brown4dac9012013-04-10 01:03:19 -07003291 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003292
3293 // Inform the dispatcher about the changes.
3294 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003295 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003296 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003297}
3298
Jeff Brownbe1aa822011-07-27 16:04:54 -07003299void TouchInputMapper::dumpSurface(String8& dump) {
Jeff Brown83d616a2012-09-09 20:33:43 -07003300 dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, "
3301 "logicalFrame=[%d, %d, %d, %d], "
3302 "physicalFrame=[%d, %d, %d, %d], "
3303 "deviceSize=[%d, %d]\n",
3304 mViewport.displayId, mViewport.orientation,
3305 mViewport.logicalLeft, mViewport.logicalTop,
3306 mViewport.logicalRight, mViewport.logicalBottom,
3307 mViewport.physicalLeft, mViewport.physicalTop,
3308 mViewport.physicalRight, mViewport.physicalBottom,
3309 mViewport.deviceWidth, mViewport.deviceHeight);
3310
Jeff Brownbe1aa822011-07-27 16:04:54 -07003311 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3312 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003313 dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft);
3314 dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003315 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003316}
3317
Jeff Brownbe1aa822011-07-27 16:04:54 -07003318void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003319 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003320 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003321
Jeff Brownbe1aa822011-07-27 16:04:54 -07003322 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003323
Jeff Brown6328cdc2010-07-29 18:18:33 -07003324 if (virtualKeyDefinitions.size() == 0) {
3325 return;
3326 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003327
Jeff Brownbe1aa822011-07-27 16:04:54 -07003328 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003329
Jeff Brownbe1aa822011-07-27 16:04:54 -07003330 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3331 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3332 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3333 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003334
3335 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003336 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003337 virtualKeyDefinitions[i];
3338
Jeff Brownbe1aa822011-07-27 16:04:54 -07003339 mVirtualKeys.add();
3340 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003341
3342 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3343 int32_t keyCode;
3344 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003345 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003346 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003347 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003348 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003349 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003350 }
3351
Jeff Brown6328cdc2010-07-29 18:18:33 -07003352 virtualKey.keyCode = keyCode;
3353 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003354
Jeff Brown6328cdc2010-07-29 18:18:33 -07003355 // convert the key definition's display coordinates into touch coordinates for a hit box
3356 int32_t halfWidth = virtualKeyDefinition.width / 2;
3357 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003358
Jeff Brown6328cdc2010-07-29 18:18:33 -07003359 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003360 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003361 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003362 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003363 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003364 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003365 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003366 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003367 }
3368}
3369
Jeff Brownbe1aa822011-07-27 16:04:54 -07003370void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3371 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003372 dump.append(INDENT3 "Virtual Keys:\n");
3373
Jeff Brownbe1aa822011-07-27 16:04:54 -07003374 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3375 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003376 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3377 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3378 i, virtualKey.scanCode, virtualKey.keyCode,
3379 virtualKey.hitLeft, virtualKey.hitRight,
3380 virtualKey.hitTop, virtualKey.hitBottom);
3381 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003382 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003383}
3384
Jeff Brown8d608662010-08-30 03:02:23 -07003385void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003386 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003387 Calibration& out = mCalibration;
3388
Jeff Browna1f89ce2011-08-11 00:05:01 -07003389 // Size
3390 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3391 String8 sizeCalibrationString;
3392 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3393 if (sizeCalibrationString == "none") {
3394 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3395 } else if (sizeCalibrationString == "geometric") {
3396 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3397 } else if (sizeCalibrationString == "diameter") {
3398 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
Jeff Brown037f7272012-06-25 17:31:23 -07003399 } else if (sizeCalibrationString == "box") {
3400 out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003401 } else if (sizeCalibrationString == "area") {
3402 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3403 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003404 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003405 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003406 }
3407 }
3408
Jeff Browna1f89ce2011-08-11 00:05:01 -07003409 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3410 out.sizeScale);
3411 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3412 out.sizeBias);
3413 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3414 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003415
3416 // Pressure
3417 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3418 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003419 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003420 if (pressureCalibrationString == "none") {
3421 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3422 } else if (pressureCalibrationString == "physical") {
3423 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3424 } else if (pressureCalibrationString == "amplitude") {
3425 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3426 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003427 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003428 pressureCalibrationString.string());
3429 }
3430 }
3431
Jeff Brown8d608662010-08-30 03:02:23 -07003432 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3433 out.pressureScale);
3434
Jeff Brown8d608662010-08-30 03:02:23 -07003435 // Orientation
3436 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3437 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003438 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003439 if (orientationCalibrationString == "none") {
3440 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3441 } else if (orientationCalibrationString == "interpolated") {
3442 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003443 } else if (orientationCalibrationString == "vector") {
3444 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003445 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003446 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003447 orientationCalibrationString.string());
3448 }
3449 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003450
3451 // Distance
3452 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3453 String8 distanceCalibrationString;
3454 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3455 if (distanceCalibrationString == "none") {
3456 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3457 } else if (distanceCalibrationString == "scaled") {
3458 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3459 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003460 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003461 distanceCalibrationString.string());
3462 }
3463 }
3464
3465 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3466 out.distanceScale);
Michael Wright5e025eb2013-05-15 23:16:54 -07003467
3468 out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_DEFAULT;
3469 String8 coverageCalibrationString;
3470 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
3471 if (coverageCalibrationString == "none") {
3472 out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE;
3473 } else if (coverageCalibrationString == "box") {
3474 out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_BOX;
3475 } else if (coverageCalibrationString != "default") {
3476 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
3477 coverageCalibrationString.string());
3478 }
3479 }
Jeff Brown8d608662010-08-30 03:02:23 -07003480}
3481
3482void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003483 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003484 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3485 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3486 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003487 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003488 } else {
3489 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3490 }
Jeff Brown8d608662010-08-30 03:02:23 -07003491
Jeff Browna1f89ce2011-08-11 00:05:01 -07003492 // Pressure
3493 if (mRawPointerAxes.pressure.valid) {
3494 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3495 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3496 }
3497 } else {
3498 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003499 }
3500
3501 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003502 if (mRawPointerAxes.orientation.valid) {
3503 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003504 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003505 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003506 } else {
3507 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003508 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003509
3510 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003511 if (mRawPointerAxes.distance.valid) {
3512 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003513 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003514 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003515 } else {
3516 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003517 }
Michael Wright5e025eb2013-05-15 23:16:54 -07003518
3519 // Coverage
3520 if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_DEFAULT) {
3521 mCalibration.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE;
3522 }
Jeff Brown8d608662010-08-30 03:02:23 -07003523}
3524
Jeff Brownef3d7e82010-09-30 14:33:04 -07003525void TouchInputMapper::dumpCalibration(String8& dump) {
3526 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003527
Jeff Browna1f89ce2011-08-11 00:05:01 -07003528 // Size
3529 switch (mCalibration.sizeCalibration) {
3530 case Calibration::SIZE_CALIBRATION_NONE:
3531 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003532 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003533 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3534 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003535 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003536 case Calibration::SIZE_CALIBRATION_DIAMETER:
3537 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3538 break;
Jeff Brown037f7272012-06-25 17:31:23 -07003539 case Calibration::SIZE_CALIBRATION_BOX:
3540 dump.append(INDENT4 "touch.size.calibration: box\n");
3541 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003542 case Calibration::SIZE_CALIBRATION_AREA:
3543 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003544 break;
3545 default:
Steve Blockec193de2012-01-09 18:35:44 +00003546 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003547 }
3548
Jeff Browna1f89ce2011-08-11 00:05:01 -07003549 if (mCalibration.haveSizeScale) {
3550 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3551 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003552 }
3553
Jeff Browna1f89ce2011-08-11 00:05:01 -07003554 if (mCalibration.haveSizeBias) {
3555 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3556 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003557 }
3558
Jeff Browna1f89ce2011-08-11 00:05:01 -07003559 if (mCalibration.haveSizeIsSummed) {
3560 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3561 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003562 }
3563
3564 // Pressure
3565 switch (mCalibration.pressureCalibration) {
3566 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003567 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003568 break;
3569 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003570 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003571 break;
3572 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003573 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003574 break;
3575 default:
Steve Blockec193de2012-01-09 18:35:44 +00003576 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003577 }
3578
Jeff Brown8d608662010-08-30 03:02:23 -07003579 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003580 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3581 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003582 }
3583
Jeff Brown8d608662010-08-30 03:02:23 -07003584 // Orientation
3585 switch (mCalibration.orientationCalibration) {
3586 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003587 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003588 break;
3589 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003590 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003591 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003592 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3593 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3594 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003595 default:
Steve Blockec193de2012-01-09 18:35:44 +00003596 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003597 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003598
3599 // Distance
3600 switch (mCalibration.distanceCalibration) {
3601 case Calibration::DISTANCE_CALIBRATION_NONE:
3602 dump.append(INDENT4 "touch.distance.calibration: none\n");
3603 break;
3604 case Calibration::DISTANCE_CALIBRATION_SCALED:
3605 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3606 break;
3607 default:
Steve Blockec193de2012-01-09 18:35:44 +00003608 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003609 }
3610
3611 if (mCalibration.haveDistanceScale) {
3612 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3613 mCalibration.distanceScale);
3614 }
Michael Wright5e025eb2013-05-15 23:16:54 -07003615
3616 switch (mCalibration.coverageCalibration) {
3617 case Calibration::COVERAGE_CALIBRATION_NONE:
3618 dump.append(INDENT4 "touch.coverage.calibration: none\n");
3619 break;
3620 case Calibration::COVERAGE_CALIBRATION_BOX:
3621 dump.append(INDENT4 "touch.coverage.calibration: box\n");
3622 break;
3623 default:
3624 ALOG_ASSERT(false);
3625 }
Jeff Brown8d608662010-08-30 03:02:23 -07003626}
3627
Jeff Brown65fd2512011-08-18 11:20:58 -07003628void TouchInputMapper::reset(nsecs_t when) {
3629 mCursorButtonAccumulator.reset(getDevice());
3630 mCursorScrollAccumulator.reset(getDevice());
3631 mTouchButtonAccumulator.reset(getDevice());
3632
3633 mPointerVelocityControl.reset();
3634 mWheelXVelocityControl.reset();
3635 mWheelYVelocityControl.reset();
3636
Jeff Brownbe1aa822011-07-27 16:04:54 -07003637 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003638 mLastRawPointerData.clear();
3639 mCurrentCookedPointerData.clear();
3640 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003641 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003642 mLastButtonState = 0;
3643 mCurrentRawVScroll = 0;
3644 mCurrentRawHScroll = 0;
3645 mCurrentFingerIdBits.clear();
3646 mLastFingerIdBits.clear();
3647 mCurrentStylusIdBits.clear();
3648 mLastStylusIdBits.clear();
3649 mCurrentMouseIdBits.clear();
3650 mLastMouseIdBits.clear();
3651 mPointerUsage = POINTER_USAGE_NONE;
3652 mSentHoverEnter = false;
3653 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003654
Jeff Brown65fd2512011-08-18 11:20:58 -07003655 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003656
Jeff Brown65fd2512011-08-18 11:20:58 -07003657 mPointerGesture.reset();
3658 mPointerSimple.reset();
3659
3660 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003661 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3662 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003663 }
3664
Jeff Brown65fd2512011-08-18 11:20:58 -07003665 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003666}
3667
Jeff Brown65fd2512011-08-18 11:20:58 -07003668void TouchInputMapper::process(const RawEvent* rawEvent) {
3669 mCursorButtonAccumulator.process(rawEvent);
3670 mCursorScrollAccumulator.process(rawEvent);
3671 mTouchButtonAccumulator.process(rawEvent);
3672
Jeff Brown49ccac52012-04-11 18:27:33 -07003673 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003674 sync(rawEvent->when);
3675 }
3676}
3677
3678void TouchInputMapper::sync(nsecs_t when) {
3679 // Sync button state.
3680 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3681 | mCursorButtonAccumulator.getButtonState();
3682
3683 // Sync scroll state.
3684 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3685 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3686 mCursorScrollAccumulator.finishSync();
3687
3688 // Sync touch state.
3689 bool havePointerIds = true;
3690 mCurrentRawPointerData.clear();
3691 syncTouch(when, &havePointerIds);
3692
Jeff Brownaa3855d2011-03-17 01:34:19 -07003693#if DEBUG_RAW_EVENTS
3694 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003695 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003696 mLastRawPointerData.pointerCount,
3697 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003698 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003699 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003700 "hovering ids 0x%08x -> 0x%08x",
3701 mLastRawPointerData.pointerCount,
3702 mCurrentRawPointerData.pointerCount,
3703 mLastRawPointerData.touchingIdBits.value,
3704 mCurrentRawPointerData.touchingIdBits.value,
3705 mLastRawPointerData.hoveringIdBits.value,
3706 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003707 }
3708#endif
3709
Jeff Brown65fd2512011-08-18 11:20:58 -07003710 // Reset state that we will compute below.
3711 mCurrentFingerIdBits.clear();
3712 mCurrentStylusIdBits.clear();
3713 mCurrentMouseIdBits.clear();
3714 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003715
Jeff Brown65fd2512011-08-18 11:20:58 -07003716 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3717 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003718 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003719 mCurrentButtonState = 0;
3720 } else {
3721 // Preprocess pointer data.
3722 if (!havePointerIds) {
3723 assignPointerIds();
3724 }
3725
3726 // Handle policy on initial down or hover events.
3727 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003728 bool initialDown = mLastRawPointerData.pointerCount == 0
3729 && mCurrentRawPointerData.pointerCount != 0;
3730 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3731 if (initialDown || buttonsPressed) {
3732 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003733 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003734 getContext()->fadePointer();
3735 }
3736
3737 // Initial downs on external touch devices should wake the device.
3738 // We don't do this for internal touch screens to prevent them from waking
3739 // up in your pocket.
3740 // TODO: Use the input device configuration to control this behavior more finely.
3741 if (getDevice()->isExternal()) {
3742 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3743 }
3744 }
3745
3746 // Synthesize key down from raw buttons if needed.
3747 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3748 policyFlags, mLastButtonState, mCurrentButtonState);
3749
3750 // Consume raw off-screen touches before cooking pointer data.
3751 // If touches are consumed, subsequent code will not receive any pointer data.
3752 if (consumeRawTouches(when, policyFlags)) {
3753 mCurrentRawPointerData.clear();
3754 }
3755
3756 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3757 // with cooked pointer data that has the same ids and indices as the raw data.
3758 // The following code can use either the raw or cooked data, as needed.
3759 cookPointerData();
3760
3761 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003762 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003763 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3764 uint32_t id = idBits.clearFirstMarkedBit();
3765 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3766 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3767 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3768 mCurrentStylusIdBits.markBit(id);
3769 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3770 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3771 mCurrentFingerIdBits.markBit(id);
3772 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3773 mCurrentMouseIdBits.markBit(id);
3774 }
3775 }
3776 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3777 uint32_t id = idBits.clearFirstMarkedBit();
3778 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3779 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3780 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3781 mCurrentStylusIdBits.markBit(id);
3782 }
3783 }
3784
3785 // Stylus takes precedence over all tools, then mouse, then finger.
3786 PointerUsage pointerUsage = mPointerUsage;
3787 if (!mCurrentStylusIdBits.isEmpty()) {
3788 mCurrentMouseIdBits.clear();
3789 mCurrentFingerIdBits.clear();
3790 pointerUsage = POINTER_USAGE_STYLUS;
3791 } else if (!mCurrentMouseIdBits.isEmpty()) {
3792 mCurrentFingerIdBits.clear();
3793 pointerUsage = POINTER_USAGE_MOUSE;
3794 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3795 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003796 }
3797
3798 dispatchPointerUsage(when, policyFlags, pointerUsage);
3799 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003800 if (mDeviceMode == DEVICE_MODE_DIRECT
3801 && mConfig.showTouches && mPointerController != NULL) {
3802 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3803 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3804
3805 mPointerController->setButtonState(mCurrentButtonState);
3806 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3807 mCurrentCookedPointerData.idToIndex,
3808 mCurrentCookedPointerData.touchingIdBits);
3809 }
3810
Jeff Brown65fd2512011-08-18 11:20:58 -07003811 dispatchHoverExit(when, policyFlags);
3812 dispatchTouches(when, policyFlags);
3813 dispatchHoverEnterAndMove(when, policyFlags);
3814 }
3815
3816 // Synthesize key up from raw buttons if needed.
3817 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3818 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003819 }
3820
Jeff Brown6328cdc2010-07-29 18:18:33 -07003821 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003822 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3823 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3824 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003825 mLastFingerIdBits = mCurrentFingerIdBits;
3826 mLastStylusIdBits = mCurrentStylusIdBits;
3827 mLastMouseIdBits = mCurrentMouseIdBits;
3828
3829 // Clear some transient state.
3830 mCurrentRawVScroll = 0;
3831 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003832}
3833
Jeff Brown79ac9692011-04-19 21:20:10 -07003834void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003835 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003836 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3837 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3838 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003839 }
3840}
3841
Jeff Brownbe1aa822011-07-27 16:04:54 -07003842bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3843 // Check for release of a virtual key.
3844 if (mCurrentVirtualKey.down) {
3845 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3846 // Pointer went up while virtual key was down.
3847 mCurrentVirtualKey.down = false;
3848 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003849#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003850 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003851 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003852#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003853 dispatchVirtualKey(when, policyFlags,
3854 AKEY_EVENT_ACTION_UP,
3855 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003856 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003857 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003858 }
3859
Jeff Brownbe1aa822011-07-27 16:04:54 -07003860 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3861 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3862 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3863 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3864 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3865 // Pointer is still within the space of the virtual key.
3866 return true;
3867 }
3868 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003869
Jeff Brownbe1aa822011-07-27 16:04:54 -07003870 // Pointer left virtual key area or another pointer also went down.
3871 // Send key cancellation but do not consume the touch yet.
3872 // This is useful when the user swipes through from the virtual key area
3873 // into the main display surface.
3874 mCurrentVirtualKey.down = false;
3875 if (!mCurrentVirtualKey.ignored) {
3876#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003877 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003878 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3879#endif
3880 dispatchVirtualKey(when, policyFlags,
3881 AKEY_EVENT_ACTION_UP,
3882 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3883 | AKEY_EVENT_FLAG_CANCELED);
3884 }
3885 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003886
Jeff Brownbe1aa822011-07-27 16:04:54 -07003887 if (mLastRawPointerData.touchingIdBits.isEmpty()
3888 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3889 // Pointer just went down. Check for virtual key press or off-screen touches.
3890 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3891 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3892 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3893 // If exactly one pointer went down, check for virtual key hit.
3894 // Otherwise we will drop the entire stroke.
3895 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3896 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3897 if (virtualKey) {
3898 mCurrentVirtualKey.down = true;
3899 mCurrentVirtualKey.downTime = when;
3900 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3901 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3902 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3903 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3904
3905 if (!mCurrentVirtualKey.ignored) {
3906#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003907 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003908 mCurrentVirtualKey.keyCode,
3909 mCurrentVirtualKey.scanCode);
3910#endif
3911 dispatchVirtualKey(when, policyFlags,
3912 AKEY_EVENT_ACTION_DOWN,
3913 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3914 }
3915 }
3916 }
3917 return true;
3918 }
3919 }
3920
Jeff Brownfe508922011-01-18 15:10:10 -08003921 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003922 // most recent touch within the screen area. The idea is to filter out stray
3923 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003924 //
3925 // Problems we're trying to solve:
3926 //
3927 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3928 // virtual key area that is implemented by a separate touch panel and accidentally
3929 // triggers a virtual key.
3930 //
3931 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3932 // area and accidentally triggers a virtual key. This often happens when virtual keys
3933 // are layed out below the screen near to where the on screen keyboard's space bar
3934 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003935 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003936 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003937 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003938 return false;
3939}
3940
3941void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3942 int32_t keyEventAction, int32_t keyEventFlags) {
3943 int32_t keyCode = mCurrentVirtualKey.keyCode;
3944 int32_t scanCode = mCurrentVirtualKey.scanCode;
3945 nsecs_t downTime = mCurrentVirtualKey.downTime;
3946 int32_t metaState = mContext->getGlobalMetaState();
3947 policyFlags |= POLICY_FLAG_VIRTUAL;
3948
3949 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3950 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3951 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003952}
3953
Jeff Brown6d0fec22010-07-23 21:28:06 -07003954void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003955 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3956 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003957 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003958 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003959
3960 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003961 if (!currentIdBits.isEmpty()) {
3962 // No pointer id changes so this is a move event.
3963 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003964 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003965 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3966 AMOTION_EVENT_EDGE_FLAG_NONE,
3967 mCurrentCookedPointerData.pointerProperties,
3968 mCurrentCookedPointerData.pointerCoords,
3969 mCurrentCookedPointerData.idToIndex,
3970 currentIdBits, -1,
3971 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3972 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003973 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003974 // There may be pointers going up and pointers going down and pointers moving
3975 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003976 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3977 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003978 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003979 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003980
Jeff Brownace13b12011-03-09 17:39:48 -08003981 // Update last coordinates of pointers that have moved so that we observe the new
3982 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003983 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003984 mCurrentCookedPointerData.pointerProperties,
3985 mCurrentCookedPointerData.pointerCoords,
3986 mCurrentCookedPointerData.idToIndex,
3987 mLastCookedPointerData.pointerProperties,
3988 mLastCookedPointerData.pointerCoords,
3989 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003990 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003991 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003992 moveNeeded = true;
3993 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003994
Jeff Brownace13b12011-03-09 17:39:48 -08003995 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003996 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003997 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003998
Jeff Brown65fd2512011-08-18 11:20:58 -07003999 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004000 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004001 mLastCookedPointerData.pointerProperties,
4002 mLastCookedPointerData.pointerCoords,
4003 mLastCookedPointerData.idToIndex,
4004 dispatchedIdBits, upId,
4005 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004006 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004007 }
4008
Jeff Brownc3db8582010-10-20 15:33:38 -07004009 // Dispatch move events if any of the remaining pointers moved from their old locations.
4010 // Although applications receive new locations as part of individual pointer up
4011 // events, they do not generally handle them except when presented in a move event.
4012 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00004013 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07004014 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004015 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004016 mCurrentCookedPointerData.pointerProperties,
4017 mCurrentCookedPointerData.pointerCoords,
4018 mCurrentCookedPointerData.idToIndex,
4019 dispatchedIdBits, -1,
4020 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07004021 }
4022
4023 // Dispatch pointer down events using the new pointer locations.
4024 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004025 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004026 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004027
Jeff Brownace13b12011-03-09 17:39:48 -08004028 if (dispatchedIdBits.count() == 1) {
4029 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07004030 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004031 }
4032
Jeff Brown65fd2512011-08-18 11:20:58 -07004033 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004034 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004035 mCurrentCookedPointerData.pointerProperties,
4036 mCurrentCookedPointerData.pointerCoords,
4037 mCurrentCookedPointerData.idToIndex,
4038 dispatchedIdBits, downId,
4039 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004040 }
4041 }
Jeff Brownace13b12011-03-09 17:39:48 -08004042}
4043
Jeff Brownbe1aa822011-07-27 16:04:54 -07004044void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
4045 if (mSentHoverEnter &&
4046 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
4047 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
4048 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07004049 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004050 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
4051 mLastCookedPointerData.pointerProperties,
4052 mLastCookedPointerData.pointerCoords,
4053 mLastCookedPointerData.idToIndex,
4054 mLastCookedPointerData.hoveringIdBits, -1,
4055 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4056 mSentHoverEnter = false;
4057 }
4058}
Jeff Brownace13b12011-03-09 17:39:48 -08004059
Jeff Brownbe1aa822011-07-27 16:04:54 -07004060void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
4061 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
4062 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
4063 int32_t metaState = getContext()->getGlobalMetaState();
4064 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004065 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004066 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
4067 mCurrentCookedPointerData.pointerProperties,
4068 mCurrentCookedPointerData.pointerCoords,
4069 mCurrentCookedPointerData.idToIndex,
4070 mCurrentCookedPointerData.hoveringIdBits, -1,
4071 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4072 mSentHoverEnter = true;
4073 }
Jeff Brownace13b12011-03-09 17:39:48 -08004074
Jeff Brown65fd2512011-08-18 11:20:58 -07004075 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004076 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
4077 mCurrentCookedPointerData.pointerProperties,
4078 mCurrentCookedPointerData.pointerCoords,
4079 mCurrentCookedPointerData.idToIndex,
4080 mCurrentCookedPointerData.hoveringIdBits, -1,
4081 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4082 }
4083}
4084
4085void TouchInputMapper::cookPointerData() {
4086 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4087
4088 mCurrentCookedPointerData.clear();
4089 mCurrentCookedPointerData.pointerCount = currentPointerCount;
4090 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
4091 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
4092
4093 // Walk through the the active pointers and map device coordinates onto
4094 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08004095 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004096 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004097
Jeff Browna1f89ce2011-08-11 00:05:01 -07004098 // Size
4099 float touchMajor, touchMinor, toolMajor, toolMinor, size;
4100 switch (mCalibration.sizeCalibration) {
4101 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
4102 case Calibration::SIZE_CALIBRATION_DIAMETER:
Jeff Brown037f7272012-06-25 17:31:23 -07004103 case Calibration::SIZE_CALIBRATION_BOX:
Jeff Browna1f89ce2011-08-11 00:05:01 -07004104 case Calibration::SIZE_CALIBRATION_AREA:
4105 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
4106 touchMajor = in.touchMajor;
4107 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
4108 toolMajor = in.toolMajor;
4109 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
4110 size = mRawPointerAxes.touchMinor.valid
4111 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4112 } else if (mRawPointerAxes.touchMajor.valid) {
4113 toolMajor = touchMajor = in.touchMajor;
4114 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
4115 ? in.touchMinor : in.touchMajor;
4116 size = mRawPointerAxes.touchMinor.valid
4117 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4118 } else if (mRawPointerAxes.toolMajor.valid) {
4119 touchMajor = toolMajor = in.toolMajor;
4120 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
4121 ? in.toolMinor : in.toolMajor;
4122 size = mRawPointerAxes.toolMinor.valid
4123 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004124 } else {
Steve Blockec193de2012-01-09 18:35:44 +00004125 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07004126 "Size calibration should have been resolved to NONE.");
4127 touchMajor = 0;
4128 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004129 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004130 toolMinor = 0;
4131 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004132 }
Jeff Brownace13b12011-03-09 17:39:48 -08004133
Jeff Browna1f89ce2011-08-11 00:05:01 -07004134 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
4135 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
4136 if (touchingCount > 1) {
4137 touchMajor /= touchingCount;
4138 touchMinor /= touchingCount;
4139 toolMajor /= touchingCount;
4140 toolMinor /= touchingCount;
4141 size /= touchingCount;
4142 }
4143 }
Jeff Brownace13b12011-03-09 17:39:48 -08004144
Jeff Browna1f89ce2011-08-11 00:05:01 -07004145 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4146 touchMajor *= mGeometricScale;
4147 touchMinor *= mGeometricScale;
4148 toolMajor *= mGeometricScale;
4149 toolMinor *= mGeometricScale;
4150 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4151 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004152 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004153 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4154 toolMinor = toolMajor;
4155 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4156 touchMinor = touchMajor;
4157 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004158 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004159
4160 mCalibration.applySizeScaleAndBias(&touchMajor);
4161 mCalibration.applySizeScaleAndBias(&touchMinor);
4162 mCalibration.applySizeScaleAndBias(&toolMajor);
4163 mCalibration.applySizeScaleAndBias(&toolMinor);
4164 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004165 break;
4166 default:
4167 touchMajor = 0;
4168 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004169 toolMajor = 0;
4170 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004171 size = 0;
4172 break;
4173 }
4174
Jeff Browna1f89ce2011-08-11 00:05:01 -07004175 // Pressure
4176 float pressure;
4177 switch (mCalibration.pressureCalibration) {
4178 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4179 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4180 pressure = in.pressure * mPressureScale;
4181 break;
4182 default:
4183 pressure = in.isHovering ? 0 : 1;
4184 break;
4185 }
4186
Jeff Brown65fd2512011-08-18 11:20:58 -07004187 // Tilt and Orientation
4188 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004189 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004190 if (mHaveTilt) {
4191 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4192 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4193 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4194 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4195 } else {
4196 tilt = 0;
4197
4198 switch (mCalibration.orientationCalibration) {
4199 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown037f7272012-06-25 17:31:23 -07004200 orientation = in.orientation * mOrientationScale;
Jeff Brown65fd2512011-08-18 11:20:58 -07004201 break;
4202 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4203 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4204 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4205 if (c1 != 0 || c2 != 0) {
4206 orientation = atan2f(c1, c2) * 0.5f;
4207 float confidence = hypotf(c1, c2);
4208 float scale = 1.0f + confidence / 16.0f;
4209 touchMajor *= scale;
4210 touchMinor /= scale;
4211 toolMajor *= scale;
4212 toolMinor /= scale;
4213 } else {
4214 orientation = 0;
4215 }
4216 break;
4217 }
4218 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004219 orientation = 0;
4220 }
Jeff Brownace13b12011-03-09 17:39:48 -08004221 }
4222
Jeff Brown80fd47c2011-05-24 01:07:44 -07004223 // Distance
4224 float distance;
4225 switch (mCalibration.distanceCalibration) {
4226 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004227 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004228 break;
4229 default:
4230 distance = 0;
4231 }
4232
Michael Wright5e025eb2013-05-15 23:16:54 -07004233 // Coverage
4234 int32_t rawLeft, rawTop, rawRight, rawBottom;
4235 switch (mCalibration.coverageCalibration) {
4236 case Calibration::COVERAGE_CALIBRATION_BOX:
4237 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
4238 rawRight = in.toolMinor & 0x0000ffff;
4239 rawBottom = in.toolMajor & 0x0000ffff;
4240 rawTop = (in.toolMajor & 0xffff0000) >> 16;
4241 break;
4242 default:
4243 rawLeft = rawTop = rawRight = rawBottom = 0;
4244 break;
4245 }
4246
4247 // X, Y, and the bounding box for coverage information
Jeff Brownace13b12011-03-09 17:39:48 -08004248 // Adjust coords for surface orientation.
Michael Wright5e025eb2013-05-15 23:16:54 -07004249 float x, y, left, top, right, bottom;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004250 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004251 case DISPLAY_ORIENTATION_90:
Jeff Brown83d616a2012-09-09 20:33:43 -07004252 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4253 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004254 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4255 right = float(rawBottom- mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4256 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate;
4257 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004258 orientation -= M_PI_2;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004259 if (orientation < mOrientedRanges.orientation.min) {
4260 orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
Jeff Brownace13b12011-03-09 17:39:48 -08004261 }
4262 break;
4263 case DISPLAY_ORIENTATION_180:
Jeff Brown83d616a2012-09-09 20:33:43 -07004264 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
4265 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004266 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate;
4267 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate;
4268 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate;
4269 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004270 orientation -= M_PI;
4271 if (orientation < mOrientedRanges.orientation.min) {
4272 orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
4273 }
Jeff Brownace13b12011-03-09 17:39:48 -08004274 break;
4275 case DISPLAY_ORIENTATION_270:
Jeff Brown83d616a2012-09-09 20:33:43 -07004276 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
4277 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004278 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate;
4279 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate;
4280 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4281 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004282 orientation += M_PI_2;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004283 if (orientation > mOrientedRanges.orientation.max) {
4284 orientation -= (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
Jeff Brownace13b12011-03-09 17:39:48 -08004285 }
4286 break;
4287 default:
Jeff Brown83d616a2012-09-09 20:33:43 -07004288 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4289 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004290 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4291 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4292 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4293 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004294 break;
4295 }
4296
4297 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004298 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004299 out.clear();
4300 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4301 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4302 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4303 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4304 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4305 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
Jeff Brownace13b12011-03-09 17:39:48 -08004306 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004307 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004308 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright5e025eb2013-05-15 23:16:54 -07004309 if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) {
4310 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
4311 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
4312 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
4313 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
4314 } else {
4315 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4316 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4317 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004318
4319 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004320 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4321 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004322 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004323 properties.id = id;
4324 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004325
Jeff Brownbe1aa822011-07-27 16:04:54 -07004326 // Write id index.
4327 mCurrentCookedPointerData.idToIndex[id] = i;
4328 }
Jeff Brownace13b12011-03-09 17:39:48 -08004329}
4330
Jeff Brown65fd2512011-08-18 11:20:58 -07004331void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4332 PointerUsage pointerUsage) {
4333 if (pointerUsage != mPointerUsage) {
4334 abortPointerUsage(when, policyFlags);
4335 mPointerUsage = pointerUsage;
4336 }
4337
4338 switch (mPointerUsage) {
4339 case POINTER_USAGE_GESTURES:
4340 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4341 break;
4342 case POINTER_USAGE_STYLUS:
4343 dispatchPointerStylus(when, policyFlags);
4344 break;
4345 case POINTER_USAGE_MOUSE:
4346 dispatchPointerMouse(when, policyFlags);
4347 break;
4348 default:
4349 break;
4350 }
4351}
4352
4353void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4354 switch (mPointerUsage) {
4355 case POINTER_USAGE_GESTURES:
4356 abortPointerGestures(when, policyFlags);
4357 break;
4358 case POINTER_USAGE_STYLUS:
4359 abortPointerStylus(when, policyFlags);
4360 break;
4361 case POINTER_USAGE_MOUSE:
4362 abortPointerMouse(when, policyFlags);
4363 break;
4364 default:
4365 break;
4366 }
4367
4368 mPointerUsage = POINTER_USAGE_NONE;
4369}
4370
Jeff Brown79ac9692011-04-19 21:20:10 -07004371void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4372 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004373 // Update current gesture coordinates.
4374 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004375 bool sendEvents = preparePointerGestures(when,
4376 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4377 if (!sendEvents) {
4378 return;
4379 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004380 if (finishPreviousGesture) {
4381 cancelPreviousGesture = false;
4382 }
Jeff Brownace13b12011-03-09 17:39:48 -08004383
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004384 // Update the pointer presentation and spots.
4385 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4386 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4387 if (finishPreviousGesture || cancelPreviousGesture) {
4388 mPointerController->clearSpots();
4389 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004390 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4391 mPointerGesture.currentGestureIdToIndex,
4392 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004393 } else {
4394 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4395 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004396
Jeff Brown538881e2011-05-25 18:23:38 -07004397 // Show or hide the pointer if needed.
4398 switch (mPointerGesture.currentGestureMode) {
4399 case PointerGesture::NEUTRAL:
4400 case PointerGesture::QUIET:
4401 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4402 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4403 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4404 // Remind the user of where the pointer is after finishing a gesture with spots.
4405 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4406 }
4407 break;
4408 case PointerGesture::TAP:
4409 case PointerGesture::TAP_DRAG:
4410 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4411 case PointerGesture::HOVER:
4412 case PointerGesture::PRESS:
4413 // Unfade the pointer when the current gesture manipulates the
4414 // area directly under the pointer.
4415 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4416 break;
4417 case PointerGesture::SWIPE:
4418 case PointerGesture::FREEFORM:
4419 // Fade the pointer when the current gesture manipulates a different
4420 // area and there are spots to guide the user experience.
4421 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4422 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4423 } else {
4424 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4425 }
4426 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004427 }
4428
Jeff Brownace13b12011-03-09 17:39:48 -08004429 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004430 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004431 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004432
4433 // Update last coordinates of pointers that have moved so that we observe the new
4434 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004435 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4436 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4437 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004438 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004439 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4440 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4441 bool moveNeeded = false;
4442 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004443 && !mPointerGesture.lastGestureIdBits.isEmpty()
4444 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004445 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4446 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004447 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004448 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004449 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004450 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4451 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004452 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004453 moveNeeded = true;
4454 }
Jeff Brownace13b12011-03-09 17:39:48 -08004455 }
4456
4457 // Send motion events for all pointers that went up or were canceled.
4458 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4459 if (!dispatchedGestureIdBits.isEmpty()) {
4460 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004461 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004462 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4463 AMOTION_EVENT_EDGE_FLAG_NONE,
4464 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004465 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4466 dispatchedGestureIdBits, -1,
4467 0, 0, mPointerGesture.downTime);
4468
4469 dispatchedGestureIdBits.clear();
4470 } else {
4471 BitSet32 upGestureIdBits;
4472 if (finishPreviousGesture) {
4473 upGestureIdBits = dispatchedGestureIdBits;
4474 } else {
4475 upGestureIdBits.value = dispatchedGestureIdBits.value
4476 & ~mPointerGesture.currentGestureIdBits.value;
4477 }
4478 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004479 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004480
Jeff Brown65fd2512011-08-18 11:20:58 -07004481 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004482 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004483 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4484 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004485 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4486 dispatchedGestureIdBits, id,
4487 0, 0, mPointerGesture.downTime);
4488
4489 dispatchedGestureIdBits.clearBit(id);
4490 }
4491 }
4492 }
4493
4494 // Send motion events for all pointers that moved.
4495 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004496 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004497 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4498 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004499 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4500 dispatchedGestureIdBits, -1,
4501 0, 0, mPointerGesture.downTime);
4502 }
4503
4504 // Send motion events for all pointers that went down.
4505 if (down) {
4506 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4507 & ~dispatchedGestureIdBits.value);
4508 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004509 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004510 dispatchedGestureIdBits.markBit(id);
4511
Jeff Brownace13b12011-03-09 17:39:48 -08004512 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004513 mPointerGesture.downTime = when;
4514 }
4515
Jeff Brown65fd2512011-08-18 11:20:58 -07004516 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004517 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004518 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004519 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4520 dispatchedGestureIdBits, id,
4521 0, 0, mPointerGesture.downTime);
4522 }
4523 }
4524
Jeff Brownace13b12011-03-09 17:39:48 -08004525 // Send motion events for hover.
4526 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004527 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004528 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4529 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4530 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004531 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4532 mPointerGesture.currentGestureIdBits, -1,
4533 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004534 } else if (dispatchedGestureIdBits.isEmpty()
4535 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4536 // Synthesize a hover move event after all pointers go up to indicate that
4537 // the pointer is hovering again even if the user is not currently touching
4538 // the touch pad. This ensures that a view will receive a fresh hover enter
4539 // event after a tap.
4540 float x, y;
4541 mPointerController->getPosition(&x, &y);
4542
4543 PointerProperties pointerProperties;
4544 pointerProperties.clear();
4545 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004546 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004547
4548 PointerCoords pointerCoords;
4549 pointerCoords.clear();
4550 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4551 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4552
Jeff Brown65fd2512011-08-18 11:20:58 -07004553 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004554 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4555 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07004556 mViewport.displayId, 1, &pointerProperties, &pointerCoords,
4557 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004558 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004559 }
4560
4561 // Update state.
4562 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4563 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004564 mPointerGesture.lastGestureIdBits.clear();
4565 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004566 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4567 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004568 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004569 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004570 mPointerGesture.lastGestureProperties[index].copyFrom(
4571 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004572 mPointerGesture.lastGestureCoords[index].copyFrom(
4573 mPointerGesture.currentGestureCoords[index]);
4574 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004575 }
4576 }
4577}
4578
Jeff Brown65fd2512011-08-18 11:20:58 -07004579void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4580 // Cancel previously dispatches pointers.
4581 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4582 int32_t metaState = getContext()->getGlobalMetaState();
4583 int32_t buttonState = mCurrentButtonState;
4584 dispatchMotion(when, policyFlags, mSource,
4585 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4586 AMOTION_EVENT_EDGE_FLAG_NONE,
4587 mPointerGesture.lastGestureProperties,
4588 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4589 mPointerGesture.lastGestureIdBits, -1,
4590 0, 0, mPointerGesture.downTime);
4591 }
4592
4593 // Reset the current pointer gesture.
4594 mPointerGesture.reset();
4595 mPointerVelocityControl.reset();
4596
4597 // Remove any current spots.
4598 if (mPointerController != NULL) {
4599 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4600 mPointerController->clearSpots();
4601 }
4602}
4603
Jeff Brown79ac9692011-04-19 21:20:10 -07004604bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4605 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004606 *outCancelPreviousGesture = false;
4607 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004608
Jeff Brown79ac9692011-04-19 21:20:10 -07004609 // Handle TAP timeout.
4610 if (isTimeout) {
4611#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004612 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004613#endif
4614
4615 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004616 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004617 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004618 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004619 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004620 } else {
4621 // The tap is finished.
4622#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004623 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004624#endif
4625 *outFinishPreviousGesture = true;
4626
4627 mPointerGesture.activeGestureId = -1;
4628 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4629 mPointerGesture.currentGestureIdBits.clear();
4630
Jeff Brown65fd2512011-08-18 11:20:58 -07004631 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004632 return true;
4633 }
4634 }
4635
4636 // We did not handle this timeout.
4637 return false;
4638 }
4639
Jeff Brown65fd2512011-08-18 11:20:58 -07004640 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4641 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4642
Jeff Brownace13b12011-03-09 17:39:48 -08004643 // Update the velocity tracker.
4644 {
4645 VelocityTracker::Position positions[MAX_POINTERS];
4646 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004647 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004648 uint32_t id = idBits.clearFirstMarkedBit();
4649 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004650 positions[count].x = pointer.x * mPointerXMovementScale;
4651 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004652 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004653 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004654 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004655 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004656
Michael Wright398d3092013-07-02 17:35:00 -07004657 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
4658 // to NEUTRAL, then we should not generate tap event.
4659 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER
4660 && mPointerGesture.lastGestureMode != PointerGesture::TAP
4661 && mPointerGesture.lastGestureMode != PointerGesture::TAP_DRAG) {
4662 mPointerGesture.resetTap();
4663 }
4664
Jeff Brownace13b12011-03-09 17:39:48 -08004665 // Pick a new active touch id if needed.
4666 // Choose an arbitrary pointer that just went down, if there is one.
4667 // Otherwise choose an arbitrary remaining pointer.
4668 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004669 // We keep the same active touch id for as long as possible.
4670 bool activeTouchChanged = false;
4671 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4672 int32_t activeTouchId = lastActiveTouchId;
4673 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004674 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004675 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004676 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004677 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004678 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004679 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004680 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004681 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004682 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004683 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004684 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004685 } else {
4686 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004687 }
4688 }
4689
4690 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004691 bool isQuietTime = false;
4692 if (activeTouchId < 0) {
4693 mPointerGesture.resetQuietTime();
4694 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004695 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004696 if (!isQuietTime) {
4697 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4698 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4699 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004700 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004701 // Enter quiet time when exiting swipe or freeform state.
4702 // This is to prevent accidentally entering the hover state and flinging the
4703 // pointer when finishing a swipe and there is still one pointer left onscreen.
4704 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004705 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004706 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004707 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004708 // Enter quiet time when releasing the button and there are still two or more
4709 // fingers down. This may indicate that one finger was used to press the button
4710 // but it has not gone up yet.
4711 isQuietTime = true;
4712 }
4713 if (isQuietTime) {
4714 mPointerGesture.quietTime = when;
4715 }
Jeff Brownace13b12011-03-09 17:39:48 -08004716 }
4717 }
4718
4719 // Switch states based on button and pointer state.
4720 if (isQuietTime) {
4721 // Case 1: Quiet time. (QUIET)
4722#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004723 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004724 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004725#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004726 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4727 *outFinishPreviousGesture = true;
4728 }
Jeff Brownace13b12011-03-09 17:39:48 -08004729
4730 mPointerGesture.activeGestureId = -1;
4731 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004732 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004733
Jeff Brown65fd2512011-08-18 11:20:58 -07004734 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004735 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004736 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004737 // The pointer follows the active touch point.
4738 // Emit DOWN, MOVE, UP events at the pointer location.
4739 //
4740 // Only the active touch matters; other fingers are ignored. This policy helps
4741 // to handle the case where the user places a second finger on the touch pad
4742 // to apply the necessary force to depress an integrated button below the surface.
4743 // We don't want the second finger to be delivered to applications.
4744 //
4745 // For this to work well, we need to make sure to track the pointer that is really
4746 // active. If the user first puts one finger down to click then adds another
4747 // finger to drag then the active pointer should switch to the finger that is
4748 // being dragged.
4749#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004750 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004751 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004752#endif
4753 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004754 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004755 *outFinishPreviousGesture = true;
4756 mPointerGesture.activeGestureId = 0;
4757 }
4758
4759 // Switch pointers if needed.
4760 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004761 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004762 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004763 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004764 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004765 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004766 float vx, vy;
4767 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4768 float speed = hypotf(vx, vy);
4769 if (speed > bestSpeed) {
4770 bestId = id;
4771 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004772 }
Jeff Brown8d608662010-08-30 03:02:23 -07004773 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004774 }
4775 if (bestId >= 0 && bestId != activeTouchId) {
4776 mPointerGesture.activeTouchId = activeTouchId = bestId;
4777 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004778#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004779 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004780 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004781#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004782 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004783 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004784
Jeff Brown65fd2512011-08-18 11:20:58 -07004785 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004786 const RawPointerData::Pointer& currentPointer =
4787 mCurrentRawPointerData.pointerForId(activeTouchId);
4788 const RawPointerData::Pointer& lastPointer =
4789 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004790 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4791 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004792
Jeff Brownbe1aa822011-07-27 16:04:54 -07004793 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004794 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004795
4796 // Move the pointer using a relative motion.
4797 // When using spots, the click will occur at the position of the anchor
4798 // spot and all other spots will move there.
4799 mPointerController->move(deltaX, deltaY);
4800 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004801 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004802 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004803
Jeff Brownace13b12011-03-09 17:39:48 -08004804 float x, y;
4805 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004806
Jeff Brown79ac9692011-04-19 21:20:10 -07004807 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004808 mPointerGesture.currentGestureIdBits.clear();
4809 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4810 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004811 mPointerGesture.currentGestureProperties[0].clear();
4812 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004813 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004814 mPointerGesture.currentGestureCoords[0].clear();
4815 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4816 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4817 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004818 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004819 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004820 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4821 *outFinishPreviousGesture = true;
4822 }
Jeff Brownace13b12011-03-09 17:39:48 -08004823
Jeff Brown79ac9692011-04-19 21:20:10 -07004824 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004825 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004826 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004827 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4828 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004829 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004830 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004831 float x, y;
4832 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004833 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4834 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004835#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004836 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004837#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004838
4839 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004840 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004841 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004842
Jeff Brownace13b12011-03-09 17:39:48 -08004843 mPointerGesture.activeGestureId = 0;
4844 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004845 mPointerGesture.currentGestureIdBits.clear();
4846 mPointerGesture.currentGestureIdBits.markBit(
4847 mPointerGesture.activeGestureId);
4848 mPointerGesture.currentGestureIdToIndex[
4849 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004850 mPointerGesture.currentGestureProperties[0].clear();
4851 mPointerGesture.currentGestureProperties[0].id =
4852 mPointerGesture.activeGestureId;
4853 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004854 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004855 mPointerGesture.currentGestureCoords[0].clear();
4856 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004857 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004858 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004859 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004860 mPointerGesture.currentGestureCoords[0].setAxisValue(
4861 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004862
Jeff Brownace13b12011-03-09 17:39:48 -08004863 tapped = true;
4864 } else {
4865#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004866 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004867 x - mPointerGesture.tapX,
4868 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004869#endif
4870 }
4871 } else {
4872#if DEBUG_GESTURES
Michael Wright398d3092013-07-02 17:35:00 -07004873 if (mPointerGesture.tapDownTime != LLONG_MIN) {
4874 ALOGD("Gestures: Not a TAP, %0.3fms since down",
4875 (when - mPointerGesture.tapDownTime) * 0.000001f);
4876 } else {
4877 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
4878 }
Jeff Brownace13b12011-03-09 17:39:48 -08004879#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004880 }
Jeff Brownace13b12011-03-09 17:39:48 -08004881 }
Jeff Brown2352b972011-04-12 22:39:53 -07004882
Jeff Brown65fd2512011-08-18 11:20:58 -07004883 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004884
Jeff Brownace13b12011-03-09 17:39:48 -08004885 if (!tapped) {
4886#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004887 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004888#endif
4889 mPointerGesture.activeGestureId = -1;
4890 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004891 mPointerGesture.currentGestureIdBits.clear();
4892 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004893 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004894 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004895 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004896 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4897 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004898 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004899
Jeff Brown79ac9692011-04-19 21:20:10 -07004900 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4901 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004902 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004903 float x, y;
4904 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004905 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4906 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004907 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4908 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004909#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004910 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004911 x - mPointerGesture.tapX,
4912 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004913#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004914 }
4915 } else {
4916#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004917 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004918 (when - mPointerGesture.tapUpTime) * 0.000001f);
4919#endif
4920 }
4921 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4922 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4923 }
Jeff Brownace13b12011-03-09 17:39:48 -08004924
Jeff Brown65fd2512011-08-18 11:20:58 -07004925 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004926 const RawPointerData::Pointer& currentPointer =
4927 mCurrentRawPointerData.pointerForId(activeTouchId);
4928 const RawPointerData::Pointer& lastPointer =
4929 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004930 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004931 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004932 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004933 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004934
Jeff Brownbe1aa822011-07-27 16:04:54 -07004935 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004936 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004937
Jeff Brown2352b972011-04-12 22:39:53 -07004938 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004939 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004940 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004941 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004942 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004943 }
4944
Jeff Brown79ac9692011-04-19 21:20:10 -07004945 bool down;
4946 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4947#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004948 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004949#endif
4950 down = true;
4951 } else {
4952#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004953 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004954#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004955 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4956 *outFinishPreviousGesture = true;
4957 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004958 mPointerGesture.activeGestureId = 0;
4959 down = false;
4960 }
Jeff Brownace13b12011-03-09 17:39:48 -08004961
4962 float x, y;
4963 mPointerController->getPosition(&x, &y);
4964
Jeff Brownace13b12011-03-09 17:39:48 -08004965 mPointerGesture.currentGestureIdBits.clear();
4966 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4967 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004968 mPointerGesture.currentGestureProperties[0].clear();
4969 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4970 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004971 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004972 mPointerGesture.currentGestureCoords[0].clear();
4973 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4974 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004975 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4976 down ? 1.0f : 0.0f);
4977
Jeff Brown65fd2512011-08-18 11:20:58 -07004978 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004979 mPointerGesture.resetTap();
4980 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004981 mPointerGesture.tapX = x;
4982 mPointerGesture.tapY = y;
4983 }
Jeff Brownace13b12011-03-09 17:39:48 -08004984 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004985 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4986 // We need to provide feedback for each finger that goes down so we cannot wait
4987 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004988 //
Jeff Brown2352b972011-04-12 22:39:53 -07004989 // The ambiguous case is deciding what to do when there are two fingers down but they
4990 // have not moved enough to determine whether they are part of a drag or part of a
4991 // freeform gesture, or just a press or long-press at the pointer location.
4992 //
4993 // When there are two fingers we start with the PRESS hypothesis and we generate a
4994 // down at the pointer location.
4995 //
4996 // When the two fingers move enough or when additional fingers are added, we make
4997 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004998 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004999
Jeff Brown214eaf42011-05-26 19:17:02 -07005000 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07005001 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07005002 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08005003 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
5004 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08005005 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07005006 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07005007 // Additional pointers have gone down but not yet settled.
5008 // Reset the gesture.
5009#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005010 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005011 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07005012 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07005013 * 0.000001f);
5014#endif
5015 *outCancelPreviousGesture = true;
5016 } else {
5017 // Continue previous gesture.
5018 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
5019 }
5020
5021 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07005022 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
5023 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07005024 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07005025 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08005026
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005027 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07005028#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005029 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005030 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07005031 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005032 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07005033#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005034 mCurrentRawPointerData.getCentroidOfTouchingPointers(
5035 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005036 &mPointerGesture.referenceTouchY);
5037 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
5038 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07005039 }
Jeff Brownace13b12011-03-09 17:39:48 -08005040
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005041 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07005042 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07005043 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
5044 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005045 mPointerGesture.referenceDeltas[id].dx = 0;
5046 mPointerGesture.referenceDeltas[id].dy = 0;
5047 }
Jeff Brown65fd2512011-08-18 11:20:58 -07005048 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005049
5050 // Add delta for all fingers and calculate a common movement delta.
5051 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07005052 BitSet32 commonIdBits(mLastFingerIdBits.value
5053 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005054 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
5055 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005056 uint32_t id = idBits.clearFirstMarkedBit();
5057 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
5058 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005059 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
5060 delta.dx += cpd.x - lpd.x;
5061 delta.dy += cpd.y - lpd.y;
5062
5063 if (first) {
5064 commonDeltaX = delta.dx;
5065 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07005066 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005067 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
5068 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
5069 }
5070 }
Jeff Brownace13b12011-03-09 17:39:48 -08005071
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005072 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
5073 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
5074 float dist[MAX_POINTER_ID + 1];
5075 int32_t distOverThreshold = 0;
5076 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005077 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005078 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07005079 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
5080 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07005081 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005082 distOverThreshold += 1;
5083 }
5084 }
5085
5086 // Only transition when at least two pointers have moved further than
5087 // the minimum distance threshold.
5088 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005089 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005090 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07005091#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005092 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005093 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005094#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005095 *outCancelPreviousGesture = true;
5096 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5097 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005098 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07005099 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005100 uint32_t id1 = idBits.clearFirstMarkedBit();
5101 uint32_t id2 = idBits.firstMarkedBit();
5102 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
5103 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
5104 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
5105 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
5106 // There are two pointers but they are too far apart for a SWIPE,
5107 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005108#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005109 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005110 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005111#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005112 *outCancelPreviousGesture = true;
5113 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5114 } else {
5115 // There are two pointers. Wait for both pointers to start moving
5116 // before deciding whether this is a SWIPE or FREEFORM gesture.
5117 float dist1 = dist[id1];
5118 float dist2 = dist[id2];
5119 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
5120 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
5121 // Calculate the dot product of the displacement vectors.
5122 // When the vectors are oriented in approximately the same direction,
5123 // the angle betweeen them is near zero and the cosine of the angle
5124 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
5125 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
5126 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07005127 float dx1 = delta1.dx * mPointerXZoomScale;
5128 float dy1 = delta1.dy * mPointerYZoomScale;
5129 float dx2 = delta2.dx * mPointerXZoomScale;
5130 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005131 float dot = dx1 * dx2 + dy1 * dy2;
5132 float cosine = dot / (dist1 * dist2); // denominator always > 0
5133 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
5134 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005135#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005136 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005137 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5138 "cosine %0.3f >= %0.3f",
5139 dist1, mConfig.pointerGestureMultitouchMinDistance,
5140 dist2, mConfig.pointerGestureMultitouchMinDistance,
5141 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005142#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005143 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
5144 } else {
5145 // Pointers are moving in different directions. Switch to FREEFORM.
5146#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005147 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005148 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5149 "cosine %0.3f < %0.3f",
5150 dist1, mConfig.pointerGestureMultitouchMinDistance,
5151 dist2, mConfig.pointerGestureMultitouchMinDistance,
5152 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
5153#endif
5154 *outCancelPreviousGesture = true;
5155 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5156 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005157 }
Jeff Brownace13b12011-03-09 17:39:48 -08005158 }
5159 }
Jeff Brownace13b12011-03-09 17:39:48 -08005160 }
5161 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07005162 // Switch from SWIPE to FREEFORM if additional pointers go down.
5163 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07005164 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07005165#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005166 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005167 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005168#endif
Jeff Brownace13b12011-03-09 17:39:48 -08005169 *outCancelPreviousGesture = true;
5170 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07005171 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005172 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005173
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005174 // Move the reference points based on the overall group motion of the fingers
5175 // except in PRESS mode while waiting for a transition to occur.
5176 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
5177 && (commonDeltaX || commonDeltaY)) {
5178 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005179 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07005180 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005181 delta.dx = 0;
5182 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07005183 }
5184
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005185 mPointerGesture.referenceTouchX += commonDeltaX;
5186 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07005187
Jeff Brown65fd2512011-08-18 11:20:58 -07005188 commonDeltaX *= mPointerXMovementScale;
5189 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07005190
Jeff Brownbe1aa822011-07-27 16:04:54 -07005191 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07005192 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07005193
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005194 mPointerGesture.referenceGestureX += commonDeltaX;
5195 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005196 }
5197
5198 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005199 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5200 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5201 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005202#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005203 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005204 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005205 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005206#endif
Steve Blockec193de2012-01-09 18:35:44 +00005207 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005208
5209 mPointerGesture.currentGestureIdBits.clear();
5210 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5211 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005212 mPointerGesture.currentGestureProperties[0].clear();
5213 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5214 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005215 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005216 mPointerGesture.currentGestureCoords[0].clear();
5217 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5218 mPointerGesture.referenceGestureX);
5219 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5220 mPointerGesture.referenceGestureY);
5221 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005222 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5223 // FREEFORM mode.
5224#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005225 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005226 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005227 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005228#endif
Steve Blockec193de2012-01-09 18:35:44 +00005229 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005230
Jeff Brownace13b12011-03-09 17:39:48 -08005231 mPointerGesture.currentGestureIdBits.clear();
5232
5233 BitSet32 mappedTouchIdBits;
5234 BitSet32 usedGestureIdBits;
5235 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5236 // Initially, assign the active gesture id to the active touch point
5237 // if there is one. No other touch id bits are mapped yet.
5238 if (!*outCancelPreviousGesture) {
5239 mappedTouchIdBits.markBit(activeTouchId);
5240 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5241 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5242 mPointerGesture.activeGestureId;
5243 } else {
5244 mPointerGesture.activeGestureId = -1;
5245 }
5246 } else {
5247 // Otherwise, assume we mapped all touches from the previous frame.
5248 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005249 mappedTouchIdBits.value = mLastFingerIdBits.value
5250 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005251 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5252
5253 // Check whether we need to choose a new active gesture id because the
5254 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005255 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5256 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005257 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005258 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005259 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5260 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5261 mPointerGesture.activeGestureId = -1;
5262 break;
5263 }
5264 }
5265 }
5266
5267#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005268 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005269 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5270 "activeGestureId=%d",
5271 mappedTouchIdBits.value, usedGestureIdBits.value,
5272 mPointerGesture.activeGestureId);
5273#endif
5274
Jeff Brown65fd2512011-08-18 11:20:58 -07005275 BitSet32 idBits(mCurrentFingerIdBits);
5276 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005277 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005278 uint32_t gestureId;
5279 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005280 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005281 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5282#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005283 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005284 "new mapping for touch id %d -> gesture id %d",
5285 touchId, gestureId);
5286#endif
5287 } else {
5288 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5289#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005290 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005291 "existing mapping for touch id %d -> gesture id %d",
5292 touchId, gestureId);
5293#endif
5294 }
5295 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5296 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5297
Jeff Brownbe1aa822011-07-27 16:04:54 -07005298 const RawPointerData::Pointer& pointer =
5299 mCurrentRawPointerData.pointerForId(touchId);
5300 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005301 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005302 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005303 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005304 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005305
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005306 mPointerGesture.currentGestureProperties[i].clear();
5307 mPointerGesture.currentGestureProperties[i].id = gestureId;
5308 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005309 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005310 mPointerGesture.currentGestureCoords[i].clear();
5311 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005312 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005313 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005314 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005315 mPointerGesture.currentGestureCoords[i].setAxisValue(
5316 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5317 }
5318
5319 if (mPointerGesture.activeGestureId < 0) {
5320 mPointerGesture.activeGestureId =
5321 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5322#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005323 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005324 "activeGestureId=%d", mPointerGesture.activeGestureId);
5325#endif
5326 }
Jeff Brown2352b972011-04-12 22:39:53 -07005327 }
Jeff Brownace13b12011-03-09 17:39:48 -08005328 }
5329
Jeff Brownbe1aa822011-07-27 16:04:54 -07005330 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005331
Jeff Brownace13b12011-03-09 17:39:48 -08005332#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005333 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005334 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5335 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005336 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005337 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5338 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005339 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005340 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005341 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005342 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005343 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005344 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005345 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5346 id, index, properties.toolType,
5347 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005348 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5349 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5350 }
5351 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005352 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005353 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005354 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005355 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005356 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005357 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5358 id, index, properties.toolType,
5359 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005360 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5361 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5362 }
5363#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005364 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005365}
5366
Jeff Brown65fd2512011-08-18 11:20:58 -07005367void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5368 mPointerSimple.currentCoords.clear();
5369 mPointerSimple.currentProperties.clear();
5370
5371 bool down, hovering;
5372 if (!mCurrentStylusIdBits.isEmpty()) {
5373 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5374 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5375 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5376 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5377 mPointerController->setPosition(x, y);
5378
5379 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5380 down = !hovering;
5381
5382 mPointerController->getPosition(&x, &y);
5383 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5384 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5385 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5386 mPointerSimple.currentProperties.id = 0;
5387 mPointerSimple.currentProperties.toolType =
5388 mCurrentCookedPointerData.pointerProperties[index].toolType;
5389 } else {
5390 down = false;
5391 hovering = false;
5392 }
5393
5394 dispatchPointerSimple(when, policyFlags, down, hovering);
5395}
5396
5397void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5398 abortPointerSimple(when, policyFlags);
5399}
5400
5401void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5402 mPointerSimple.currentCoords.clear();
5403 mPointerSimple.currentProperties.clear();
5404
5405 bool down, hovering;
5406 if (!mCurrentMouseIdBits.isEmpty()) {
5407 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5408 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5409 if (mLastMouseIdBits.hasBit(id)) {
5410 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5411 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5412 - mLastRawPointerData.pointers[lastIndex].x)
5413 * mPointerXMovementScale;
5414 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5415 - mLastRawPointerData.pointers[lastIndex].y)
5416 * mPointerYMovementScale;
5417
5418 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5419 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5420
5421 mPointerController->move(deltaX, deltaY);
5422 } else {
5423 mPointerVelocityControl.reset();
5424 }
5425
5426 down = isPointerDown(mCurrentButtonState);
5427 hovering = !down;
5428
5429 float x, y;
5430 mPointerController->getPosition(&x, &y);
5431 mPointerSimple.currentCoords.copyFrom(
5432 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5433 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5434 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5435 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5436 hovering ? 0.0f : 1.0f);
5437 mPointerSimple.currentProperties.id = 0;
5438 mPointerSimple.currentProperties.toolType =
5439 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5440 } else {
5441 mPointerVelocityControl.reset();
5442
5443 down = false;
5444 hovering = false;
5445 }
5446
5447 dispatchPointerSimple(when, policyFlags, down, hovering);
5448}
5449
5450void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5451 abortPointerSimple(when, policyFlags);
5452
5453 mPointerVelocityControl.reset();
5454}
5455
5456void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5457 bool down, bool hovering) {
5458 int32_t metaState = getContext()->getGlobalMetaState();
5459
5460 if (mPointerController != NULL) {
5461 if (down || hovering) {
5462 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5463 mPointerController->clearSpots();
5464 mPointerController->setButtonState(mCurrentButtonState);
5465 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5466 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5467 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5468 }
5469 }
5470
5471 if (mPointerSimple.down && !down) {
5472 mPointerSimple.down = false;
5473
5474 // Send up.
5475 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5476 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005477 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005478 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5479 mOrientedXPrecision, mOrientedYPrecision,
5480 mPointerSimple.downTime);
5481 getListener()->notifyMotion(&args);
5482 }
5483
5484 if (mPointerSimple.hovering && !hovering) {
5485 mPointerSimple.hovering = false;
5486
5487 // Send hover exit.
5488 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5489 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005490 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005491 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5492 mOrientedXPrecision, mOrientedYPrecision,
5493 mPointerSimple.downTime);
5494 getListener()->notifyMotion(&args);
5495 }
5496
5497 if (down) {
5498 if (!mPointerSimple.down) {
5499 mPointerSimple.down = true;
5500 mPointerSimple.downTime = when;
5501
5502 // Send down.
5503 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5504 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005505 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005506 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5507 mOrientedXPrecision, mOrientedYPrecision,
5508 mPointerSimple.downTime);
5509 getListener()->notifyMotion(&args);
5510 }
5511
5512 // Send move.
5513 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5514 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005515 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005516 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5517 mOrientedXPrecision, mOrientedYPrecision,
5518 mPointerSimple.downTime);
5519 getListener()->notifyMotion(&args);
5520 }
5521
5522 if (hovering) {
5523 if (!mPointerSimple.hovering) {
5524 mPointerSimple.hovering = true;
5525
5526 // Send hover enter.
5527 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5528 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005529 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005530 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5531 mOrientedXPrecision, mOrientedYPrecision,
5532 mPointerSimple.downTime);
5533 getListener()->notifyMotion(&args);
5534 }
5535
5536 // Send hover move.
5537 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5538 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005539 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005540 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5541 mOrientedXPrecision, mOrientedYPrecision,
5542 mPointerSimple.downTime);
5543 getListener()->notifyMotion(&args);
5544 }
5545
5546 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5547 float vscroll = mCurrentRawVScroll;
5548 float hscroll = mCurrentRawHScroll;
5549 mWheelYVelocityControl.move(when, NULL, &vscroll);
5550 mWheelXVelocityControl.move(when, &hscroll, NULL);
5551
5552 // Send scroll.
5553 PointerCoords pointerCoords;
5554 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5555 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5556 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5557
5558 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5559 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005560 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005561 1, &mPointerSimple.currentProperties, &pointerCoords,
5562 mOrientedXPrecision, mOrientedYPrecision,
5563 mPointerSimple.downTime);
5564 getListener()->notifyMotion(&args);
5565 }
5566
5567 // Save state.
5568 if (down || hovering) {
5569 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5570 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5571 } else {
5572 mPointerSimple.reset();
5573 }
5574}
5575
5576void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5577 mPointerSimple.currentCoords.clear();
5578 mPointerSimple.currentProperties.clear();
5579
5580 dispatchPointerSimple(when, policyFlags, false, false);
5581}
5582
Jeff Brownace13b12011-03-09 17:39:48 -08005583void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005584 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5585 const PointerProperties* properties, const PointerCoords* coords,
5586 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005587 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5588 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005589 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005590 uint32_t pointerCount = 0;
5591 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005592 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005593 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005594 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005595 pointerCoords[pointerCount].copyFrom(coords[index]);
5596
5597 if (changedId >= 0 && id == uint32_t(changedId)) {
5598 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5599 }
5600
5601 pointerCount += 1;
5602 }
5603
Steve Blockec193de2012-01-09 18:35:44 +00005604 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005605
5606 if (changedId >= 0 && pointerCount == 1) {
5607 // Replace initial down and final up action.
5608 // We can compare the action without masking off the changed pointer index
5609 // because we know the index is 0.
5610 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5611 action = AMOTION_EVENT_ACTION_DOWN;
5612 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5613 action = AMOTION_EVENT_ACTION_UP;
5614 } else {
5615 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005616 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005617 }
5618 }
5619
Jeff Brownbe1aa822011-07-27 16:04:54 -07005620 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005621 action, flags, metaState, buttonState, edgeFlags,
Jeff Brown83d616a2012-09-09 20:33:43 -07005622 mViewport.displayId, pointerCount, pointerProperties, pointerCoords,
5623 xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005624 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005625}
5626
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005627bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005628 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005629 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5630 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005631 bool changed = false;
5632 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005633 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005634 uint32_t inIndex = inIdToIndex[id];
5635 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005636
5637 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005638 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005639 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005640 PointerCoords& curOutCoords = outCoords[outIndex];
5641
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005642 if (curInProperties != curOutProperties) {
5643 curOutProperties.copyFrom(curInProperties);
5644 changed = true;
5645 }
5646
Jeff Brownace13b12011-03-09 17:39:48 -08005647 if (curInCoords != curOutCoords) {
5648 curOutCoords.copyFrom(curInCoords);
5649 changed = true;
5650 }
5651 }
5652 return changed;
5653}
5654
5655void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005656 if (mPointerController != NULL) {
5657 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5658 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005659}
5660
Jeff Brownbe1aa822011-07-27 16:04:54 -07005661bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5662 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5663 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005664}
5665
Jeff Brownbe1aa822011-07-27 16:04:54 -07005666const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005667 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005668 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005669 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005670 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005671
5672#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005673 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005674 "left=%d, top=%d, right=%d, bottom=%d",
5675 x, y,
5676 virtualKey.keyCode, virtualKey.scanCode,
5677 virtualKey.hitLeft, virtualKey.hitTop,
5678 virtualKey.hitRight, virtualKey.hitBottom);
5679#endif
5680
5681 if (virtualKey.isHit(x, y)) {
5682 return & virtualKey;
5683 }
5684 }
5685
5686 return NULL;
5687}
5688
Jeff Brownbe1aa822011-07-27 16:04:54 -07005689void TouchInputMapper::assignPointerIds() {
5690 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5691 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5692
5693 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005694
5695 if (currentPointerCount == 0) {
5696 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005697 return;
5698 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005699
Jeff Brownbe1aa822011-07-27 16:04:54 -07005700 if (lastPointerCount == 0) {
5701 // All pointers are new.
5702 for (uint32_t i = 0; i < currentPointerCount; i++) {
5703 uint32_t id = i;
5704 mCurrentRawPointerData.pointers[i].id = id;
5705 mCurrentRawPointerData.idToIndex[id] = i;
5706 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5707 }
5708 return;
5709 }
5710
5711 if (currentPointerCount == 1 && lastPointerCount == 1
5712 && mCurrentRawPointerData.pointers[0].toolType
5713 == mLastRawPointerData.pointers[0].toolType) {
5714 // Only one pointer and no change in count so it must have the same id as before.
5715 uint32_t id = mLastRawPointerData.pointers[0].id;
5716 mCurrentRawPointerData.pointers[0].id = id;
5717 mCurrentRawPointerData.idToIndex[id] = 0;
5718 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5719 return;
5720 }
5721
5722 // General case.
5723 // We build a heap of squared euclidean distances between current and last pointers
5724 // associated with the current and last pointer indices. Then, we find the best
5725 // match (by distance) for each current pointer.
5726 // The pointers must have the same tool type but it is possible for them to
5727 // transition from hovering to touching or vice-versa while retaining the same id.
5728 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5729
5730 uint32_t heapSize = 0;
5731 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5732 currentPointerIndex++) {
5733 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5734 lastPointerIndex++) {
5735 const RawPointerData::Pointer& currentPointer =
5736 mCurrentRawPointerData.pointers[currentPointerIndex];
5737 const RawPointerData::Pointer& lastPointer =
5738 mLastRawPointerData.pointers[lastPointerIndex];
5739 if (currentPointer.toolType == lastPointer.toolType) {
5740 int64_t deltaX = currentPointer.x - lastPointer.x;
5741 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005742
5743 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5744
5745 // Insert new element into the heap (sift up).
5746 heap[heapSize].currentPointerIndex = currentPointerIndex;
5747 heap[heapSize].lastPointerIndex = lastPointerIndex;
5748 heap[heapSize].distance = distance;
5749 heapSize += 1;
5750 }
5751 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005752 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005753
Jeff Brownbe1aa822011-07-27 16:04:54 -07005754 // Heapify
5755 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5756 startIndex -= 1;
5757 for (uint32_t parentIndex = startIndex; ;) {
5758 uint32_t childIndex = parentIndex * 2 + 1;
5759 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005760 break;
5761 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005762
5763 if (childIndex + 1 < heapSize
5764 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5765 childIndex += 1;
5766 }
5767
5768 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5769 break;
5770 }
5771
5772 swap(heap[parentIndex], heap[childIndex]);
5773 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005774 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005775 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005776
5777#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005778 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005779 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005780 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005781 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5782 heap[i].distance);
5783 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005784#endif
5785
Jeff Brownbe1aa822011-07-27 16:04:54 -07005786 // Pull matches out by increasing order of distance.
5787 // To avoid reassigning pointers that have already been matched, the loop keeps track
5788 // of which last and current pointers have been matched using the matchedXXXBits variables.
5789 // It also tracks the used pointer id bits.
5790 BitSet32 matchedLastBits(0);
5791 BitSet32 matchedCurrentBits(0);
5792 BitSet32 usedIdBits(0);
5793 bool first = true;
5794 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5795 while (heapSize > 0) {
5796 if (first) {
5797 // The first time through the loop, we just consume the root element of
5798 // the heap (the one with smallest distance).
5799 first = false;
5800 } else {
5801 // Previous iterations consumed the root element of the heap.
5802 // Pop root element off of the heap (sift down).
5803 heap[0] = heap[heapSize];
5804 for (uint32_t parentIndex = 0; ;) {
5805 uint32_t childIndex = parentIndex * 2 + 1;
5806 if (childIndex >= heapSize) {
5807 break;
5808 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005809
Jeff Brownbe1aa822011-07-27 16:04:54 -07005810 if (childIndex + 1 < heapSize
5811 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5812 childIndex += 1;
5813 }
5814
5815 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5816 break;
5817 }
5818
5819 swap(heap[parentIndex], heap[childIndex]);
5820 parentIndex = childIndex;
5821 }
5822
5823#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005824 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005825 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005826 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005827 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5828 heap[i].distance);
5829 }
5830#endif
5831 }
5832
5833 heapSize -= 1;
5834
5835 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5836 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5837
5838 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5839 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5840
5841 matchedCurrentBits.markBit(currentPointerIndex);
5842 matchedLastBits.markBit(lastPointerIndex);
5843
5844 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5845 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5846 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5847 mCurrentRawPointerData.markIdBit(id,
5848 mCurrentRawPointerData.isHovering(currentPointerIndex));
5849 usedIdBits.markBit(id);
5850
5851#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005852 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005853 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5854#endif
5855 break;
5856 }
5857 }
5858
5859 // Assign fresh ids to pointers that were not matched in the process.
5860 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5861 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5862 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5863
5864 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5865 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5866 mCurrentRawPointerData.markIdBit(id,
5867 mCurrentRawPointerData.isHovering(currentPointerIndex));
5868
5869#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005870 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005871 currentPointerIndex, id);
5872#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005873 }
5874}
5875
Jeff Brown6d0fec22010-07-23 21:28:06 -07005876int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005877 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5878 return AKEY_STATE_VIRTUAL;
5879 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005880
Jeff Brownbe1aa822011-07-27 16:04:54 -07005881 size_t numVirtualKeys = mVirtualKeys.size();
5882 for (size_t i = 0; i < numVirtualKeys; i++) {
5883 const VirtualKey& virtualKey = mVirtualKeys[i];
5884 if (virtualKey.keyCode == keyCode) {
5885 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005886 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005887 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005888
5889 return AKEY_STATE_UNKNOWN;
5890}
5891
5892int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005893 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5894 return AKEY_STATE_VIRTUAL;
5895 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005896
Jeff Brownbe1aa822011-07-27 16:04:54 -07005897 size_t numVirtualKeys = mVirtualKeys.size();
5898 for (size_t i = 0; i < numVirtualKeys; i++) {
5899 const VirtualKey& virtualKey = mVirtualKeys[i];
5900 if (virtualKey.scanCode == scanCode) {
5901 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005902 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005903 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005904
5905 return AKEY_STATE_UNKNOWN;
5906}
5907
5908bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5909 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005910 size_t numVirtualKeys = mVirtualKeys.size();
5911 for (size_t i = 0; i < numVirtualKeys; i++) {
5912 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005913
Jeff Brownbe1aa822011-07-27 16:04:54 -07005914 for (size_t i = 0; i < numCodes; i++) {
5915 if (virtualKey.keyCode == keyCodes[i]) {
5916 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005917 }
5918 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005919 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005920
5921 return true;
5922}
5923
5924
5925// --- SingleTouchInputMapper ---
5926
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005927SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5928 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005929}
5930
5931SingleTouchInputMapper::~SingleTouchInputMapper() {
5932}
5933
Jeff Brown65fd2512011-08-18 11:20:58 -07005934void SingleTouchInputMapper::reset(nsecs_t when) {
5935 mSingleTouchMotionAccumulator.reset(getDevice());
5936
5937 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005938}
5939
Jeff Brown6d0fec22010-07-23 21:28:06 -07005940void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005941 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005942
Jeff Brown65fd2512011-08-18 11:20:58 -07005943 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005944}
5945
Jeff Brown65fd2512011-08-18 11:20:58 -07005946void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005947 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005948 mCurrentRawPointerData.pointerCount = 1;
5949 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005950
Jeff Brown65fd2512011-08-18 11:20:58 -07005951 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5952 && (mTouchButtonAccumulator.isHovering()
5953 || (mRawPointerAxes.pressure.valid
5954 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005955 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005956
Jeff Brownbe1aa822011-07-27 16:04:54 -07005957 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005958 outPointer.id = 0;
5959 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5960 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5961 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5962 outPointer.touchMajor = 0;
5963 outPointer.touchMinor = 0;
5964 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5965 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5966 outPointer.orientation = 0;
5967 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005968 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5969 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005970 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5971 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5972 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5973 }
5974 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005975 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005976}
5977
Jeff Brownbe1aa822011-07-27 16:04:54 -07005978void SingleTouchInputMapper::configureRawPointerAxes() {
5979 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005980
Jeff Brownbe1aa822011-07-27 16:04:54 -07005981 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5982 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5983 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5984 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5985 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005986 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5987 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005988}
5989
Jeff Brown00710e92012-04-19 15:18:26 -07005990bool SingleTouchInputMapper::hasStylus() const {
5991 return mTouchButtonAccumulator.hasStylus();
5992}
5993
Jeff Brown6d0fec22010-07-23 21:28:06 -07005994
5995// --- MultiTouchInputMapper ---
5996
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005997MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005998 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005999}
6000
6001MultiTouchInputMapper::~MultiTouchInputMapper() {
6002}
6003
Jeff Brown65fd2512011-08-18 11:20:58 -07006004void MultiTouchInputMapper::reset(nsecs_t when) {
6005 mMultiTouchMotionAccumulator.reset(getDevice());
6006
Jeff Brown6894a292011-07-01 17:59:27 -07006007 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07006008
Jeff Brown65fd2512011-08-18 11:20:58 -07006009 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07006010}
6011
6012void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07006013 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08006014
Jeff Brown65fd2512011-08-18 11:20:58 -07006015 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07006016}
6017
Jeff Brown65fd2512011-08-18 11:20:58 -07006018void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07006019 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07006020 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006021 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006022
Jeff Brown80fd47c2011-05-24 01:07:44 -07006023 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07006024 const MultiTouchMotionAccumulator::Slot* inSlot =
6025 mMultiTouchMotionAccumulator.getSlot(inIndex);
6026 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006027 continue;
6028 }
6029
Jeff Brown80fd47c2011-05-24 01:07:44 -07006030 if (outCount >= MAX_POINTERS) {
6031#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00006032 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006033 "ignoring the rest.",
6034 getDeviceName().string(), MAX_POINTERS);
6035#endif
6036 break; // too many fingers!
6037 }
6038
Jeff Brownbe1aa822011-07-27 16:04:54 -07006039 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07006040 outPointer.x = inSlot->getX();
6041 outPointer.y = inSlot->getY();
6042 outPointer.pressure = inSlot->getPressure();
6043 outPointer.touchMajor = inSlot->getTouchMajor();
6044 outPointer.touchMinor = inSlot->getTouchMinor();
6045 outPointer.toolMajor = inSlot->getToolMajor();
6046 outPointer.toolMinor = inSlot->getToolMinor();
6047 outPointer.orientation = inSlot->getOrientation();
6048 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07006049 outPointer.tiltX = 0;
6050 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006051
Jeff Brown49754db2011-07-01 17:37:58 -07006052 outPointer.toolType = inSlot->getToolType();
6053 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
6054 outPointer.toolType = mTouchButtonAccumulator.getToolType();
6055 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
6056 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
6057 }
Jeff Brown8d608662010-08-30 03:02:23 -07006058 }
6059
Jeff Brown65fd2512011-08-18 11:20:58 -07006060 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
6061 && (mTouchButtonAccumulator.isHovering()
6062 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07006063 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006064
Jeff Brown8d608662010-08-30 03:02:23 -07006065 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07006066 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07006067 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07006068 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07006069 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07006070 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07006071 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07006072 if (mPointerTrackingIdMap[n] == trackingId) {
6073 id = n;
6074 }
6075 }
6076
6077 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07006078 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07006079 mPointerTrackingIdMap[id] = trackingId;
6080 }
6081 }
6082 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07006083 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006084 mCurrentRawPointerData.clearIdBits();
6085 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07006086 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07006087 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006088 mCurrentRawPointerData.idToIndex[id] = outCount;
6089 mCurrentRawPointerData.markIdBit(id, isHovering);
6090 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006091 }
6092 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006093
Jeff Brown6d0fec22010-07-23 21:28:06 -07006094 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006095 }
6096
Jeff Brownbe1aa822011-07-27 16:04:54 -07006097 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006098 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07006099
Jeff Brown65fd2512011-08-18 11:20:58 -07006100 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006101}
6102
Jeff Brownbe1aa822011-07-27 16:04:54 -07006103void MultiTouchInputMapper::configureRawPointerAxes() {
6104 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006105
Jeff Brownbe1aa822011-07-27 16:04:54 -07006106 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
6107 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
6108 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
6109 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
6110 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
6111 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
6112 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
6113 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
6114 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
6115 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
6116 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006117
Jeff Brownbe1aa822011-07-27 16:04:54 -07006118 if (mRawPointerAxes.trackingId.valid
6119 && mRawPointerAxes.slot.valid
6120 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
6121 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07006122 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00006123 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006124 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07006125 getDeviceName().string(), slotCount, MAX_SLOTS);
6126 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07006127 }
Jeff Brown00710e92012-04-19 15:18:26 -07006128 mMultiTouchMotionAccumulator.configure(getDevice(),
6129 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006130 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07006131 mMultiTouchMotionAccumulator.configure(getDevice(),
6132 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006133 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07006134}
6135
Jeff Brown00710e92012-04-19 15:18:26 -07006136bool MultiTouchInputMapper::hasStylus() const {
6137 return mMultiTouchMotionAccumulator.hasStylus()
6138 || mTouchButtonAccumulator.hasStylus();
6139}
6140
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006141
Jeff Browncb1404e2011-01-15 18:14:15 -08006142// --- JoystickInputMapper ---
6143
6144JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
6145 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006146}
6147
6148JoystickInputMapper::~JoystickInputMapper() {
6149}
6150
6151uint32_t JoystickInputMapper::getSources() {
6152 return AINPUT_SOURCE_JOYSTICK;
6153}
6154
6155void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
6156 InputMapper::populateDeviceInfo(info);
6157
Jeff Brown6f2fba42011-02-19 01:08:02 -08006158 for (size_t i = 0; i < mAxes.size(); i++) {
6159 const Axis& axis = mAxes.valueAt(i);
Michael Wright2b08c612013-04-24 20:05:10 -07006160 addMotionRange(axis.axisInfo.axis, axis, info);
6161
Jeff Brown85297452011-03-04 13:07:49 -08006162 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Michael Wright2b08c612013-04-24 20:05:10 -07006163 addMotionRange(axis.axisInfo.highAxis, axis, info);
6164
Jeff Brown85297452011-03-04 13:07:49 -08006165 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006166 }
6167}
6168
Michael Wright2b08c612013-04-24 20:05:10 -07006169void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis,
6170 InputDeviceInfo* info) {
6171 info->addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK,
6172 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
6173 /* In order to ease the transition for developers from using the old axes
6174 * to the newer, more semantically correct axes, we'll continue to register
6175 * the old axes as duplicates of their corresponding new ones. */
6176 int32_t compatAxis = getCompatAxis(axisId);
6177 if (compatAxis >= 0) {
6178 info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK,
6179 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
6180 }
6181}
6182
6183/* A mapping from axes the joystick actually has to the axes that should be
6184 * artificially created for compatibility purposes.
6185 * Returns -1 if no compatibility axis is needed. */
6186int32_t JoystickInputMapper::getCompatAxis(int32_t axis) {
6187 switch(axis) {
6188 case AMOTION_EVENT_AXIS_LTRIGGER:
6189 return AMOTION_EVENT_AXIS_BRAKE;
6190 case AMOTION_EVENT_AXIS_RTRIGGER:
6191 return AMOTION_EVENT_AXIS_GAS;
6192 }
6193 return -1;
6194}
6195
Jeff Browncb1404e2011-01-15 18:14:15 -08006196void JoystickInputMapper::dump(String8& dump) {
6197 dump.append(INDENT2 "Joystick Input Mapper:\n");
6198
Jeff Brown6f2fba42011-02-19 01:08:02 -08006199 dump.append(INDENT3 "Axes:\n");
6200 size_t numAxes = mAxes.size();
6201 for (size_t i = 0; i < numAxes; i++) {
6202 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006203 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006204 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08006205 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006206 } else {
Jeff Brown85297452011-03-04 13:07:49 -08006207 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006208 }
Jeff Brown85297452011-03-04 13:07:49 -08006209 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6210 label = getAxisLabel(axis.axisInfo.highAxis);
6211 if (label) {
6212 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
6213 } else {
6214 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
6215 axis.axisInfo.splitValue);
6216 }
6217 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
6218 dump.append(" (invert)");
6219 }
6220
Michael Wrightc6091c62013-04-01 20:56:04 -07006221 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n",
6222 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
Jeff Brown85297452011-03-04 13:07:49 -08006223 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
6224 "highScale=%0.5f, highOffset=%0.5f\n",
6225 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07006226 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
6227 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006228 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006229 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006230 }
6231}
6232
Jeff Brown65fd2512011-08-18 11:20:58 -07006233void JoystickInputMapper::configure(nsecs_t when,
6234 const InputReaderConfiguration* config, uint32_t changes) {
6235 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006236
Jeff Brown474dcb52011-06-14 20:22:50 -07006237 if (!changes) { // first time only
6238 // Collect all axes.
6239 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07006240 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6241 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6242 continue; // axis must be claimed by a different device
6243 }
6244
Jeff Brown474dcb52011-06-14 20:22:50 -07006245 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006246 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006247 if (rawAxisInfo.valid) {
6248 // Map axis.
6249 AxisInfo axisInfo;
6250 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6251 if (!explicitlyMapped) {
6252 // Axis is not explicitly mapped, will choose a generic axis later.
6253 axisInfo.mode = AxisInfo::MODE_NORMAL;
6254 axisInfo.axis = -1;
6255 }
6256
6257 // Apply flat override.
6258 int32_t rawFlat = axisInfo.flatOverride < 0
6259 ? rawAxisInfo.flat : axisInfo.flatOverride;
6260
6261 // Calculate scaling factors and limits.
6262 Axis axis;
6263 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6264 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6265 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6266 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6267 scale, 0.0f, highScale, 0.0f,
Michael Wrightc6091c62013-04-01 20:56:04 -07006268 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6269 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006270 } else if (isCenteredAxis(axisInfo.axis)) {
6271 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6272 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6273 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6274 scale, offset, scale, offset,
Michael Wrightc6091c62013-04-01 20:56:04 -07006275 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6276 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006277 } else {
6278 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6279 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6280 scale, 0.0f, scale, 0.0f,
Michael Wrightc6091c62013-04-01 20:56:04 -07006281 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6282 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006283 }
6284
6285 // To eliminate noise while the joystick is at rest, filter out small variations
6286 // in axis values up front.
6287 axis.filter = axis.flat * 0.25f;
6288
6289 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006290 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006291 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006292
Jeff Brown474dcb52011-06-14 20:22:50 -07006293 // If there are too many axes, start dropping them.
6294 // Prefer to keep explicitly mapped axes.
6295 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006296 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006297 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6298 pruneAxes(true);
6299 pruneAxes(false);
6300 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006301
Jeff Brown474dcb52011-06-14 20:22:50 -07006302 // Assign generic axis ids to remaining axes.
6303 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6304 size_t numAxes = mAxes.size();
6305 for (size_t i = 0; i < numAxes; i++) {
6306 Axis& axis = mAxes.editValueAt(i);
6307 if (axis.axisInfo.axis < 0) {
6308 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6309 && haveAxis(nextGenericAxisId)) {
6310 nextGenericAxisId += 1;
6311 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006312
Jeff Brown474dcb52011-06-14 20:22:50 -07006313 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6314 axis.axisInfo.axis = nextGenericAxisId;
6315 nextGenericAxisId += 1;
6316 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006317 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006318 "have already been assigned to other axes.",
6319 getDeviceName().string(), mAxes.keyAt(i));
6320 mAxes.removeItemsAt(i--);
6321 numAxes -= 1;
6322 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006323 }
6324 }
6325 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006326}
6327
Jeff Brown85297452011-03-04 13:07:49 -08006328bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006329 size_t numAxes = mAxes.size();
6330 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006331 const Axis& axis = mAxes.valueAt(i);
6332 if (axis.axisInfo.axis == axisId
6333 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6334 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006335 return true;
6336 }
6337 }
6338 return false;
6339}
Jeff Browncb1404e2011-01-15 18:14:15 -08006340
Jeff Brown6f2fba42011-02-19 01:08:02 -08006341void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6342 size_t i = mAxes.size();
6343 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6344 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6345 continue;
6346 }
Steve Block6215d3f2012-01-04 20:05:49 +00006347 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006348 getDeviceName().string(), mAxes.keyAt(i));
6349 mAxes.removeItemsAt(i);
6350 }
6351}
6352
6353bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6354 switch (axis) {
6355 case AMOTION_EVENT_AXIS_X:
6356 case AMOTION_EVENT_AXIS_Y:
6357 case AMOTION_EVENT_AXIS_Z:
6358 case AMOTION_EVENT_AXIS_RX:
6359 case AMOTION_EVENT_AXIS_RY:
6360 case AMOTION_EVENT_AXIS_RZ:
6361 case AMOTION_EVENT_AXIS_HAT_X:
6362 case AMOTION_EVENT_AXIS_HAT_Y:
6363 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006364 case AMOTION_EVENT_AXIS_RUDDER:
6365 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006366 return true;
6367 default:
6368 return false;
6369 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006370}
6371
Jeff Brown65fd2512011-08-18 11:20:58 -07006372void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006373 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006374 size_t numAxes = mAxes.size();
6375 for (size_t i = 0; i < numAxes; i++) {
6376 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006377 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006378 }
6379
Jeff Brown65fd2512011-08-18 11:20:58 -07006380 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006381}
6382
6383void JoystickInputMapper::process(const RawEvent* rawEvent) {
6384 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006385 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006386 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006387 if (index >= 0) {
6388 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006389 float newValue, highNewValue;
6390 switch (axis.axisInfo.mode) {
6391 case AxisInfo::MODE_INVERT:
6392 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6393 * axis.scale + axis.offset;
6394 highNewValue = 0.0f;
6395 break;
6396 case AxisInfo::MODE_SPLIT:
6397 if (rawEvent->value < axis.axisInfo.splitValue) {
6398 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6399 * axis.scale + axis.offset;
6400 highNewValue = 0.0f;
6401 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6402 newValue = 0.0f;
6403 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6404 * axis.highScale + axis.highOffset;
6405 } else {
6406 newValue = 0.0f;
6407 highNewValue = 0.0f;
6408 }
6409 break;
6410 default:
6411 newValue = rawEvent->value * axis.scale + axis.offset;
6412 highNewValue = 0.0f;
6413 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006414 }
Jeff Brown85297452011-03-04 13:07:49 -08006415 axis.newValue = newValue;
6416 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006417 }
6418 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006419 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006420
6421 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006422 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006423 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006424 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006425 break;
6426 }
6427 break;
6428 }
6429}
6430
Jeff Brown6f2fba42011-02-19 01:08:02 -08006431void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006432 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006433 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006434 }
6435
6436 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006437 int32_t buttonState = 0;
6438
6439 PointerProperties pointerProperties;
6440 pointerProperties.clear();
6441 pointerProperties.id = 0;
6442 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006443
Jeff Brown6f2fba42011-02-19 01:08:02 -08006444 PointerCoords pointerCoords;
6445 pointerCoords.clear();
6446
6447 size_t numAxes = mAxes.size();
6448 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006449 const Axis& axis = mAxes.valueAt(i);
Michael Wright2b08c612013-04-24 20:05:10 -07006450 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue);
Jeff Brown85297452011-03-04 13:07:49 -08006451 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Michael Wright2b08c612013-04-24 20:05:10 -07006452 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis,
6453 axis.highCurrentValue);
Jeff Brown85297452011-03-04 13:07:49 -08006454 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006455 }
6456
Jeff Brown83d616a2012-09-09 20:33:43 -07006457 // Moving a joystick axis should not wake the device because joysticks can
Jeff Brown56194eb2011-03-02 19:23:13 -08006458 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6459 // button will likely wake the device.
6460 // TODO: Use the input device configuration to control this behavior more finely.
6461 uint32_t policyFlags = 0;
6462
Jeff Brownbe1aa822011-07-27 16:04:54 -07006463 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006464 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07006465 ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006466 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006467}
6468
Michael Wright2b08c612013-04-24 20:05:10 -07006469void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords,
6470 int32_t axis, float value) {
6471 pointerCoords->setAxisValue(axis, value);
6472 /* In order to ease the transition for developers from using the old axes
6473 * to the newer, more semantically correct axes, we'll continue to produce
6474 * values for the old axes as mirrors of the value of their corresponding
6475 * new axes. */
6476 int32_t compatAxis = getCompatAxis(axis);
6477 if (compatAxis >= 0) {
6478 pointerCoords->setAxisValue(compatAxis, value);
6479 }
6480}
6481
Jeff Brown85297452011-03-04 13:07:49 -08006482bool JoystickInputMapper::filterAxes(bool force) {
6483 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006484 size_t numAxes = mAxes.size();
6485 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006486 Axis& axis = mAxes.editValueAt(i);
6487 if (force || hasValueChangedSignificantly(axis.filter,
6488 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6489 axis.currentValue = axis.newValue;
6490 atLeastOneSignificantChange = true;
6491 }
6492 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6493 if (force || hasValueChangedSignificantly(axis.filter,
6494 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6495 axis.highCurrentValue = axis.highNewValue;
6496 atLeastOneSignificantChange = true;
6497 }
6498 }
6499 }
6500 return atLeastOneSignificantChange;
6501}
6502
6503bool JoystickInputMapper::hasValueChangedSignificantly(
6504 float filter, float newValue, float currentValue, float min, float max) {
6505 if (newValue != currentValue) {
6506 // Filter out small changes in value unless the value is converging on the axis
6507 // bounds or center point. This is intended to reduce the amount of information
6508 // sent to applications by particularly noisy joysticks (such as PS3).
6509 if (fabs(newValue - currentValue) > filter
6510 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6511 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6512 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6513 return true;
6514 }
6515 }
6516 return false;
6517}
6518
6519bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6520 float filter, float newValue, float currentValue, float thresholdValue) {
6521 float newDistance = fabs(newValue - thresholdValue);
6522 if (newDistance < filter) {
6523 float oldDistance = fabs(currentValue - thresholdValue);
6524 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006525 return true;
6526 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006527 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006528 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006529}
6530
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006531} // namespace android