blob: 6022f107ec899d68145345e7f5ec79d13b3974cc [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Browna47425a2012-04-13 04:09:27 -070039// Log debug messages about the vibrator.
40#define DEBUG_VIBRATOR 0
41
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042#include "InputReader.h"
43
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044#include <cutils/log.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080045#include <androidfw/Keyboard.h>
46#include <androidfw/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047
48#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070049#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070050#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051#include <errno.h>
52#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070053#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070054
Jeff Brown8d608662010-08-30 03:02:23 -070055#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070056#define INDENT2 " "
57#define INDENT3 " "
58#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070059#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070060
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070061namespace android {
62
Jeff Brownace13b12011-03-09 17:39:48 -080063// --- Constants ---
64
Jeff Brown80fd47c2011-05-24 01:07:44 -070065// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
66static const size_t MAX_SLOTS = 32;
67
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070068// --- Static Functions ---
69
70template<typename T>
71inline static T abs(const T& value) {
72 return value < 0 ? - value : value;
73}
74
75template<typename T>
76inline static T min(const T& a, const T& b) {
77 return a < b ? a : b;
78}
79
Jeff Brown5c225b12010-06-16 01:53:36 -070080template<typename T>
81inline static void swap(T& a, T& b) {
82 T temp = a;
83 a = b;
84 b = temp;
85}
86
Jeff Brown8d608662010-08-30 03:02:23 -070087inline static float avg(float x, float y) {
88 return (x + y) / 2;
89}
90
Jeff Brown2352b972011-04-12 22:39:53 -070091inline static float distance(float x1, float y1, float x2, float y2) {
92 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080093}
94
Jeff Brown517bb4c2011-01-14 19:09:23 -080095inline static int32_t signExtendNybble(int32_t value) {
96 return value >= 8 ? value - 16 : value;
97}
98
Jeff Brownef3d7e82010-09-30 14:33:04 -070099static inline const char* toString(bool value) {
100 return value ? "true" : "false";
101}
102
Jeff Brown9626b142011-03-03 02:09:54 -0800103static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
104 const int32_t map[][4], size_t mapSize) {
105 if (orientation != DISPLAY_ORIENTATION_0) {
106 for (size_t i = 0; i < mapSize; i++) {
107 if (value == map[i][0]) {
108 return map[i][orientation];
109 }
110 }
111 }
112 return value;
113}
114
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700115static const int32_t keyCodeRotationMap[][4] = {
116 // key codes enumerated counter-clockwise with the original (unrotated) key first
117 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700118 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
119 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
120 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
121 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700122};
Jeff Brown9626b142011-03-03 02:09:54 -0800123static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700124 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
125
Jeff Brown60691392011-07-15 19:08:26 -0700126static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800127 return rotateValueUsingRotationMap(keyCode, orientation,
128 keyCodeRotationMap, keyCodeRotationMapSize);
129}
130
Jeff Brown612891e2011-07-15 20:44:17 -0700131static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
132 float temp;
133 switch (orientation) {
134 case DISPLAY_ORIENTATION_90:
135 temp = *deltaX;
136 *deltaX = *deltaY;
137 *deltaY = -temp;
138 break;
139
140 case DISPLAY_ORIENTATION_180:
141 *deltaX = -*deltaX;
142 *deltaY = -*deltaY;
143 break;
144
145 case DISPLAY_ORIENTATION_270:
146 temp = *deltaX;
147 *deltaX = -*deltaY;
148 *deltaY = temp;
149 break;
150 }
151}
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
154 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
155}
156
Jeff Brownefd32662011-03-08 15:13:06 -0800157// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700158// button states. This determines whether the event is reported as a touch event.
159static bool isPointerDown(int32_t buttonState) {
160 return buttonState &
161 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700162 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800163}
164
Jeff Brown2352b972011-04-12 22:39:53 -0700165static float calculateCommonVector(float a, float b) {
166 if (a > 0 && b > 0) {
167 return a < b ? a : b;
168 } else if (a < 0 && b < 0) {
169 return a > b ? a : b;
170 } else {
171 return 0;
172 }
173}
174
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700175static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
176 nsecs_t when, int32_t deviceId, uint32_t source,
177 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
178 int32_t buttonState, int32_t keyCode) {
179 if (
180 (action == AKEY_EVENT_ACTION_DOWN
181 && !(lastButtonState & buttonState)
182 && (currentButtonState & buttonState))
183 || (action == AKEY_EVENT_ACTION_UP
184 && (lastButtonState & buttonState)
185 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700186 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700187 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700188 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700189 }
190}
191
192static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
193 nsecs_t when, int32_t deviceId, uint32_t source,
194 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
198 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
199 lastButtonState, currentButtonState,
200 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
201}
202
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700203
Jeff Brown65fd2512011-08-18 11:20:58 -0700204// --- InputReaderConfiguration ---
205
206bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,
207 int32_t* width, int32_t* height, int32_t* orientation) const {
208 if (displayId == 0) {
209 const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
210 if (info.width > 0 && info.height > 0) {
211 if (width) {
212 *width = info.width;
213 }
214 if (height) {
215 *height = info.height;
216 }
217 if (orientation) {
218 *orientation = info.orientation;
219 }
220 return true;
221 }
222 }
223 return false;
224}
225
226void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,
227 int32_t width, int32_t height, int32_t orientation) {
228 if (displayId == 0) {
229 DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
230 info.width = width;
231 info.height = height;
232 info.orientation = orientation;
233 }
234}
235
236
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700237// --- InputReader ---
238
239InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700240 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700241 const sp<InputListenerInterface>& listener) :
242 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700243 mGlobalMetaState(0), mGeneration(1),
244 mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700245 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700246 mQueuedListener = new QueuedInputListener(listener);
247
248 { // acquire lock
249 AutoMutex _l(mLock);
250
251 refreshConfigurationLocked(0);
252 updateGlobalMetaStateLocked();
253 updateInputConfigurationLocked();
254 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700255}
256
257InputReader::~InputReader() {
258 for (size_t i = 0; i < mDevices.size(); i++) {
259 delete mDevices.valueAt(i);
260 }
261}
262
263void InputReader::loopOnce() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700264 int32_t oldGeneration;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700265 int32_t timeoutMillis;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700266 bool inputDevicesChanged = false;
267 Vector<InputDeviceInfo> inputDevices;
Jeff Brown474dcb52011-06-14 20:22:50 -0700268 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700269 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700270
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700271 oldGeneration = mGeneration;
272 timeoutMillis = -1;
273
Jeff Brownbe1aa822011-07-27 16:04:54 -0700274 uint32_t changes = mConfigurationChangesToRefresh;
275 if (changes) {
276 mConfigurationChangesToRefresh = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700277 timeoutMillis = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700278 refreshConfigurationLocked(changes);
Jeff Browna47425a2012-04-13 04:09:27 -0700279 } else if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700280 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
281 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
282 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700283 } // release lock
284
Jeff Brownb7198742011-03-18 18:14:26 -0700285 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700286
287 { // acquire lock
288 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800289 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700290
291 if (count) {
292 processEventsLocked(mEventBuffer, count);
293 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700294
295 if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700296 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800297 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700298#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800299 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700300#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800301 mNextTimeout = LLONG_MAX;
302 timeoutExpiredLocked(now);
303 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700304 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700305
306 if (oldGeneration != mGeneration) {
307 inputDevicesChanged = true;
308 getInputDevicesLocked(inputDevices);
309 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700310 } // release lock
311
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700312 // Send out a message that the describes the changed input devices.
313 if (inputDevicesChanged) {
314 mPolicy->notifyInputDevicesChanged(inputDevices);
315 }
316
Jeff Brownbe1aa822011-07-27 16:04:54 -0700317 // Flush queued events out to the listener.
318 // This must happen outside of the lock because the listener could potentially call
319 // back into the InputReader's methods, such as getScanCodeState, or become blocked
320 // on another thread similarly waiting to acquire the InputReader lock thereby
321 // resulting in a deadlock. This situation is actually quite plausible because the
322 // listener is actually the input dispatcher, which calls into the window manager,
323 // which occasionally calls into the input reader.
324 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700325}
326
Jeff Brownbe1aa822011-07-27 16:04:54 -0700327void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700328 for (const RawEvent* rawEvent = rawEvents; count;) {
329 int32_t type = rawEvent->type;
330 size_t batchSize = 1;
331 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
332 int32_t deviceId = rawEvent->deviceId;
333 while (batchSize < count) {
334 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
335 || rawEvent[batchSize].deviceId != deviceId) {
336 break;
337 }
338 batchSize += 1;
339 }
340#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000341 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700342#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700343 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700344 } else {
345 switch (rawEvent->type) {
346 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700347 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700348 break;
349 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700350 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700351 break;
352 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700353 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700354 break;
355 default:
Steve Blockec193de2012-01-09 18:35:44 +0000356 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700357 break;
358 }
359 }
360 count -= batchSize;
361 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700362 }
363}
364
Jeff Brown65fd2512011-08-18 11:20:58 -0700365void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700366 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
367 if (deviceIndex >= 0) {
368 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
369 return;
370 }
371
Jeff Browne38fdfa2012-04-06 14:51:01 -0700372 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700373 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
374
Jeff Browne38fdfa2012-04-06 14:51:01 -0700375 InputDevice* device = createDeviceLocked(deviceId, identifier, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700376 device->configure(when, &mConfig, 0);
377 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700378
Jeff Brown8d608662010-08-30 03:02:23 -0700379 if (device->isIgnored()) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700380 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
381 identifier.name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700382 } else {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700383 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
384 identifier.name.string(), device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700385 }
386
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700387 mDevices.add(deviceId, device);
388 bumpGenerationLocked();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700389}
390
Jeff Brown65fd2512011-08-18 11:20:58 -0700391void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700393 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700394 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000395 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700396 return;
397 }
398
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700399 device = mDevices.valueAt(deviceIndex);
400 mDevices.removeItemsAt(deviceIndex, 1);
401 bumpGenerationLocked();
402
Jeff Brown6d0fec22010-07-23 21:28:06 -0700403 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000404 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700405 device->getId(), device->getName().string());
406 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000407 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408 device->getId(), device->getName().string(), device->getSources());
409 }
410
Jeff Brown65fd2512011-08-18 11:20:58 -0700411 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700412 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700413}
414
Jeff Brownbe1aa822011-07-27 16:04:54 -0700415InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700416 const InputDeviceIdentifier& identifier, uint32_t classes) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700417 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
418 identifier, classes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700419
Jeff Brown56194eb2011-03-02 19:23:13 -0800420 // External devices.
421 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
422 device->setExternal(true);
423 }
424
Jeff Brown6d0fec22010-07-23 21:28:06 -0700425 // Switch-like devices.
426 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
427 device->addMapper(new SwitchInputMapper(device));
428 }
429
Jeff Browna47425a2012-04-13 04:09:27 -0700430 // Vibrator-like devices.
431 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
432 device->addMapper(new VibratorInputMapper(device));
433 }
434
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800436 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700437 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
438 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800439 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700440 }
441 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
442 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
443 }
444 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800445 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800447 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800448 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800449 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700450
Jeff Brownefd32662011-03-08 15:13:06 -0800451 if (keyboardSource != 0) {
452 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700453 }
454
Jeff Brown83c09682010-12-23 17:50:18 -0800455 // Cursor-like devices.
456 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
457 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700458 }
459
Jeff Brown58a2da82011-01-25 16:02:22 -0800460 // Touchscreens and touchpad devices.
461 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800462 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800463 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800464 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465 }
466
Jeff Browncb1404e2011-01-15 18:14:15 -0800467 // Joystick-like devices.
468 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
469 device->addMapper(new JoystickInputMapper(device));
470 }
471
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 return device;
473}
474
Jeff Brownbe1aa822011-07-27 16:04:54 -0700475void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700476 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700477 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
478 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000479 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700480 return;
481 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700482
Jeff Brownbe1aa822011-07-27 16:04:54 -0700483 InputDevice* device = mDevices.valueAt(deviceIndex);
484 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000485 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700486 return;
487 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700488
Jeff Brownbe1aa822011-07-27 16:04:54 -0700489 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490}
491
Jeff Brownbe1aa822011-07-27 16:04:54 -0700492void InputReader::timeoutExpiredLocked(nsecs_t when) {
493 for (size_t i = 0; i < mDevices.size(); i++) {
494 InputDevice* device = mDevices.valueAt(i);
495 if (!device->isIgnored()) {
496 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700497 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700498 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700499}
500
Jeff Brownbe1aa822011-07-27 16:04:54 -0700501void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700502 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700503 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504
505 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700506 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507
508 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700509 NotifyConfigurationChangedArgs args(when);
510 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700511}
512
Jeff Brownbe1aa822011-07-27 16:04:54 -0700513void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700514 mPolicy->getReaderConfiguration(&mConfig);
515 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
516
Jeff Brown474dcb52011-06-14 20:22:50 -0700517 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000518 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700519 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700520
521 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
522 mEventHub->requestReopenDevices();
523 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700524 for (size_t i = 0; i < mDevices.size(); i++) {
525 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700526 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700527 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700528 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700529 }
530}
531
Jeff Brownbe1aa822011-07-27 16:04:54 -0700532void InputReader::updateGlobalMetaStateLocked() {
533 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700534
Jeff Brownbe1aa822011-07-27 16:04:54 -0700535 for (size_t i = 0; i < mDevices.size(); i++) {
536 InputDevice* device = mDevices.valueAt(i);
537 mGlobalMetaState |= device->getMetaState();
538 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700539}
540
Jeff Brownbe1aa822011-07-27 16:04:54 -0700541int32_t InputReader::getGlobalMetaStateLocked() {
542 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700543}
544
Jeff Brownbe1aa822011-07-27 16:04:54 -0700545void InputReader::updateInputConfigurationLocked() {
546 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
547 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
548 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
549 InputDeviceInfo deviceInfo;
550 for (size_t i = 0; i < mDevices.size(); i++) {
551 InputDevice* device = mDevices.valueAt(i);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700552 if (!(device->getClasses() & INPUT_DEVICE_CLASS_VIRTUAL)) {
553 device->getDeviceInfo(& deviceInfo);
554 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700555
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700556 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
557 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
558 }
559 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
560 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
561 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
562 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
563 }
564 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
565 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
566 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700567 }
568 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700569
Jeff Brownbe1aa822011-07-27 16:04:54 -0700570 mInputConfiguration.touchScreen = touchScreenConfig;
571 mInputConfiguration.keyboard = keyboardConfig;
572 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700573}
574
Jeff Brownbe1aa822011-07-27 16:04:54 -0700575void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800576 mDisableVirtualKeysTimeout = time;
577}
578
Jeff Brownbe1aa822011-07-27 16:04:54 -0700579bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800580 InputDevice* device, int32_t keyCode, int32_t scanCode) {
581 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000582 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800583 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
584 device->getName().string(),
585 (mDisableVirtualKeysTimeout - now) * 0.000001,
586 keyCode, scanCode);
587 return true;
588 } else {
589 return false;
590 }
591}
592
Jeff Brownbe1aa822011-07-27 16:04:54 -0700593void InputReader::fadePointerLocked() {
594 for (size_t i = 0; i < mDevices.size(); i++) {
595 InputDevice* device = mDevices.valueAt(i);
596 device->fadePointer();
597 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800598}
599
Jeff Brownbe1aa822011-07-27 16:04:54 -0700600void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700601 if (when < mNextTimeout) {
602 mNextTimeout = when;
Jeff Browna47425a2012-04-13 04:09:27 -0700603 mEventHub->wake();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700604 }
605}
606
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700607int32_t InputReader::bumpGenerationLocked() {
608 return ++mGeneration;
609}
610
Jeff Brown6d0fec22010-07-23 21:28:06 -0700611void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700612 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700613
Jeff Brownbe1aa822011-07-27 16:04:54 -0700614 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700615}
616
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700617void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700618 AutoMutex _l(mLock);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700619 getInputDevicesLocked(outInputDevices);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700620}
621
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700622void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) {
623 outInputDevices.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700624
Jeff Brownbe1aa822011-07-27 16:04:54 -0700625 size_t numDevices = mDevices.size();
626 for (size_t i = 0; i < numDevices; i++) {
627 InputDevice* device = mDevices.valueAt(i);
628 if (!device->isIgnored()) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700629 outInputDevices.push();
630 device->getDeviceInfo(&outInputDevices.editTop());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700631 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700632 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700633}
634
635int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
636 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700637 AutoMutex _l(mLock);
638
639 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700640}
641
642int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
643 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700644 AutoMutex _l(mLock);
645
646 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700647}
648
649int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700650 AutoMutex _l(mLock);
651
652 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700653}
654
Jeff Brownbe1aa822011-07-27 16:04:54 -0700655int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700656 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700657 int32_t result = AKEY_STATE_UNKNOWN;
658 if (deviceId >= 0) {
659 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
660 if (deviceIndex >= 0) {
661 InputDevice* device = mDevices.valueAt(deviceIndex);
662 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
663 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700664 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700665 }
666 } else {
667 size_t numDevices = mDevices.size();
668 for (size_t i = 0; i < numDevices; i++) {
669 InputDevice* device = mDevices.valueAt(i);
670 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800671 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
672 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
673 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
674 if (currentResult >= AKEY_STATE_DOWN) {
675 return currentResult;
676 } else if (currentResult == AKEY_STATE_UP) {
677 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700678 }
679 }
680 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700681 }
682 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700683}
684
685bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
686 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700687 AutoMutex _l(mLock);
688
Jeff Brown6d0fec22010-07-23 21:28:06 -0700689 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700690 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700691}
692
Jeff Brownbe1aa822011-07-27 16:04:54 -0700693bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
694 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
695 bool result = false;
696 if (deviceId >= 0) {
697 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
698 if (deviceIndex >= 0) {
699 InputDevice* device = mDevices.valueAt(deviceIndex);
700 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
701 result = device->markSupportedKeyCodes(sourceMask,
702 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700703 }
704 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700705 } else {
706 size_t numDevices = mDevices.size();
707 for (size_t i = 0; i < numDevices; i++) {
708 InputDevice* device = mDevices.valueAt(i);
709 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
710 result |= device->markSupportedKeyCodes(sourceMask,
711 numCodes, keyCodes, outFlags);
712 }
713 }
714 }
715 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700716}
717
Jeff Brown474dcb52011-06-14 20:22:50 -0700718void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700719 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700720
Jeff Brownbe1aa822011-07-27 16:04:54 -0700721 if (changes) {
722 bool needWake = !mConfigurationChangesToRefresh;
723 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700724
725 if (needWake) {
726 mEventHub->wake();
727 }
728 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700729}
730
Jeff Browna47425a2012-04-13 04:09:27 -0700731void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
732 ssize_t repeat, int32_t token) {
733 AutoMutex _l(mLock);
734
735 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
736 if (deviceIndex >= 0) {
737 InputDevice* device = mDevices.valueAt(deviceIndex);
738 device->vibrate(pattern, patternSize, repeat, token);
739 }
740}
741
742void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
743 AutoMutex _l(mLock);
744
745 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
746 if (deviceIndex >= 0) {
747 InputDevice* device = mDevices.valueAt(deviceIndex);
748 device->cancelVibrate(token);
749 }
750}
751
Jeff Brownb88102f2010-09-08 11:49:43 -0700752void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700753 AutoMutex _l(mLock);
754
Jeff Brownf2f487182010-10-01 17:46:21 -0700755 mEventHub->dump(dump);
756 dump.append("\n");
757
758 dump.append("Input Reader State:\n");
759
Jeff Brownbe1aa822011-07-27 16:04:54 -0700760 for (size_t i = 0; i < mDevices.size(); i++) {
761 mDevices.valueAt(i)->dump(dump);
762 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700763
764 dump.append(INDENT "Configuration:\n");
765 dump.append(INDENT2 "ExcludedDeviceNames: [");
766 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
767 if (i != 0) {
768 dump.append(", ");
769 }
770 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
771 }
772 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700773 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
774 mConfig.virtualKeyQuietTime * 0.000001f);
775
Jeff Brown19c97d462011-06-01 12:33:19 -0700776 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
777 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
778 mConfig.pointerVelocityControlParameters.scale,
779 mConfig.pointerVelocityControlParameters.lowThreshold,
780 mConfig.pointerVelocityControlParameters.highThreshold,
781 mConfig.pointerVelocityControlParameters.acceleration);
782
783 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
784 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
785 mConfig.wheelVelocityControlParameters.scale,
786 mConfig.wheelVelocityControlParameters.lowThreshold,
787 mConfig.wheelVelocityControlParameters.highThreshold,
788 mConfig.wheelVelocityControlParameters.acceleration);
789
Jeff Brown214eaf42011-05-26 19:17:02 -0700790 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700791 dump.appendFormat(INDENT3 "Enabled: %s\n",
792 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700793 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
794 mConfig.pointerGestureQuietInterval * 0.000001f);
795 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
796 mConfig.pointerGestureDragMinSwitchSpeed);
797 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
798 mConfig.pointerGestureTapInterval * 0.000001f);
799 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
800 mConfig.pointerGestureTapDragInterval * 0.000001f);
801 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
802 mConfig.pointerGestureTapSlop);
803 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
804 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700805 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
806 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700807 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
808 mConfig.pointerGestureSwipeTransitionAngleCosine);
809 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
810 mConfig.pointerGestureSwipeMaxWidthRatio);
811 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
812 mConfig.pointerGestureMovementSpeedRatio);
813 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
814 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700815}
816
Jeff Brown89ef0722011-08-10 16:25:21 -0700817void InputReader::monitor() {
818 // Acquire and release the lock to ensure that the reader has not deadlocked.
819 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800820 mEventHub->wake();
821 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700822 mLock.unlock();
823
824 // Check the EventHub
825 mEventHub->monitor();
826}
827
Jeff Brown6d0fec22010-07-23 21:28:06 -0700828
Jeff Brownbe1aa822011-07-27 16:04:54 -0700829// --- InputReader::ContextImpl ---
830
831InputReader::ContextImpl::ContextImpl(InputReader* reader) :
832 mReader(reader) {
833}
834
835void InputReader::ContextImpl::updateGlobalMetaState() {
836 // lock is already held by the input loop
837 mReader->updateGlobalMetaStateLocked();
838}
839
840int32_t InputReader::ContextImpl::getGlobalMetaState() {
841 // lock is already held by the input loop
842 return mReader->getGlobalMetaStateLocked();
843}
844
845void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
846 // lock is already held by the input loop
847 mReader->disableVirtualKeysUntilLocked(time);
848}
849
850bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
851 InputDevice* device, int32_t keyCode, int32_t scanCode) {
852 // lock is already held by the input loop
853 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
854}
855
856void InputReader::ContextImpl::fadePointer() {
857 // lock is already held by the input loop
858 mReader->fadePointerLocked();
859}
860
861void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
862 // lock is already held by the input loop
863 mReader->requestTimeoutAtTimeLocked(when);
864}
865
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700866int32_t InputReader::ContextImpl::bumpGeneration() {
867 // lock is already held by the input loop
868 return mReader->bumpGenerationLocked();
869}
870
Jeff Brownbe1aa822011-07-27 16:04:54 -0700871InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
872 return mReader->mPolicy.get();
873}
874
875InputListenerInterface* InputReader::ContextImpl::getListener() {
876 return mReader->mQueuedListener.get();
877}
878
879EventHubInterface* InputReader::ContextImpl::getEventHub() {
880 return mReader->mEventHub.get();
881}
882
883
Jeff Brown6d0fec22010-07-23 21:28:06 -0700884// --- InputReaderThread ---
885
886InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
887 Thread(/*canCallJava*/ true), mReader(reader) {
888}
889
890InputReaderThread::~InputReaderThread() {
891}
892
893bool InputReaderThread::threadLoop() {
894 mReader->loopOnce();
895 return true;
896}
897
898
899// --- InputDevice ---
900
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700901InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700902 const InputDeviceIdentifier& identifier, uint32_t classes) :
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700903 mContext(context), mId(id), mGeneration(generation),
904 mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285af2011-08-31 12:56:34 -0700905 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700906}
907
908InputDevice::~InputDevice() {
909 size_t numMappers = mMappers.size();
910 for (size_t i = 0; i < numMappers; i++) {
911 delete mMappers[i];
912 }
913 mMappers.clear();
914}
915
Jeff Brownef3d7e82010-09-30 14:33:04 -0700916void InputDevice::dump(String8& dump) {
917 InputDeviceInfo deviceInfo;
918 getDeviceInfo(& deviceInfo);
919
Jeff Brown90655042010-12-02 13:50:46 -0800920 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700921 deviceInfo.getDisplayName().string());
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700922 dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration);
Jeff Brown56194eb2011-03-02 19:23:13 -0800923 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700924 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
925 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800926
Jeff Brownefd32662011-03-08 15:13:06 -0800927 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800928 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700929 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800930 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800931 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
932 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800933 char name[32];
934 if (label) {
935 strncpy(name, label, sizeof(name));
936 name[sizeof(name) - 1] = '\0';
937 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800938 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800939 }
Jeff Brownefd32662011-03-08 15:13:06 -0800940 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
941 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
942 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800943 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700944 }
945
946 size_t numMappers = mMappers.size();
947 for (size_t i = 0; i < numMappers; i++) {
948 InputMapper* mapper = mMappers[i];
949 mapper->dump(dump);
950 }
951}
952
Jeff Brown6d0fec22010-07-23 21:28:06 -0700953void InputDevice::addMapper(InputMapper* mapper) {
954 mMappers.add(mapper);
955}
956
Jeff Brown65fd2512011-08-18 11:20:58 -0700957void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700958 mSources = 0;
959
Jeff Brown474dcb52011-06-14 20:22:50 -0700960 if (!isIgnored()) {
961 if (!changes) { // first time only
962 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
963 }
964
Jeff Brown6ec6f792012-04-17 16:52:41 -0700965 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Jeff Brown61c08242012-04-19 11:14:33 -0700966 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
967 sp<KeyCharacterMap> keyboardLayout =
968 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor);
969 if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
970 bumpGeneration();
971 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700972 }
973 }
974
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700975 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
976 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
977 String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
978 if (mAlias != alias) {
979 mAlias = alias;
980 bumpGeneration();
981 }
982 }
983 }
984
Jeff Brown474dcb52011-06-14 20:22:50 -0700985 size_t numMappers = mMappers.size();
986 for (size_t i = 0; i < numMappers; i++) {
987 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700988 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700989 mSources |= mapper->getSources();
990 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700991 }
992}
993
Jeff Brown65fd2512011-08-18 11:20:58 -0700994void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700995 size_t numMappers = mMappers.size();
996 for (size_t i = 0; i < numMappers; i++) {
997 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700998 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700999 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001000
1001 mContext->updateGlobalMetaState();
1002
1003 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001004}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001005
Jeff Brownb7198742011-03-18 18:14:26 -07001006void InputDevice::process(const RawEvent* rawEvents, size_t count) {
1007 // Process all of the events in order for each mapper.
1008 // We cannot simply ask each mapper to process them in bulk because mappers may
1009 // have side-effects that must be interleaved. For example, joystick movement events and
1010 // gamepad button presses are handled by different mappers but they should be dispatched
1011 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001012 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -07001013 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
1014#if DEBUG_RAW_EVENTS
Jeff Brown49ccac52012-04-11 18:27:33 -07001015 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
1016 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
Jeff Brownb7198742011-03-18 18:14:26 -07001017#endif
1018
Jeff Brown80fd47c2011-05-24 01:07:44 -07001019 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001020 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07001021 mDropUntilNextSync = false;
1022#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +00001023 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -07001024#endif
1025 } else {
1026#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +00001027 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -07001028#endif
1029 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001030 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -07001031 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -07001032 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07001033 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001034 } else {
1035 for (size_t i = 0; i < numMappers; i++) {
1036 InputMapper* mapper = mMappers[i];
1037 mapper->process(rawEvent);
1038 }
Jeff Brownb7198742011-03-18 18:14:26 -07001039 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001040 }
1041}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001042
Jeff Brownaa3855d2011-03-17 01:34:19 -07001043void InputDevice::timeoutExpired(nsecs_t when) {
1044 size_t numMappers = mMappers.size();
1045 for (size_t i = 0; i < numMappers; i++) {
1046 InputMapper* mapper = mMappers[i];
1047 mapper->timeoutExpired(when);
1048 }
1049}
1050
Jeff Brown6d0fec22010-07-23 21:28:06 -07001051void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001052 outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001053
1054 size_t numMappers = mMappers.size();
1055 for (size_t i = 0; i < numMappers; i++) {
1056 InputMapper* mapper = mMappers[i];
1057 mapper->populateDeviceInfo(outDeviceInfo);
1058 }
1059}
1060
1061int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1062 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1063}
1064
1065int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1066 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1067}
1068
1069int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1070 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1071}
1072
1073int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1074 int32_t result = AKEY_STATE_UNKNOWN;
1075 size_t numMappers = mMappers.size();
1076 for (size_t i = 0; i < numMappers; i++) {
1077 InputMapper* mapper = mMappers[i];
1078 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001079 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1080 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1081 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1082 if (currentResult >= AKEY_STATE_DOWN) {
1083 return currentResult;
1084 } else if (currentResult == AKEY_STATE_UP) {
1085 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001086 }
1087 }
1088 }
1089 return result;
1090}
1091
1092bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1093 const int32_t* keyCodes, uint8_t* outFlags) {
1094 bool result = false;
1095 size_t numMappers = mMappers.size();
1096 for (size_t i = 0; i < numMappers; i++) {
1097 InputMapper* mapper = mMappers[i];
1098 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1099 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1100 }
1101 }
1102 return result;
1103}
1104
Jeff Browna47425a2012-04-13 04:09:27 -07001105void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1106 int32_t token) {
1107 size_t numMappers = mMappers.size();
1108 for (size_t i = 0; i < numMappers; i++) {
1109 InputMapper* mapper = mMappers[i];
1110 mapper->vibrate(pattern, patternSize, repeat, token);
1111 }
1112}
1113
1114void InputDevice::cancelVibrate(int32_t token) {
1115 size_t numMappers = mMappers.size();
1116 for (size_t i = 0; i < numMappers; i++) {
1117 InputMapper* mapper = mMappers[i];
1118 mapper->cancelVibrate(token);
1119 }
1120}
1121
Jeff Brown6d0fec22010-07-23 21:28:06 -07001122int32_t InputDevice::getMetaState() {
1123 int32_t result = 0;
1124 size_t numMappers = mMappers.size();
1125 for (size_t i = 0; i < numMappers; i++) {
1126 InputMapper* mapper = mMappers[i];
1127 result |= mapper->getMetaState();
1128 }
1129 return result;
1130}
1131
Jeff Brown05dc66a2011-03-02 14:41:58 -08001132void InputDevice::fadePointer() {
1133 size_t numMappers = mMappers.size();
1134 for (size_t i = 0; i < numMappers; i++) {
1135 InputMapper* mapper = mMappers[i];
1136 mapper->fadePointer();
1137 }
1138}
1139
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001140void InputDevice::bumpGeneration() {
1141 mGeneration = mContext->bumpGeneration();
1142}
1143
Jeff Brown65fd2512011-08-18 11:20:58 -07001144void InputDevice::notifyReset(nsecs_t when) {
1145 NotifyDeviceResetArgs args(when, mId);
1146 mContext->getListener()->notifyDeviceReset(&args);
1147}
1148
Jeff Brown6d0fec22010-07-23 21:28:06 -07001149
Jeff Brown49754db2011-07-01 17:37:58 -07001150// --- CursorButtonAccumulator ---
1151
1152CursorButtonAccumulator::CursorButtonAccumulator() {
1153 clearButtons();
1154}
1155
Jeff Brown65fd2512011-08-18 11:20:58 -07001156void CursorButtonAccumulator::reset(InputDevice* device) {
1157 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1158 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1159 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1160 mBtnBack = device->isKeyPressed(BTN_BACK);
1161 mBtnSide = device->isKeyPressed(BTN_SIDE);
1162 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1163 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1164 mBtnTask = device->isKeyPressed(BTN_TASK);
1165}
1166
Jeff Brown49754db2011-07-01 17:37:58 -07001167void CursorButtonAccumulator::clearButtons() {
1168 mBtnLeft = 0;
1169 mBtnRight = 0;
1170 mBtnMiddle = 0;
1171 mBtnBack = 0;
1172 mBtnSide = 0;
1173 mBtnForward = 0;
1174 mBtnExtra = 0;
1175 mBtnTask = 0;
1176}
1177
1178void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1179 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001180 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001181 case BTN_LEFT:
1182 mBtnLeft = rawEvent->value;
1183 break;
1184 case BTN_RIGHT:
1185 mBtnRight = rawEvent->value;
1186 break;
1187 case BTN_MIDDLE:
1188 mBtnMiddle = rawEvent->value;
1189 break;
1190 case BTN_BACK:
1191 mBtnBack = rawEvent->value;
1192 break;
1193 case BTN_SIDE:
1194 mBtnSide = rawEvent->value;
1195 break;
1196 case BTN_FORWARD:
1197 mBtnForward = rawEvent->value;
1198 break;
1199 case BTN_EXTRA:
1200 mBtnExtra = rawEvent->value;
1201 break;
1202 case BTN_TASK:
1203 mBtnTask = rawEvent->value;
1204 break;
1205 }
1206 }
1207}
1208
1209uint32_t CursorButtonAccumulator::getButtonState() const {
1210 uint32_t result = 0;
1211 if (mBtnLeft) {
1212 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1213 }
1214 if (mBtnRight) {
1215 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1216 }
1217 if (mBtnMiddle) {
1218 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1219 }
1220 if (mBtnBack || mBtnSide) {
1221 result |= AMOTION_EVENT_BUTTON_BACK;
1222 }
1223 if (mBtnForward || mBtnExtra) {
1224 result |= AMOTION_EVENT_BUTTON_FORWARD;
1225 }
1226 return result;
1227}
1228
1229
1230// --- CursorMotionAccumulator ---
1231
Jeff Brown65fd2512011-08-18 11:20:58 -07001232CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001233 clearRelativeAxes();
1234}
1235
Jeff Brown65fd2512011-08-18 11:20:58 -07001236void CursorMotionAccumulator::reset(InputDevice* device) {
1237 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001238}
1239
1240void CursorMotionAccumulator::clearRelativeAxes() {
1241 mRelX = 0;
1242 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001243}
1244
1245void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1246 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001247 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001248 case REL_X:
1249 mRelX = rawEvent->value;
1250 break;
1251 case REL_Y:
1252 mRelY = rawEvent->value;
1253 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001254 }
1255 }
1256}
1257
1258void CursorMotionAccumulator::finishSync() {
1259 clearRelativeAxes();
1260}
1261
1262
1263// --- CursorScrollAccumulator ---
1264
1265CursorScrollAccumulator::CursorScrollAccumulator() :
1266 mHaveRelWheel(false), mHaveRelHWheel(false) {
1267 clearRelativeAxes();
1268}
1269
1270void CursorScrollAccumulator::configure(InputDevice* device) {
1271 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1272 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1273}
1274
1275void CursorScrollAccumulator::reset(InputDevice* device) {
1276 clearRelativeAxes();
1277}
1278
1279void CursorScrollAccumulator::clearRelativeAxes() {
1280 mRelWheel = 0;
1281 mRelHWheel = 0;
1282}
1283
1284void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1285 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001286 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001287 case REL_WHEEL:
1288 mRelWheel = rawEvent->value;
1289 break;
1290 case REL_HWHEEL:
1291 mRelHWheel = rawEvent->value;
1292 break;
1293 }
1294 }
1295}
1296
Jeff Brown65fd2512011-08-18 11:20:58 -07001297void CursorScrollAccumulator::finishSync() {
1298 clearRelativeAxes();
1299}
1300
Jeff Brown49754db2011-07-01 17:37:58 -07001301
1302// --- TouchButtonAccumulator ---
1303
1304TouchButtonAccumulator::TouchButtonAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001305 mHaveBtnTouch(false), mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001306 clearButtons();
1307}
1308
1309void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001310 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
Jeff Brown00710e92012-04-19 15:18:26 -07001311 mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1312 || device->hasKey(BTN_TOOL_RUBBER)
1313 || device->hasKey(BTN_TOOL_BRUSH)
1314 || device->hasKey(BTN_TOOL_PENCIL)
1315 || device->hasKey(BTN_TOOL_AIRBRUSH);
Jeff Brown65fd2512011-08-18 11:20:58 -07001316}
1317
1318void TouchButtonAccumulator::reset(InputDevice* device) {
1319 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1320 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1321 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1322 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1323 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1324 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1325 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1326 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1327 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1328 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1329 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001330 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1331 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1332 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001333}
1334
1335void TouchButtonAccumulator::clearButtons() {
1336 mBtnTouch = 0;
1337 mBtnStylus = 0;
1338 mBtnStylus2 = 0;
1339 mBtnToolFinger = 0;
1340 mBtnToolPen = 0;
1341 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001342 mBtnToolBrush = 0;
1343 mBtnToolPencil = 0;
1344 mBtnToolAirbrush = 0;
1345 mBtnToolMouse = 0;
1346 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001347 mBtnToolDoubleTap = 0;
1348 mBtnToolTripleTap = 0;
1349 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001350}
1351
1352void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1353 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001354 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001355 case BTN_TOUCH:
1356 mBtnTouch = rawEvent->value;
1357 break;
1358 case BTN_STYLUS:
1359 mBtnStylus = rawEvent->value;
1360 break;
1361 case BTN_STYLUS2:
1362 mBtnStylus2 = rawEvent->value;
1363 break;
1364 case BTN_TOOL_FINGER:
1365 mBtnToolFinger = rawEvent->value;
1366 break;
1367 case BTN_TOOL_PEN:
1368 mBtnToolPen = rawEvent->value;
1369 break;
1370 case BTN_TOOL_RUBBER:
1371 mBtnToolRubber = rawEvent->value;
1372 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001373 case BTN_TOOL_BRUSH:
1374 mBtnToolBrush = rawEvent->value;
1375 break;
1376 case BTN_TOOL_PENCIL:
1377 mBtnToolPencil = rawEvent->value;
1378 break;
1379 case BTN_TOOL_AIRBRUSH:
1380 mBtnToolAirbrush = rawEvent->value;
1381 break;
1382 case BTN_TOOL_MOUSE:
1383 mBtnToolMouse = rawEvent->value;
1384 break;
1385 case BTN_TOOL_LENS:
1386 mBtnToolLens = rawEvent->value;
1387 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001388 case BTN_TOOL_DOUBLETAP:
1389 mBtnToolDoubleTap = rawEvent->value;
1390 break;
1391 case BTN_TOOL_TRIPLETAP:
1392 mBtnToolTripleTap = rawEvent->value;
1393 break;
1394 case BTN_TOOL_QUADTAP:
1395 mBtnToolQuadTap = rawEvent->value;
1396 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001397 }
1398 }
1399}
1400
1401uint32_t TouchButtonAccumulator::getButtonState() const {
1402 uint32_t result = 0;
1403 if (mBtnStylus) {
1404 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1405 }
1406 if (mBtnStylus2) {
1407 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1408 }
1409 return result;
1410}
1411
1412int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001413 if (mBtnToolMouse || mBtnToolLens) {
1414 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1415 }
Jeff Brown49754db2011-07-01 17:37:58 -07001416 if (mBtnToolRubber) {
1417 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1418 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001419 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001420 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1421 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001422 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001423 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1424 }
1425 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1426}
1427
Jeff Brownd87c6d52011-08-10 14:55:59 -07001428bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001429 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1430 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001431 || mBtnToolMouse || mBtnToolLens
1432 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001433}
1434
1435bool TouchButtonAccumulator::isHovering() const {
1436 return mHaveBtnTouch && !mBtnTouch;
1437}
1438
Jeff Brown00710e92012-04-19 15:18:26 -07001439bool TouchButtonAccumulator::hasStylus() const {
1440 return mHaveStylus;
1441}
1442
Jeff Brown49754db2011-07-01 17:37:58 -07001443
Jeff Brownbe1aa822011-07-27 16:04:54 -07001444// --- RawPointerAxes ---
1445
1446RawPointerAxes::RawPointerAxes() {
1447 clear();
1448}
1449
1450void RawPointerAxes::clear() {
1451 x.clear();
1452 y.clear();
1453 pressure.clear();
1454 touchMajor.clear();
1455 touchMinor.clear();
1456 toolMajor.clear();
1457 toolMinor.clear();
1458 orientation.clear();
1459 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001460 tiltX.clear();
1461 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001462 trackingId.clear();
1463 slot.clear();
1464}
1465
1466
1467// --- RawPointerData ---
1468
1469RawPointerData::RawPointerData() {
1470 clear();
1471}
1472
1473void RawPointerData::clear() {
1474 pointerCount = 0;
1475 clearIdBits();
1476}
1477
1478void RawPointerData::copyFrom(const RawPointerData& other) {
1479 pointerCount = other.pointerCount;
1480 hoveringIdBits = other.hoveringIdBits;
1481 touchingIdBits = other.touchingIdBits;
1482
1483 for (uint32_t i = 0; i < pointerCount; i++) {
1484 pointers[i] = other.pointers[i];
1485
1486 int id = pointers[i].id;
1487 idToIndex[id] = other.idToIndex[id];
1488 }
1489}
1490
1491void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1492 float x = 0, y = 0;
1493 uint32_t count = touchingIdBits.count();
1494 if (count) {
1495 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1496 uint32_t id = idBits.clearFirstMarkedBit();
1497 const Pointer& pointer = pointerForId(id);
1498 x += pointer.x;
1499 y += pointer.y;
1500 }
1501 x /= count;
1502 y /= count;
1503 }
1504 *outX = x;
1505 *outY = y;
1506}
1507
1508
1509// --- CookedPointerData ---
1510
1511CookedPointerData::CookedPointerData() {
1512 clear();
1513}
1514
1515void CookedPointerData::clear() {
1516 pointerCount = 0;
1517 hoveringIdBits.clear();
1518 touchingIdBits.clear();
1519}
1520
1521void CookedPointerData::copyFrom(const CookedPointerData& other) {
1522 pointerCount = other.pointerCount;
1523 hoveringIdBits = other.hoveringIdBits;
1524 touchingIdBits = other.touchingIdBits;
1525
1526 for (uint32_t i = 0; i < pointerCount; i++) {
1527 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1528 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1529
1530 int id = pointerProperties[i].id;
1531 idToIndex[id] = other.idToIndex[id];
1532 }
1533}
1534
1535
Jeff Brown49754db2011-07-01 17:37:58 -07001536// --- SingleTouchMotionAccumulator ---
1537
1538SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1539 clearAbsoluteAxes();
1540}
1541
Jeff Brown65fd2512011-08-18 11:20:58 -07001542void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1543 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1544 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1545 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1546 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1547 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1548 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1549 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1550}
1551
Jeff Brown49754db2011-07-01 17:37:58 -07001552void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1553 mAbsX = 0;
1554 mAbsY = 0;
1555 mAbsPressure = 0;
1556 mAbsToolWidth = 0;
1557 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001558 mAbsTiltX = 0;
1559 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001560}
1561
1562void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1563 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001564 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001565 case ABS_X:
1566 mAbsX = rawEvent->value;
1567 break;
1568 case ABS_Y:
1569 mAbsY = rawEvent->value;
1570 break;
1571 case ABS_PRESSURE:
1572 mAbsPressure = rawEvent->value;
1573 break;
1574 case ABS_TOOL_WIDTH:
1575 mAbsToolWidth = rawEvent->value;
1576 break;
1577 case ABS_DISTANCE:
1578 mAbsDistance = rawEvent->value;
1579 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001580 case ABS_TILT_X:
1581 mAbsTiltX = rawEvent->value;
1582 break;
1583 case ABS_TILT_Y:
1584 mAbsTiltY = rawEvent->value;
1585 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001586 }
1587 }
1588}
1589
1590
1591// --- MultiTouchMotionAccumulator ---
1592
1593MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001594 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1595 mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001596}
1597
1598MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1599 delete[] mSlots;
1600}
1601
Jeff Brown00710e92012-04-19 15:18:26 -07001602void MultiTouchMotionAccumulator::configure(InputDevice* device,
1603 size_t slotCount, bool usingSlotsProtocol) {
Jeff Brown49754db2011-07-01 17:37:58 -07001604 mSlotCount = slotCount;
1605 mUsingSlotsProtocol = usingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -07001606 mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Jeff Brown49754db2011-07-01 17:37:58 -07001607
1608 delete[] mSlots;
1609 mSlots = new Slot[slotCount];
1610}
1611
Jeff Brown65fd2512011-08-18 11:20:58 -07001612void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1613 // Unfortunately there is no way to read the initial contents of the slots.
1614 // So when we reset the accumulator, we must assume they are all zeroes.
1615 if (mUsingSlotsProtocol) {
1616 // Query the driver for the current slot index and use it as the initial slot
1617 // before we start reading events from the device. It is possible that the
1618 // current slot index will not be the same as it was when the first event was
1619 // written into the evdev buffer, which means the input mapper could start
1620 // out of sync with the initial state of the events in the evdev buffer.
1621 // In the extremely unlikely case that this happens, the data from
1622 // two slots will be confused until the next ABS_MT_SLOT event is received.
1623 // This can cause the touch point to "jump", but at least there will be
1624 // no stuck touches.
1625 int32_t initialSlot;
1626 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1627 ABS_MT_SLOT, &initialSlot);
1628 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001629 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001630 initialSlot = -1;
1631 }
1632 clearSlots(initialSlot);
1633 } else {
1634 clearSlots(-1);
1635 }
1636}
1637
Jeff Brown49754db2011-07-01 17:37:58 -07001638void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001639 if (mSlots) {
1640 for (size_t i = 0; i < mSlotCount; i++) {
1641 mSlots[i].clear();
1642 }
Jeff Brown49754db2011-07-01 17:37:58 -07001643 }
1644 mCurrentSlot = initialSlot;
1645}
1646
1647void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1648 if (rawEvent->type == EV_ABS) {
1649 bool newSlot = false;
1650 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001651 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001652 mCurrentSlot = rawEvent->value;
1653 newSlot = true;
1654 }
1655 } else if (mCurrentSlot < 0) {
1656 mCurrentSlot = 0;
1657 }
1658
1659 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1660#if DEBUG_POINTERS
1661 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001662 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001663 "should be between 0 and %d; ignoring this slot.",
1664 mCurrentSlot, mSlotCount - 1);
1665 }
1666#endif
1667 } else {
1668 Slot* slot = &mSlots[mCurrentSlot];
1669
Jeff Brown49ccac52012-04-11 18:27:33 -07001670 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001671 case ABS_MT_POSITION_X:
1672 slot->mInUse = true;
1673 slot->mAbsMTPositionX = rawEvent->value;
1674 break;
1675 case ABS_MT_POSITION_Y:
1676 slot->mInUse = true;
1677 slot->mAbsMTPositionY = rawEvent->value;
1678 break;
1679 case ABS_MT_TOUCH_MAJOR:
1680 slot->mInUse = true;
1681 slot->mAbsMTTouchMajor = rawEvent->value;
1682 break;
1683 case ABS_MT_TOUCH_MINOR:
1684 slot->mInUse = true;
1685 slot->mAbsMTTouchMinor = rawEvent->value;
1686 slot->mHaveAbsMTTouchMinor = true;
1687 break;
1688 case ABS_MT_WIDTH_MAJOR:
1689 slot->mInUse = true;
1690 slot->mAbsMTWidthMajor = rawEvent->value;
1691 break;
1692 case ABS_MT_WIDTH_MINOR:
1693 slot->mInUse = true;
1694 slot->mAbsMTWidthMinor = rawEvent->value;
1695 slot->mHaveAbsMTWidthMinor = true;
1696 break;
1697 case ABS_MT_ORIENTATION:
1698 slot->mInUse = true;
1699 slot->mAbsMTOrientation = rawEvent->value;
1700 break;
1701 case ABS_MT_TRACKING_ID:
1702 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001703 // The slot is no longer in use but it retains its previous contents,
1704 // which may be reused for subsequent touches.
1705 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001706 } else {
1707 slot->mInUse = true;
1708 slot->mAbsMTTrackingId = rawEvent->value;
1709 }
1710 break;
1711 case ABS_MT_PRESSURE:
1712 slot->mInUse = true;
1713 slot->mAbsMTPressure = rawEvent->value;
1714 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001715 case ABS_MT_DISTANCE:
1716 slot->mInUse = true;
1717 slot->mAbsMTDistance = rawEvent->value;
1718 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001719 case ABS_MT_TOOL_TYPE:
1720 slot->mInUse = true;
1721 slot->mAbsMTToolType = rawEvent->value;
1722 slot->mHaveAbsMTToolType = true;
1723 break;
1724 }
1725 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001726 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001727 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1728 mCurrentSlot += 1;
1729 }
1730}
1731
Jeff Brown65fd2512011-08-18 11:20:58 -07001732void MultiTouchMotionAccumulator::finishSync() {
1733 if (!mUsingSlotsProtocol) {
1734 clearSlots(-1);
1735 }
1736}
1737
Jeff Brown00710e92012-04-19 15:18:26 -07001738bool MultiTouchMotionAccumulator::hasStylus() const {
1739 return mHaveStylus;
1740}
1741
Jeff Brown49754db2011-07-01 17:37:58 -07001742
1743// --- MultiTouchMotionAccumulator::Slot ---
1744
1745MultiTouchMotionAccumulator::Slot::Slot() {
1746 clear();
1747}
1748
Jeff Brown49754db2011-07-01 17:37:58 -07001749void MultiTouchMotionAccumulator::Slot::clear() {
1750 mInUse = false;
1751 mHaveAbsMTTouchMinor = false;
1752 mHaveAbsMTWidthMinor = false;
1753 mHaveAbsMTToolType = false;
1754 mAbsMTPositionX = 0;
1755 mAbsMTPositionY = 0;
1756 mAbsMTTouchMajor = 0;
1757 mAbsMTTouchMinor = 0;
1758 mAbsMTWidthMajor = 0;
1759 mAbsMTWidthMinor = 0;
1760 mAbsMTOrientation = 0;
1761 mAbsMTTrackingId = -1;
1762 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001763 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001764 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001765}
1766
1767int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1768 if (mHaveAbsMTToolType) {
1769 switch (mAbsMTToolType) {
1770 case MT_TOOL_FINGER:
1771 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1772 case MT_TOOL_PEN:
1773 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1774 }
1775 }
1776 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1777}
1778
1779
Jeff Brown6d0fec22010-07-23 21:28:06 -07001780// --- InputMapper ---
1781
1782InputMapper::InputMapper(InputDevice* device) :
1783 mDevice(device), mContext(device->getContext()) {
1784}
1785
1786InputMapper::~InputMapper() {
1787}
1788
1789void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1790 info->addSource(getSources());
1791}
1792
Jeff Brownef3d7e82010-09-30 14:33:04 -07001793void InputMapper::dump(String8& dump) {
1794}
1795
Jeff Brown65fd2512011-08-18 11:20:58 -07001796void InputMapper::configure(nsecs_t when,
1797 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001798}
1799
Jeff Brown65fd2512011-08-18 11:20:58 -07001800void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001801}
1802
Jeff Brownaa3855d2011-03-17 01:34:19 -07001803void InputMapper::timeoutExpired(nsecs_t when) {
1804}
1805
Jeff Brown6d0fec22010-07-23 21:28:06 -07001806int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1807 return AKEY_STATE_UNKNOWN;
1808}
1809
1810int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1811 return AKEY_STATE_UNKNOWN;
1812}
1813
1814int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1815 return AKEY_STATE_UNKNOWN;
1816}
1817
1818bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1819 const int32_t* keyCodes, uint8_t* outFlags) {
1820 return false;
1821}
1822
Jeff Browna47425a2012-04-13 04:09:27 -07001823void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1824 int32_t token) {
1825}
1826
1827void InputMapper::cancelVibrate(int32_t token) {
1828}
1829
Jeff Brown6d0fec22010-07-23 21:28:06 -07001830int32_t InputMapper::getMetaState() {
1831 return 0;
1832}
1833
Jeff Brown05dc66a2011-03-02 14:41:58 -08001834void InputMapper::fadePointer() {
1835}
1836
Jeff Brownbe1aa822011-07-27 16:04:54 -07001837status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1838 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1839}
1840
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001841void InputMapper::bumpGeneration() {
1842 mDevice->bumpGeneration();
1843}
1844
Jeff Browncb1404e2011-01-15 18:14:15 -08001845void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1846 const RawAbsoluteAxisInfo& axis, const char* name) {
1847 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001848 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1849 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001850 } else {
1851 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1852 }
1853}
1854
Jeff Brown6d0fec22010-07-23 21:28:06 -07001855
1856// --- SwitchInputMapper ---
1857
1858SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1859 InputMapper(device) {
1860}
1861
1862SwitchInputMapper::~SwitchInputMapper() {
1863}
1864
1865uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001866 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001867}
1868
1869void SwitchInputMapper::process(const RawEvent* rawEvent) {
1870 switch (rawEvent->type) {
1871 case EV_SW:
Jeff Brown49ccac52012-04-11 18:27:33 -07001872 processSwitch(rawEvent->when, rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001873 break;
1874 }
1875}
1876
1877void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001878 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1879 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001880}
1881
1882int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1883 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1884}
1885
1886
Jeff Browna47425a2012-04-13 04:09:27 -07001887// --- VibratorInputMapper ---
1888
1889VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1890 InputMapper(device), mVibrating(false) {
1891}
1892
1893VibratorInputMapper::~VibratorInputMapper() {
1894}
1895
1896uint32_t VibratorInputMapper::getSources() {
1897 return 0;
1898}
1899
1900void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1901 InputMapper::populateDeviceInfo(info);
1902
1903 info->setVibrator(true);
1904}
1905
1906void VibratorInputMapper::process(const RawEvent* rawEvent) {
1907 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1908}
1909
1910void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1911 int32_t token) {
1912#if DEBUG_VIBRATOR
1913 String8 patternStr;
1914 for (size_t i = 0; i < patternSize; i++) {
1915 if (i != 0) {
1916 patternStr.append(", ");
1917 }
1918 patternStr.appendFormat("%lld", pattern[i]);
1919 }
1920 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1921 getDeviceId(), patternStr.string(), repeat, token);
1922#endif
1923
1924 mVibrating = true;
1925 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1926 mPatternSize = patternSize;
1927 mRepeat = repeat;
1928 mToken = token;
1929 mIndex = -1;
1930
1931 nextStep();
1932}
1933
1934void VibratorInputMapper::cancelVibrate(int32_t token) {
1935#if DEBUG_VIBRATOR
1936 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1937#endif
1938
1939 if (mVibrating && mToken == token) {
1940 stopVibrating();
1941 }
1942}
1943
1944void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1945 if (mVibrating) {
1946 if (when >= mNextStepTime) {
1947 nextStep();
1948 } else {
1949 getContext()->requestTimeoutAtTime(mNextStepTime);
1950 }
1951 }
1952}
1953
1954void VibratorInputMapper::nextStep() {
1955 mIndex += 1;
1956 if (size_t(mIndex) >= mPatternSize) {
1957 if (mRepeat < 0) {
1958 // We are done.
1959 stopVibrating();
1960 return;
1961 }
1962 mIndex = mRepeat;
1963 }
1964
1965 bool vibratorOn = mIndex & 1;
1966 nsecs_t duration = mPattern[mIndex];
1967 if (vibratorOn) {
1968#if DEBUG_VIBRATOR
1969 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1970 getDeviceId(), duration);
1971#endif
1972 getEventHub()->vibrate(getDeviceId(), duration);
1973 } else {
1974#if DEBUG_VIBRATOR
1975 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1976#endif
1977 getEventHub()->cancelVibrate(getDeviceId());
1978 }
1979 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1980 mNextStepTime = now + duration;
1981 getContext()->requestTimeoutAtTime(mNextStepTime);
1982#if DEBUG_VIBRATOR
1983 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1984#endif
1985}
1986
1987void VibratorInputMapper::stopVibrating() {
1988 mVibrating = false;
1989#if DEBUG_VIBRATOR
1990 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1991#endif
1992 getEventHub()->cancelVibrate(getDeviceId());
1993}
1994
1995void VibratorInputMapper::dump(String8& dump) {
1996 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1997 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1998}
1999
2000
Jeff Brown6d0fec22010-07-23 21:28:06 -07002001// --- KeyboardInputMapper ---
2002
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002003KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08002004 uint32_t source, int32_t keyboardType) :
2005 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07002006 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002007}
2008
2009KeyboardInputMapper::~KeyboardInputMapper() {
2010}
2011
Jeff Brown6d0fec22010-07-23 21:28:06 -07002012uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002013 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002014}
2015
2016void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2017 InputMapper::populateDeviceInfo(info);
2018
2019 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07002020 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07002021}
2022
Jeff Brownef3d7e82010-09-30 14:33:04 -07002023void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002024 dump.append(INDENT2 "Keyboard Input Mapper:\n");
2025 dumpParameters(dump);
2026 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07002027 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002028 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
2029 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
2030 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002031}
2032
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002033
Jeff Brown65fd2512011-08-18 11:20:58 -07002034void KeyboardInputMapper::configure(nsecs_t when,
2035 const InputReaderConfiguration* config, uint32_t changes) {
2036 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002037
Jeff Brown474dcb52011-06-14 20:22:50 -07002038 if (!changes) { // first time only
2039 // Configure basic parameters.
2040 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002041 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002042
Jeff Brown65fd2512011-08-18 11:20:58 -07002043 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2044 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2045 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2046 false /*external*/, NULL, NULL, &mOrientation)) {
2047 mOrientation = DISPLAY_ORIENTATION_0;
2048 }
2049 } else {
2050 mOrientation = DISPLAY_ORIENTATION_0;
2051 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002052 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002053}
2054
2055void KeyboardInputMapper::configureParameters() {
2056 mParameters.orientationAware = false;
2057 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2058 mParameters.orientationAware);
2059
Jeff Brownbc68a592011-07-25 12:58:12 -07002060 mParameters.associatedDisplayId = -1;
2061 if (mParameters.orientationAware) {
2062 mParameters.associatedDisplayId = 0;
2063 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002064}
2065
2066void KeyboardInputMapper::dumpParameters(String8& dump) {
2067 dump.append(INDENT3 "Parameters:\n");
2068 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2069 mParameters.associatedDisplayId);
2070 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2071 toString(mParameters.orientationAware));
2072}
2073
Jeff Brown65fd2512011-08-18 11:20:58 -07002074void KeyboardInputMapper::reset(nsecs_t when) {
2075 mMetaState = AMETA_NONE;
2076 mDownTime = 0;
2077 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002078 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002079
Jeff Brownbe1aa822011-07-27 16:04:54 -07002080 resetLedState();
2081
Jeff Brown65fd2512011-08-18 11:20:58 -07002082 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002083}
2084
2085void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2086 switch (rawEvent->type) {
2087 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002088 int32_t scanCode = rawEvent->code;
2089 int32_t usageCode = mCurrentHidUsage;
2090 mCurrentHidUsage = 0;
2091
Jeff Brown6d0fec22010-07-23 21:28:06 -07002092 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002093 int32_t keyCode;
2094 uint32_t flags;
2095 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2096 keyCode = AKEYCODE_UNKNOWN;
2097 flags = 0;
2098 }
2099 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002100 }
2101 break;
2102 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002103 case EV_MSC: {
2104 if (rawEvent->code == MSC_SCAN) {
2105 mCurrentHidUsage = rawEvent->value;
2106 }
2107 break;
2108 }
2109 case EV_SYN: {
2110 if (rawEvent->code == SYN_REPORT) {
2111 mCurrentHidUsage = 0;
2112 }
2113 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002114 }
2115}
2116
2117bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2118 return scanCode < BTN_MOUSE
2119 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002120 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002121 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002122}
2123
Jeff Brown6328cdc2010-07-29 18:18:33 -07002124void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2125 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002126
Jeff Brownbe1aa822011-07-27 16:04:54 -07002127 if (down) {
2128 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002129 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002130 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002131 }
Jeff Brownfe508922011-01-18 15:10:10 -08002132
Jeff Brownbe1aa822011-07-27 16:04:54 -07002133 // Add key down.
2134 ssize_t keyDownIndex = findKeyDown(scanCode);
2135 if (keyDownIndex >= 0) {
2136 // key repeat, be sure to use same keycode as before in case of rotation
2137 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002138 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002139 // key down
2140 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2141 && mContext->shouldDropVirtualKey(when,
2142 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002143 return;
2144 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002145
2146 mKeyDowns.push();
2147 KeyDown& keyDown = mKeyDowns.editTop();
2148 keyDown.keyCode = keyCode;
2149 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002150 }
2151
Jeff Brownbe1aa822011-07-27 16:04:54 -07002152 mDownTime = when;
2153 } else {
2154 // Remove key down.
2155 ssize_t keyDownIndex = findKeyDown(scanCode);
2156 if (keyDownIndex >= 0) {
2157 // key up, be sure to use same keycode as before in case of rotation
2158 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2159 mKeyDowns.removeAt(size_t(keyDownIndex));
2160 } else {
2161 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002162 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002163 "keyCode=%d, scanCode=%d",
2164 getDeviceName().string(), keyCode, scanCode);
2165 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002166 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002167 }
Jeff Brownfd0358292010-06-30 16:10:35 -07002168
Jeff Brownbe1aa822011-07-27 16:04:54 -07002169 bool metaStateChanged = false;
2170 int32_t oldMetaState = mMetaState;
2171 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2172 if (oldMetaState != newMetaState) {
2173 mMetaState = newMetaState;
2174 metaStateChanged = true;
2175 updateLedState(false);
2176 }
2177
2178 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002179
Jeff Brown56194eb2011-03-02 19:23:13 -08002180 // Key down on external an keyboard should wake the device.
2181 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2182 // For internal keyboards, the key layout file should specify the policy flags for
2183 // each wake key individually.
2184 // TODO: Use the input device configuration to control this behavior more finely.
2185 if (down && getDevice()->isExternal()
2186 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2187 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2188 }
2189
Jeff Brown6328cdc2010-07-29 18:18:33 -07002190 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002191 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002192 }
2193
Jeff Brown05dc66a2011-03-02 14:41:58 -08002194 if (down && !isMetaKey(keyCode)) {
2195 getContext()->fadePointer();
2196 }
2197
Jeff Brownbe1aa822011-07-27 16:04:54 -07002198 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002199 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2200 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002201 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002202}
2203
Jeff Brownbe1aa822011-07-27 16:04:54 -07002204ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2205 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002206 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002207 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002208 return i;
2209 }
2210 }
2211 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002212}
2213
Jeff Brown6d0fec22010-07-23 21:28:06 -07002214int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2215 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2216}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002217
Jeff Brown6d0fec22010-07-23 21:28:06 -07002218int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2219 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2220}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002221
Jeff Brown6d0fec22010-07-23 21:28:06 -07002222bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2223 const int32_t* keyCodes, uint8_t* outFlags) {
2224 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2225}
2226
2227int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002228 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002229}
2230
Jeff Brownbe1aa822011-07-27 16:04:54 -07002231void KeyboardInputMapper::resetLedState() {
2232 initializeLedState(mCapsLockLedState, LED_CAPSL);
2233 initializeLedState(mNumLockLedState, LED_NUML);
2234 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002235
Jeff Brownbe1aa822011-07-27 16:04:54 -07002236 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002237}
2238
Jeff Brownbe1aa822011-07-27 16:04:54 -07002239void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002240 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2241 ledState.on = false;
2242}
2243
Jeff Brownbe1aa822011-07-27 16:04:54 -07002244void KeyboardInputMapper::updateLedState(bool reset) {
2245 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002246 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002247 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002248 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002249 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002250 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002251}
2252
Jeff Brownbe1aa822011-07-27 16:04:54 -07002253void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002254 int32_t led, int32_t modifier, bool reset) {
2255 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002256 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002257 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002258 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2259 ledState.on = desiredState;
2260 }
2261 }
2262}
2263
Jeff Brown6d0fec22010-07-23 21:28:06 -07002264
Jeff Brown83c09682010-12-23 17:50:18 -08002265// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002266
Jeff Brown83c09682010-12-23 17:50:18 -08002267CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002268 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002269}
2270
Jeff Brown83c09682010-12-23 17:50:18 -08002271CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002272}
2273
Jeff Brown83c09682010-12-23 17:50:18 -08002274uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002275 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002276}
2277
Jeff Brown83c09682010-12-23 17:50:18 -08002278void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002279 InputMapper::populateDeviceInfo(info);
2280
Jeff Brown83c09682010-12-23 17:50:18 -08002281 if (mParameters.mode == Parameters::MODE_POINTER) {
2282 float minX, minY, maxX, maxY;
2283 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002284 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2285 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002286 }
2287 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002288 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2289 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002290 }
Jeff Brownefd32662011-03-08 15:13:06 -08002291 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002292
Jeff Brown65fd2512011-08-18 11:20:58 -07002293 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002294 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002295 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002296 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002297 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002298 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002299}
2300
Jeff Brown83c09682010-12-23 17:50:18 -08002301void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002302 dump.append(INDENT2 "Cursor Input Mapper:\n");
2303 dumpParameters(dump);
2304 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2305 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2306 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2307 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2308 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002309 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002310 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002311 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002312 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2313 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002314 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002315 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2316 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2317 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002318}
2319
Jeff Brown65fd2512011-08-18 11:20:58 -07002320void CursorInputMapper::configure(nsecs_t when,
2321 const InputReaderConfiguration* config, uint32_t changes) {
2322 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002323
Jeff Brown474dcb52011-06-14 20:22:50 -07002324 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002325 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002326
Jeff Brown474dcb52011-06-14 20:22:50 -07002327 // Configure basic parameters.
2328 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002329
Jeff Brown474dcb52011-06-14 20:22:50 -07002330 // Configure device mode.
2331 switch (mParameters.mode) {
2332 case Parameters::MODE_POINTER:
2333 mSource = AINPUT_SOURCE_MOUSE;
2334 mXPrecision = 1.0f;
2335 mYPrecision = 1.0f;
2336 mXScale = 1.0f;
2337 mYScale = 1.0f;
2338 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2339 break;
2340 case Parameters::MODE_NAVIGATION:
2341 mSource = AINPUT_SOURCE_TRACKBALL;
2342 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2343 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2344 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2345 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2346 break;
2347 }
2348
2349 mVWheelScale = 1.0f;
2350 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002351 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002352
Jeff Brown474dcb52011-06-14 20:22:50 -07002353 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2354 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2355 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2356 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2357 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002358
2359 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2360 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2361 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2362 false /*external*/, NULL, NULL, &mOrientation)) {
2363 mOrientation = DISPLAY_ORIENTATION_0;
2364 }
2365 } else {
2366 mOrientation = DISPLAY_ORIENTATION_0;
2367 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002368 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002369 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002370}
2371
Jeff Brown83c09682010-12-23 17:50:18 -08002372void CursorInputMapper::configureParameters() {
2373 mParameters.mode = Parameters::MODE_POINTER;
2374 String8 cursorModeString;
2375 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2376 if (cursorModeString == "navigation") {
2377 mParameters.mode = Parameters::MODE_NAVIGATION;
2378 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002379 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002380 }
2381 }
2382
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002383 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002384 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002385 mParameters.orientationAware);
2386
Jeff Brownbc68a592011-07-25 12:58:12 -07002387 mParameters.associatedDisplayId = -1;
2388 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2389 mParameters.associatedDisplayId = 0;
2390 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002391}
2392
Jeff Brown83c09682010-12-23 17:50:18 -08002393void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002394 dump.append(INDENT3 "Parameters:\n");
2395 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2396 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002397
2398 switch (mParameters.mode) {
2399 case Parameters::MODE_POINTER:
2400 dump.append(INDENT4 "Mode: pointer\n");
2401 break;
2402 case Parameters::MODE_NAVIGATION:
2403 dump.append(INDENT4 "Mode: navigation\n");
2404 break;
2405 default:
Steve Blockec193de2012-01-09 18:35:44 +00002406 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002407 }
2408
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002409 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2410 toString(mParameters.orientationAware));
2411}
2412
Jeff Brown65fd2512011-08-18 11:20:58 -07002413void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002414 mButtonState = 0;
2415 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002416
Jeff Brownbe1aa822011-07-27 16:04:54 -07002417 mPointerVelocityControl.reset();
2418 mWheelXVelocityControl.reset();
2419 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002420
Jeff Brown65fd2512011-08-18 11:20:58 -07002421 mCursorButtonAccumulator.reset(getDevice());
2422 mCursorMotionAccumulator.reset(getDevice());
2423 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002424
Jeff Brown65fd2512011-08-18 11:20:58 -07002425 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002426}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002427
Jeff Brown83c09682010-12-23 17:50:18 -08002428void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002429 mCursorButtonAccumulator.process(rawEvent);
2430 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002431 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002432
Jeff Brown49ccac52012-04-11 18:27:33 -07002433 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002434 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002435 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002436}
2437
Jeff Brown83c09682010-12-23 17:50:18 -08002438void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002439 int32_t lastButtonState = mButtonState;
2440 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2441 mButtonState = currentButtonState;
2442
2443 bool wasDown = isPointerDown(lastButtonState);
2444 bool down = isPointerDown(currentButtonState);
2445 bool downChanged;
2446 if (!wasDown && down) {
2447 mDownTime = when;
2448 downChanged = true;
2449 } else if (wasDown && !down) {
2450 downChanged = true;
2451 } else {
2452 downChanged = false;
2453 }
2454 nsecs_t downTime = mDownTime;
2455 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002456 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002457
2458 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2459 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2460 bool moved = deltaX != 0 || deltaY != 0;
2461
Jeff Brown65fd2512011-08-18 11:20:58 -07002462 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002463 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2464 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002465 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002466 }
2467
Jeff Brown65fd2512011-08-18 11:20:58 -07002468 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002469 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002470 pointerProperties.clear();
2471 pointerProperties.id = 0;
2472 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2473
Jeff Brown6328cdc2010-07-29 18:18:33 -07002474 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002475 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002476
Jeff Brown65fd2512011-08-18 11:20:58 -07002477 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2478 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002479 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002480
Jeff Brownbe1aa822011-07-27 16:04:54 -07002481 mWheelYVelocityControl.move(when, NULL, &vscroll);
2482 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002483
Jeff Brownbe1aa822011-07-27 16:04:54 -07002484 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002485
Jeff Brownbe1aa822011-07-27 16:04:54 -07002486 if (mPointerController != NULL) {
2487 if (moved || scrolled || buttonsChanged) {
2488 mPointerController->setPresentation(
2489 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002490
Jeff Brownbe1aa822011-07-27 16:04:54 -07002491 if (moved) {
2492 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002493 }
2494
Jeff Brownbe1aa822011-07-27 16:04:54 -07002495 if (buttonsChanged) {
2496 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002497 }
Jeff Brownefd32662011-03-08 15:13:06 -08002498
Jeff Brownbe1aa822011-07-27 16:04:54 -07002499 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002500 }
2501
Jeff Brownbe1aa822011-07-27 16:04:54 -07002502 float x, y;
2503 mPointerController->getPosition(&x, &y);
2504 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2505 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2506 } else {
2507 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2508 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2509 }
2510
2511 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002512
Jeff Brown56194eb2011-03-02 19:23:13 -08002513 // Moving an external trackball or mouse should wake the device.
2514 // We don't do this for internal cursor devices to prevent them from waking up
2515 // the device in your pocket.
2516 // TODO: Use the input device configuration to control this behavior more finely.
2517 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002518 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002519 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2520 }
2521
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002522 // Synthesize key down from buttons if needed.
2523 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2524 policyFlags, lastButtonState, currentButtonState);
2525
2526 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002527 if (downChanged || moved || scrolled || buttonsChanged) {
2528 int32_t metaState = mContext->getGlobalMetaState();
2529 int32_t motionEventAction;
2530 if (downChanged) {
2531 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2532 } else if (down || mPointerController == NULL) {
2533 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2534 } else {
2535 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2536 }
Jeff Brownb6997262010-10-08 22:31:17 -07002537
Jeff Brownbe1aa822011-07-27 16:04:54 -07002538 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2539 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002540 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002541 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002542
Jeff Brownbe1aa822011-07-27 16:04:54 -07002543 // Send hover move after UP to tell the application that the mouse is hovering now.
2544 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2545 && mPointerController != NULL) {
2546 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2547 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2548 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2549 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2550 getListener()->notifyMotion(&hoverArgs);
2551 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002552
Jeff Brownbe1aa822011-07-27 16:04:54 -07002553 // Send scroll events.
2554 if (scrolled) {
2555 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2556 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2557
2558 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2559 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2560 AMOTION_EVENT_EDGE_FLAG_NONE,
2561 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2562 getListener()->notifyMotion(&scrollArgs);
2563 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002564 }
Jeff Browna032cc02011-03-07 16:56:21 -08002565
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002566 // Synthesize key up from buttons if needed.
2567 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2568 policyFlags, lastButtonState, currentButtonState);
2569
Jeff Brown65fd2512011-08-18 11:20:58 -07002570 mCursorMotionAccumulator.finishSync();
2571 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002572}
2573
Jeff Brown83c09682010-12-23 17:50:18 -08002574int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002575 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2576 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2577 } else {
2578 return AKEY_STATE_UNKNOWN;
2579 }
2580}
2581
Jeff Brown05dc66a2011-03-02 14:41:58 -08002582void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002583 if (mPointerController != NULL) {
2584 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2585 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002586}
2587
Jeff Brown6d0fec22010-07-23 21:28:06 -07002588
2589// --- TouchInputMapper ---
2590
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002591TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002592 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002593 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002594 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002595}
2596
2597TouchInputMapper::~TouchInputMapper() {
2598}
2599
2600uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002601 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002602}
2603
2604void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2605 InputMapper::populateDeviceInfo(info);
2606
Jeff Brown65fd2512011-08-18 11:20:58 -07002607 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2608 info->addMotionRange(mOrientedRanges.x);
2609 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002610 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002611
Jeff Brown65fd2512011-08-18 11:20:58 -07002612 if (mOrientedRanges.haveSize) {
2613 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002614 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002615
2616 if (mOrientedRanges.haveTouchSize) {
2617 info->addMotionRange(mOrientedRanges.touchMajor);
2618 info->addMotionRange(mOrientedRanges.touchMinor);
2619 }
2620
2621 if (mOrientedRanges.haveToolSize) {
2622 info->addMotionRange(mOrientedRanges.toolMajor);
2623 info->addMotionRange(mOrientedRanges.toolMinor);
2624 }
2625
2626 if (mOrientedRanges.haveOrientation) {
2627 info->addMotionRange(mOrientedRanges.orientation);
2628 }
2629
2630 if (mOrientedRanges.haveDistance) {
2631 info->addMotionRange(mOrientedRanges.distance);
2632 }
2633
2634 if (mOrientedRanges.haveTilt) {
2635 info->addMotionRange(mOrientedRanges.tilt);
2636 }
2637
2638 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2639 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2640 }
2641 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2642 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2643 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002644 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002645}
2646
Jeff Brownef3d7e82010-09-30 14:33:04 -07002647void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002648 dump.append(INDENT2 "Touch Input Mapper:\n");
2649 dumpParameters(dump);
2650 dumpVirtualKeys(dump);
2651 dumpRawPointerAxes(dump);
2652 dumpCalibration(dump);
2653 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002654
Jeff Brownbe1aa822011-07-27 16:04:54 -07002655 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2656 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2657 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2658 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2659 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2660 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002661 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2662 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002663 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002664 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2665 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002666 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2667 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2668 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2669 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2670 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002671
Jeff Brownbe1aa822011-07-27 16:04:54 -07002672 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002673
Jeff Brownbe1aa822011-07-27 16:04:54 -07002674 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2675 mLastRawPointerData.pointerCount);
2676 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2677 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2678 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2679 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002680 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2681 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002682 pointer.id, pointer.x, pointer.y, pointer.pressure,
2683 pointer.touchMajor, pointer.touchMinor,
2684 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002685 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002686 pointer.toolType, toString(pointer.isHovering));
2687 }
2688
2689 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2690 mLastCookedPointerData.pointerCount);
2691 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2692 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2693 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2694 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2695 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002696 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2697 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002698 pointerProperties.id,
2699 pointerCoords.getX(),
2700 pointerCoords.getY(),
2701 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2702 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2703 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2704 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2705 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2706 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002707 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002708 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2709 pointerProperties.toolType,
2710 toString(mLastCookedPointerData.isHovering(i)));
2711 }
2712
Jeff Brown65fd2512011-08-18 11:20:58 -07002713 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002714 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2715 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002716 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002717 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002718 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002719 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002720 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002721 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002722 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002723 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2724 mPointerGestureMaxSwipeWidth);
2725 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002726}
2727
Jeff Brown65fd2512011-08-18 11:20:58 -07002728void TouchInputMapper::configure(nsecs_t when,
2729 const InputReaderConfiguration* config, uint32_t changes) {
2730 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002731
Jeff Brown474dcb52011-06-14 20:22:50 -07002732 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002733
Jeff Brown474dcb52011-06-14 20:22:50 -07002734 if (!changes) { // first time only
2735 // Configure basic parameters.
2736 configureParameters();
2737
Jeff Brown65fd2512011-08-18 11:20:58 -07002738 // Configure common accumulators.
2739 mCursorScrollAccumulator.configure(getDevice());
2740 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002741
2742 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002743 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002744
2745 // Prepare input device calibration.
2746 parseCalibration();
2747 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002748 }
2749
Jeff Brown474dcb52011-06-14 20:22:50 -07002750 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002751 // Update pointer speed.
2752 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2753 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2754 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002755 }
Jeff Brown8d608662010-08-30 03:02:23 -07002756
Jeff Brown65fd2512011-08-18 11:20:58 -07002757 bool resetNeeded = false;
2758 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002759 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2760 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002761 // Configure device sources, surface dimensions, orientation and
2762 // scaling factors.
2763 configureSurface(when, &resetNeeded);
2764 }
2765
2766 if (changes && resetNeeded) {
2767 // Send reset, unless this is the first time the device has been configured,
2768 // in which case the reader will call reset itself after all mappers are ready.
2769 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002770 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002771}
2772
Jeff Brown8d608662010-08-30 03:02:23 -07002773void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002774 // Use the pointer presentation mode for devices that do not support distinct
2775 // multitouch. The spot-based presentation relies on being able to accurately
2776 // locate two or more fingers on the touch pad.
2777 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2778 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002779
Jeff Brown538881e2011-05-25 18:23:38 -07002780 String8 gestureModeString;
2781 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2782 gestureModeString)) {
2783 if (gestureModeString == "pointer") {
2784 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2785 } else if (gestureModeString == "spots") {
2786 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2787 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002788 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002789 }
2790 }
2791
Jeff Browndeffe072011-08-26 18:38:46 -07002792 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2793 // The device is a touch screen.
2794 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2795 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2796 // The device is a pointing device like a track pad.
2797 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2798 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002799 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2800 // The device is a cursor device with a touch pad attached.
2801 // By default don't use the touch pad to move the pointer.
2802 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2803 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002804 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002805 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2806 }
2807
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002808 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002809 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2810 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002811 if (deviceTypeString == "touchScreen") {
2812 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002813 } else if (deviceTypeString == "touchPad") {
2814 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002815 } else if (deviceTypeString == "pointer") {
2816 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002817 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002818 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002819 }
2820 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002821
Jeff Brownefd32662011-03-08 15:13:06 -08002822 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002823 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2824 mParameters.orientationAware);
2825
Jeff Brownbc68a592011-07-25 12:58:12 -07002826 mParameters.associatedDisplayId = -1;
2827 mParameters.associatedDisplayIsExternal = false;
2828 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002829 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002830 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2831 mParameters.associatedDisplayIsExternal =
2832 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2833 && getDevice()->isExternal();
2834 mParameters.associatedDisplayId = 0;
2835 }
Jeff Brown8d608662010-08-30 03:02:23 -07002836}
2837
Jeff Brownef3d7e82010-09-30 14:33:04 -07002838void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002839 dump.append(INDENT3 "Parameters:\n");
2840
Jeff Brown538881e2011-05-25 18:23:38 -07002841 switch (mParameters.gestureMode) {
2842 case Parameters::GESTURE_MODE_POINTER:
2843 dump.append(INDENT4 "GestureMode: pointer\n");
2844 break;
2845 case Parameters::GESTURE_MODE_SPOTS:
2846 dump.append(INDENT4 "GestureMode: spots\n");
2847 break;
2848 default:
2849 assert(false);
2850 }
2851
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002852 switch (mParameters.deviceType) {
2853 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2854 dump.append(INDENT4 "DeviceType: touchScreen\n");
2855 break;
2856 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2857 dump.append(INDENT4 "DeviceType: touchPad\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 Brown65fd2512011-08-18 11:20:58 -07002866 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2867 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002868 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2869 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002870}
2871
Jeff Brownbe1aa822011-07-27 16:04:54 -07002872void TouchInputMapper::configureRawPointerAxes() {
2873 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002874}
2875
Jeff Brownbe1aa822011-07-27 16:04:54 -07002876void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2877 dump.append(INDENT3 "Raw Touch Axes:\n");
2878 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2879 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2880 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2881 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2882 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2883 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2884 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2885 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2886 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002887 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2888 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002889 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2890 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002891}
2892
Jeff Brown65fd2512011-08-18 11:20:58 -07002893void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2894 int32_t oldDeviceMode = mDeviceMode;
2895
2896 // Determine device mode.
2897 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2898 && mConfig.pointerGesturesEnabled) {
2899 mSource = AINPUT_SOURCE_MOUSE;
2900 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002901 if (hasStylus()) {
2902 mSource |= AINPUT_SOURCE_STYLUS;
2903 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002904 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2905 && mParameters.associatedDisplayId >= 0) {
2906 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2907 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002908 if (hasStylus()) {
2909 mSource |= AINPUT_SOURCE_STYLUS;
2910 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002911 } else {
2912 mSource = AINPUT_SOURCE_TOUCHPAD;
2913 mDeviceMode = DEVICE_MODE_UNSCALED;
2914 }
2915
Jeff Brown9626b142011-03-03 02:09:54 -08002916 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002917 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002918 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002919 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002920 mDeviceMode = DEVICE_MODE_DISABLED;
2921 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002922 }
2923
Jeff Brown65fd2512011-08-18 11:20:58 -07002924 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002925 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002926 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002927 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002928 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2929 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002930 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002931 "display %d. The device will be inoperable until the display size "
2932 "becomes available.",
2933 getDeviceName().string(), mParameters.associatedDisplayId);
2934 mDeviceMode = DEVICE_MODE_DISABLED;
2935 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002936 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002937 }
2938
Jeff Brown65fd2512011-08-18 11:20:58 -07002939 // Configure dimensions.
2940 int32_t width, height, orientation;
2941 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2942 width = mAssociatedDisplayWidth;
2943 height = mAssociatedDisplayHeight;
2944 orientation = mParameters.orientationAware ?
2945 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2946 } else {
2947 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2948 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2949 orientation = DISPLAY_ORIENTATION_0;
2950 }
2951
2952 // If moving between pointer modes, need to reset some state.
2953 bool deviceModeChanged;
2954 if (mDeviceMode != oldDeviceMode) {
2955 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002956 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002957 }
2958
Jeff Browndaf4a122011-08-26 17:14:14 -07002959 // Create pointer controller if needed.
2960 if (mDeviceMode == DEVICE_MODE_POINTER ||
2961 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2962 if (mPointerController == NULL) {
2963 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2964 }
2965 } else {
2966 mPointerController.clear();
2967 }
2968
Jeff Brownbe1aa822011-07-27 16:04:54 -07002969 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002970 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002971 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002972 }
2973
Jeff Brownbe1aa822011-07-27 16:04:54 -07002974 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002975 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002976 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002977 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002978
Jeff Brownbe1aa822011-07-27 16:04:54 -07002979 mSurfaceWidth = width;
2980 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002981
Jeff Brown8d608662010-08-30 03:02:23 -07002982 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002983 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2984 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2985 mXPrecision = 1.0f / mXScale;
2986 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002987
Jeff Brownbe1aa822011-07-27 16:04:54 -07002988 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002989 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002990 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002991 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002992
Jeff Brownbe1aa822011-07-27 16:04:54 -07002993 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002994
Jeff Brown8d608662010-08-30 03:02:23 -07002995 // Scale factor for terms that are not oriented in a particular axis.
2996 // If the pixels are square then xScale == yScale otherwise we fake it
2997 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002998 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002999
Jeff Brown8d608662010-08-30 03:02:23 -07003000 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07003001 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003002
Jeff Browna1f89ce2011-08-11 00:05:01 -07003003 // Size factors.
3004 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
3005 if (mRawPointerAxes.touchMajor.valid
3006 && mRawPointerAxes.touchMajor.maxValue != 0) {
3007 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
3008 } else if (mRawPointerAxes.toolMajor.valid
3009 && mRawPointerAxes.toolMajor.maxValue != 0) {
3010 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
3011 } else {
3012 mSizeScale = 0.0f;
3013 }
3014
Jeff Brownbe1aa822011-07-27 16:04:54 -07003015 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003016 mOrientedRanges.haveToolSize = true;
3017 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08003018
Jeff Brownbe1aa822011-07-27 16:04:54 -07003019 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003020 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003021 mOrientedRanges.touchMajor.min = 0;
3022 mOrientedRanges.touchMajor.max = diagonalSize;
3023 mOrientedRanges.touchMajor.flat = 0;
3024 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003025
Jeff Brownbe1aa822011-07-27 16:04:54 -07003026 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
3027 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08003028
Jeff Brownbe1aa822011-07-27 16:04:54 -07003029 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003030 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003031 mOrientedRanges.toolMajor.min = 0;
3032 mOrientedRanges.toolMajor.max = diagonalSize;
3033 mOrientedRanges.toolMajor.flat = 0;
3034 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003035
Jeff Brownbe1aa822011-07-27 16:04:54 -07003036 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3037 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003038
3039 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003040 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003041 mOrientedRanges.size.min = 0;
3042 mOrientedRanges.size.max = 1.0;
3043 mOrientedRanges.size.flat = 0;
3044 mOrientedRanges.size.fuzz = 0;
3045 } else {
3046 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003047 }
3048
3049 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003050 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003051 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3052 || mCalibration.pressureCalibration
3053 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3054 if (mCalibration.havePressureScale) {
3055 mPressureScale = mCalibration.pressureScale;
3056 } else if (mRawPointerAxes.pressure.valid
3057 && mRawPointerAxes.pressure.maxValue != 0) {
3058 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003059 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003060 }
Jeff Brown8d608662010-08-30 03:02:23 -07003061
Jeff Brown65fd2512011-08-18 11:20:58 -07003062 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3063 mOrientedRanges.pressure.source = mSource;
3064 mOrientedRanges.pressure.min = 0;
3065 mOrientedRanges.pressure.max = 1.0;
3066 mOrientedRanges.pressure.flat = 0;
3067 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003068
Jeff Brown65fd2512011-08-18 11:20:58 -07003069 // Tilt
3070 mTiltXCenter = 0;
3071 mTiltXScale = 0;
3072 mTiltYCenter = 0;
3073 mTiltYScale = 0;
3074 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3075 if (mHaveTilt) {
3076 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3077 mRawPointerAxes.tiltX.maxValue);
3078 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3079 mRawPointerAxes.tiltY.maxValue);
3080 mTiltXScale = M_PI / 180;
3081 mTiltYScale = M_PI / 180;
3082
3083 mOrientedRanges.haveTilt = true;
3084
3085 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3086 mOrientedRanges.tilt.source = mSource;
3087 mOrientedRanges.tilt.min = 0;
3088 mOrientedRanges.tilt.max = M_PI_2;
3089 mOrientedRanges.tilt.flat = 0;
3090 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003091 }
3092
Jeff Brown8d608662010-08-30 03:02:23 -07003093 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07003094 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003095 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003096 if (mHaveTilt) {
3097 mOrientedRanges.haveOrientation = true;
3098
3099 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3100 mOrientedRanges.orientation.source = mSource;
3101 mOrientedRanges.orientation.min = -M_PI;
3102 mOrientedRanges.orientation.max = M_PI;
3103 mOrientedRanges.orientation.flat = 0;
3104 mOrientedRanges.orientation.fuzz = 0;
3105 } else if (mCalibration.orientationCalibration !=
3106 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003107 if (mCalibration.orientationCalibration
3108 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003109 if (mRawPointerAxes.orientation.valid) {
3110 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
3111 mRawPointerAxes.orientation.maxValue);
3112 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
3113 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07003114 }
3115 }
3116
Jeff Brownbe1aa822011-07-27 16:04:54 -07003117 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003118
Jeff Brownbe1aa822011-07-27 16:04:54 -07003119 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003120 mOrientedRanges.orientation.source = mSource;
3121 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003122 mOrientedRanges.orientation.max = M_PI_2;
3123 mOrientedRanges.orientation.flat = 0;
3124 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003125 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003126
3127 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003128 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003129 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3130 if (mCalibration.distanceCalibration
3131 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3132 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003133 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003134 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003135 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003136 }
3137 }
3138
Jeff Brownbe1aa822011-07-27 16:04:54 -07003139 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003140
Jeff Brownbe1aa822011-07-27 16:04:54 -07003141 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003142 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003143 mOrientedRanges.distance.min =
3144 mRawPointerAxes.distance.minValue * mDistanceScale;
3145 mOrientedRanges.distance.max =
3146 mRawPointerAxes.distance.minValue * mDistanceScale;
3147 mOrientedRanges.distance.flat = 0;
3148 mOrientedRanges.distance.fuzz =
3149 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003150 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003151 }
3152
Jeff Brown65fd2512011-08-18 11:20:58 -07003153 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08003154 // Compute oriented surface dimensions, precision, scales and ranges.
3155 // Note that the maximum value reported is an inclusive maximum value so it is one
3156 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003157 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003158 case DISPLAY_ORIENTATION_90:
3159 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003160 mOrientedSurfaceWidth = mSurfaceHeight;
3161 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08003162
Jeff Brownbe1aa822011-07-27 16:04:54 -07003163 mOrientedXPrecision = mYPrecision;
3164 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003165
Jeff Brownbe1aa822011-07-27 16:04:54 -07003166 mOrientedRanges.x.min = 0;
3167 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3168 * mYScale;
3169 mOrientedRanges.x.flat = 0;
3170 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003171
Jeff Brownbe1aa822011-07-27 16:04:54 -07003172 mOrientedRanges.y.min = 0;
3173 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3174 * mXScale;
3175 mOrientedRanges.y.flat = 0;
3176 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003177 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003178
Jeff Brown6d0fec22010-07-23 21:28:06 -07003179 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003180 mOrientedSurfaceWidth = mSurfaceWidth;
3181 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08003182
Jeff Brownbe1aa822011-07-27 16:04:54 -07003183 mOrientedXPrecision = mXPrecision;
3184 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003185
Jeff Brownbe1aa822011-07-27 16:04:54 -07003186 mOrientedRanges.x.min = 0;
3187 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3188 * mXScale;
3189 mOrientedRanges.x.flat = 0;
3190 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003191
Jeff Brownbe1aa822011-07-27 16:04:54 -07003192 mOrientedRanges.y.min = 0;
3193 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3194 * mYScale;
3195 mOrientedRanges.y.flat = 0;
3196 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003197 break;
3198 }
Jeff Brownace13b12011-03-09 17:39:48 -08003199
3200 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003201 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003202 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3203 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07003204 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003205 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
3206 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003207
Jeff Brown2352b972011-04-12 22:39:53 -07003208 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003209 // given area relative to the diagonal size of the display when no acceleration
3210 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003211 // Assume that the touch pad has a square aspect ratio such that movements in
3212 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003213 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003214 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003215 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003216
3217 // Scale zooms to cover a smaller range of the display than movements do.
3218 // This value determines the area around the pointer that is affected by freeform
3219 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003220 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003221 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003222 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003223
Jeff Brown2352b972011-04-12 22:39:53 -07003224 // Max width between pointers to detect a swipe gesture is more than some fraction
3225 // of the diagonal axis of the touch pad. Touches that are wider than this are
3226 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003227 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003228 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003229 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003230
Jeff Brown65fd2512011-08-18 11:20:58 -07003231 // Abort current pointer usages because the state has changed.
3232 abortPointerUsage(when, 0 /*policyFlags*/);
3233
3234 // Inform the dispatcher about the changes.
3235 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003236 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003237 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003238}
3239
Jeff Brownbe1aa822011-07-27 16:04:54 -07003240void TouchInputMapper::dumpSurface(String8& dump) {
3241 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3242 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
3243 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003244}
3245
Jeff Brownbe1aa822011-07-27 16:04:54 -07003246void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003247 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003248 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003249
Jeff Brownbe1aa822011-07-27 16:04:54 -07003250 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003251
Jeff Brown6328cdc2010-07-29 18:18:33 -07003252 if (virtualKeyDefinitions.size() == 0) {
3253 return;
3254 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003255
Jeff Brownbe1aa822011-07-27 16:04:54 -07003256 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003257
Jeff Brownbe1aa822011-07-27 16:04:54 -07003258 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3259 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3260 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3261 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003262
3263 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003264 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003265 virtualKeyDefinitions[i];
3266
Jeff Brownbe1aa822011-07-27 16:04:54 -07003267 mVirtualKeys.add();
3268 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003269
3270 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3271 int32_t keyCode;
3272 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003273 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003274 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003275 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003276 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003277 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003278 }
3279
Jeff Brown6328cdc2010-07-29 18:18:33 -07003280 virtualKey.keyCode = keyCode;
3281 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003282
Jeff Brown6328cdc2010-07-29 18:18:33 -07003283 // convert the key definition's display coordinates into touch coordinates for a hit box
3284 int32_t halfWidth = virtualKeyDefinition.width / 2;
3285 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003286
Jeff Brown6328cdc2010-07-29 18:18:33 -07003287 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003288 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003289 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003290 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003291 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003292 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003293 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003294 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003295 }
3296}
3297
Jeff Brownbe1aa822011-07-27 16:04:54 -07003298void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3299 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003300 dump.append(INDENT3 "Virtual Keys:\n");
3301
Jeff Brownbe1aa822011-07-27 16:04:54 -07003302 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3303 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003304 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3305 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3306 i, virtualKey.scanCode, virtualKey.keyCode,
3307 virtualKey.hitLeft, virtualKey.hitRight,
3308 virtualKey.hitTop, virtualKey.hitBottom);
3309 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003310 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003311}
3312
Jeff Brown8d608662010-08-30 03:02:23 -07003313void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003314 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003315 Calibration& out = mCalibration;
3316
Jeff Browna1f89ce2011-08-11 00:05:01 -07003317 // Size
3318 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3319 String8 sizeCalibrationString;
3320 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3321 if (sizeCalibrationString == "none") {
3322 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3323 } else if (sizeCalibrationString == "geometric") {
3324 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3325 } else if (sizeCalibrationString == "diameter") {
3326 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3327 } else if (sizeCalibrationString == "area") {
3328 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3329 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003330 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003331 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003332 }
3333 }
3334
Jeff Browna1f89ce2011-08-11 00:05:01 -07003335 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3336 out.sizeScale);
3337 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3338 out.sizeBias);
3339 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3340 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003341
3342 // Pressure
3343 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3344 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003345 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003346 if (pressureCalibrationString == "none") {
3347 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3348 } else if (pressureCalibrationString == "physical") {
3349 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3350 } else if (pressureCalibrationString == "amplitude") {
3351 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3352 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003353 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003354 pressureCalibrationString.string());
3355 }
3356 }
3357
Jeff Brown8d608662010-08-30 03:02:23 -07003358 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3359 out.pressureScale);
3360
Jeff Brown8d608662010-08-30 03:02:23 -07003361 // Orientation
3362 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3363 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003364 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003365 if (orientationCalibrationString == "none") {
3366 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3367 } else if (orientationCalibrationString == "interpolated") {
3368 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003369 } else if (orientationCalibrationString == "vector") {
3370 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003371 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003372 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003373 orientationCalibrationString.string());
3374 }
3375 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003376
3377 // Distance
3378 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3379 String8 distanceCalibrationString;
3380 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3381 if (distanceCalibrationString == "none") {
3382 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3383 } else if (distanceCalibrationString == "scaled") {
3384 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3385 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003386 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003387 distanceCalibrationString.string());
3388 }
3389 }
3390
3391 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3392 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003393}
3394
3395void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003396 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003397 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3398 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3399 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003400 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003401 } else {
3402 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3403 }
Jeff Brown8d608662010-08-30 03:02:23 -07003404
Jeff Browna1f89ce2011-08-11 00:05:01 -07003405 // Pressure
3406 if (mRawPointerAxes.pressure.valid) {
3407 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3408 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3409 }
3410 } else {
3411 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003412 }
3413
3414 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003415 if (mRawPointerAxes.orientation.valid) {
3416 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003417 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003418 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003419 } else {
3420 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003421 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003422
3423 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003424 if (mRawPointerAxes.distance.valid) {
3425 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003426 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003427 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003428 } else {
3429 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003430 }
Jeff Brown8d608662010-08-30 03:02:23 -07003431}
3432
Jeff Brownef3d7e82010-09-30 14:33:04 -07003433void TouchInputMapper::dumpCalibration(String8& dump) {
3434 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003435
Jeff Browna1f89ce2011-08-11 00:05:01 -07003436 // Size
3437 switch (mCalibration.sizeCalibration) {
3438 case Calibration::SIZE_CALIBRATION_NONE:
3439 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003440 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003441 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3442 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003443 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003444 case Calibration::SIZE_CALIBRATION_DIAMETER:
3445 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3446 break;
3447 case Calibration::SIZE_CALIBRATION_AREA:
3448 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003449 break;
3450 default:
Steve Blockec193de2012-01-09 18:35:44 +00003451 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003452 }
3453
Jeff Browna1f89ce2011-08-11 00:05:01 -07003454 if (mCalibration.haveSizeScale) {
3455 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3456 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003457 }
3458
Jeff Browna1f89ce2011-08-11 00:05:01 -07003459 if (mCalibration.haveSizeBias) {
3460 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3461 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003462 }
3463
Jeff Browna1f89ce2011-08-11 00:05:01 -07003464 if (mCalibration.haveSizeIsSummed) {
3465 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3466 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003467 }
3468
3469 // Pressure
3470 switch (mCalibration.pressureCalibration) {
3471 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003472 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003473 break;
3474 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003475 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003476 break;
3477 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003478 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003479 break;
3480 default:
Steve Blockec193de2012-01-09 18:35:44 +00003481 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003482 }
3483
Jeff Brown8d608662010-08-30 03:02:23 -07003484 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003485 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3486 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003487 }
3488
Jeff Brown8d608662010-08-30 03:02:23 -07003489 // Orientation
3490 switch (mCalibration.orientationCalibration) {
3491 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003492 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003493 break;
3494 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003495 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003496 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003497 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3498 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3499 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003500 default:
Steve Blockec193de2012-01-09 18:35:44 +00003501 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003502 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003503
3504 // Distance
3505 switch (mCalibration.distanceCalibration) {
3506 case Calibration::DISTANCE_CALIBRATION_NONE:
3507 dump.append(INDENT4 "touch.distance.calibration: none\n");
3508 break;
3509 case Calibration::DISTANCE_CALIBRATION_SCALED:
3510 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3511 break;
3512 default:
Steve Blockec193de2012-01-09 18:35:44 +00003513 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003514 }
3515
3516 if (mCalibration.haveDistanceScale) {
3517 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3518 mCalibration.distanceScale);
3519 }
Jeff Brown8d608662010-08-30 03:02:23 -07003520}
3521
Jeff Brown65fd2512011-08-18 11:20:58 -07003522void TouchInputMapper::reset(nsecs_t when) {
3523 mCursorButtonAccumulator.reset(getDevice());
3524 mCursorScrollAccumulator.reset(getDevice());
3525 mTouchButtonAccumulator.reset(getDevice());
3526
3527 mPointerVelocityControl.reset();
3528 mWheelXVelocityControl.reset();
3529 mWheelYVelocityControl.reset();
3530
Jeff Brownbe1aa822011-07-27 16:04:54 -07003531 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003532 mLastRawPointerData.clear();
3533 mCurrentCookedPointerData.clear();
3534 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003535 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003536 mLastButtonState = 0;
3537 mCurrentRawVScroll = 0;
3538 mCurrentRawHScroll = 0;
3539 mCurrentFingerIdBits.clear();
3540 mLastFingerIdBits.clear();
3541 mCurrentStylusIdBits.clear();
3542 mLastStylusIdBits.clear();
3543 mCurrentMouseIdBits.clear();
3544 mLastMouseIdBits.clear();
3545 mPointerUsage = POINTER_USAGE_NONE;
3546 mSentHoverEnter = false;
3547 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003548
Jeff Brown65fd2512011-08-18 11:20:58 -07003549 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003550
Jeff Brown65fd2512011-08-18 11:20:58 -07003551 mPointerGesture.reset();
3552 mPointerSimple.reset();
3553
3554 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003555 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3556 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003557 }
3558
Jeff Brown65fd2512011-08-18 11:20:58 -07003559 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003560}
3561
Jeff Brown65fd2512011-08-18 11:20:58 -07003562void TouchInputMapper::process(const RawEvent* rawEvent) {
3563 mCursorButtonAccumulator.process(rawEvent);
3564 mCursorScrollAccumulator.process(rawEvent);
3565 mTouchButtonAccumulator.process(rawEvent);
3566
Jeff Brown49ccac52012-04-11 18:27:33 -07003567 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003568 sync(rawEvent->when);
3569 }
3570}
3571
3572void TouchInputMapper::sync(nsecs_t when) {
3573 // Sync button state.
3574 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3575 | mCursorButtonAccumulator.getButtonState();
3576
3577 // Sync scroll state.
3578 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3579 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3580 mCursorScrollAccumulator.finishSync();
3581
3582 // Sync touch state.
3583 bool havePointerIds = true;
3584 mCurrentRawPointerData.clear();
3585 syncTouch(when, &havePointerIds);
3586
Jeff Brownaa3855d2011-03-17 01:34:19 -07003587#if DEBUG_RAW_EVENTS
3588 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003589 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003590 mLastRawPointerData.pointerCount,
3591 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003592 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003593 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003594 "hovering ids 0x%08x -> 0x%08x",
3595 mLastRawPointerData.pointerCount,
3596 mCurrentRawPointerData.pointerCount,
3597 mLastRawPointerData.touchingIdBits.value,
3598 mCurrentRawPointerData.touchingIdBits.value,
3599 mLastRawPointerData.hoveringIdBits.value,
3600 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003601 }
3602#endif
3603
Jeff Brown65fd2512011-08-18 11:20:58 -07003604 // Reset state that we will compute below.
3605 mCurrentFingerIdBits.clear();
3606 mCurrentStylusIdBits.clear();
3607 mCurrentMouseIdBits.clear();
3608 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003609
Jeff Brown65fd2512011-08-18 11:20:58 -07003610 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3611 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003612 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003613 mCurrentButtonState = 0;
3614 } else {
3615 // Preprocess pointer data.
3616 if (!havePointerIds) {
3617 assignPointerIds();
3618 }
3619
3620 // Handle policy on initial down or hover events.
3621 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003622 bool initialDown = mLastRawPointerData.pointerCount == 0
3623 && mCurrentRawPointerData.pointerCount != 0;
3624 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3625 if (initialDown || buttonsPressed) {
3626 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003627 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003628 getContext()->fadePointer();
3629 }
3630
3631 // Initial downs on external touch devices should wake the device.
3632 // We don't do this for internal touch screens to prevent them from waking
3633 // up in your pocket.
3634 // TODO: Use the input device configuration to control this behavior more finely.
3635 if (getDevice()->isExternal()) {
3636 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3637 }
3638 }
3639
3640 // Synthesize key down from raw buttons if needed.
3641 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3642 policyFlags, mLastButtonState, mCurrentButtonState);
3643
3644 // Consume raw off-screen touches before cooking pointer data.
3645 // If touches are consumed, subsequent code will not receive any pointer data.
3646 if (consumeRawTouches(when, policyFlags)) {
3647 mCurrentRawPointerData.clear();
3648 }
3649
3650 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3651 // with cooked pointer data that has the same ids and indices as the raw data.
3652 // The following code can use either the raw or cooked data, as needed.
3653 cookPointerData();
3654
3655 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003656 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003657 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3658 uint32_t id = idBits.clearFirstMarkedBit();
3659 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3660 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3661 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3662 mCurrentStylusIdBits.markBit(id);
3663 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3664 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3665 mCurrentFingerIdBits.markBit(id);
3666 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3667 mCurrentMouseIdBits.markBit(id);
3668 }
3669 }
3670 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3671 uint32_t id = idBits.clearFirstMarkedBit();
3672 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3673 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3674 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3675 mCurrentStylusIdBits.markBit(id);
3676 }
3677 }
3678
3679 // Stylus takes precedence over all tools, then mouse, then finger.
3680 PointerUsage pointerUsage = mPointerUsage;
3681 if (!mCurrentStylusIdBits.isEmpty()) {
3682 mCurrentMouseIdBits.clear();
3683 mCurrentFingerIdBits.clear();
3684 pointerUsage = POINTER_USAGE_STYLUS;
3685 } else if (!mCurrentMouseIdBits.isEmpty()) {
3686 mCurrentFingerIdBits.clear();
3687 pointerUsage = POINTER_USAGE_MOUSE;
3688 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3689 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003690 }
3691
3692 dispatchPointerUsage(when, policyFlags, pointerUsage);
3693 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003694 if (mDeviceMode == DEVICE_MODE_DIRECT
3695 && mConfig.showTouches && mPointerController != NULL) {
3696 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3697 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3698
3699 mPointerController->setButtonState(mCurrentButtonState);
3700 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3701 mCurrentCookedPointerData.idToIndex,
3702 mCurrentCookedPointerData.touchingIdBits);
3703 }
3704
Jeff Brown65fd2512011-08-18 11:20:58 -07003705 dispatchHoverExit(when, policyFlags);
3706 dispatchTouches(when, policyFlags);
3707 dispatchHoverEnterAndMove(when, policyFlags);
3708 }
3709
3710 // Synthesize key up from raw buttons if needed.
3711 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3712 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003713 }
3714
Jeff Brown6328cdc2010-07-29 18:18:33 -07003715 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003716 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3717 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3718 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003719 mLastFingerIdBits = mCurrentFingerIdBits;
3720 mLastStylusIdBits = mCurrentStylusIdBits;
3721 mLastMouseIdBits = mCurrentMouseIdBits;
3722
3723 // Clear some transient state.
3724 mCurrentRawVScroll = 0;
3725 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003726}
3727
Jeff Brown79ac9692011-04-19 21:20:10 -07003728void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003729 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003730 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3731 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3732 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003733 }
3734}
3735
Jeff Brownbe1aa822011-07-27 16:04:54 -07003736bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3737 // Check for release of a virtual key.
3738 if (mCurrentVirtualKey.down) {
3739 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3740 // Pointer went up while virtual key was down.
3741 mCurrentVirtualKey.down = false;
3742 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003743#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003744 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003745 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003746#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003747 dispatchVirtualKey(when, policyFlags,
3748 AKEY_EVENT_ACTION_UP,
3749 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003750 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003751 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003752 }
3753
Jeff Brownbe1aa822011-07-27 16:04:54 -07003754 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3755 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3756 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3757 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3758 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3759 // Pointer is still within the space of the virtual key.
3760 return true;
3761 }
3762 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003763
Jeff Brownbe1aa822011-07-27 16:04:54 -07003764 // Pointer left virtual key area or another pointer also went down.
3765 // Send key cancellation but do not consume the touch yet.
3766 // This is useful when the user swipes through from the virtual key area
3767 // into the main display surface.
3768 mCurrentVirtualKey.down = false;
3769 if (!mCurrentVirtualKey.ignored) {
3770#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003771 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003772 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3773#endif
3774 dispatchVirtualKey(when, policyFlags,
3775 AKEY_EVENT_ACTION_UP,
3776 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3777 | AKEY_EVENT_FLAG_CANCELED);
3778 }
3779 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003780
Jeff Brownbe1aa822011-07-27 16:04:54 -07003781 if (mLastRawPointerData.touchingIdBits.isEmpty()
3782 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3783 // Pointer just went down. Check for virtual key press or off-screen touches.
3784 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3785 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3786 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3787 // If exactly one pointer went down, check for virtual key hit.
3788 // Otherwise we will drop the entire stroke.
3789 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3790 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3791 if (virtualKey) {
3792 mCurrentVirtualKey.down = true;
3793 mCurrentVirtualKey.downTime = when;
3794 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3795 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3796 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3797 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3798
3799 if (!mCurrentVirtualKey.ignored) {
3800#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003801 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003802 mCurrentVirtualKey.keyCode,
3803 mCurrentVirtualKey.scanCode);
3804#endif
3805 dispatchVirtualKey(when, policyFlags,
3806 AKEY_EVENT_ACTION_DOWN,
3807 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3808 }
3809 }
3810 }
3811 return true;
3812 }
3813 }
3814
Jeff Brownfe508922011-01-18 15:10:10 -08003815 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003816 // most recent touch within the screen area. The idea is to filter out stray
3817 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003818 //
3819 // Problems we're trying to solve:
3820 //
3821 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3822 // virtual key area that is implemented by a separate touch panel and accidentally
3823 // triggers a virtual key.
3824 //
3825 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3826 // area and accidentally triggers a virtual key. This often happens when virtual keys
3827 // are layed out below the screen near to where the on screen keyboard's space bar
3828 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003829 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003830 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003831 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003832 return false;
3833}
3834
3835void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3836 int32_t keyEventAction, int32_t keyEventFlags) {
3837 int32_t keyCode = mCurrentVirtualKey.keyCode;
3838 int32_t scanCode = mCurrentVirtualKey.scanCode;
3839 nsecs_t downTime = mCurrentVirtualKey.downTime;
3840 int32_t metaState = mContext->getGlobalMetaState();
3841 policyFlags |= POLICY_FLAG_VIRTUAL;
3842
3843 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3844 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3845 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003846}
3847
Jeff Brown6d0fec22010-07-23 21:28:06 -07003848void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003849 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3850 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003851 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003852 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003853
3854 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003855 if (!currentIdBits.isEmpty()) {
3856 // No pointer id changes so this is a move event.
3857 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003858 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003859 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3860 AMOTION_EVENT_EDGE_FLAG_NONE,
3861 mCurrentCookedPointerData.pointerProperties,
3862 mCurrentCookedPointerData.pointerCoords,
3863 mCurrentCookedPointerData.idToIndex,
3864 currentIdBits, -1,
3865 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3866 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003867 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003868 // There may be pointers going up and pointers going down and pointers moving
3869 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003870 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3871 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003872 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003873 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003874
Jeff Brownace13b12011-03-09 17:39:48 -08003875 // Update last coordinates of pointers that have moved so that we observe the new
3876 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003877 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003878 mCurrentCookedPointerData.pointerProperties,
3879 mCurrentCookedPointerData.pointerCoords,
3880 mCurrentCookedPointerData.idToIndex,
3881 mLastCookedPointerData.pointerProperties,
3882 mLastCookedPointerData.pointerCoords,
3883 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003884 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003885 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003886 moveNeeded = true;
3887 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003888
Jeff Brownace13b12011-03-09 17:39:48 -08003889 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003890 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003891 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003892
Jeff Brown65fd2512011-08-18 11:20:58 -07003893 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003894 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003895 mLastCookedPointerData.pointerProperties,
3896 mLastCookedPointerData.pointerCoords,
3897 mLastCookedPointerData.idToIndex,
3898 dispatchedIdBits, upId,
3899 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003900 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003901 }
3902
Jeff Brownc3db8582010-10-20 15:33:38 -07003903 // Dispatch move events if any of the remaining pointers moved from their old locations.
3904 // Although applications receive new locations as part of individual pointer up
3905 // events, they do not generally handle them except when presented in a move event.
3906 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003907 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003908 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003909 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003910 mCurrentCookedPointerData.pointerProperties,
3911 mCurrentCookedPointerData.pointerCoords,
3912 mCurrentCookedPointerData.idToIndex,
3913 dispatchedIdBits, -1,
3914 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003915 }
3916
3917 // Dispatch pointer down events using the new pointer locations.
3918 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003919 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003920 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003921
Jeff Brownace13b12011-03-09 17:39:48 -08003922 if (dispatchedIdBits.count() == 1) {
3923 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003924 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003925 }
3926
Jeff Brown65fd2512011-08-18 11:20:58 -07003927 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003928 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003929 mCurrentCookedPointerData.pointerProperties,
3930 mCurrentCookedPointerData.pointerCoords,
3931 mCurrentCookedPointerData.idToIndex,
3932 dispatchedIdBits, downId,
3933 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003934 }
3935 }
Jeff Brownace13b12011-03-09 17:39:48 -08003936}
3937
Jeff Brownbe1aa822011-07-27 16:04:54 -07003938void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3939 if (mSentHoverEnter &&
3940 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3941 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3942 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003943 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003944 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3945 mLastCookedPointerData.pointerProperties,
3946 mLastCookedPointerData.pointerCoords,
3947 mLastCookedPointerData.idToIndex,
3948 mLastCookedPointerData.hoveringIdBits, -1,
3949 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3950 mSentHoverEnter = false;
3951 }
3952}
Jeff Brownace13b12011-03-09 17:39:48 -08003953
Jeff Brownbe1aa822011-07-27 16:04:54 -07003954void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3955 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3956 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3957 int32_t metaState = getContext()->getGlobalMetaState();
3958 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003959 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003960 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3961 mCurrentCookedPointerData.pointerProperties,
3962 mCurrentCookedPointerData.pointerCoords,
3963 mCurrentCookedPointerData.idToIndex,
3964 mCurrentCookedPointerData.hoveringIdBits, -1,
3965 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3966 mSentHoverEnter = true;
3967 }
Jeff Brownace13b12011-03-09 17:39:48 -08003968
Jeff Brown65fd2512011-08-18 11:20:58 -07003969 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003970 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3971 mCurrentCookedPointerData.pointerProperties,
3972 mCurrentCookedPointerData.pointerCoords,
3973 mCurrentCookedPointerData.idToIndex,
3974 mCurrentCookedPointerData.hoveringIdBits, -1,
3975 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3976 }
3977}
3978
3979void TouchInputMapper::cookPointerData() {
3980 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3981
3982 mCurrentCookedPointerData.clear();
3983 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3984 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3985 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3986
3987 // Walk through the the active pointers and map device coordinates onto
3988 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003989 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003990 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003991
Jeff Browna1f89ce2011-08-11 00:05:01 -07003992 // Size
3993 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3994 switch (mCalibration.sizeCalibration) {
3995 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3996 case Calibration::SIZE_CALIBRATION_DIAMETER:
3997 case Calibration::SIZE_CALIBRATION_AREA:
3998 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3999 touchMajor = in.touchMajor;
4000 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
4001 toolMajor = in.toolMajor;
4002 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
4003 size = mRawPointerAxes.touchMinor.valid
4004 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4005 } else if (mRawPointerAxes.touchMajor.valid) {
4006 toolMajor = touchMajor = in.touchMajor;
4007 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
4008 ? in.touchMinor : in.touchMajor;
4009 size = mRawPointerAxes.touchMinor.valid
4010 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4011 } else if (mRawPointerAxes.toolMajor.valid) {
4012 touchMajor = toolMajor = in.toolMajor;
4013 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
4014 ? in.toolMinor : in.toolMajor;
4015 size = mRawPointerAxes.toolMinor.valid
4016 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004017 } else {
Steve Blockec193de2012-01-09 18:35:44 +00004018 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07004019 "Size calibration should have been resolved to NONE.");
4020 touchMajor = 0;
4021 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004022 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004023 toolMinor = 0;
4024 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004025 }
Jeff Brownace13b12011-03-09 17:39:48 -08004026
Jeff Browna1f89ce2011-08-11 00:05:01 -07004027 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
4028 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
4029 if (touchingCount > 1) {
4030 touchMajor /= touchingCount;
4031 touchMinor /= touchingCount;
4032 toolMajor /= touchingCount;
4033 toolMinor /= touchingCount;
4034 size /= touchingCount;
4035 }
4036 }
Jeff Brownace13b12011-03-09 17:39:48 -08004037
Jeff Browna1f89ce2011-08-11 00:05:01 -07004038 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4039 touchMajor *= mGeometricScale;
4040 touchMinor *= mGeometricScale;
4041 toolMajor *= mGeometricScale;
4042 toolMinor *= mGeometricScale;
4043 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4044 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004045 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004046 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4047 toolMinor = toolMajor;
4048 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4049 touchMinor = touchMajor;
4050 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004051 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004052
4053 mCalibration.applySizeScaleAndBias(&touchMajor);
4054 mCalibration.applySizeScaleAndBias(&touchMinor);
4055 mCalibration.applySizeScaleAndBias(&toolMajor);
4056 mCalibration.applySizeScaleAndBias(&toolMinor);
4057 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004058 break;
4059 default:
4060 touchMajor = 0;
4061 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004062 toolMajor = 0;
4063 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004064 size = 0;
4065 break;
4066 }
4067
Jeff Browna1f89ce2011-08-11 00:05:01 -07004068 // Pressure
4069 float pressure;
4070 switch (mCalibration.pressureCalibration) {
4071 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4072 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4073 pressure = in.pressure * mPressureScale;
4074 break;
4075 default:
4076 pressure = in.isHovering ? 0 : 1;
4077 break;
4078 }
4079
Jeff Brown65fd2512011-08-18 11:20:58 -07004080 // Tilt and Orientation
4081 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004082 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004083 if (mHaveTilt) {
4084 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4085 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4086 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4087 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4088 } else {
4089 tilt = 0;
4090
4091 switch (mCalibration.orientationCalibration) {
4092 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
4093 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
4094 break;
4095 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4096 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4097 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4098 if (c1 != 0 || c2 != 0) {
4099 orientation = atan2f(c1, c2) * 0.5f;
4100 float confidence = hypotf(c1, c2);
4101 float scale = 1.0f + confidence / 16.0f;
4102 touchMajor *= scale;
4103 touchMinor /= scale;
4104 toolMajor *= scale;
4105 toolMinor /= scale;
4106 } else {
4107 orientation = 0;
4108 }
4109 break;
4110 }
4111 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004112 orientation = 0;
4113 }
Jeff Brownace13b12011-03-09 17:39:48 -08004114 }
4115
Jeff Brown80fd47c2011-05-24 01:07:44 -07004116 // Distance
4117 float distance;
4118 switch (mCalibration.distanceCalibration) {
4119 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004120 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004121 break;
4122 default:
4123 distance = 0;
4124 }
4125
Jeff Brownace13b12011-03-09 17:39:48 -08004126 // X and Y
4127 // Adjust coords for surface orientation.
4128 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004129 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004130 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004131 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
4132 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004133 orientation -= M_PI_2;
4134 if (orientation < - M_PI_2) {
4135 orientation += M_PI;
4136 }
4137 break;
4138 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004139 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
4140 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004141 break;
4142 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004143 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
4144 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004145 orientation += M_PI_2;
4146 if (orientation > M_PI_2) {
4147 orientation -= M_PI;
4148 }
4149 break;
4150 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004151 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
4152 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004153 break;
4154 }
4155
4156 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004157 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004158 out.clear();
4159 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4160 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4161 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4162 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4163 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4164 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4165 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4166 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4167 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004168 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004169 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004170
4171 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004172 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4173 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004174 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004175 properties.id = id;
4176 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004177
Jeff Brownbe1aa822011-07-27 16:04:54 -07004178 // Write id index.
4179 mCurrentCookedPointerData.idToIndex[id] = i;
4180 }
Jeff Brownace13b12011-03-09 17:39:48 -08004181}
4182
Jeff Brown65fd2512011-08-18 11:20:58 -07004183void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4184 PointerUsage pointerUsage) {
4185 if (pointerUsage != mPointerUsage) {
4186 abortPointerUsage(when, policyFlags);
4187 mPointerUsage = pointerUsage;
4188 }
4189
4190 switch (mPointerUsage) {
4191 case POINTER_USAGE_GESTURES:
4192 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4193 break;
4194 case POINTER_USAGE_STYLUS:
4195 dispatchPointerStylus(when, policyFlags);
4196 break;
4197 case POINTER_USAGE_MOUSE:
4198 dispatchPointerMouse(when, policyFlags);
4199 break;
4200 default:
4201 break;
4202 }
4203}
4204
4205void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4206 switch (mPointerUsage) {
4207 case POINTER_USAGE_GESTURES:
4208 abortPointerGestures(when, policyFlags);
4209 break;
4210 case POINTER_USAGE_STYLUS:
4211 abortPointerStylus(when, policyFlags);
4212 break;
4213 case POINTER_USAGE_MOUSE:
4214 abortPointerMouse(when, policyFlags);
4215 break;
4216 default:
4217 break;
4218 }
4219
4220 mPointerUsage = POINTER_USAGE_NONE;
4221}
4222
Jeff Brown79ac9692011-04-19 21:20:10 -07004223void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4224 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004225 // Update current gesture coordinates.
4226 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004227 bool sendEvents = preparePointerGestures(when,
4228 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4229 if (!sendEvents) {
4230 return;
4231 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004232 if (finishPreviousGesture) {
4233 cancelPreviousGesture = false;
4234 }
Jeff Brownace13b12011-03-09 17:39:48 -08004235
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004236 // Update the pointer presentation and spots.
4237 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4238 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4239 if (finishPreviousGesture || cancelPreviousGesture) {
4240 mPointerController->clearSpots();
4241 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004242 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4243 mPointerGesture.currentGestureIdToIndex,
4244 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004245 } else {
4246 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4247 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004248
Jeff Brown538881e2011-05-25 18:23:38 -07004249 // Show or hide the pointer if needed.
4250 switch (mPointerGesture.currentGestureMode) {
4251 case PointerGesture::NEUTRAL:
4252 case PointerGesture::QUIET:
4253 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4254 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4255 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4256 // Remind the user of where the pointer is after finishing a gesture with spots.
4257 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4258 }
4259 break;
4260 case PointerGesture::TAP:
4261 case PointerGesture::TAP_DRAG:
4262 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4263 case PointerGesture::HOVER:
4264 case PointerGesture::PRESS:
4265 // Unfade the pointer when the current gesture manipulates the
4266 // area directly under the pointer.
4267 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4268 break;
4269 case PointerGesture::SWIPE:
4270 case PointerGesture::FREEFORM:
4271 // Fade the pointer when the current gesture manipulates a different
4272 // area and there are spots to guide the user experience.
4273 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4274 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4275 } else {
4276 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4277 }
4278 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004279 }
4280
Jeff Brownace13b12011-03-09 17:39:48 -08004281 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004282 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004283 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004284
4285 // Update last coordinates of pointers that have moved so that we observe the new
4286 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004287 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4288 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4289 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004290 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004291 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4292 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4293 bool moveNeeded = false;
4294 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004295 && !mPointerGesture.lastGestureIdBits.isEmpty()
4296 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004297 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4298 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004299 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004300 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004301 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004302 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4303 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004304 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004305 moveNeeded = true;
4306 }
Jeff Brownace13b12011-03-09 17:39:48 -08004307 }
4308
4309 // Send motion events for all pointers that went up or were canceled.
4310 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4311 if (!dispatchedGestureIdBits.isEmpty()) {
4312 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004313 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004314 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4315 AMOTION_EVENT_EDGE_FLAG_NONE,
4316 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004317 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4318 dispatchedGestureIdBits, -1,
4319 0, 0, mPointerGesture.downTime);
4320
4321 dispatchedGestureIdBits.clear();
4322 } else {
4323 BitSet32 upGestureIdBits;
4324 if (finishPreviousGesture) {
4325 upGestureIdBits = dispatchedGestureIdBits;
4326 } else {
4327 upGestureIdBits.value = dispatchedGestureIdBits.value
4328 & ~mPointerGesture.currentGestureIdBits.value;
4329 }
4330 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004331 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004332
Jeff Brown65fd2512011-08-18 11:20:58 -07004333 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004334 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004335 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4336 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004337 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4338 dispatchedGestureIdBits, id,
4339 0, 0, mPointerGesture.downTime);
4340
4341 dispatchedGestureIdBits.clearBit(id);
4342 }
4343 }
4344 }
4345
4346 // Send motion events for all pointers that moved.
4347 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004348 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004349 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4350 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004351 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4352 dispatchedGestureIdBits, -1,
4353 0, 0, mPointerGesture.downTime);
4354 }
4355
4356 // Send motion events for all pointers that went down.
4357 if (down) {
4358 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4359 & ~dispatchedGestureIdBits.value);
4360 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004361 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004362 dispatchedGestureIdBits.markBit(id);
4363
Jeff Brownace13b12011-03-09 17:39:48 -08004364 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004365 mPointerGesture.downTime = when;
4366 }
4367
Jeff Brown65fd2512011-08-18 11:20:58 -07004368 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004369 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004370 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004371 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4372 dispatchedGestureIdBits, id,
4373 0, 0, mPointerGesture.downTime);
4374 }
4375 }
4376
Jeff Brownace13b12011-03-09 17:39:48 -08004377 // Send motion events for hover.
4378 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004379 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004380 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4381 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4382 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004383 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4384 mPointerGesture.currentGestureIdBits, -1,
4385 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004386 } else if (dispatchedGestureIdBits.isEmpty()
4387 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4388 // Synthesize a hover move event after all pointers go up to indicate that
4389 // the pointer is hovering again even if the user is not currently touching
4390 // the touch pad. This ensures that a view will receive a fresh hover enter
4391 // event after a tap.
4392 float x, y;
4393 mPointerController->getPosition(&x, &y);
4394
4395 PointerProperties pointerProperties;
4396 pointerProperties.clear();
4397 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004398 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004399
4400 PointerCoords pointerCoords;
4401 pointerCoords.clear();
4402 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4403 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4404
Jeff Brown65fd2512011-08-18 11:20:58 -07004405 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004406 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4407 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4408 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004409 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004410 }
4411
4412 // Update state.
4413 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4414 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004415 mPointerGesture.lastGestureIdBits.clear();
4416 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004417 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4418 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004419 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004420 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004421 mPointerGesture.lastGestureProperties[index].copyFrom(
4422 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004423 mPointerGesture.lastGestureCoords[index].copyFrom(
4424 mPointerGesture.currentGestureCoords[index]);
4425 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004426 }
4427 }
4428}
4429
Jeff Brown65fd2512011-08-18 11:20:58 -07004430void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4431 // Cancel previously dispatches pointers.
4432 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4433 int32_t metaState = getContext()->getGlobalMetaState();
4434 int32_t buttonState = mCurrentButtonState;
4435 dispatchMotion(when, policyFlags, mSource,
4436 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4437 AMOTION_EVENT_EDGE_FLAG_NONE,
4438 mPointerGesture.lastGestureProperties,
4439 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4440 mPointerGesture.lastGestureIdBits, -1,
4441 0, 0, mPointerGesture.downTime);
4442 }
4443
4444 // Reset the current pointer gesture.
4445 mPointerGesture.reset();
4446 mPointerVelocityControl.reset();
4447
4448 // Remove any current spots.
4449 if (mPointerController != NULL) {
4450 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4451 mPointerController->clearSpots();
4452 }
4453}
4454
Jeff Brown79ac9692011-04-19 21:20:10 -07004455bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4456 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004457 *outCancelPreviousGesture = false;
4458 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004459
Jeff Brown79ac9692011-04-19 21:20:10 -07004460 // Handle TAP timeout.
4461 if (isTimeout) {
4462#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004463 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004464#endif
4465
4466 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004467 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004468 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004469 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004470 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004471 } else {
4472 // The tap is finished.
4473#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004474 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004475#endif
4476 *outFinishPreviousGesture = true;
4477
4478 mPointerGesture.activeGestureId = -1;
4479 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4480 mPointerGesture.currentGestureIdBits.clear();
4481
Jeff Brown65fd2512011-08-18 11:20:58 -07004482 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004483 return true;
4484 }
4485 }
4486
4487 // We did not handle this timeout.
4488 return false;
4489 }
4490
Jeff Brown65fd2512011-08-18 11:20:58 -07004491 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4492 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4493
Jeff Brownace13b12011-03-09 17:39:48 -08004494 // Update the velocity tracker.
4495 {
4496 VelocityTracker::Position positions[MAX_POINTERS];
4497 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004498 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004499 uint32_t id = idBits.clearFirstMarkedBit();
4500 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004501 positions[count].x = pointer.x * mPointerXMovementScale;
4502 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004503 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004504 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004505 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004506 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004507
Jeff Brownace13b12011-03-09 17:39:48 -08004508 // Pick a new active touch id if needed.
4509 // Choose an arbitrary pointer that just went down, if there is one.
4510 // Otherwise choose an arbitrary remaining pointer.
4511 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004512 // We keep the same active touch id for as long as possible.
4513 bool activeTouchChanged = false;
4514 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4515 int32_t activeTouchId = lastActiveTouchId;
4516 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004517 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004518 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004519 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004520 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004521 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004522 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004523 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004524 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004525 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004526 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004527 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004528 } else {
4529 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004530 }
4531 }
4532
4533 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004534 bool isQuietTime = false;
4535 if (activeTouchId < 0) {
4536 mPointerGesture.resetQuietTime();
4537 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004538 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004539 if (!isQuietTime) {
4540 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4541 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4542 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004543 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004544 // Enter quiet time when exiting swipe or freeform state.
4545 // This is to prevent accidentally entering the hover state and flinging the
4546 // pointer when finishing a swipe and there is still one pointer left onscreen.
4547 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004548 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004549 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004550 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004551 // Enter quiet time when releasing the button and there are still two or more
4552 // fingers down. This may indicate that one finger was used to press the button
4553 // but it has not gone up yet.
4554 isQuietTime = true;
4555 }
4556 if (isQuietTime) {
4557 mPointerGesture.quietTime = when;
4558 }
Jeff Brownace13b12011-03-09 17:39:48 -08004559 }
4560 }
4561
4562 // Switch states based on button and pointer state.
4563 if (isQuietTime) {
4564 // Case 1: Quiet time. (QUIET)
4565#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004566 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004567 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004568#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004569 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4570 *outFinishPreviousGesture = true;
4571 }
Jeff Brownace13b12011-03-09 17:39:48 -08004572
4573 mPointerGesture.activeGestureId = -1;
4574 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004575 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004576
Jeff Brown65fd2512011-08-18 11:20:58 -07004577 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004578 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004579 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004580 // The pointer follows the active touch point.
4581 // Emit DOWN, MOVE, UP events at the pointer location.
4582 //
4583 // Only the active touch matters; other fingers are ignored. This policy helps
4584 // to handle the case where the user places a second finger on the touch pad
4585 // to apply the necessary force to depress an integrated button below the surface.
4586 // We don't want the second finger to be delivered to applications.
4587 //
4588 // For this to work well, we need to make sure to track the pointer that is really
4589 // active. If the user first puts one finger down to click then adds another
4590 // finger to drag then the active pointer should switch to the finger that is
4591 // being dragged.
4592#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004593 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004594 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004595#endif
4596 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004597 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004598 *outFinishPreviousGesture = true;
4599 mPointerGesture.activeGestureId = 0;
4600 }
4601
4602 // Switch pointers if needed.
4603 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004604 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004605 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004606 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004607 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004608 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004609 float vx, vy;
4610 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4611 float speed = hypotf(vx, vy);
4612 if (speed > bestSpeed) {
4613 bestId = id;
4614 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004615 }
Jeff Brown8d608662010-08-30 03:02:23 -07004616 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004617 }
4618 if (bestId >= 0 && bestId != activeTouchId) {
4619 mPointerGesture.activeTouchId = activeTouchId = bestId;
4620 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004621#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004622 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004623 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004624#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004625 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004626 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004627
Jeff Brown65fd2512011-08-18 11:20:58 -07004628 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004629 const RawPointerData::Pointer& currentPointer =
4630 mCurrentRawPointerData.pointerForId(activeTouchId);
4631 const RawPointerData::Pointer& lastPointer =
4632 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004633 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4634 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004635
Jeff Brownbe1aa822011-07-27 16:04:54 -07004636 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004637 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004638
4639 // Move the pointer using a relative motion.
4640 // When using spots, the click will occur at the position of the anchor
4641 // spot and all other spots will move there.
4642 mPointerController->move(deltaX, deltaY);
4643 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004644 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004645 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004646
Jeff Brownace13b12011-03-09 17:39:48 -08004647 float x, y;
4648 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004649
Jeff Brown79ac9692011-04-19 21:20:10 -07004650 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004651 mPointerGesture.currentGestureIdBits.clear();
4652 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4653 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004654 mPointerGesture.currentGestureProperties[0].clear();
4655 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004656 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004657 mPointerGesture.currentGestureCoords[0].clear();
4658 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4659 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4660 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004661 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004662 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004663 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4664 *outFinishPreviousGesture = true;
4665 }
Jeff Brownace13b12011-03-09 17:39:48 -08004666
Jeff Brown79ac9692011-04-19 21:20:10 -07004667 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004668 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004669 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004670 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4671 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004672 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004673 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004674 float x, y;
4675 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004676 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4677 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004678#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004679 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004680#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004681
4682 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004683 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004684 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004685
Jeff Brownace13b12011-03-09 17:39:48 -08004686 mPointerGesture.activeGestureId = 0;
4687 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004688 mPointerGesture.currentGestureIdBits.clear();
4689 mPointerGesture.currentGestureIdBits.markBit(
4690 mPointerGesture.activeGestureId);
4691 mPointerGesture.currentGestureIdToIndex[
4692 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004693 mPointerGesture.currentGestureProperties[0].clear();
4694 mPointerGesture.currentGestureProperties[0].id =
4695 mPointerGesture.activeGestureId;
4696 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004697 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004698 mPointerGesture.currentGestureCoords[0].clear();
4699 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004700 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004701 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004702 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004703 mPointerGesture.currentGestureCoords[0].setAxisValue(
4704 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004705
Jeff Brownace13b12011-03-09 17:39:48 -08004706 tapped = true;
4707 } else {
4708#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004709 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004710 x - mPointerGesture.tapX,
4711 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004712#endif
4713 }
4714 } else {
4715#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004716 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004717 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004718#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004719 }
Jeff Brownace13b12011-03-09 17:39:48 -08004720 }
Jeff Brown2352b972011-04-12 22:39:53 -07004721
Jeff Brown65fd2512011-08-18 11:20:58 -07004722 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004723
Jeff Brownace13b12011-03-09 17:39:48 -08004724 if (!tapped) {
4725#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004726 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004727#endif
4728 mPointerGesture.activeGestureId = -1;
4729 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004730 mPointerGesture.currentGestureIdBits.clear();
4731 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004732 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004733 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004734 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004735 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4736 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004737 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004738
Jeff Brown79ac9692011-04-19 21:20:10 -07004739 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4740 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004741 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004742 float x, y;
4743 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004744 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4745 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004746 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4747 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004748#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004749 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004750 x - mPointerGesture.tapX,
4751 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004752#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004753 }
4754 } else {
4755#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004756 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004757 (when - mPointerGesture.tapUpTime) * 0.000001f);
4758#endif
4759 }
4760 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4761 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4762 }
Jeff Brownace13b12011-03-09 17:39:48 -08004763
Jeff Brown65fd2512011-08-18 11:20:58 -07004764 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004765 const RawPointerData::Pointer& currentPointer =
4766 mCurrentRawPointerData.pointerForId(activeTouchId);
4767 const RawPointerData::Pointer& lastPointer =
4768 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004769 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004770 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004771 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004772 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004773
Jeff Brownbe1aa822011-07-27 16:04:54 -07004774 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004775 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004776
Jeff Brown2352b972011-04-12 22:39:53 -07004777 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004778 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004779 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004780 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004781 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004782 }
4783
Jeff Brown79ac9692011-04-19 21:20:10 -07004784 bool down;
4785 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4786#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004787 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004788#endif
4789 down = true;
4790 } else {
4791#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004792 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004793#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004794 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4795 *outFinishPreviousGesture = true;
4796 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004797 mPointerGesture.activeGestureId = 0;
4798 down = false;
4799 }
Jeff Brownace13b12011-03-09 17:39:48 -08004800
4801 float x, y;
4802 mPointerController->getPosition(&x, &y);
4803
Jeff Brownace13b12011-03-09 17:39:48 -08004804 mPointerGesture.currentGestureIdBits.clear();
4805 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4806 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004807 mPointerGesture.currentGestureProperties[0].clear();
4808 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4809 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004810 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004811 mPointerGesture.currentGestureCoords[0].clear();
4812 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4813 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004814 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4815 down ? 1.0f : 0.0f);
4816
Jeff Brown65fd2512011-08-18 11:20:58 -07004817 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004818 mPointerGesture.resetTap();
4819 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004820 mPointerGesture.tapX = x;
4821 mPointerGesture.tapY = y;
4822 }
Jeff Brownace13b12011-03-09 17:39:48 -08004823 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004824 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4825 // We need to provide feedback for each finger that goes down so we cannot wait
4826 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004827 //
Jeff Brown2352b972011-04-12 22:39:53 -07004828 // The ambiguous case is deciding what to do when there are two fingers down but they
4829 // have not moved enough to determine whether they are part of a drag or part of a
4830 // freeform gesture, or just a press or long-press at the pointer location.
4831 //
4832 // When there are two fingers we start with the PRESS hypothesis and we generate a
4833 // down at the pointer location.
4834 //
4835 // When the two fingers move enough or when additional fingers are added, we make
4836 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004837 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004838
Jeff Brown214eaf42011-05-26 19:17:02 -07004839 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004840 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004841 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004842 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4843 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004844 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004845 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004846 // Additional pointers have gone down but not yet settled.
4847 // Reset the gesture.
4848#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004849 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004850 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004851 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004852 * 0.000001f);
4853#endif
4854 *outCancelPreviousGesture = true;
4855 } else {
4856 // Continue previous gesture.
4857 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4858 }
4859
4860 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004861 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4862 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004863 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004864 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004865
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004866 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004867#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004868 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004869 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004870 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004871 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004872#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004873 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4874 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004875 &mPointerGesture.referenceTouchY);
4876 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4877 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004878 }
Jeff Brownace13b12011-03-09 17:39:48 -08004879
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004880 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004881 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004882 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4883 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004884 mPointerGesture.referenceDeltas[id].dx = 0;
4885 mPointerGesture.referenceDeltas[id].dy = 0;
4886 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004887 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004888
4889 // Add delta for all fingers and calculate a common movement delta.
4890 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004891 BitSet32 commonIdBits(mLastFingerIdBits.value
4892 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004893 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4894 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004895 uint32_t id = idBits.clearFirstMarkedBit();
4896 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4897 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004898 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4899 delta.dx += cpd.x - lpd.x;
4900 delta.dy += cpd.y - lpd.y;
4901
4902 if (first) {
4903 commonDeltaX = delta.dx;
4904 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004905 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004906 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4907 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4908 }
4909 }
Jeff Brownace13b12011-03-09 17:39:48 -08004910
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004911 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4912 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4913 float dist[MAX_POINTER_ID + 1];
4914 int32_t distOverThreshold = 0;
4915 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004916 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004917 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004918 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4919 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004920 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004921 distOverThreshold += 1;
4922 }
4923 }
4924
4925 // Only transition when at least two pointers have moved further than
4926 // the minimum distance threshold.
4927 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004928 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004929 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004930#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004931 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004932 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004933#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004934 *outCancelPreviousGesture = true;
4935 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4936 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004937 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004938 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004939 uint32_t id1 = idBits.clearFirstMarkedBit();
4940 uint32_t id2 = idBits.firstMarkedBit();
4941 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4942 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4943 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4944 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4945 // There are two pointers but they are too far apart for a SWIPE,
4946 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004947#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004948 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004949 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004950#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004951 *outCancelPreviousGesture = true;
4952 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4953 } else {
4954 // There are two pointers. Wait for both pointers to start moving
4955 // before deciding whether this is a SWIPE or FREEFORM gesture.
4956 float dist1 = dist[id1];
4957 float dist2 = dist[id2];
4958 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4959 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4960 // Calculate the dot product of the displacement vectors.
4961 // When the vectors are oriented in approximately the same direction,
4962 // the angle betweeen them is near zero and the cosine of the angle
4963 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4964 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4965 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004966 float dx1 = delta1.dx * mPointerXZoomScale;
4967 float dy1 = delta1.dy * mPointerYZoomScale;
4968 float dx2 = delta2.dx * mPointerXZoomScale;
4969 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004970 float dot = dx1 * dx2 + dy1 * dy2;
4971 float cosine = dot / (dist1 * dist2); // denominator always > 0
4972 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4973 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004974#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004975 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004976 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4977 "cosine %0.3f >= %0.3f",
4978 dist1, mConfig.pointerGestureMultitouchMinDistance,
4979 dist2, mConfig.pointerGestureMultitouchMinDistance,
4980 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004981#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004982 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4983 } else {
4984 // Pointers are moving in different directions. Switch to FREEFORM.
4985#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004986 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004987 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4988 "cosine %0.3f < %0.3f",
4989 dist1, mConfig.pointerGestureMultitouchMinDistance,
4990 dist2, mConfig.pointerGestureMultitouchMinDistance,
4991 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4992#endif
4993 *outCancelPreviousGesture = true;
4994 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4995 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004996 }
Jeff Brownace13b12011-03-09 17:39:48 -08004997 }
4998 }
Jeff Brownace13b12011-03-09 17:39:48 -08004999 }
5000 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07005001 // Switch from SWIPE to FREEFORM if additional pointers go down.
5002 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07005003 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07005004#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005005 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005006 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005007#endif
Jeff Brownace13b12011-03-09 17:39:48 -08005008 *outCancelPreviousGesture = true;
5009 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07005010 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005011 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005012
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005013 // Move the reference points based on the overall group motion of the fingers
5014 // except in PRESS mode while waiting for a transition to occur.
5015 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
5016 && (commonDeltaX || commonDeltaY)) {
5017 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005018 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07005019 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005020 delta.dx = 0;
5021 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07005022 }
5023
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005024 mPointerGesture.referenceTouchX += commonDeltaX;
5025 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07005026
Jeff Brown65fd2512011-08-18 11:20:58 -07005027 commonDeltaX *= mPointerXMovementScale;
5028 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07005029
Jeff Brownbe1aa822011-07-27 16:04:54 -07005030 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07005031 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07005032
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005033 mPointerGesture.referenceGestureX += commonDeltaX;
5034 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005035 }
5036
5037 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005038 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5039 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5040 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005041#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005042 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005043 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005044 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005045#endif
Steve Blockec193de2012-01-09 18:35:44 +00005046 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005047
5048 mPointerGesture.currentGestureIdBits.clear();
5049 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5050 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005051 mPointerGesture.currentGestureProperties[0].clear();
5052 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5053 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005054 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005055 mPointerGesture.currentGestureCoords[0].clear();
5056 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5057 mPointerGesture.referenceGestureX);
5058 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5059 mPointerGesture.referenceGestureY);
5060 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005061 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5062 // FREEFORM mode.
5063#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005064 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005065 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005066 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005067#endif
Steve Blockec193de2012-01-09 18:35:44 +00005068 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005069
Jeff Brownace13b12011-03-09 17:39:48 -08005070 mPointerGesture.currentGestureIdBits.clear();
5071
5072 BitSet32 mappedTouchIdBits;
5073 BitSet32 usedGestureIdBits;
5074 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5075 // Initially, assign the active gesture id to the active touch point
5076 // if there is one. No other touch id bits are mapped yet.
5077 if (!*outCancelPreviousGesture) {
5078 mappedTouchIdBits.markBit(activeTouchId);
5079 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5080 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5081 mPointerGesture.activeGestureId;
5082 } else {
5083 mPointerGesture.activeGestureId = -1;
5084 }
5085 } else {
5086 // Otherwise, assume we mapped all touches from the previous frame.
5087 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005088 mappedTouchIdBits.value = mLastFingerIdBits.value
5089 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005090 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5091
5092 // Check whether we need to choose a new active gesture id because the
5093 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005094 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5095 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005096 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005097 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005098 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5099 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5100 mPointerGesture.activeGestureId = -1;
5101 break;
5102 }
5103 }
5104 }
5105
5106#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005107 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005108 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5109 "activeGestureId=%d",
5110 mappedTouchIdBits.value, usedGestureIdBits.value,
5111 mPointerGesture.activeGestureId);
5112#endif
5113
Jeff Brown65fd2512011-08-18 11:20:58 -07005114 BitSet32 idBits(mCurrentFingerIdBits);
5115 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005116 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005117 uint32_t gestureId;
5118 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005119 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005120 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5121#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005122 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005123 "new mapping for touch id %d -> gesture id %d",
5124 touchId, gestureId);
5125#endif
5126 } else {
5127 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5128#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005129 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005130 "existing mapping for touch id %d -> gesture id %d",
5131 touchId, gestureId);
5132#endif
5133 }
5134 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5135 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5136
Jeff Brownbe1aa822011-07-27 16:04:54 -07005137 const RawPointerData::Pointer& pointer =
5138 mCurrentRawPointerData.pointerForId(touchId);
5139 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005140 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005141 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005142 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005143 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005144
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005145 mPointerGesture.currentGestureProperties[i].clear();
5146 mPointerGesture.currentGestureProperties[i].id = gestureId;
5147 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005148 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005149 mPointerGesture.currentGestureCoords[i].clear();
5150 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005151 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005152 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005153 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005154 mPointerGesture.currentGestureCoords[i].setAxisValue(
5155 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5156 }
5157
5158 if (mPointerGesture.activeGestureId < 0) {
5159 mPointerGesture.activeGestureId =
5160 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5161#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005162 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005163 "activeGestureId=%d", mPointerGesture.activeGestureId);
5164#endif
5165 }
Jeff Brown2352b972011-04-12 22:39:53 -07005166 }
Jeff Brownace13b12011-03-09 17:39:48 -08005167 }
5168
Jeff Brownbe1aa822011-07-27 16:04:54 -07005169 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005170
Jeff Brownace13b12011-03-09 17:39:48 -08005171#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005172 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005173 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5174 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005175 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005176 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5177 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005178 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005179 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005180 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005181 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005182 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005183 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005184 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5185 id, index, properties.toolType,
5186 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005187 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5188 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5189 }
5190 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005191 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005192 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005193 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005194 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005195 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005196 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5197 id, index, properties.toolType,
5198 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005199 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5200 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5201 }
5202#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005203 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005204}
5205
Jeff Brown65fd2512011-08-18 11:20:58 -07005206void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5207 mPointerSimple.currentCoords.clear();
5208 mPointerSimple.currentProperties.clear();
5209
5210 bool down, hovering;
5211 if (!mCurrentStylusIdBits.isEmpty()) {
5212 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5213 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5214 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5215 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5216 mPointerController->setPosition(x, y);
5217
5218 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5219 down = !hovering;
5220
5221 mPointerController->getPosition(&x, &y);
5222 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5223 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5224 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5225 mPointerSimple.currentProperties.id = 0;
5226 mPointerSimple.currentProperties.toolType =
5227 mCurrentCookedPointerData.pointerProperties[index].toolType;
5228 } else {
5229 down = false;
5230 hovering = false;
5231 }
5232
5233 dispatchPointerSimple(when, policyFlags, down, hovering);
5234}
5235
5236void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5237 abortPointerSimple(when, policyFlags);
5238}
5239
5240void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5241 mPointerSimple.currentCoords.clear();
5242 mPointerSimple.currentProperties.clear();
5243
5244 bool down, hovering;
5245 if (!mCurrentMouseIdBits.isEmpty()) {
5246 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5247 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5248 if (mLastMouseIdBits.hasBit(id)) {
5249 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5250 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5251 - mLastRawPointerData.pointers[lastIndex].x)
5252 * mPointerXMovementScale;
5253 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5254 - mLastRawPointerData.pointers[lastIndex].y)
5255 * mPointerYMovementScale;
5256
5257 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5258 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5259
5260 mPointerController->move(deltaX, deltaY);
5261 } else {
5262 mPointerVelocityControl.reset();
5263 }
5264
5265 down = isPointerDown(mCurrentButtonState);
5266 hovering = !down;
5267
5268 float x, y;
5269 mPointerController->getPosition(&x, &y);
5270 mPointerSimple.currentCoords.copyFrom(
5271 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5272 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5273 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5274 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5275 hovering ? 0.0f : 1.0f);
5276 mPointerSimple.currentProperties.id = 0;
5277 mPointerSimple.currentProperties.toolType =
5278 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5279 } else {
5280 mPointerVelocityControl.reset();
5281
5282 down = false;
5283 hovering = false;
5284 }
5285
5286 dispatchPointerSimple(when, policyFlags, down, hovering);
5287}
5288
5289void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5290 abortPointerSimple(when, policyFlags);
5291
5292 mPointerVelocityControl.reset();
5293}
5294
5295void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5296 bool down, bool hovering) {
5297 int32_t metaState = getContext()->getGlobalMetaState();
5298
5299 if (mPointerController != NULL) {
5300 if (down || hovering) {
5301 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5302 mPointerController->clearSpots();
5303 mPointerController->setButtonState(mCurrentButtonState);
5304 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5305 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5306 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5307 }
5308 }
5309
5310 if (mPointerSimple.down && !down) {
5311 mPointerSimple.down = false;
5312
5313 // Send up.
5314 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5315 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5316 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5317 mOrientedXPrecision, mOrientedYPrecision,
5318 mPointerSimple.downTime);
5319 getListener()->notifyMotion(&args);
5320 }
5321
5322 if (mPointerSimple.hovering && !hovering) {
5323 mPointerSimple.hovering = false;
5324
5325 // Send hover exit.
5326 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5327 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5328 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5329 mOrientedXPrecision, mOrientedYPrecision,
5330 mPointerSimple.downTime);
5331 getListener()->notifyMotion(&args);
5332 }
5333
5334 if (down) {
5335 if (!mPointerSimple.down) {
5336 mPointerSimple.down = true;
5337 mPointerSimple.downTime = when;
5338
5339 // Send down.
5340 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5341 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5342 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5343 mOrientedXPrecision, mOrientedYPrecision,
5344 mPointerSimple.downTime);
5345 getListener()->notifyMotion(&args);
5346 }
5347
5348 // Send move.
5349 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5350 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5351 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5352 mOrientedXPrecision, mOrientedYPrecision,
5353 mPointerSimple.downTime);
5354 getListener()->notifyMotion(&args);
5355 }
5356
5357 if (hovering) {
5358 if (!mPointerSimple.hovering) {
5359 mPointerSimple.hovering = true;
5360
5361 // Send hover enter.
5362 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5363 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5364 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5365 mOrientedXPrecision, mOrientedYPrecision,
5366 mPointerSimple.downTime);
5367 getListener()->notifyMotion(&args);
5368 }
5369
5370 // Send hover move.
5371 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5372 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5373 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5374 mOrientedXPrecision, mOrientedYPrecision,
5375 mPointerSimple.downTime);
5376 getListener()->notifyMotion(&args);
5377 }
5378
5379 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5380 float vscroll = mCurrentRawVScroll;
5381 float hscroll = mCurrentRawHScroll;
5382 mWheelYVelocityControl.move(when, NULL, &vscroll);
5383 mWheelXVelocityControl.move(when, &hscroll, NULL);
5384
5385 // Send scroll.
5386 PointerCoords pointerCoords;
5387 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5388 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5389 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5390
5391 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5392 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5393 1, &mPointerSimple.currentProperties, &pointerCoords,
5394 mOrientedXPrecision, mOrientedYPrecision,
5395 mPointerSimple.downTime);
5396 getListener()->notifyMotion(&args);
5397 }
5398
5399 // Save state.
5400 if (down || hovering) {
5401 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5402 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5403 } else {
5404 mPointerSimple.reset();
5405 }
5406}
5407
5408void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5409 mPointerSimple.currentCoords.clear();
5410 mPointerSimple.currentProperties.clear();
5411
5412 dispatchPointerSimple(when, policyFlags, false, false);
5413}
5414
Jeff Brownace13b12011-03-09 17:39:48 -08005415void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005416 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5417 const PointerProperties* properties, const PointerCoords* coords,
5418 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005419 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5420 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005421 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005422 uint32_t pointerCount = 0;
5423 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005424 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005425 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005426 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005427 pointerCoords[pointerCount].copyFrom(coords[index]);
5428
5429 if (changedId >= 0 && id == uint32_t(changedId)) {
5430 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5431 }
5432
5433 pointerCount += 1;
5434 }
5435
Steve Blockec193de2012-01-09 18:35:44 +00005436 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005437
5438 if (changedId >= 0 && pointerCount == 1) {
5439 // Replace initial down and final up action.
5440 // We can compare the action without masking off the changed pointer index
5441 // because we know the index is 0.
5442 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5443 action = AMOTION_EVENT_ACTION_DOWN;
5444 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5445 action = AMOTION_EVENT_ACTION_UP;
5446 } else {
5447 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005448 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005449 }
5450 }
5451
Jeff Brownbe1aa822011-07-27 16:04:54 -07005452 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005453 action, flags, metaState, buttonState, edgeFlags,
5454 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005455 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005456}
5457
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005458bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005459 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005460 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5461 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005462 bool changed = false;
5463 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005464 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005465 uint32_t inIndex = inIdToIndex[id];
5466 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005467
5468 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005469 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005470 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005471 PointerCoords& curOutCoords = outCoords[outIndex];
5472
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005473 if (curInProperties != curOutProperties) {
5474 curOutProperties.copyFrom(curInProperties);
5475 changed = true;
5476 }
5477
Jeff Brownace13b12011-03-09 17:39:48 -08005478 if (curInCoords != curOutCoords) {
5479 curOutCoords.copyFrom(curInCoords);
5480 changed = true;
5481 }
5482 }
5483 return changed;
5484}
5485
5486void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005487 if (mPointerController != NULL) {
5488 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5489 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005490}
5491
Jeff Brownbe1aa822011-07-27 16:04:54 -07005492bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5493 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5494 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005495}
5496
Jeff Brownbe1aa822011-07-27 16:04:54 -07005497const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005498 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005499 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005500 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005501 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005502
5503#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005504 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005505 "left=%d, top=%d, right=%d, bottom=%d",
5506 x, y,
5507 virtualKey.keyCode, virtualKey.scanCode,
5508 virtualKey.hitLeft, virtualKey.hitTop,
5509 virtualKey.hitRight, virtualKey.hitBottom);
5510#endif
5511
5512 if (virtualKey.isHit(x, y)) {
5513 return & virtualKey;
5514 }
5515 }
5516
5517 return NULL;
5518}
5519
Jeff Brownbe1aa822011-07-27 16:04:54 -07005520void TouchInputMapper::assignPointerIds() {
5521 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5522 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5523
5524 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005525
5526 if (currentPointerCount == 0) {
5527 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005528 return;
5529 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005530
Jeff Brownbe1aa822011-07-27 16:04:54 -07005531 if (lastPointerCount == 0) {
5532 // All pointers are new.
5533 for (uint32_t i = 0; i < currentPointerCount; i++) {
5534 uint32_t id = i;
5535 mCurrentRawPointerData.pointers[i].id = id;
5536 mCurrentRawPointerData.idToIndex[id] = i;
5537 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5538 }
5539 return;
5540 }
5541
5542 if (currentPointerCount == 1 && lastPointerCount == 1
5543 && mCurrentRawPointerData.pointers[0].toolType
5544 == mLastRawPointerData.pointers[0].toolType) {
5545 // Only one pointer and no change in count so it must have the same id as before.
5546 uint32_t id = mLastRawPointerData.pointers[0].id;
5547 mCurrentRawPointerData.pointers[0].id = id;
5548 mCurrentRawPointerData.idToIndex[id] = 0;
5549 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5550 return;
5551 }
5552
5553 // General case.
5554 // We build a heap of squared euclidean distances between current and last pointers
5555 // associated with the current and last pointer indices. Then, we find the best
5556 // match (by distance) for each current pointer.
5557 // The pointers must have the same tool type but it is possible for them to
5558 // transition from hovering to touching or vice-versa while retaining the same id.
5559 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5560
5561 uint32_t heapSize = 0;
5562 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5563 currentPointerIndex++) {
5564 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5565 lastPointerIndex++) {
5566 const RawPointerData::Pointer& currentPointer =
5567 mCurrentRawPointerData.pointers[currentPointerIndex];
5568 const RawPointerData::Pointer& lastPointer =
5569 mLastRawPointerData.pointers[lastPointerIndex];
5570 if (currentPointer.toolType == lastPointer.toolType) {
5571 int64_t deltaX = currentPointer.x - lastPointer.x;
5572 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005573
5574 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5575
5576 // Insert new element into the heap (sift up).
5577 heap[heapSize].currentPointerIndex = currentPointerIndex;
5578 heap[heapSize].lastPointerIndex = lastPointerIndex;
5579 heap[heapSize].distance = distance;
5580 heapSize += 1;
5581 }
5582 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005583 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005584
Jeff Brownbe1aa822011-07-27 16:04:54 -07005585 // Heapify
5586 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5587 startIndex -= 1;
5588 for (uint32_t parentIndex = startIndex; ;) {
5589 uint32_t childIndex = parentIndex * 2 + 1;
5590 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005591 break;
5592 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005593
5594 if (childIndex + 1 < heapSize
5595 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5596 childIndex += 1;
5597 }
5598
5599 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5600 break;
5601 }
5602
5603 swap(heap[parentIndex], heap[childIndex]);
5604 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005605 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005606 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005607
5608#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005609 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005610 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005611 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005612 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5613 heap[i].distance);
5614 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005615#endif
5616
Jeff Brownbe1aa822011-07-27 16:04:54 -07005617 // Pull matches out by increasing order of distance.
5618 // To avoid reassigning pointers that have already been matched, the loop keeps track
5619 // of which last and current pointers have been matched using the matchedXXXBits variables.
5620 // It also tracks the used pointer id bits.
5621 BitSet32 matchedLastBits(0);
5622 BitSet32 matchedCurrentBits(0);
5623 BitSet32 usedIdBits(0);
5624 bool first = true;
5625 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5626 while (heapSize > 0) {
5627 if (first) {
5628 // The first time through the loop, we just consume the root element of
5629 // the heap (the one with smallest distance).
5630 first = false;
5631 } else {
5632 // Previous iterations consumed the root element of the heap.
5633 // Pop root element off of the heap (sift down).
5634 heap[0] = heap[heapSize];
5635 for (uint32_t parentIndex = 0; ;) {
5636 uint32_t childIndex = parentIndex * 2 + 1;
5637 if (childIndex >= heapSize) {
5638 break;
5639 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005640
Jeff Brownbe1aa822011-07-27 16:04:54 -07005641 if (childIndex + 1 < heapSize
5642 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5643 childIndex += 1;
5644 }
5645
5646 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5647 break;
5648 }
5649
5650 swap(heap[parentIndex], heap[childIndex]);
5651 parentIndex = childIndex;
5652 }
5653
5654#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005655 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005656 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005657 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005658 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5659 heap[i].distance);
5660 }
5661#endif
5662 }
5663
5664 heapSize -= 1;
5665
5666 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5667 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5668
5669 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5670 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5671
5672 matchedCurrentBits.markBit(currentPointerIndex);
5673 matchedLastBits.markBit(lastPointerIndex);
5674
5675 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5676 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5677 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5678 mCurrentRawPointerData.markIdBit(id,
5679 mCurrentRawPointerData.isHovering(currentPointerIndex));
5680 usedIdBits.markBit(id);
5681
5682#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005683 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005684 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5685#endif
5686 break;
5687 }
5688 }
5689
5690 // Assign fresh ids to pointers that were not matched in the process.
5691 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5692 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5693 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5694
5695 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5696 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5697 mCurrentRawPointerData.markIdBit(id,
5698 mCurrentRawPointerData.isHovering(currentPointerIndex));
5699
5700#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005701 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005702 currentPointerIndex, id);
5703#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005704 }
5705}
5706
Jeff Brown6d0fec22010-07-23 21:28:06 -07005707int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005708 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5709 return AKEY_STATE_VIRTUAL;
5710 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005711
Jeff Brownbe1aa822011-07-27 16:04:54 -07005712 size_t numVirtualKeys = mVirtualKeys.size();
5713 for (size_t i = 0; i < numVirtualKeys; i++) {
5714 const VirtualKey& virtualKey = mVirtualKeys[i];
5715 if (virtualKey.keyCode == keyCode) {
5716 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005717 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005718 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005719
5720 return AKEY_STATE_UNKNOWN;
5721}
5722
5723int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005724 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5725 return AKEY_STATE_VIRTUAL;
5726 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005727
Jeff Brownbe1aa822011-07-27 16:04:54 -07005728 size_t numVirtualKeys = mVirtualKeys.size();
5729 for (size_t i = 0; i < numVirtualKeys; i++) {
5730 const VirtualKey& virtualKey = mVirtualKeys[i];
5731 if (virtualKey.scanCode == scanCode) {
5732 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005733 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005734 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005735
5736 return AKEY_STATE_UNKNOWN;
5737}
5738
5739bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5740 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005741 size_t numVirtualKeys = mVirtualKeys.size();
5742 for (size_t i = 0; i < numVirtualKeys; i++) {
5743 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005744
Jeff Brownbe1aa822011-07-27 16:04:54 -07005745 for (size_t i = 0; i < numCodes; i++) {
5746 if (virtualKey.keyCode == keyCodes[i]) {
5747 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005748 }
5749 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005750 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005751
5752 return true;
5753}
5754
5755
5756// --- SingleTouchInputMapper ---
5757
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005758SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5759 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005760}
5761
5762SingleTouchInputMapper::~SingleTouchInputMapper() {
5763}
5764
Jeff Brown65fd2512011-08-18 11:20:58 -07005765void SingleTouchInputMapper::reset(nsecs_t when) {
5766 mSingleTouchMotionAccumulator.reset(getDevice());
5767
5768 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005769}
5770
Jeff Brown6d0fec22010-07-23 21:28:06 -07005771void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005772 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005773
Jeff Brown65fd2512011-08-18 11:20:58 -07005774 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005775}
5776
Jeff Brown65fd2512011-08-18 11:20:58 -07005777void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005778 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005779 mCurrentRawPointerData.pointerCount = 1;
5780 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005781
Jeff Brown65fd2512011-08-18 11:20:58 -07005782 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5783 && (mTouchButtonAccumulator.isHovering()
5784 || (mRawPointerAxes.pressure.valid
5785 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005786 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005787
Jeff Brownbe1aa822011-07-27 16:04:54 -07005788 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005789 outPointer.id = 0;
5790 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5791 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5792 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5793 outPointer.touchMajor = 0;
5794 outPointer.touchMinor = 0;
5795 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5796 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5797 outPointer.orientation = 0;
5798 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005799 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5800 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005801 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5802 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5803 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5804 }
5805 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005806 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005807}
5808
Jeff Brownbe1aa822011-07-27 16:04:54 -07005809void SingleTouchInputMapper::configureRawPointerAxes() {
5810 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005811
Jeff Brownbe1aa822011-07-27 16:04:54 -07005812 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5813 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5814 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5815 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5816 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005817 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5818 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005819}
5820
Jeff Brown00710e92012-04-19 15:18:26 -07005821bool SingleTouchInputMapper::hasStylus() const {
5822 return mTouchButtonAccumulator.hasStylus();
5823}
5824
Jeff Brown6d0fec22010-07-23 21:28:06 -07005825
5826// --- MultiTouchInputMapper ---
5827
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005828MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005829 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005830}
5831
5832MultiTouchInputMapper::~MultiTouchInputMapper() {
5833}
5834
Jeff Brown65fd2512011-08-18 11:20:58 -07005835void MultiTouchInputMapper::reset(nsecs_t when) {
5836 mMultiTouchMotionAccumulator.reset(getDevice());
5837
Jeff Brown6894a292011-07-01 17:59:27 -07005838 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005839
Jeff Brown65fd2512011-08-18 11:20:58 -07005840 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005841}
5842
5843void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005844 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005845
Jeff Brown65fd2512011-08-18 11:20:58 -07005846 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005847}
5848
Jeff Brown65fd2512011-08-18 11:20:58 -07005849void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005850 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005851 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005852 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005853
Jeff Brown80fd47c2011-05-24 01:07:44 -07005854 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005855 const MultiTouchMotionAccumulator::Slot* inSlot =
5856 mMultiTouchMotionAccumulator.getSlot(inIndex);
5857 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005858 continue;
5859 }
5860
Jeff Brown80fd47c2011-05-24 01:07:44 -07005861 if (outCount >= MAX_POINTERS) {
5862#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005863 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005864 "ignoring the rest.",
5865 getDeviceName().string(), MAX_POINTERS);
5866#endif
5867 break; // too many fingers!
5868 }
5869
Jeff Brownbe1aa822011-07-27 16:04:54 -07005870 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005871 outPointer.x = inSlot->getX();
5872 outPointer.y = inSlot->getY();
5873 outPointer.pressure = inSlot->getPressure();
5874 outPointer.touchMajor = inSlot->getTouchMajor();
5875 outPointer.touchMinor = inSlot->getTouchMinor();
5876 outPointer.toolMajor = inSlot->getToolMajor();
5877 outPointer.toolMinor = inSlot->getToolMinor();
5878 outPointer.orientation = inSlot->getOrientation();
5879 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005880 outPointer.tiltX = 0;
5881 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005882
Jeff Brown49754db2011-07-01 17:37:58 -07005883 outPointer.toolType = inSlot->getToolType();
5884 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5885 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5886 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5887 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5888 }
Jeff Brown8d608662010-08-30 03:02:23 -07005889 }
5890
Jeff Brown65fd2512011-08-18 11:20:58 -07005891 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5892 && (mTouchButtonAccumulator.isHovering()
5893 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005894 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005895
Jeff Brown8d608662010-08-30 03:02:23 -07005896 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005897 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005898 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005899 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005900 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005901 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005902 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005903 if (mPointerTrackingIdMap[n] == trackingId) {
5904 id = n;
5905 }
5906 }
5907
5908 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005909 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005910 mPointerTrackingIdMap[id] = trackingId;
5911 }
5912 }
5913 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005914 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005915 mCurrentRawPointerData.clearIdBits();
5916 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005917 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005918 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005919 mCurrentRawPointerData.idToIndex[id] = outCount;
5920 mCurrentRawPointerData.markIdBit(id, isHovering);
5921 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005922 }
5923 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005924
Jeff Brown6d0fec22010-07-23 21:28:06 -07005925 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005926 }
5927
Jeff Brownbe1aa822011-07-27 16:04:54 -07005928 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005929 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005930
Jeff Brown65fd2512011-08-18 11:20:58 -07005931 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005932}
5933
Jeff Brownbe1aa822011-07-27 16:04:54 -07005934void MultiTouchInputMapper::configureRawPointerAxes() {
5935 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005936
Jeff Brownbe1aa822011-07-27 16:04:54 -07005937 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5938 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5939 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5940 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5941 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5942 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5943 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5944 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5945 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5946 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5947 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005948
Jeff Brownbe1aa822011-07-27 16:04:54 -07005949 if (mRawPointerAxes.trackingId.valid
5950 && mRawPointerAxes.slot.valid
5951 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5952 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005953 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005954 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005955 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005956 getDeviceName().string(), slotCount, MAX_SLOTS);
5957 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005958 }
Jeff Brown00710e92012-04-19 15:18:26 -07005959 mMultiTouchMotionAccumulator.configure(getDevice(),
5960 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005961 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07005962 mMultiTouchMotionAccumulator.configure(getDevice(),
5963 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005964 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005965}
5966
Jeff Brown00710e92012-04-19 15:18:26 -07005967bool MultiTouchInputMapper::hasStylus() const {
5968 return mMultiTouchMotionAccumulator.hasStylus()
5969 || mTouchButtonAccumulator.hasStylus();
5970}
5971
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005972
Jeff Browncb1404e2011-01-15 18:14:15 -08005973// --- JoystickInputMapper ---
5974
5975JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5976 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005977}
5978
5979JoystickInputMapper::~JoystickInputMapper() {
5980}
5981
5982uint32_t JoystickInputMapper::getSources() {
5983 return AINPUT_SOURCE_JOYSTICK;
5984}
5985
5986void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5987 InputMapper::populateDeviceInfo(info);
5988
Jeff Brown6f2fba42011-02-19 01:08:02 -08005989 for (size_t i = 0; i < mAxes.size(); i++) {
5990 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005991 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5992 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005993 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005994 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5995 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005996 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005997 }
5998}
5999
6000void JoystickInputMapper::dump(String8& dump) {
6001 dump.append(INDENT2 "Joystick Input Mapper:\n");
6002
Jeff Brown6f2fba42011-02-19 01:08:02 -08006003 dump.append(INDENT3 "Axes:\n");
6004 size_t numAxes = mAxes.size();
6005 for (size_t i = 0; i < numAxes; i++) {
6006 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006007 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006008 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08006009 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006010 } else {
Jeff Brown85297452011-03-04 13:07:49 -08006011 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006012 }
Jeff Brown85297452011-03-04 13:07:49 -08006013 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6014 label = getAxisLabel(axis.axisInfo.highAxis);
6015 if (label) {
6016 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
6017 } else {
6018 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
6019 axis.axisInfo.splitValue);
6020 }
6021 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
6022 dump.append(" (invert)");
6023 }
6024
6025 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
6026 axis.min, axis.max, axis.flat, axis.fuzz);
6027 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
6028 "highScale=%0.5f, highOffset=%0.5f\n",
6029 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07006030 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
6031 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006032 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006033 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006034 }
6035}
6036
Jeff Brown65fd2512011-08-18 11:20:58 -07006037void JoystickInputMapper::configure(nsecs_t when,
6038 const InputReaderConfiguration* config, uint32_t changes) {
6039 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006040
Jeff Brown474dcb52011-06-14 20:22:50 -07006041 if (!changes) { // first time only
6042 // Collect all axes.
6043 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07006044 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6045 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6046 continue; // axis must be claimed by a different device
6047 }
6048
Jeff Brown474dcb52011-06-14 20:22:50 -07006049 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006050 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006051 if (rawAxisInfo.valid) {
6052 // Map axis.
6053 AxisInfo axisInfo;
6054 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6055 if (!explicitlyMapped) {
6056 // Axis is not explicitly mapped, will choose a generic axis later.
6057 axisInfo.mode = AxisInfo::MODE_NORMAL;
6058 axisInfo.axis = -1;
6059 }
6060
6061 // Apply flat override.
6062 int32_t rawFlat = axisInfo.flatOverride < 0
6063 ? rawAxisInfo.flat : axisInfo.flatOverride;
6064
6065 // Calculate scaling factors and limits.
6066 Axis axis;
6067 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6068 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6069 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6070 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6071 scale, 0.0f, highScale, 0.0f,
6072 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6073 } else if (isCenteredAxis(axisInfo.axis)) {
6074 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6075 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6076 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6077 scale, offset, scale, offset,
6078 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6079 } else {
6080 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6081 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6082 scale, 0.0f, scale, 0.0f,
6083 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6084 }
6085
6086 // To eliminate noise while the joystick is at rest, filter out small variations
6087 // in axis values up front.
6088 axis.filter = axis.flat * 0.25f;
6089
6090 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006091 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006092 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006093
Jeff Brown474dcb52011-06-14 20:22:50 -07006094 // If there are too many axes, start dropping them.
6095 // Prefer to keep explicitly mapped axes.
6096 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006097 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006098 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6099 pruneAxes(true);
6100 pruneAxes(false);
6101 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006102
Jeff Brown474dcb52011-06-14 20:22:50 -07006103 // Assign generic axis ids to remaining axes.
6104 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6105 size_t numAxes = mAxes.size();
6106 for (size_t i = 0; i < numAxes; i++) {
6107 Axis& axis = mAxes.editValueAt(i);
6108 if (axis.axisInfo.axis < 0) {
6109 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6110 && haveAxis(nextGenericAxisId)) {
6111 nextGenericAxisId += 1;
6112 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006113
Jeff Brown474dcb52011-06-14 20:22:50 -07006114 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6115 axis.axisInfo.axis = nextGenericAxisId;
6116 nextGenericAxisId += 1;
6117 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006118 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006119 "have already been assigned to other axes.",
6120 getDeviceName().string(), mAxes.keyAt(i));
6121 mAxes.removeItemsAt(i--);
6122 numAxes -= 1;
6123 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006124 }
6125 }
6126 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006127}
6128
Jeff Brown85297452011-03-04 13:07:49 -08006129bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006130 size_t numAxes = mAxes.size();
6131 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006132 const Axis& axis = mAxes.valueAt(i);
6133 if (axis.axisInfo.axis == axisId
6134 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6135 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006136 return true;
6137 }
6138 }
6139 return false;
6140}
Jeff Browncb1404e2011-01-15 18:14:15 -08006141
Jeff Brown6f2fba42011-02-19 01:08:02 -08006142void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6143 size_t i = mAxes.size();
6144 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6145 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6146 continue;
6147 }
Steve Block6215d3f2012-01-04 20:05:49 +00006148 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006149 getDeviceName().string(), mAxes.keyAt(i));
6150 mAxes.removeItemsAt(i);
6151 }
6152}
6153
6154bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6155 switch (axis) {
6156 case AMOTION_EVENT_AXIS_X:
6157 case AMOTION_EVENT_AXIS_Y:
6158 case AMOTION_EVENT_AXIS_Z:
6159 case AMOTION_EVENT_AXIS_RX:
6160 case AMOTION_EVENT_AXIS_RY:
6161 case AMOTION_EVENT_AXIS_RZ:
6162 case AMOTION_EVENT_AXIS_HAT_X:
6163 case AMOTION_EVENT_AXIS_HAT_Y:
6164 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006165 case AMOTION_EVENT_AXIS_RUDDER:
6166 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006167 return true;
6168 default:
6169 return false;
6170 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006171}
6172
Jeff Brown65fd2512011-08-18 11:20:58 -07006173void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006174 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006175 size_t numAxes = mAxes.size();
6176 for (size_t i = 0; i < numAxes; i++) {
6177 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006178 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006179 }
6180
Jeff Brown65fd2512011-08-18 11:20:58 -07006181 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006182}
6183
6184void JoystickInputMapper::process(const RawEvent* rawEvent) {
6185 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006186 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006187 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006188 if (index >= 0) {
6189 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006190 float newValue, highNewValue;
6191 switch (axis.axisInfo.mode) {
6192 case AxisInfo::MODE_INVERT:
6193 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6194 * axis.scale + axis.offset;
6195 highNewValue = 0.0f;
6196 break;
6197 case AxisInfo::MODE_SPLIT:
6198 if (rawEvent->value < axis.axisInfo.splitValue) {
6199 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6200 * axis.scale + axis.offset;
6201 highNewValue = 0.0f;
6202 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6203 newValue = 0.0f;
6204 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6205 * axis.highScale + axis.highOffset;
6206 } else {
6207 newValue = 0.0f;
6208 highNewValue = 0.0f;
6209 }
6210 break;
6211 default:
6212 newValue = rawEvent->value * axis.scale + axis.offset;
6213 highNewValue = 0.0f;
6214 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006215 }
Jeff Brown85297452011-03-04 13:07:49 -08006216 axis.newValue = newValue;
6217 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006218 }
6219 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006220 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006221
6222 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006223 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006224 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006225 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006226 break;
6227 }
6228 break;
6229 }
6230}
6231
Jeff Brown6f2fba42011-02-19 01:08:02 -08006232void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006233 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006234 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006235 }
6236
6237 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006238 int32_t buttonState = 0;
6239
6240 PointerProperties pointerProperties;
6241 pointerProperties.clear();
6242 pointerProperties.id = 0;
6243 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006244
Jeff Brown6f2fba42011-02-19 01:08:02 -08006245 PointerCoords pointerCoords;
6246 pointerCoords.clear();
6247
6248 size_t numAxes = mAxes.size();
6249 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006250 const Axis& axis = mAxes.valueAt(i);
6251 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6252 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6253 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6254 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006255 }
6256
Jeff Brown56194eb2011-03-02 19:23:13 -08006257 // Moving a joystick axis should not wake the devide because joysticks can
6258 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6259 // button will likely wake the device.
6260 // TODO: Use the input device configuration to control this behavior more finely.
6261 uint32_t policyFlags = 0;
6262
Jeff Brownbe1aa822011-07-27 16:04:54 -07006263 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006264 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
6265 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006266 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006267}
6268
Jeff Brown85297452011-03-04 13:07:49 -08006269bool JoystickInputMapper::filterAxes(bool force) {
6270 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006271 size_t numAxes = mAxes.size();
6272 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006273 Axis& axis = mAxes.editValueAt(i);
6274 if (force || hasValueChangedSignificantly(axis.filter,
6275 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6276 axis.currentValue = axis.newValue;
6277 atLeastOneSignificantChange = true;
6278 }
6279 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6280 if (force || hasValueChangedSignificantly(axis.filter,
6281 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6282 axis.highCurrentValue = axis.highNewValue;
6283 atLeastOneSignificantChange = true;
6284 }
6285 }
6286 }
6287 return atLeastOneSignificantChange;
6288}
6289
6290bool JoystickInputMapper::hasValueChangedSignificantly(
6291 float filter, float newValue, float currentValue, float min, float max) {
6292 if (newValue != currentValue) {
6293 // Filter out small changes in value unless the value is converging on the axis
6294 // bounds or center point. This is intended to reduce the amount of information
6295 // sent to applications by particularly noisy joysticks (such as PS3).
6296 if (fabs(newValue - currentValue) > filter
6297 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6298 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6299 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6300 return true;
6301 }
6302 }
6303 return false;
6304}
6305
6306bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6307 float filter, float newValue, float currentValue, float thresholdValue) {
6308 float newDistance = fabs(newValue - thresholdValue);
6309 if (newDistance < filter) {
6310 float oldDistance = fabs(currentValue - thresholdValue);
6311 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006312 return true;
6313 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006314 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006315 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006316}
6317
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006318} // namespace android