blob: 8c37fbb6ffae9ecc6435cc1deb9c474b8a0f1a37 [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 Brownef3d7e82010-09-30 14:33:04 -0700921 deviceInfo.getName().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
965 size_t numMappers = mMappers.size();
966 for (size_t i = 0; i < numMappers; i++) {
967 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700968 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700969 mSources |= mapper->getSources();
970 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700971 }
972}
973
Jeff Brown65fd2512011-08-18 11:20:58 -0700974void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700975 size_t numMappers = mMappers.size();
976 for (size_t i = 0; i < numMappers; i++) {
977 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700978 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700979 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700980
981 mContext->updateGlobalMetaState();
982
983 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700984}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700985
Jeff Brownb7198742011-03-18 18:14:26 -0700986void InputDevice::process(const RawEvent* rawEvents, size_t count) {
987 // Process all of the events in order for each mapper.
988 // We cannot simply ask each mapper to process them in bulk because mappers may
989 // have side-effects that must be interleaved. For example, joystick movement events and
990 // gamepad button presses are handled by different mappers but they should be dispatched
991 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700992 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700993 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
994#if DEBUG_RAW_EVENTS
Jeff Brown49ccac52012-04-11 18:27:33 -0700995 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
996 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
Jeff Brownb7198742011-03-18 18:14:26 -0700997#endif
998
Jeff Brown80fd47c2011-05-24 01:07:44 -0700999 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001000 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07001001 mDropUntilNextSync = false;
1002#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +00001003 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -07001004#endif
1005 } else {
1006#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +00001007 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -07001008#endif
1009 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001010 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -07001011 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -07001012 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07001013 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001014 } else {
1015 for (size_t i = 0; i < numMappers; i++) {
1016 InputMapper* mapper = mMappers[i];
1017 mapper->process(rawEvent);
1018 }
Jeff Brownb7198742011-03-18 18:14:26 -07001019 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001020 }
1021}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001022
Jeff Brownaa3855d2011-03-17 01:34:19 -07001023void InputDevice::timeoutExpired(nsecs_t when) {
1024 size_t numMappers = mMappers.size();
1025 for (size_t i = 0; i < numMappers; i++) {
1026 InputMapper* mapper = mMappers[i];
1027 mapper->timeoutExpired(when);
1028 }
1029}
1030
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001032 outDeviceInfo->initialize(mId, mGeneration, mIdentifier.name, mIdentifier.descriptor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001033
1034 size_t numMappers = mMappers.size();
1035 for (size_t i = 0; i < numMappers; i++) {
1036 InputMapper* mapper = mMappers[i];
1037 mapper->populateDeviceInfo(outDeviceInfo);
1038 }
1039}
1040
1041int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1042 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1043}
1044
1045int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1046 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1047}
1048
1049int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1050 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1051}
1052
1053int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1054 int32_t result = AKEY_STATE_UNKNOWN;
1055 size_t numMappers = mMappers.size();
1056 for (size_t i = 0; i < numMappers; i++) {
1057 InputMapper* mapper = mMappers[i];
1058 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001059 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1060 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1061 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1062 if (currentResult >= AKEY_STATE_DOWN) {
1063 return currentResult;
1064 } else if (currentResult == AKEY_STATE_UP) {
1065 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001066 }
1067 }
1068 }
1069 return result;
1070}
1071
1072bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1073 const int32_t* keyCodes, uint8_t* outFlags) {
1074 bool result = false;
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)) {
1079 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1080 }
1081 }
1082 return result;
1083}
1084
Jeff Browna47425a2012-04-13 04:09:27 -07001085void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1086 int32_t token) {
1087 size_t numMappers = mMappers.size();
1088 for (size_t i = 0; i < numMappers; i++) {
1089 InputMapper* mapper = mMappers[i];
1090 mapper->vibrate(pattern, patternSize, repeat, token);
1091 }
1092}
1093
1094void InputDevice::cancelVibrate(int32_t token) {
1095 size_t numMappers = mMappers.size();
1096 for (size_t i = 0; i < numMappers; i++) {
1097 InputMapper* mapper = mMappers[i];
1098 mapper->cancelVibrate(token);
1099 }
1100}
1101
Jeff Brown6d0fec22010-07-23 21:28:06 -07001102int32_t InputDevice::getMetaState() {
1103 int32_t result = 0;
1104 size_t numMappers = mMappers.size();
1105 for (size_t i = 0; i < numMappers; i++) {
1106 InputMapper* mapper = mMappers[i];
1107 result |= mapper->getMetaState();
1108 }
1109 return result;
1110}
1111
Jeff Brown05dc66a2011-03-02 14:41:58 -08001112void InputDevice::fadePointer() {
1113 size_t numMappers = mMappers.size();
1114 for (size_t i = 0; i < numMappers; i++) {
1115 InputMapper* mapper = mMappers[i];
1116 mapper->fadePointer();
1117 }
1118}
1119
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001120void InputDevice::bumpGeneration() {
1121 mGeneration = mContext->bumpGeneration();
1122}
1123
Jeff Brown65fd2512011-08-18 11:20:58 -07001124void InputDevice::notifyReset(nsecs_t when) {
1125 NotifyDeviceResetArgs args(when, mId);
1126 mContext->getListener()->notifyDeviceReset(&args);
1127}
1128
Jeff Brown6d0fec22010-07-23 21:28:06 -07001129
Jeff Brown49754db2011-07-01 17:37:58 -07001130// --- CursorButtonAccumulator ---
1131
1132CursorButtonAccumulator::CursorButtonAccumulator() {
1133 clearButtons();
1134}
1135
Jeff Brown65fd2512011-08-18 11:20:58 -07001136void CursorButtonAccumulator::reset(InputDevice* device) {
1137 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1138 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1139 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1140 mBtnBack = device->isKeyPressed(BTN_BACK);
1141 mBtnSide = device->isKeyPressed(BTN_SIDE);
1142 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1143 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1144 mBtnTask = device->isKeyPressed(BTN_TASK);
1145}
1146
Jeff Brown49754db2011-07-01 17:37:58 -07001147void CursorButtonAccumulator::clearButtons() {
1148 mBtnLeft = 0;
1149 mBtnRight = 0;
1150 mBtnMiddle = 0;
1151 mBtnBack = 0;
1152 mBtnSide = 0;
1153 mBtnForward = 0;
1154 mBtnExtra = 0;
1155 mBtnTask = 0;
1156}
1157
1158void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1159 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001160 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001161 case BTN_LEFT:
1162 mBtnLeft = rawEvent->value;
1163 break;
1164 case BTN_RIGHT:
1165 mBtnRight = rawEvent->value;
1166 break;
1167 case BTN_MIDDLE:
1168 mBtnMiddle = rawEvent->value;
1169 break;
1170 case BTN_BACK:
1171 mBtnBack = rawEvent->value;
1172 break;
1173 case BTN_SIDE:
1174 mBtnSide = rawEvent->value;
1175 break;
1176 case BTN_FORWARD:
1177 mBtnForward = rawEvent->value;
1178 break;
1179 case BTN_EXTRA:
1180 mBtnExtra = rawEvent->value;
1181 break;
1182 case BTN_TASK:
1183 mBtnTask = rawEvent->value;
1184 break;
1185 }
1186 }
1187}
1188
1189uint32_t CursorButtonAccumulator::getButtonState() const {
1190 uint32_t result = 0;
1191 if (mBtnLeft) {
1192 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1193 }
1194 if (mBtnRight) {
1195 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1196 }
1197 if (mBtnMiddle) {
1198 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1199 }
1200 if (mBtnBack || mBtnSide) {
1201 result |= AMOTION_EVENT_BUTTON_BACK;
1202 }
1203 if (mBtnForward || mBtnExtra) {
1204 result |= AMOTION_EVENT_BUTTON_FORWARD;
1205 }
1206 return result;
1207}
1208
1209
1210// --- CursorMotionAccumulator ---
1211
Jeff Brown65fd2512011-08-18 11:20:58 -07001212CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001213 clearRelativeAxes();
1214}
1215
Jeff Brown65fd2512011-08-18 11:20:58 -07001216void CursorMotionAccumulator::reset(InputDevice* device) {
1217 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001218}
1219
1220void CursorMotionAccumulator::clearRelativeAxes() {
1221 mRelX = 0;
1222 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001223}
1224
1225void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1226 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001227 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001228 case REL_X:
1229 mRelX = rawEvent->value;
1230 break;
1231 case REL_Y:
1232 mRelY = rawEvent->value;
1233 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001234 }
1235 }
1236}
1237
1238void CursorMotionAccumulator::finishSync() {
1239 clearRelativeAxes();
1240}
1241
1242
1243// --- CursorScrollAccumulator ---
1244
1245CursorScrollAccumulator::CursorScrollAccumulator() :
1246 mHaveRelWheel(false), mHaveRelHWheel(false) {
1247 clearRelativeAxes();
1248}
1249
1250void CursorScrollAccumulator::configure(InputDevice* device) {
1251 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1252 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1253}
1254
1255void CursorScrollAccumulator::reset(InputDevice* device) {
1256 clearRelativeAxes();
1257}
1258
1259void CursorScrollAccumulator::clearRelativeAxes() {
1260 mRelWheel = 0;
1261 mRelHWheel = 0;
1262}
1263
1264void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1265 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001266 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001267 case REL_WHEEL:
1268 mRelWheel = rawEvent->value;
1269 break;
1270 case REL_HWHEEL:
1271 mRelHWheel = rawEvent->value;
1272 break;
1273 }
1274 }
1275}
1276
Jeff Brown65fd2512011-08-18 11:20:58 -07001277void CursorScrollAccumulator::finishSync() {
1278 clearRelativeAxes();
1279}
1280
Jeff Brown49754db2011-07-01 17:37:58 -07001281
1282// --- TouchButtonAccumulator ---
1283
1284TouchButtonAccumulator::TouchButtonAccumulator() :
1285 mHaveBtnTouch(false) {
1286 clearButtons();
1287}
1288
1289void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001290 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1291}
1292
1293void TouchButtonAccumulator::reset(InputDevice* device) {
1294 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1295 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1296 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1297 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1298 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1299 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1300 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1301 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1302 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1303 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1304 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001305 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1306 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1307 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001308}
1309
1310void TouchButtonAccumulator::clearButtons() {
1311 mBtnTouch = 0;
1312 mBtnStylus = 0;
1313 mBtnStylus2 = 0;
1314 mBtnToolFinger = 0;
1315 mBtnToolPen = 0;
1316 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001317 mBtnToolBrush = 0;
1318 mBtnToolPencil = 0;
1319 mBtnToolAirbrush = 0;
1320 mBtnToolMouse = 0;
1321 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001322 mBtnToolDoubleTap = 0;
1323 mBtnToolTripleTap = 0;
1324 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001325}
1326
1327void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1328 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001329 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001330 case BTN_TOUCH:
1331 mBtnTouch = rawEvent->value;
1332 break;
1333 case BTN_STYLUS:
1334 mBtnStylus = rawEvent->value;
1335 break;
1336 case BTN_STYLUS2:
1337 mBtnStylus2 = rawEvent->value;
1338 break;
1339 case BTN_TOOL_FINGER:
1340 mBtnToolFinger = rawEvent->value;
1341 break;
1342 case BTN_TOOL_PEN:
1343 mBtnToolPen = rawEvent->value;
1344 break;
1345 case BTN_TOOL_RUBBER:
1346 mBtnToolRubber = rawEvent->value;
1347 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001348 case BTN_TOOL_BRUSH:
1349 mBtnToolBrush = rawEvent->value;
1350 break;
1351 case BTN_TOOL_PENCIL:
1352 mBtnToolPencil = rawEvent->value;
1353 break;
1354 case BTN_TOOL_AIRBRUSH:
1355 mBtnToolAirbrush = rawEvent->value;
1356 break;
1357 case BTN_TOOL_MOUSE:
1358 mBtnToolMouse = rawEvent->value;
1359 break;
1360 case BTN_TOOL_LENS:
1361 mBtnToolLens = rawEvent->value;
1362 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001363 case BTN_TOOL_DOUBLETAP:
1364 mBtnToolDoubleTap = rawEvent->value;
1365 break;
1366 case BTN_TOOL_TRIPLETAP:
1367 mBtnToolTripleTap = rawEvent->value;
1368 break;
1369 case BTN_TOOL_QUADTAP:
1370 mBtnToolQuadTap = rawEvent->value;
1371 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001372 }
1373 }
1374}
1375
1376uint32_t TouchButtonAccumulator::getButtonState() const {
1377 uint32_t result = 0;
1378 if (mBtnStylus) {
1379 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1380 }
1381 if (mBtnStylus2) {
1382 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1383 }
1384 return result;
1385}
1386
1387int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001388 if (mBtnToolMouse || mBtnToolLens) {
1389 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1390 }
Jeff Brown49754db2011-07-01 17:37:58 -07001391 if (mBtnToolRubber) {
1392 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1393 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001394 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001395 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1396 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001397 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001398 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1399 }
1400 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1401}
1402
Jeff Brownd87c6d52011-08-10 14:55:59 -07001403bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001404 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1405 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001406 || mBtnToolMouse || mBtnToolLens
1407 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001408}
1409
1410bool TouchButtonAccumulator::isHovering() const {
1411 return mHaveBtnTouch && !mBtnTouch;
1412}
1413
1414
Jeff Brownbe1aa822011-07-27 16:04:54 -07001415// --- RawPointerAxes ---
1416
1417RawPointerAxes::RawPointerAxes() {
1418 clear();
1419}
1420
1421void RawPointerAxes::clear() {
1422 x.clear();
1423 y.clear();
1424 pressure.clear();
1425 touchMajor.clear();
1426 touchMinor.clear();
1427 toolMajor.clear();
1428 toolMinor.clear();
1429 orientation.clear();
1430 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001431 tiltX.clear();
1432 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001433 trackingId.clear();
1434 slot.clear();
1435}
1436
1437
1438// --- RawPointerData ---
1439
1440RawPointerData::RawPointerData() {
1441 clear();
1442}
1443
1444void RawPointerData::clear() {
1445 pointerCount = 0;
1446 clearIdBits();
1447}
1448
1449void RawPointerData::copyFrom(const RawPointerData& other) {
1450 pointerCount = other.pointerCount;
1451 hoveringIdBits = other.hoveringIdBits;
1452 touchingIdBits = other.touchingIdBits;
1453
1454 for (uint32_t i = 0; i < pointerCount; i++) {
1455 pointers[i] = other.pointers[i];
1456
1457 int id = pointers[i].id;
1458 idToIndex[id] = other.idToIndex[id];
1459 }
1460}
1461
1462void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1463 float x = 0, y = 0;
1464 uint32_t count = touchingIdBits.count();
1465 if (count) {
1466 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1467 uint32_t id = idBits.clearFirstMarkedBit();
1468 const Pointer& pointer = pointerForId(id);
1469 x += pointer.x;
1470 y += pointer.y;
1471 }
1472 x /= count;
1473 y /= count;
1474 }
1475 *outX = x;
1476 *outY = y;
1477}
1478
1479
1480// --- CookedPointerData ---
1481
1482CookedPointerData::CookedPointerData() {
1483 clear();
1484}
1485
1486void CookedPointerData::clear() {
1487 pointerCount = 0;
1488 hoveringIdBits.clear();
1489 touchingIdBits.clear();
1490}
1491
1492void CookedPointerData::copyFrom(const CookedPointerData& other) {
1493 pointerCount = other.pointerCount;
1494 hoveringIdBits = other.hoveringIdBits;
1495 touchingIdBits = other.touchingIdBits;
1496
1497 for (uint32_t i = 0; i < pointerCount; i++) {
1498 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1499 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1500
1501 int id = pointerProperties[i].id;
1502 idToIndex[id] = other.idToIndex[id];
1503 }
1504}
1505
1506
Jeff Brown49754db2011-07-01 17:37:58 -07001507// --- SingleTouchMotionAccumulator ---
1508
1509SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1510 clearAbsoluteAxes();
1511}
1512
Jeff Brown65fd2512011-08-18 11:20:58 -07001513void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1514 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1515 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1516 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1517 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1518 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1519 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1520 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1521}
1522
Jeff Brown49754db2011-07-01 17:37:58 -07001523void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1524 mAbsX = 0;
1525 mAbsY = 0;
1526 mAbsPressure = 0;
1527 mAbsToolWidth = 0;
1528 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001529 mAbsTiltX = 0;
1530 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001531}
1532
1533void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1534 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001535 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001536 case ABS_X:
1537 mAbsX = rawEvent->value;
1538 break;
1539 case ABS_Y:
1540 mAbsY = rawEvent->value;
1541 break;
1542 case ABS_PRESSURE:
1543 mAbsPressure = rawEvent->value;
1544 break;
1545 case ABS_TOOL_WIDTH:
1546 mAbsToolWidth = rawEvent->value;
1547 break;
1548 case ABS_DISTANCE:
1549 mAbsDistance = rawEvent->value;
1550 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001551 case ABS_TILT_X:
1552 mAbsTiltX = rawEvent->value;
1553 break;
1554 case ABS_TILT_Y:
1555 mAbsTiltY = rawEvent->value;
1556 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001557 }
1558 }
1559}
1560
1561
1562// --- MultiTouchMotionAccumulator ---
1563
1564MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1565 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1566}
1567
1568MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1569 delete[] mSlots;
1570}
1571
1572void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1573 mSlotCount = slotCount;
1574 mUsingSlotsProtocol = usingSlotsProtocol;
1575
1576 delete[] mSlots;
1577 mSlots = new Slot[slotCount];
1578}
1579
Jeff Brown65fd2512011-08-18 11:20:58 -07001580void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1581 // Unfortunately there is no way to read the initial contents of the slots.
1582 // So when we reset the accumulator, we must assume they are all zeroes.
1583 if (mUsingSlotsProtocol) {
1584 // Query the driver for the current slot index and use it as the initial slot
1585 // before we start reading events from the device. It is possible that the
1586 // current slot index will not be the same as it was when the first event was
1587 // written into the evdev buffer, which means the input mapper could start
1588 // out of sync with the initial state of the events in the evdev buffer.
1589 // In the extremely unlikely case that this happens, the data from
1590 // two slots will be confused until the next ABS_MT_SLOT event is received.
1591 // This can cause the touch point to "jump", but at least there will be
1592 // no stuck touches.
1593 int32_t initialSlot;
1594 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1595 ABS_MT_SLOT, &initialSlot);
1596 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001597 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001598 initialSlot = -1;
1599 }
1600 clearSlots(initialSlot);
1601 } else {
1602 clearSlots(-1);
1603 }
1604}
1605
Jeff Brown49754db2011-07-01 17:37:58 -07001606void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001607 if (mSlots) {
1608 for (size_t i = 0; i < mSlotCount; i++) {
1609 mSlots[i].clear();
1610 }
Jeff Brown49754db2011-07-01 17:37:58 -07001611 }
1612 mCurrentSlot = initialSlot;
1613}
1614
1615void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1616 if (rawEvent->type == EV_ABS) {
1617 bool newSlot = false;
1618 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001619 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001620 mCurrentSlot = rawEvent->value;
1621 newSlot = true;
1622 }
1623 } else if (mCurrentSlot < 0) {
1624 mCurrentSlot = 0;
1625 }
1626
1627 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1628#if DEBUG_POINTERS
1629 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001630 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001631 "should be between 0 and %d; ignoring this slot.",
1632 mCurrentSlot, mSlotCount - 1);
1633 }
1634#endif
1635 } else {
1636 Slot* slot = &mSlots[mCurrentSlot];
1637
Jeff Brown49ccac52012-04-11 18:27:33 -07001638 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001639 case ABS_MT_POSITION_X:
1640 slot->mInUse = true;
1641 slot->mAbsMTPositionX = rawEvent->value;
1642 break;
1643 case ABS_MT_POSITION_Y:
1644 slot->mInUse = true;
1645 slot->mAbsMTPositionY = rawEvent->value;
1646 break;
1647 case ABS_MT_TOUCH_MAJOR:
1648 slot->mInUse = true;
1649 slot->mAbsMTTouchMajor = rawEvent->value;
1650 break;
1651 case ABS_MT_TOUCH_MINOR:
1652 slot->mInUse = true;
1653 slot->mAbsMTTouchMinor = rawEvent->value;
1654 slot->mHaveAbsMTTouchMinor = true;
1655 break;
1656 case ABS_MT_WIDTH_MAJOR:
1657 slot->mInUse = true;
1658 slot->mAbsMTWidthMajor = rawEvent->value;
1659 break;
1660 case ABS_MT_WIDTH_MINOR:
1661 slot->mInUse = true;
1662 slot->mAbsMTWidthMinor = rawEvent->value;
1663 slot->mHaveAbsMTWidthMinor = true;
1664 break;
1665 case ABS_MT_ORIENTATION:
1666 slot->mInUse = true;
1667 slot->mAbsMTOrientation = rawEvent->value;
1668 break;
1669 case ABS_MT_TRACKING_ID:
1670 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001671 // The slot is no longer in use but it retains its previous contents,
1672 // which may be reused for subsequent touches.
1673 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001674 } else {
1675 slot->mInUse = true;
1676 slot->mAbsMTTrackingId = rawEvent->value;
1677 }
1678 break;
1679 case ABS_MT_PRESSURE:
1680 slot->mInUse = true;
1681 slot->mAbsMTPressure = rawEvent->value;
1682 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001683 case ABS_MT_DISTANCE:
1684 slot->mInUse = true;
1685 slot->mAbsMTDistance = rawEvent->value;
1686 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001687 case ABS_MT_TOOL_TYPE:
1688 slot->mInUse = true;
1689 slot->mAbsMTToolType = rawEvent->value;
1690 slot->mHaveAbsMTToolType = true;
1691 break;
1692 }
1693 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001694 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001695 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1696 mCurrentSlot += 1;
1697 }
1698}
1699
Jeff Brown65fd2512011-08-18 11:20:58 -07001700void MultiTouchMotionAccumulator::finishSync() {
1701 if (!mUsingSlotsProtocol) {
1702 clearSlots(-1);
1703 }
1704}
1705
Jeff Brown49754db2011-07-01 17:37:58 -07001706
1707// --- MultiTouchMotionAccumulator::Slot ---
1708
1709MultiTouchMotionAccumulator::Slot::Slot() {
1710 clear();
1711}
1712
Jeff Brown49754db2011-07-01 17:37:58 -07001713void MultiTouchMotionAccumulator::Slot::clear() {
1714 mInUse = false;
1715 mHaveAbsMTTouchMinor = false;
1716 mHaveAbsMTWidthMinor = false;
1717 mHaveAbsMTToolType = false;
1718 mAbsMTPositionX = 0;
1719 mAbsMTPositionY = 0;
1720 mAbsMTTouchMajor = 0;
1721 mAbsMTTouchMinor = 0;
1722 mAbsMTWidthMajor = 0;
1723 mAbsMTWidthMinor = 0;
1724 mAbsMTOrientation = 0;
1725 mAbsMTTrackingId = -1;
1726 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001727 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001728 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001729}
1730
1731int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1732 if (mHaveAbsMTToolType) {
1733 switch (mAbsMTToolType) {
1734 case MT_TOOL_FINGER:
1735 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1736 case MT_TOOL_PEN:
1737 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1738 }
1739 }
1740 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1741}
1742
1743
Jeff Brown6d0fec22010-07-23 21:28:06 -07001744// --- InputMapper ---
1745
1746InputMapper::InputMapper(InputDevice* device) :
1747 mDevice(device), mContext(device->getContext()) {
1748}
1749
1750InputMapper::~InputMapper() {
1751}
1752
1753void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1754 info->addSource(getSources());
1755}
1756
Jeff Brownef3d7e82010-09-30 14:33:04 -07001757void InputMapper::dump(String8& dump) {
1758}
1759
Jeff Brown65fd2512011-08-18 11:20:58 -07001760void InputMapper::configure(nsecs_t when,
1761 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001762}
1763
Jeff Brown65fd2512011-08-18 11:20:58 -07001764void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001765}
1766
Jeff Brownaa3855d2011-03-17 01:34:19 -07001767void InputMapper::timeoutExpired(nsecs_t when) {
1768}
1769
Jeff Brown6d0fec22010-07-23 21:28:06 -07001770int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1771 return AKEY_STATE_UNKNOWN;
1772}
1773
1774int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1775 return AKEY_STATE_UNKNOWN;
1776}
1777
1778int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1779 return AKEY_STATE_UNKNOWN;
1780}
1781
1782bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1783 const int32_t* keyCodes, uint8_t* outFlags) {
1784 return false;
1785}
1786
Jeff Browna47425a2012-04-13 04:09:27 -07001787void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1788 int32_t token) {
1789}
1790
1791void InputMapper::cancelVibrate(int32_t token) {
1792}
1793
Jeff Brown6d0fec22010-07-23 21:28:06 -07001794int32_t InputMapper::getMetaState() {
1795 return 0;
1796}
1797
Jeff Brown05dc66a2011-03-02 14:41:58 -08001798void InputMapper::fadePointer() {
1799}
1800
Jeff Brownbe1aa822011-07-27 16:04:54 -07001801status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1802 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1803}
1804
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001805void InputMapper::bumpGeneration() {
1806 mDevice->bumpGeneration();
1807}
1808
Jeff Browncb1404e2011-01-15 18:14:15 -08001809void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1810 const RawAbsoluteAxisInfo& axis, const char* name) {
1811 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001812 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1813 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001814 } else {
1815 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1816 }
1817}
1818
Jeff Brown6d0fec22010-07-23 21:28:06 -07001819
1820// --- SwitchInputMapper ---
1821
1822SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1823 InputMapper(device) {
1824}
1825
1826SwitchInputMapper::~SwitchInputMapper() {
1827}
1828
1829uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001830 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001831}
1832
1833void SwitchInputMapper::process(const RawEvent* rawEvent) {
1834 switch (rawEvent->type) {
1835 case EV_SW:
Jeff Brown49ccac52012-04-11 18:27:33 -07001836 processSwitch(rawEvent->when, rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001837 break;
1838 }
1839}
1840
1841void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001842 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1843 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001844}
1845
1846int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1847 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1848}
1849
1850
Jeff Browna47425a2012-04-13 04:09:27 -07001851// --- VibratorInputMapper ---
1852
1853VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1854 InputMapper(device), mVibrating(false) {
1855}
1856
1857VibratorInputMapper::~VibratorInputMapper() {
1858}
1859
1860uint32_t VibratorInputMapper::getSources() {
1861 return 0;
1862}
1863
1864void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1865 InputMapper::populateDeviceInfo(info);
1866
1867 info->setVibrator(true);
1868}
1869
1870void VibratorInputMapper::process(const RawEvent* rawEvent) {
1871 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1872}
1873
1874void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1875 int32_t token) {
1876#if DEBUG_VIBRATOR
1877 String8 patternStr;
1878 for (size_t i = 0; i < patternSize; i++) {
1879 if (i != 0) {
1880 patternStr.append(", ");
1881 }
1882 patternStr.appendFormat("%lld", pattern[i]);
1883 }
1884 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1885 getDeviceId(), patternStr.string(), repeat, token);
1886#endif
1887
1888 mVibrating = true;
1889 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1890 mPatternSize = patternSize;
1891 mRepeat = repeat;
1892 mToken = token;
1893 mIndex = -1;
1894
1895 nextStep();
1896}
1897
1898void VibratorInputMapper::cancelVibrate(int32_t token) {
1899#if DEBUG_VIBRATOR
1900 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1901#endif
1902
1903 if (mVibrating && mToken == token) {
1904 stopVibrating();
1905 }
1906}
1907
1908void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1909 if (mVibrating) {
1910 if (when >= mNextStepTime) {
1911 nextStep();
1912 } else {
1913 getContext()->requestTimeoutAtTime(mNextStepTime);
1914 }
1915 }
1916}
1917
1918void VibratorInputMapper::nextStep() {
1919 mIndex += 1;
1920 if (size_t(mIndex) >= mPatternSize) {
1921 if (mRepeat < 0) {
1922 // We are done.
1923 stopVibrating();
1924 return;
1925 }
1926 mIndex = mRepeat;
1927 }
1928
1929 bool vibratorOn = mIndex & 1;
1930 nsecs_t duration = mPattern[mIndex];
1931 if (vibratorOn) {
1932#if DEBUG_VIBRATOR
1933 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1934 getDeviceId(), duration);
1935#endif
1936 getEventHub()->vibrate(getDeviceId(), duration);
1937 } else {
1938#if DEBUG_VIBRATOR
1939 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1940#endif
1941 getEventHub()->cancelVibrate(getDeviceId());
1942 }
1943 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1944 mNextStepTime = now + duration;
1945 getContext()->requestTimeoutAtTime(mNextStepTime);
1946#if DEBUG_VIBRATOR
1947 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1948#endif
1949}
1950
1951void VibratorInputMapper::stopVibrating() {
1952 mVibrating = false;
1953#if DEBUG_VIBRATOR
1954 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1955#endif
1956 getEventHub()->cancelVibrate(getDeviceId());
1957}
1958
1959void VibratorInputMapper::dump(String8& dump) {
1960 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1961 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1962}
1963
1964
Jeff Brown6d0fec22010-07-23 21:28:06 -07001965// --- KeyboardInputMapper ---
1966
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001967KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001968 uint32_t source, int32_t keyboardType) :
1969 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001971}
1972
1973KeyboardInputMapper::~KeyboardInputMapper() {
1974}
1975
Jeff Brown6d0fec22010-07-23 21:28:06 -07001976uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001977 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001978}
1979
1980void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1981 InputMapper::populateDeviceInfo(info);
1982
1983 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001984 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001985}
1986
Jeff Brownef3d7e82010-09-30 14:33:04 -07001987void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001988 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1989 dumpParameters(dump);
1990 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001991 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001992 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1993 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1994 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001995}
1996
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001997
Jeff Brown65fd2512011-08-18 11:20:58 -07001998void KeyboardInputMapper::configure(nsecs_t when,
1999 const InputReaderConfiguration* config, uint32_t changes) {
2000 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002001
Jeff Brown474dcb52011-06-14 20:22:50 -07002002 if (!changes) { // first time only
2003 // Configure basic parameters.
2004 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002005 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002006
Jeff Brown65fd2512011-08-18 11:20:58 -07002007 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2008 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2009 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2010 false /*external*/, NULL, NULL, &mOrientation)) {
2011 mOrientation = DISPLAY_ORIENTATION_0;
2012 }
2013 } else {
2014 mOrientation = DISPLAY_ORIENTATION_0;
2015 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002016 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002017}
2018
2019void KeyboardInputMapper::configureParameters() {
2020 mParameters.orientationAware = false;
2021 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2022 mParameters.orientationAware);
2023
Jeff Brownbc68a592011-07-25 12:58:12 -07002024 mParameters.associatedDisplayId = -1;
2025 if (mParameters.orientationAware) {
2026 mParameters.associatedDisplayId = 0;
2027 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002028}
2029
2030void KeyboardInputMapper::dumpParameters(String8& dump) {
2031 dump.append(INDENT3 "Parameters:\n");
2032 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2033 mParameters.associatedDisplayId);
2034 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2035 toString(mParameters.orientationAware));
2036}
2037
Jeff Brown65fd2512011-08-18 11:20:58 -07002038void KeyboardInputMapper::reset(nsecs_t when) {
2039 mMetaState = AMETA_NONE;
2040 mDownTime = 0;
2041 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002042 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002043
Jeff Brownbe1aa822011-07-27 16:04:54 -07002044 resetLedState();
2045
Jeff Brown65fd2512011-08-18 11:20:58 -07002046 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002047}
2048
2049void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2050 switch (rawEvent->type) {
2051 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002052 int32_t scanCode = rawEvent->code;
2053 int32_t usageCode = mCurrentHidUsage;
2054 mCurrentHidUsage = 0;
2055
Jeff Brown6d0fec22010-07-23 21:28:06 -07002056 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002057 int32_t keyCode;
2058 uint32_t flags;
2059 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2060 keyCode = AKEYCODE_UNKNOWN;
2061 flags = 0;
2062 }
2063 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002064 }
2065 break;
2066 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002067 case EV_MSC: {
2068 if (rawEvent->code == MSC_SCAN) {
2069 mCurrentHidUsage = rawEvent->value;
2070 }
2071 break;
2072 }
2073 case EV_SYN: {
2074 if (rawEvent->code == SYN_REPORT) {
2075 mCurrentHidUsage = 0;
2076 }
2077 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002078 }
2079}
2080
2081bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2082 return scanCode < BTN_MOUSE
2083 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002084 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002085 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002086}
2087
Jeff Brown6328cdc2010-07-29 18:18:33 -07002088void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2089 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002090
Jeff Brownbe1aa822011-07-27 16:04:54 -07002091 if (down) {
2092 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002093 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002094 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002095 }
Jeff Brownfe508922011-01-18 15:10:10 -08002096
Jeff Brownbe1aa822011-07-27 16:04:54 -07002097 // Add key down.
2098 ssize_t keyDownIndex = findKeyDown(scanCode);
2099 if (keyDownIndex >= 0) {
2100 // key repeat, be sure to use same keycode as before in case of rotation
2101 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002102 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002103 // key down
2104 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2105 && mContext->shouldDropVirtualKey(when,
2106 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002107 return;
2108 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002109
2110 mKeyDowns.push();
2111 KeyDown& keyDown = mKeyDowns.editTop();
2112 keyDown.keyCode = keyCode;
2113 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002114 }
2115
Jeff Brownbe1aa822011-07-27 16:04:54 -07002116 mDownTime = when;
2117 } else {
2118 // Remove key down.
2119 ssize_t keyDownIndex = findKeyDown(scanCode);
2120 if (keyDownIndex >= 0) {
2121 // key up, be sure to use same keycode as before in case of rotation
2122 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2123 mKeyDowns.removeAt(size_t(keyDownIndex));
2124 } else {
2125 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002126 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002127 "keyCode=%d, scanCode=%d",
2128 getDeviceName().string(), keyCode, scanCode);
2129 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002130 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002131 }
Jeff Brownfd0358292010-06-30 16:10:35 -07002132
Jeff Brownbe1aa822011-07-27 16:04:54 -07002133 bool metaStateChanged = false;
2134 int32_t oldMetaState = mMetaState;
2135 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2136 if (oldMetaState != newMetaState) {
2137 mMetaState = newMetaState;
2138 metaStateChanged = true;
2139 updateLedState(false);
2140 }
2141
2142 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002143
Jeff Brown56194eb2011-03-02 19:23:13 -08002144 // Key down on external an keyboard should wake the device.
2145 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2146 // For internal keyboards, the key layout file should specify the policy flags for
2147 // each wake key individually.
2148 // TODO: Use the input device configuration to control this behavior more finely.
2149 if (down && getDevice()->isExternal()
2150 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2151 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2152 }
2153
Jeff Brown6328cdc2010-07-29 18:18:33 -07002154 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002155 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002156 }
2157
Jeff Brown05dc66a2011-03-02 14:41:58 -08002158 if (down && !isMetaKey(keyCode)) {
2159 getContext()->fadePointer();
2160 }
2161
Jeff Brownbe1aa822011-07-27 16:04:54 -07002162 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002163 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2164 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002165 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002166}
2167
Jeff Brownbe1aa822011-07-27 16:04:54 -07002168ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2169 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002170 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002171 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002172 return i;
2173 }
2174 }
2175 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002176}
2177
Jeff Brown6d0fec22010-07-23 21:28:06 -07002178int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2179 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2180}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002181
Jeff Brown6d0fec22010-07-23 21:28:06 -07002182int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2183 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2184}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002185
Jeff Brown6d0fec22010-07-23 21:28:06 -07002186bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2187 const int32_t* keyCodes, uint8_t* outFlags) {
2188 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2189}
2190
2191int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002192 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002193}
2194
Jeff Brownbe1aa822011-07-27 16:04:54 -07002195void KeyboardInputMapper::resetLedState() {
2196 initializeLedState(mCapsLockLedState, LED_CAPSL);
2197 initializeLedState(mNumLockLedState, LED_NUML);
2198 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002199
Jeff Brownbe1aa822011-07-27 16:04:54 -07002200 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002201}
2202
Jeff Brownbe1aa822011-07-27 16:04:54 -07002203void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002204 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2205 ledState.on = false;
2206}
2207
Jeff Brownbe1aa822011-07-27 16:04:54 -07002208void KeyboardInputMapper::updateLedState(bool reset) {
2209 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002210 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002211 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002212 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002213 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002214 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002215}
2216
Jeff Brownbe1aa822011-07-27 16:04:54 -07002217void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002218 int32_t led, int32_t modifier, bool reset) {
2219 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002221 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002222 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2223 ledState.on = desiredState;
2224 }
2225 }
2226}
2227
Jeff Brown6d0fec22010-07-23 21:28:06 -07002228
Jeff Brown83c09682010-12-23 17:50:18 -08002229// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002230
Jeff Brown83c09682010-12-23 17:50:18 -08002231CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002232 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002233}
2234
Jeff Brown83c09682010-12-23 17:50:18 -08002235CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002236}
2237
Jeff Brown83c09682010-12-23 17:50:18 -08002238uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002239 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002240}
2241
Jeff Brown83c09682010-12-23 17:50:18 -08002242void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002243 InputMapper::populateDeviceInfo(info);
2244
Jeff Brown83c09682010-12-23 17:50:18 -08002245 if (mParameters.mode == Parameters::MODE_POINTER) {
2246 float minX, minY, maxX, maxY;
2247 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002248 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2249 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002250 }
2251 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002252 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2253 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002254 }
Jeff Brownefd32662011-03-08 15:13:06 -08002255 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002256
Jeff Brown65fd2512011-08-18 11:20:58 -07002257 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002258 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002259 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002260 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002261 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002262 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002263}
2264
Jeff Brown83c09682010-12-23 17:50:18 -08002265void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002266 dump.append(INDENT2 "Cursor Input Mapper:\n");
2267 dumpParameters(dump);
2268 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2269 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2270 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2271 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2272 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002273 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002274 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002275 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002276 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2277 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002278 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002279 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2280 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2281 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002282}
2283
Jeff Brown65fd2512011-08-18 11:20:58 -07002284void CursorInputMapper::configure(nsecs_t when,
2285 const InputReaderConfiguration* config, uint32_t changes) {
2286 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002287
Jeff Brown474dcb52011-06-14 20:22:50 -07002288 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002289 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002290
Jeff Brown474dcb52011-06-14 20:22:50 -07002291 // Configure basic parameters.
2292 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002293
Jeff Brown474dcb52011-06-14 20:22:50 -07002294 // Configure device mode.
2295 switch (mParameters.mode) {
2296 case Parameters::MODE_POINTER:
2297 mSource = AINPUT_SOURCE_MOUSE;
2298 mXPrecision = 1.0f;
2299 mYPrecision = 1.0f;
2300 mXScale = 1.0f;
2301 mYScale = 1.0f;
2302 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2303 break;
2304 case Parameters::MODE_NAVIGATION:
2305 mSource = AINPUT_SOURCE_TRACKBALL;
2306 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2307 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2308 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2309 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2310 break;
2311 }
2312
2313 mVWheelScale = 1.0f;
2314 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002315 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002316
Jeff Brown474dcb52011-06-14 20:22:50 -07002317 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2318 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2319 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2320 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2321 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002322
2323 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2324 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2325 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2326 false /*external*/, NULL, NULL, &mOrientation)) {
2327 mOrientation = DISPLAY_ORIENTATION_0;
2328 }
2329 } else {
2330 mOrientation = DISPLAY_ORIENTATION_0;
2331 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002332 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002333 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002334}
2335
Jeff Brown83c09682010-12-23 17:50:18 -08002336void CursorInputMapper::configureParameters() {
2337 mParameters.mode = Parameters::MODE_POINTER;
2338 String8 cursorModeString;
2339 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2340 if (cursorModeString == "navigation") {
2341 mParameters.mode = Parameters::MODE_NAVIGATION;
2342 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002343 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002344 }
2345 }
2346
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002347 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002348 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002349 mParameters.orientationAware);
2350
Jeff Brownbc68a592011-07-25 12:58:12 -07002351 mParameters.associatedDisplayId = -1;
2352 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2353 mParameters.associatedDisplayId = 0;
2354 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002355}
2356
Jeff Brown83c09682010-12-23 17:50:18 -08002357void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002358 dump.append(INDENT3 "Parameters:\n");
2359 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2360 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002361
2362 switch (mParameters.mode) {
2363 case Parameters::MODE_POINTER:
2364 dump.append(INDENT4 "Mode: pointer\n");
2365 break;
2366 case Parameters::MODE_NAVIGATION:
2367 dump.append(INDENT4 "Mode: navigation\n");
2368 break;
2369 default:
Steve Blockec193de2012-01-09 18:35:44 +00002370 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002371 }
2372
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002373 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2374 toString(mParameters.orientationAware));
2375}
2376
Jeff Brown65fd2512011-08-18 11:20:58 -07002377void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002378 mButtonState = 0;
2379 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002380
Jeff Brownbe1aa822011-07-27 16:04:54 -07002381 mPointerVelocityControl.reset();
2382 mWheelXVelocityControl.reset();
2383 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002384
Jeff Brown65fd2512011-08-18 11:20:58 -07002385 mCursorButtonAccumulator.reset(getDevice());
2386 mCursorMotionAccumulator.reset(getDevice());
2387 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002388
Jeff Brown65fd2512011-08-18 11:20:58 -07002389 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002390}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002391
Jeff Brown83c09682010-12-23 17:50:18 -08002392void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002393 mCursorButtonAccumulator.process(rawEvent);
2394 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002395 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002396
Jeff Brown49ccac52012-04-11 18:27:33 -07002397 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002398 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002399 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002400}
2401
Jeff Brown83c09682010-12-23 17:50:18 -08002402void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002403 int32_t lastButtonState = mButtonState;
2404 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2405 mButtonState = currentButtonState;
2406
2407 bool wasDown = isPointerDown(lastButtonState);
2408 bool down = isPointerDown(currentButtonState);
2409 bool downChanged;
2410 if (!wasDown && down) {
2411 mDownTime = when;
2412 downChanged = true;
2413 } else if (wasDown && !down) {
2414 downChanged = true;
2415 } else {
2416 downChanged = false;
2417 }
2418 nsecs_t downTime = mDownTime;
2419 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002420 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002421
2422 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2423 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2424 bool moved = deltaX != 0 || deltaY != 0;
2425
Jeff Brown65fd2512011-08-18 11:20:58 -07002426 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002427 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2428 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002429 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002430 }
2431
Jeff Brown65fd2512011-08-18 11:20:58 -07002432 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002433 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002434 pointerProperties.clear();
2435 pointerProperties.id = 0;
2436 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2437
Jeff Brown6328cdc2010-07-29 18:18:33 -07002438 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002439 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002440
Jeff Brown65fd2512011-08-18 11:20:58 -07002441 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2442 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002443 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002444
Jeff Brownbe1aa822011-07-27 16:04:54 -07002445 mWheelYVelocityControl.move(when, NULL, &vscroll);
2446 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002447
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002449
Jeff Brownbe1aa822011-07-27 16:04:54 -07002450 if (mPointerController != NULL) {
2451 if (moved || scrolled || buttonsChanged) {
2452 mPointerController->setPresentation(
2453 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002454
Jeff Brownbe1aa822011-07-27 16:04:54 -07002455 if (moved) {
2456 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002457 }
2458
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 if (buttonsChanged) {
2460 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002461 }
Jeff Brownefd32662011-03-08 15:13:06 -08002462
Jeff Brownbe1aa822011-07-27 16:04:54 -07002463 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002464 }
2465
Jeff Brownbe1aa822011-07-27 16:04:54 -07002466 float x, y;
2467 mPointerController->getPosition(&x, &y);
2468 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2469 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2470 } else {
2471 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2472 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2473 }
2474
2475 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002476
Jeff Brown56194eb2011-03-02 19:23:13 -08002477 // Moving an external trackball or mouse should wake the device.
2478 // We don't do this for internal cursor devices to prevent them from waking up
2479 // the device in your pocket.
2480 // TODO: Use the input device configuration to control this behavior more finely.
2481 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002482 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002483 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2484 }
2485
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002486 // Synthesize key down from buttons if needed.
2487 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2488 policyFlags, lastButtonState, currentButtonState);
2489
2490 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002491 if (downChanged || moved || scrolled || buttonsChanged) {
2492 int32_t metaState = mContext->getGlobalMetaState();
2493 int32_t motionEventAction;
2494 if (downChanged) {
2495 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2496 } else if (down || mPointerController == NULL) {
2497 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2498 } else {
2499 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2500 }
Jeff Brownb6997262010-10-08 22:31:17 -07002501
Jeff Brownbe1aa822011-07-27 16:04:54 -07002502 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2503 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002504 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002505 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002506
Jeff Brownbe1aa822011-07-27 16:04:54 -07002507 // Send hover move after UP to tell the application that the mouse is hovering now.
2508 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2509 && mPointerController != NULL) {
2510 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2511 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2512 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2513 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2514 getListener()->notifyMotion(&hoverArgs);
2515 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002516
Jeff Brownbe1aa822011-07-27 16:04:54 -07002517 // Send scroll events.
2518 if (scrolled) {
2519 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2520 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2521
2522 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2523 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2524 AMOTION_EVENT_EDGE_FLAG_NONE,
2525 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2526 getListener()->notifyMotion(&scrollArgs);
2527 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002528 }
Jeff Browna032cc02011-03-07 16:56:21 -08002529
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002530 // Synthesize key up from buttons if needed.
2531 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2532 policyFlags, lastButtonState, currentButtonState);
2533
Jeff Brown65fd2512011-08-18 11:20:58 -07002534 mCursorMotionAccumulator.finishSync();
2535 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002536}
2537
Jeff Brown83c09682010-12-23 17:50:18 -08002538int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002539 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2540 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2541 } else {
2542 return AKEY_STATE_UNKNOWN;
2543 }
2544}
2545
Jeff Brown05dc66a2011-03-02 14:41:58 -08002546void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002547 if (mPointerController != NULL) {
2548 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2549 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002550}
2551
Jeff Brown6d0fec22010-07-23 21:28:06 -07002552
2553// --- TouchInputMapper ---
2554
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002555TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002556 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002557 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002558 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002559}
2560
2561TouchInputMapper::~TouchInputMapper() {
2562}
2563
2564uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002565 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002566}
2567
2568void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2569 InputMapper::populateDeviceInfo(info);
2570
Jeff Brown65fd2512011-08-18 11:20:58 -07002571 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2572 info->addMotionRange(mOrientedRanges.x);
2573 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002574 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002575
Jeff Brown65fd2512011-08-18 11:20:58 -07002576 if (mOrientedRanges.haveSize) {
2577 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002578 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002579
2580 if (mOrientedRanges.haveTouchSize) {
2581 info->addMotionRange(mOrientedRanges.touchMajor);
2582 info->addMotionRange(mOrientedRanges.touchMinor);
2583 }
2584
2585 if (mOrientedRanges.haveToolSize) {
2586 info->addMotionRange(mOrientedRanges.toolMajor);
2587 info->addMotionRange(mOrientedRanges.toolMinor);
2588 }
2589
2590 if (mOrientedRanges.haveOrientation) {
2591 info->addMotionRange(mOrientedRanges.orientation);
2592 }
2593
2594 if (mOrientedRanges.haveDistance) {
2595 info->addMotionRange(mOrientedRanges.distance);
2596 }
2597
2598 if (mOrientedRanges.haveTilt) {
2599 info->addMotionRange(mOrientedRanges.tilt);
2600 }
2601
2602 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2603 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2604 }
2605 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2606 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2607 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002608 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002609}
2610
Jeff Brownef3d7e82010-09-30 14:33:04 -07002611void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002612 dump.append(INDENT2 "Touch Input Mapper:\n");
2613 dumpParameters(dump);
2614 dumpVirtualKeys(dump);
2615 dumpRawPointerAxes(dump);
2616 dumpCalibration(dump);
2617 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002618
Jeff Brownbe1aa822011-07-27 16:04:54 -07002619 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2620 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2621 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2622 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2623 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2624 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002625 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2626 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002627 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002628 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2629 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002630 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2631 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2632 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2633 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2634 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002635
Jeff Brownbe1aa822011-07-27 16:04:54 -07002636 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002637
Jeff Brownbe1aa822011-07-27 16:04:54 -07002638 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2639 mLastRawPointerData.pointerCount);
2640 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2641 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2642 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2643 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002644 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2645 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002646 pointer.id, pointer.x, pointer.y, pointer.pressure,
2647 pointer.touchMajor, pointer.touchMinor,
2648 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002649 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002650 pointer.toolType, toString(pointer.isHovering));
2651 }
2652
2653 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2654 mLastCookedPointerData.pointerCount);
2655 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2656 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2657 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2658 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2659 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002660 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2661 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002662 pointerProperties.id,
2663 pointerCoords.getX(),
2664 pointerCoords.getY(),
2665 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2666 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2667 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2668 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2669 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2670 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002671 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002672 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2673 pointerProperties.toolType,
2674 toString(mLastCookedPointerData.isHovering(i)));
2675 }
2676
Jeff Brown65fd2512011-08-18 11:20:58 -07002677 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002678 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2679 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002680 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002681 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002682 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002683 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002684 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002685 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002686 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002687 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2688 mPointerGestureMaxSwipeWidth);
2689 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002690}
2691
Jeff Brown65fd2512011-08-18 11:20:58 -07002692void TouchInputMapper::configure(nsecs_t when,
2693 const InputReaderConfiguration* config, uint32_t changes) {
2694 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002695
Jeff Brown474dcb52011-06-14 20:22:50 -07002696 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002697
Jeff Brown474dcb52011-06-14 20:22:50 -07002698 if (!changes) { // first time only
2699 // Configure basic parameters.
2700 configureParameters();
2701
Jeff Brown65fd2512011-08-18 11:20:58 -07002702 // Configure common accumulators.
2703 mCursorScrollAccumulator.configure(getDevice());
2704 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002705
2706 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002707 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002708
2709 // Prepare input device calibration.
2710 parseCalibration();
2711 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002712 }
2713
Jeff Brown474dcb52011-06-14 20:22:50 -07002714 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002715 // Update pointer speed.
2716 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2717 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2718 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002719 }
Jeff Brown8d608662010-08-30 03:02:23 -07002720
Jeff Brown65fd2512011-08-18 11:20:58 -07002721 bool resetNeeded = false;
2722 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002723 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2724 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002725 // Configure device sources, surface dimensions, orientation and
2726 // scaling factors.
2727 configureSurface(when, &resetNeeded);
2728 }
2729
2730 if (changes && resetNeeded) {
2731 // Send reset, unless this is the first time the device has been configured,
2732 // in which case the reader will call reset itself after all mappers are ready.
2733 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002734 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002735}
2736
Jeff Brown8d608662010-08-30 03:02:23 -07002737void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002738 // Use the pointer presentation mode for devices that do not support distinct
2739 // multitouch. The spot-based presentation relies on being able to accurately
2740 // locate two or more fingers on the touch pad.
2741 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2742 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002743
Jeff Brown538881e2011-05-25 18:23:38 -07002744 String8 gestureModeString;
2745 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2746 gestureModeString)) {
2747 if (gestureModeString == "pointer") {
2748 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2749 } else if (gestureModeString == "spots") {
2750 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2751 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002752 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002753 }
2754 }
2755
Jeff Browndeffe072011-08-26 18:38:46 -07002756 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2757 // The device is a touch screen.
2758 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2759 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2760 // The device is a pointing device like a track pad.
2761 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2762 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002763 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2764 // The device is a cursor device with a touch pad attached.
2765 // By default don't use the touch pad to move the pointer.
2766 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2767 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002768 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002769 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2770 }
2771
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002772 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002773 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2774 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002775 if (deviceTypeString == "touchScreen") {
2776 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002777 } else if (deviceTypeString == "touchPad") {
2778 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002779 } else if (deviceTypeString == "pointer") {
2780 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002781 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002782 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002783 }
2784 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002785
Jeff Brownefd32662011-03-08 15:13:06 -08002786 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002787 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2788 mParameters.orientationAware);
2789
Jeff Brownbc68a592011-07-25 12:58:12 -07002790 mParameters.associatedDisplayId = -1;
2791 mParameters.associatedDisplayIsExternal = false;
2792 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002793 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002794 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2795 mParameters.associatedDisplayIsExternal =
2796 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2797 && getDevice()->isExternal();
2798 mParameters.associatedDisplayId = 0;
2799 }
Jeff Brown8d608662010-08-30 03:02:23 -07002800}
2801
Jeff Brownef3d7e82010-09-30 14:33:04 -07002802void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002803 dump.append(INDENT3 "Parameters:\n");
2804
Jeff Brown538881e2011-05-25 18:23:38 -07002805 switch (mParameters.gestureMode) {
2806 case Parameters::GESTURE_MODE_POINTER:
2807 dump.append(INDENT4 "GestureMode: pointer\n");
2808 break;
2809 case Parameters::GESTURE_MODE_SPOTS:
2810 dump.append(INDENT4 "GestureMode: spots\n");
2811 break;
2812 default:
2813 assert(false);
2814 }
2815
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002816 switch (mParameters.deviceType) {
2817 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2818 dump.append(INDENT4 "DeviceType: touchScreen\n");
2819 break;
2820 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2821 dump.append(INDENT4 "DeviceType: touchPad\n");
2822 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002823 case Parameters::DEVICE_TYPE_POINTER:
2824 dump.append(INDENT4 "DeviceType: pointer\n");
2825 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002826 default:
Steve Blockec193de2012-01-09 18:35:44 +00002827 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002828 }
2829
Jeff Brown65fd2512011-08-18 11:20:58 -07002830 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2831 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002832 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2833 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002834}
2835
Jeff Brownbe1aa822011-07-27 16:04:54 -07002836void TouchInputMapper::configureRawPointerAxes() {
2837 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002838}
2839
Jeff Brownbe1aa822011-07-27 16:04:54 -07002840void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2841 dump.append(INDENT3 "Raw Touch Axes:\n");
2842 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2843 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2844 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2845 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2846 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2847 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2848 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2849 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2850 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002851 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2852 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002853 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2854 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002855}
2856
Jeff Brown65fd2512011-08-18 11:20:58 -07002857void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2858 int32_t oldDeviceMode = mDeviceMode;
2859
2860 // Determine device mode.
2861 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2862 && mConfig.pointerGesturesEnabled) {
2863 mSource = AINPUT_SOURCE_MOUSE;
2864 mDeviceMode = DEVICE_MODE_POINTER;
2865 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2866 && mParameters.associatedDisplayId >= 0) {
2867 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2868 mDeviceMode = DEVICE_MODE_DIRECT;
2869 } else {
2870 mSource = AINPUT_SOURCE_TOUCHPAD;
2871 mDeviceMode = DEVICE_MODE_UNSCALED;
2872 }
2873
Jeff Brown9626b142011-03-03 02:09:54 -08002874 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002875 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002876 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002877 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002878 mDeviceMode = DEVICE_MODE_DISABLED;
2879 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002880 }
2881
Jeff Brown65fd2512011-08-18 11:20:58 -07002882 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002883 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002884 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002885 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002886 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2887 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002888 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002889 "display %d. The device will be inoperable until the display size "
2890 "becomes available.",
2891 getDeviceName().string(), mParameters.associatedDisplayId);
2892 mDeviceMode = DEVICE_MODE_DISABLED;
2893 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002894 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002895 }
2896
Jeff Brown65fd2512011-08-18 11:20:58 -07002897 // Configure dimensions.
2898 int32_t width, height, orientation;
2899 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2900 width = mAssociatedDisplayWidth;
2901 height = mAssociatedDisplayHeight;
2902 orientation = mParameters.orientationAware ?
2903 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2904 } else {
2905 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2906 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2907 orientation = DISPLAY_ORIENTATION_0;
2908 }
2909
2910 // If moving between pointer modes, need to reset some state.
2911 bool deviceModeChanged;
2912 if (mDeviceMode != oldDeviceMode) {
2913 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002914 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002915 }
2916
Jeff Browndaf4a122011-08-26 17:14:14 -07002917 // Create pointer controller if needed.
2918 if (mDeviceMode == DEVICE_MODE_POINTER ||
2919 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2920 if (mPointerController == NULL) {
2921 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2922 }
2923 } else {
2924 mPointerController.clear();
2925 }
2926
Jeff Brownbe1aa822011-07-27 16:04:54 -07002927 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002928 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002929 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002930 }
2931
Jeff Brownbe1aa822011-07-27 16:04:54 -07002932 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002933 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002934 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002935 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002936
Jeff Brownbe1aa822011-07-27 16:04:54 -07002937 mSurfaceWidth = width;
2938 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002939
Jeff Brown8d608662010-08-30 03:02:23 -07002940 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002941 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2942 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2943 mXPrecision = 1.0f / mXScale;
2944 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002945
Jeff Brownbe1aa822011-07-27 16:04:54 -07002946 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002947 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002948 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002949 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002950
Jeff Brownbe1aa822011-07-27 16:04:54 -07002951 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002952
Jeff Brown8d608662010-08-30 03:02:23 -07002953 // Scale factor for terms that are not oriented in a particular axis.
2954 // If the pixels are square then xScale == yScale otherwise we fake it
2955 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002956 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002957
Jeff Brown8d608662010-08-30 03:02:23 -07002958 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002959 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002960
Jeff Browna1f89ce2011-08-11 00:05:01 -07002961 // Size factors.
2962 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2963 if (mRawPointerAxes.touchMajor.valid
2964 && mRawPointerAxes.touchMajor.maxValue != 0) {
2965 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2966 } else if (mRawPointerAxes.toolMajor.valid
2967 && mRawPointerAxes.toolMajor.maxValue != 0) {
2968 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2969 } else {
2970 mSizeScale = 0.0f;
2971 }
2972
Jeff Brownbe1aa822011-07-27 16:04:54 -07002973 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002974 mOrientedRanges.haveToolSize = true;
2975 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002976
Jeff Brownbe1aa822011-07-27 16:04:54 -07002977 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002978 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002979 mOrientedRanges.touchMajor.min = 0;
2980 mOrientedRanges.touchMajor.max = diagonalSize;
2981 mOrientedRanges.touchMajor.flat = 0;
2982 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002983
Jeff Brownbe1aa822011-07-27 16:04:54 -07002984 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2985 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002986
Jeff Brownbe1aa822011-07-27 16:04:54 -07002987 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002988 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002989 mOrientedRanges.toolMajor.min = 0;
2990 mOrientedRanges.toolMajor.max = diagonalSize;
2991 mOrientedRanges.toolMajor.flat = 0;
2992 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002993
Jeff Brownbe1aa822011-07-27 16:04:54 -07002994 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2995 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002996
2997 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002998 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002999 mOrientedRanges.size.min = 0;
3000 mOrientedRanges.size.max = 1.0;
3001 mOrientedRanges.size.flat = 0;
3002 mOrientedRanges.size.fuzz = 0;
3003 } else {
3004 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003005 }
3006
3007 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003008 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003009 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3010 || mCalibration.pressureCalibration
3011 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3012 if (mCalibration.havePressureScale) {
3013 mPressureScale = mCalibration.pressureScale;
3014 } else if (mRawPointerAxes.pressure.valid
3015 && mRawPointerAxes.pressure.maxValue != 0) {
3016 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003017 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003018 }
Jeff Brown8d608662010-08-30 03:02:23 -07003019
Jeff Brown65fd2512011-08-18 11:20:58 -07003020 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3021 mOrientedRanges.pressure.source = mSource;
3022 mOrientedRanges.pressure.min = 0;
3023 mOrientedRanges.pressure.max = 1.0;
3024 mOrientedRanges.pressure.flat = 0;
3025 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003026
Jeff Brown65fd2512011-08-18 11:20:58 -07003027 // Tilt
3028 mTiltXCenter = 0;
3029 mTiltXScale = 0;
3030 mTiltYCenter = 0;
3031 mTiltYScale = 0;
3032 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3033 if (mHaveTilt) {
3034 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3035 mRawPointerAxes.tiltX.maxValue);
3036 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3037 mRawPointerAxes.tiltY.maxValue);
3038 mTiltXScale = M_PI / 180;
3039 mTiltYScale = M_PI / 180;
3040
3041 mOrientedRanges.haveTilt = true;
3042
3043 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3044 mOrientedRanges.tilt.source = mSource;
3045 mOrientedRanges.tilt.min = 0;
3046 mOrientedRanges.tilt.max = M_PI_2;
3047 mOrientedRanges.tilt.flat = 0;
3048 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003049 }
3050
Jeff Brown8d608662010-08-30 03:02:23 -07003051 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07003052 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003053 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003054 if (mHaveTilt) {
3055 mOrientedRanges.haveOrientation = true;
3056
3057 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3058 mOrientedRanges.orientation.source = mSource;
3059 mOrientedRanges.orientation.min = -M_PI;
3060 mOrientedRanges.orientation.max = M_PI;
3061 mOrientedRanges.orientation.flat = 0;
3062 mOrientedRanges.orientation.fuzz = 0;
3063 } else if (mCalibration.orientationCalibration !=
3064 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003065 if (mCalibration.orientationCalibration
3066 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003067 if (mRawPointerAxes.orientation.valid) {
3068 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
3069 mRawPointerAxes.orientation.maxValue);
3070 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
3071 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07003072 }
3073 }
3074
Jeff Brownbe1aa822011-07-27 16:04:54 -07003075 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003076
Jeff Brownbe1aa822011-07-27 16:04:54 -07003077 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003078 mOrientedRanges.orientation.source = mSource;
3079 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003080 mOrientedRanges.orientation.max = M_PI_2;
3081 mOrientedRanges.orientation.flat = 0;
3082 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003083 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003084
3085 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003086 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003087 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3088 if (mCalibration.distanceCalibration
3089 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3090 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003091 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003092 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003093 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003094 }
3095 }
3096
Jeff Brownbe1aa822011-07-27 16:04:54 -07003097 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003098
Jeff Brownbe1aa822011-07-27 16:04:54 -07003099 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003100 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003101 mOrientedRanges.distance.min =
3102 mRawPointerAxes.distance.minValue * mDistanceScale;
3103 mOrientedRanges.distance.max =
3104 mRawPointerAxes.distance.minValue * mDistanceScale;
3105 mOrientedRanges.distance.flat = 0;
3106 mOrientedRanges.distance.fuzz =
3107 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003108 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003109 }
3110
Jeff Brown65fd2512011-08-18 11:20:58 -07003111 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08003112 // Compute oriented surface dimensions, precision, scales and ranges.
3113 // Note that the maximum value reported is an inclusive maximum value so it is one
3114 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003115 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003116 case DISPLAY_ORIENTATION_90:
3117 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003118 mOrientedSurfaceWidth = mSurfaceHeight;
3119 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08003120
Jeff Brownbe1aa822011-07-27 16:04:54 -07003121 mOrientedXPrecision = mYPrecision;
3122 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003123
Jeff Brownbe1aa822011-07-27 16:04:54 -07003124 mOrientedRanges.x.min = 0;
3125 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3126 * mYScale;
3127 mOrientedRanges.x.flat = 0;
3128 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003129
Jeff Brownbe1aa822011-07-27 16:04:54 -07003130 mOrientedRanges.y.min = 0;
3131 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3132 * mXScale;
3133 mOrientedRanges.y.flat = 0;
3134 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003135 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003136
Jeff Brown6d0fec22010-07-23 21:28:06 -07003137 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003138 mOrientedSurfaceWidth = mSurfaceWidth;
3139 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08003140
Jeff Brownbe1aa822011-07-27 16:04:54 -07003141 mOrientedXPrecision = mXPrecision;
3142 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003143
Jeff Brownbe1aa822011-07-27 16:04:54 -07003144 mOrientedRanges.x.min = 0;
3145 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3146 * mXScale;
3147 mOrientedRanges.x.flat = 0;
3148 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003149
Jeff Brownbe1aa822011-07-27 16:04:54 -07003150 mOrientedRanges.y.min = 0;
3151 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3152 * mYScale;
3153 mOrientedRanges.y.flat = 0;
3154 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003155 break;
3156 }
Jeff Brownace13b12011-03-09 17:39:48 -08003157
3158 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003159 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003160 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3161 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07003162 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003163 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
3164 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003165
Jeff Brown2352b972011-04-12 22:39:53 -07003166 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003167 // given area relative to the diagonal size of the display when no acceleration
3168 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003169 // Assume that the touch pad has a square aspect ratio such that movements in
3170 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003171 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003172 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003173 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003174
3175 // Scale zooms to cover a smaller range of the display than movements do.
3176 // This value determines the area around the pointer that is affected by freeform
3177 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003178 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003179 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003180 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003181
Jeff Brown2352b972011-04-12 22:39:53 -07003182 // Max width between pointers to detect a swipe gesture is more than some fraction
3183 // of the diagonal axis of the touch pad. Touches that are wider than this are
3184 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003185 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003186 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003187 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003188
Jeff Brown65fd2512011-08-18 11:20:58 -07003189 // Abort current pointer usages because the state has changed.
3190 abortPointerUsage(when, 0 /*policyFlags*/);
3191
3192 // Inform the dispatcher about the changes.
3193 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003194 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003195 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003196}
3197
Jeff Brownbe1aa822011-07-27 16:04:54 -07003198void TouchInputMapper::dumpSurface(String8& dump) {
3199 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3200 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
3201 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003202}
3203
Jeff Brownbe1aa822011-07-27 16:04:54 -07003204void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003205 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003206 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003207
Jeff Brownbe1aa822011-07-27 16:04:54 -07003208 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003209
Jeff Brown6328cdc2010-07-29 18:18:33 -07003210 if (virtualKeyDefinitions.size() == 0) {
3211 return;
3212 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003213
Jeff Brownbe1aa822011-07-27 16:04:54 -07003214 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003215
Jeff Brownbe1aa822011-07-27 16:04:54 -07003216 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3217 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3218 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3219 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003220
3221 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003222 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003223 virtualKeyDefinitions[i];
3224
Jeff Brownbe1aa822011-07-27 16:04:54 -07003225 mVirtualKeys.add();
3226 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003227
3228 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3229 int32_t keyCode;
3230 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003231 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003232 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003233 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003234 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003235 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003236 }
3237
Jeff Brown6328cdc2010-07-29 18:18:33 -07003238 virtualKey.keyCode = keyCode;
3239 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003240
Jeff Brown6328cdc2010-07-29 18:18:33 -07003241 // convert the key definition's display coordinates into touch coordinates for a hit box
3242 int32_t halfWidth = virtualKeyDefinition.width / 2;
3243 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003244
Jeff Brown6328cdc2010-07-29 18:18:33 -07003245 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003246 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003247 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003248 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003249 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003250 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003251 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003252 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003253 }
3254}
3255
Jeff Brownbe1aa822011-07-27 16:04:54 -07003256void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3257 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003258 dump.append(INDENT3 "Virtual Keys:\n");
3259
Jeff Brownbe1aa822011-07-27 16:04:54 -07003260 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3261 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003262 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3263 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3264 i, virtualKey.scanCode, virtualKey.keyCode,
3265 virtualKey.hitLeft, virtualKey.hitRight,
3266 virtualKey.hitTop, virtualKey.hitBottom);
3267 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003268 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003269}
3270
Jeff Brown8d608662010-08-30 03:02:23 -07003271void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003272 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003273 Calibration& out = mCalibration;
3274
Jeff Browna1f89ce2011-08-11 00:05:01 -07003275 // Size
3276 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3277 String8 sizeCalibrationString;
3278 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3279 if (sizeCalibrationString == "none") {
3280 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3281 } else if (sizeCalibrationString == "geometric") {
3282 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3283 } else if (sizeCalibrationString == "diameter") {
3284 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3285 } else if (sizeCalibrationString == "area") {
3286 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3287 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003288 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003289 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003290 }
3291 }
3292
Jeff Browna1f89ce2011-08-11 00:05:01 -07003293 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3294 out.sizeScale);
3295 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3296 out.sizeBias);
3297 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3298 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003299
3300 // Pressure
3301 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3302 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003303 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003304 if (pressureCalibrationString == "none") {
3305 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3306 } else if (pressureCalibrationString == "physical") {
3307 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3308 } else if (pressureCalibrationString == "amplitude") {
3309 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3310 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003311 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003312 pressureCalibrationString.string());
3313 }
3314 }
3315
Jeff Brown8d608662010-08-30 03:02:23 -07003316 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3317 out.pressureScale);
3318
Jeff Brown8d608662010-08-30 03:02:23 -07003319 // Orientation
3320 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3321 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003322 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003323 if (orientationCalibrationString == "none") {
3324 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3325 } else if (orientationCalibrationString == "interpolated") {
3326 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003327 } else if (orientationCalibrationString == "vector") {
3328 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003329 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003330 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003331 orientationCalibrationString.string());
3332 }
3333 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003334
3335 // Distance
3336 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3337 String8 distanceCalibrationString;
3338 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3339 if (distanceCalibrationString == "none") {
3340 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3341 } else if (distanceCalibrationString == "scaled") {
3342 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3343 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003344 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003345 distanceCalibrationString.string());
3346 }
3347 }
3348
3349 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3350 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003351}
3352
3353void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003354 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003355 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3356 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3357 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003358 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003359 } else {
3360 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3361 }
Jeff Brown8d608662010-08-30 03:02:23 -07003362
Jeff Browna1f89ce2011-08-11 00:05:01 -07003363 // Pressure
3364 if (mRawPointerAxes.pressure.valid) {
3365 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3366 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3367 }
3368 } else {
3369 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003370 }
3371
3372 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003373 if (mRawPointerAxes.orientation.valid) {
3374 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003375 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003376 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003377 } else {
3378 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003379 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003380
3381 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003382 if (mRawPointerAxes.distance.valid) {
3383 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003384 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003385 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003386 } else {
3387 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003388 }
Jeff Brown8d608662010-08-30 03:02:23 -07003389}
3390
Jeff Brownef3d7e82010-09-30 14:33:04 -07003391void TouchInputMapper::dumpCalibration(String8& dump) {
3392 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003393
Jeff Browna1f89ce2011-08-11 00:05:01 -07003394 // Size
3395 switch (mCalibration.sizeCalibration) {
3396 case Calibration::SIZE_CALIBRATION_NONE:
3397 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003398 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003399 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3400 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003401 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003402 case Calibration::SIZE_CALIBRATION_DIAMETER:
3403 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3404 break;
3405 case Calibration::SIZE_CALIBRATION_AREA:
3406 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003407 break;
3408 default:
Steve Blockec193de2012-01-09 18:35:44 +00003409 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003410 }
3411
Jeff Browna1f89ce2011-08-11 00:05:01 -07003412 if (mCalibration.haveSizeScale) {
3413 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3414 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003415 }
3416
Jeff Browna1f89ce2011-08-11 00:05:01 -07003417 if (mCalibration.haveSizeBias) {
3418 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3419 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003420 }
3421
Jeff Browna1f89ce2011-08-11 00:05:01 -07003422 if (mCalibration.haveSizeIsSummed) {
3423 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3424 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003425 }
3426
3427 // Pressure
3428 switch (mCalibration.pressureCalibration) {
3429 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003430 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003431 break;
3432 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003433 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003434 break;
3435 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003436 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003437 break;
3438 default:
Steve Blockec193de2012-01-09 18:35:44 +00003439 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003440 }
3441
Jeff Brown8d608662010-08-30 03:02:23 -07003442 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003443 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3444 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003445 }
3446
Jeff Brown8d608662010-08-30 03:02:23 -07003447 // Orientation
3448 switch (mCalibration.orientationCalibration) {
3449 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003450 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003451 break;
3452 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003453 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003454 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003455 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3456 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3457 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003458 default:
Steve Blockec193de2012-01-09 18:35:44 +00003459 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003460 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003461
3462 // Distance
3463 switch (mCalibration.distanceCalibration) {
3464 case Calibration::DISTANCE_CALIBRATION_NONE:
3465 dump.append(INDENT4 "touch.distance.calibration: none\n");
3466 break;
3467 case Calibration::DISTANCE_CALIBRATION_SCALED:
3468 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3469 break;
3470 default:
Steve Blockec193de2012-01-09 18:35:44 +00003471 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003472 }
3473
3474 if (mCalibration.haveDistanceScale) {
3475 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3476 mCalibration.distanceScale);
3477 }
Jeff Brown8d608662010-08-30 03:02:23 -07003478}
3479
Jeff Brown65fd2512011-08-18 11:20:58 -07003480void TouchInputMapper::reset(nsecs_t when) {
3481 mCursorButtonAccumulator.reset(getDevice());
3482 mCursorScrollAccumulator.reset(getDevice());
3483 mTouchButtonAccumulator.reset(getDevice());
3484
3485 mPointerVelocityControl.reset();
3486 mWheelXVelocityControl.reset();
3487 mWheelYVelocityControl.reset();
3488
Jeff Brownbe1aa822011-07-27 16:04:54 -07003489 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003490 mLastRawPointerData.clear();
3491 mCurrentCookedPointerData.clear();
3492 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003493 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003494 mLastButtonState = 0;
3495 mCurrentRawVScroll = 0;
3496 mCurrentRawHScroll = 0;
3497 mCurrentFingerIdBits.clear();
3498 mLastFingerIdBits.clear();
3499 mCurrentStylusIdBits.clear();
3500 mLastStylusIdBits.clear();
3501 mCurrentMouseIdBits.clear();
3502 mLastMouseIdBits.clear();
3503 mPointerUsage = POINTER_USAGE_NONE;
3504 mSentHoverEnter = false;
3505 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003506
Jeff Brown65fd2512011-08-18 11:20:58 -07003507 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003508
Jeff Brown65fd2512011-08-18 11:20:58 -07003509 mPointerGesture.reset();
3510 mPointerSimple.reset();
3511
3512 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003513 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3514 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003515 }
3516
Jeff Brown65fd2512011-08-18 11:20:58 -07003517 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003518}
3519
Jeff Brown65fd2512011-08-18 11:20:58 -07003520void TouchInputMapper::process(const RawEvent* rawEvent) {
3521 mCursorButtonAccumulator.process(rawEvent);
3522 mCursorScrollAccumulator.process(rawEvent);
3523 mTouchButtonAccumulator.process(rawEvent);
3524
Jeff Brown49ccac52012-04-11 18:27:33 -07003525 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003526 sync(rawEvent->when);
3527 }
3528}
3529
3530void TouchInputMapper::sync(nsecs_t when) {
3531 // Sync button state.
3532 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3533 | mCursorButtonAccumulator.getButtonState();
3534
3535 // Sync scroll state.
3536 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3537 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3538 mCursorScrollAccumulator.finishSync();
3539
3540 // Sync touch state.
3541 bool havePointerIds = true;
3542 mCurrentRawPointerData.clear();
3543 syncTouch(when, &havePointerIds);
3544
Jeff Brownaa3855d2011-03-17 01:34:19 -07003545#if DEBUG_RAW_EVENTS
3546 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003547 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003548 mLastRawPointerData.pointerCount,
3549 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003550 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003551 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003552 "hovering ids 0x%08x -> 0x%08x",
3553 mLastRawPointerData.pointerCount,
3554 mCurrentRawPointerData.pointerCount,
3555 mLastRawPointerData.touchingIdBits.value,
3556 mCurrentRawPointerData.touchingIdBits.value,
3557 mLastRawPointerData.hoveringIdBits.value,
3558 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003559 }
3560#endif
3561
Jeff Brown65fd2512011-08-18 11:20:58 -07003562 // Reset state that we will compute below.
3563 mCurrentFingerIdBits.clear();
3564 mCurrentStylusIdBits.clear();
3565 mCurrentMouseIdBits.clear();
3566 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003567
Jeff Brown65fd2512011-08-18 11:20:58 -07003568 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3569 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003570 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003571 mCurrentButtonState = 0;
3572 } else {
3573 // Preprocess pointer data.
3574 if (!havePointerIds) {
3575 assignPointerIds();
3576 }
3577
3578 // Handle policy on initial down or hover events.
3579 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003580 bool initialDown = mLastRawPointerData.pointerCount == 0
3581 && mCurrentRawPointerData.pointerCount != 0;
3582 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3583 if (initialDown || buttonsPressed) {
3584 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003585 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003586 getContext()->fadePointer();
3587 }
3588
3589 // Initial downs on external touch devices should wake the device.
3590 // We don't do this for internal touch screens to prevent them from waking
3591 // up in your pocket.
3592 // TODO: Use the input device configuration to control this behavior more finely.
3593 if (getDevice()->isExternal()) {
3594 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3595 }
3596 }
3597
3598 // Synthesize key down from raw buttons if needed.
3599 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3600 policyFlags, mLastButtonState, mCurrentButtonState);
3601
3602 // Consume raw off-screen touches before cooking pointer data.
3603 // If touches are consumed, subsequent code will not receive any pointer data.
3604 if (consumeRawTouches(when, policyFlags)) {
3605 mCurrentRawPointerData.clear();
3606 }
3607
3608 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3609 // with cooked pointer data that has the same ids and indices as the raw data.
3610 // The following code can use either the raw or cooked data, as needed.
3611 cookPointerData();
3612
3613 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003614 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003615 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3616 uint32_t id = idBits.clearFirstMarkedBit();
3617 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3618 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3619 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3620 mCurrentStylusIdBits.markBit(id);
3621 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3622 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3623 mCurrentFingerIdBits.markBit(id);
3624 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3625 mCurrentMouseIdBits.markBit(id);
3626 }
3627 }
3628 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3629 uint32_t id = idBits.clearFirstMarkedBit();
3630 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3631 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3632 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3633 mCurrentStylusIdBits.markBit(id);
3634 }
3635 }
3636
3637 // Stylus takes precedence over all tools, then mouse, then finger.
3638 PointerUsage pointerUsage = mPointerUsage;
3639 if (!mCurrentStylusIdBits.isEmpty()) {
3640 mCurrentMouseIdBits.clear();
3641 mCurrentFingerIdBits.clear();
3642 pointerUsage = POINTER_USAGE_STYLUS;
3643 } else if (!mCurrentMouseIdBits.isEmpty()) {
3644 mCurrentFingerIdBits.clear();
3645 pointerUsage = POINTER_USAGE_MOUSE;
3646 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3647 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003648 }
3649
3650 dispatchPointerUsage(when, policyFlags, pointerUsage);
3651 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003652 if (mDeviceMode == DEVICE_MODE_DIRECT
3653 && mConfig.showTouches && mPointerController != NULL) {
3654 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3655 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3656
3657 mPointerController->setButtonState(mCurrentButtonState);
3658 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3659 mCurrentCookedPointerData.idToIndex,
3660 mCurrentCookedPointerData.touchingIdBits);
3661 }
3662
Jeff Brown65fd2512011-08-18 11:20:58 -07003663 dispatchHoverExit(when, policyFlags);
3664 dispatchTouches(when, policyFlags);
3665 dispatchHoverEnterAndMove(when, policyFlags);
3666 }
3667
3668 // Synthesize key up from raw buttons if needed.
3669 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3670 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003671 }
3672
Jeff Brown6328cdc2010-07-29 18:18:33 -07003673 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003674 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3675 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3676 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003677 mLastFingerIdBits = mCurrentFingerIdBits;
3678 mLastStylusIdBits = mCurrentStylusIdBits;
3679 mLastMouseIdBits = mCurrentMouseIdBits;
3680
3681 // Clear some transient state.
3682 mCurrentRawVScroll = 0;
3683 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003684}
3685
Jeff Brown79ac9692011-04-19 21:20:10 -07003686void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003687 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003688 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3689 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3690 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003691 }
3692}
3693
Jeff Brownbe1aa822011-07-27 16:04:54 -07003694bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3695 // Check for release of a virtual key.
3696 if (mCurrentVirtualKey.down) {
3697 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3698 // Pointer went up while virtual key was down.
3699 mCurrentVirtualKey.down = false;
3700 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003701#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003702 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003703 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003704#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003705 dispatchVirtualKey(when, policyFlags,
3706 AKEY_EVENT_ACTION_UP,
3707 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003708 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003709 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003710 }
3711
Jeff Brownbe1aa822011-07-27 16:04:54 -07003712 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3713 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3714 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3715 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3716 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3717 // Pointer is still within the space of the virtual key.
3718 return true;
3719 }
3720 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003721
Jeff Brownbe1aa822011-07-27 16:04:54 -07003722 // Pointer left virtual key area or another pointer also went down.
3723 // Send key cancellation but do not consume the touch yet.
3724 // This is useful when the user swipes through from the virtual key area
3725 // into the main display surface.
3726 mCurrentVirtualKey.down = false;
3727 if (!mCurrentVirtualKey.ignored) {
3728#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003729 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003730 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3731#endif
3732 dispatchVirtualKey(when, policyFlags,
3733 AKEY_EVENT_ACTION_UP,
3734 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3735 | AKEY_EVENT_FLAG_CANCELED);
3736 }
3737 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003738
Jeff Brownbe1aa822011-07-27 16:04:54 -07003739 if (mLastRawPointerData.touchingIdBits.isEmpty()
3740 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3741 // Pointer just went down. Check for virtual key press or off-screen touches.
3742 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3743 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3744 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3745 // If exactly one pointer went down, check for virtual key hit.
3746 // Otherwise we will drop the entire stroke.
3747 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3748 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3749 if (virtualKey) {
3750 mCurrentVirtualKey.down = true;
3751 mCurrentVirtualKey.downTime = when;
3752 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3753 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3754 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3755 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3756
3757 if (!mCurrentVirtualKey.ignored) {
3758#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003759 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003760 mCurrentVirtualKey.keyCode,
3761 mCurrentVirtualKey.scanCode);
3762#endif
3763 dispatchVirtualKey(when, policyFlags,
3764 AKEY_EVENT_ACTION_DOWN,
3765 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3766 }
3767 }
3768 }
3769 return true;
3770 }
3771 }
3772
Jeff Brownfe508922011-01-18 15:10:10 -08003773 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003774 // most recent touch within the screen area. The idea is to filter out stray
3775 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003776 //
3777 // Problems we're trying to solve:
3778 //
3779 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3780 // virtual key area that is implemented by a separate touch panel and accidentally
3781 // triggers a virtual key.
3782 //
3783 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3784 // area and accidentally triggers a virtual key. This often happens when virtual keys
3785 // are layed out below the screen near to where the on screen keyboard's space bar
3786 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003787 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003788 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003789 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003790 return false;
3791}
3792
3793void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3794 int32_t keyEventAction, int32_t keyEventFlags) {
3795 int32_t keyCode = mCurrentVirtualKey.keyCode;
3796 int32_t scanCode = mCurrentVirtualKey.scanCode;
3797 nsecs_t downTime = mCurrentVirtualKey.downTime;
3798 int32_t metaState = mContext->getGlobalMetaState();
3799 policyFlags |= POLICY_FLAG_VIRTUAL;
3800
3801 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3802 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3803 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003804}
3805
Jeff Brown6d0fec22010-07-23 21:28:06 -07003806void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003807 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3808 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003809 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003810 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003811
3812 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003813 if (!currentIdBits.isEmpty()) {
3814 // No pointer id changes so this is a move event.
3815 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003816 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003817 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3818 AMOTION_EVENT_EDGE_FLAG_NONE,
3819 mCurrentCookedPointerData.pointerProperties,
3820 mCurrentCookedPointerData.pointerCoords,
3821 mCurrentCookedPointerData.idToIndex,
3822 currentIdBits, -1,
3823 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3824 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003825 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003826 // There may be pointers going up and pointers going down and pointers moving
3827 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003828 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3829 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003830 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003831 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003832
Jeff Brownace13b12011-03-09 17:39:48 -08003833 // Update last coordinates of pointers that have moved so that we observe the new
3834 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003835 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003836 mCurrentCookedPointerData.pointerProperties,
3837 mCurrentCookedPointerData.pointerCoords,
3838 mCurrentCookedPointerData.idToIndex,
3839 mLastCookedPointerData.pointerProperties,
3840 mLastCookedPointerData.pointerCoords,
3841 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003842 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003843 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003844 moveNeeded = true;
3845 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003846
Jeff Brownace13b12011-03-09 17:39:48 -08003847 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003848 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003849 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003850
Jeff Brown65fd2512011-08-18 11:20:58 -07003851 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003852 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003853 mLastCookedPointerData.pointerProperties,
3854 mLastCookedPointerData.pointerCoords,
3855 mLastCookedPointerData.idToIndex,
3856 dispatchedIdBits, upId,
3857 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003858 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003859 }
3860
Jeff Brownc3db8582010-10-20 15:33:38 -07003861 // Dispatch move events if any of the remaining pointers moved from their old locations.
3862 // Although applications receive new locations as part of individual pointer up
3863 // events, they do not generally handle them except when presented in a move event.
3864 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003865 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003866 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003867 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003868 mCurrentCookedPointerData.pointerProperties,
3869 mCurrentCookedPointerData.pointerCoords,
3870 mCurrentCookedPointerData.idToIndex,
3871 dispatchedIdBits, -1,
3872 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003873 }
3874
3875 // Dispatch pointer down events using the new pointer locations.
3876 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003877 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003878 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003879
Jeff Brownace13b12011-03-09 17:39:48 -08003880 if (dispatchedIdBits.count() == 1) {
3881 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003882 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003883 }
3884
Jeff Brown65fd2512011-08-18 11:20:58 -07003885 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003886 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003887 mCurrentCookedPointerData.pointerProperties,
3888 mCurrentCookedPointerData.pointerCoords,
3889 mCurrentCookedPointerData.idToIndex,
3890 dispatchedIdBits, downId,
3891 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003892 }
3893 }
Jeff Brownace13b12011-03-09 17:39:48 -08003894}
3895
Jeff Brownbe1aa822011-07-27 16:04:54 -07003896void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3897 if (mSentHoverEnter &&
3898 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3899 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3900 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003901 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003902 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3903 mLastCookedPointerData.pointerProperties,
3904 mLastCookedPointerData.pointerCoords,
3905 mLastCookedPointerData.idToIndex,
3906 mLastCookedPointerData.hoveringIdBits, -1,
3907 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3908 mSentHoverEnter = false;
3909 }
3910}
Jeff Brownace13b12011-03-09 17:39:48 -08003911
Jeff Brownbe1aa822011-07-27 16:04:54 -07003912void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3913 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3914 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3915 int32_t metaState = getContext()->getGlobalMetaState();
3916 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003917 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003918 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3919 mCurrentCookedPointerData.pointerProperties,
3920 mCurrentCookedPointerData.pointerCoords,
3921 mCurrentCookedPointerData.idToIndex,
3922 mCurrentCookedPointerData.hoveringIdBits, -1,
3923 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3924 mSentHoverEnter = true;
3925 }
Jeff Brownace13b12011-03-09 17:39:48 -08003926
Jeff Brown65fd2512011-08-18 11:20:58 -07003927 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003928 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3929 mCurrentCookedPointerData.pointerProperties,
3930 mCurrentCookedPointerData.pointerCoords,
3931 mCurrentCookedPointerData.idToIndex,
3932 mCurrentCookedPointerData.hoveringIdBits, -1,
3933 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3934 }
3935}
3936
3937void TouchInputMapper::cookPointerData() {
3938 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3939
3940 mCurrentCookedPointerData.clear();
3941 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3942 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3943 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3944
3945 // Walk through the the active pointers and map device coordinates onto
3946 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003947 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003948 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003949
Jeff Browna1f89ce2011-08-11 00:05:01 -07003950 // Size
3951 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3952 switch (mCalibration.sizeCalibration) {
3953 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3954 case Calibration::SIZE_CALIBRATION_DIAMETER:
3955 case Calibration::SIZE_CALIBRATION_AREA:
3956 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3957 touchMajor = in.touchMajor;
3958 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3959 toolMajor = in.toolMajor;
3960 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3961 size = mRawPointerAxes.touchMinor.valid
3962 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3963 } else if (mRawPointerAxes.touchMajor.valid) {
3964 toolMajor = touchMajor = in.touchMajor;
3965 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3966 ? in.touchMinor : in.touchMajor;
3967 size = mRawPointerAxes.touchMinor.valid
3968 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3969 } else if (mRawPointerAxes.toolMajor.valid) {
3970 touchMajor = toolMajor = in.toolMajor;
3971 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3972 ? in.toolMinor : in.toolMajor;
3973 size = mRawPointerAxes.toolMinor.valid
3974 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003975 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003976 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003977 "Size calibration should have been resolved to NONE.");
3978 touchMajor = 0;
3979 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003980 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003981 toolMinor = 0;
3982 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003983 }
Jeff Brownace13b12011-03-09 17:39:48 -08003984
Jeff Browna1f89ce2011-08-11 00:05:01 -07003985 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3986 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3987 if (touchingCount > 1) {
3988 touchMajor /= touchingCount;
3989 touchMinor /= touchingCount;
3990 toolMajor /= touchingCount;
3991 toolMinor /= touchingCount;
3992 size /= touchingCount;
3993 }
3994 }
Jeff Brownace13b12011-03-09 17:39:48 -08003995
Jeff Browna1f89ce2011-08-11 00:05:01 -07003996 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3997 touchMajor *= mGeometricScale;
3998 touchMinor *= mGeometricScale;
3999 toolMajor *= mGeometricScale;
4000 toolMinor *= mGeometricScale;
4001 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4002 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004003 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004004 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4005 toolMinor = toolMajor;
4006 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4007 touchMinor = touchMajor;
4008 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004009 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004010
4011 mCalibration.applySizeScaleAndBias(&touchMajor);
4012 mCalibration.applySizeScaleAndBias(&touchMinor);
4013 mCalibration.applySizeScaleAndBias(&toolMajor);
4014 mCalibration.applySizeScaleAndBias(&toolMinor);
4015 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004016 break;
4017 default:
4018 touchMajor = 0;
4019 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004020 toolMajor = 0;
4021 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004022 size = 0;
4023 break;
4024 }
4025
Jeff Browna1f89ce2011-08-11 00:05:01 -07004026 // Pressure
4027 float pressure;
4028 switch (mCalibration.pressureCalibration) {
4029 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4030 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4031 pressure = in.pressure * mPressureScale;
4032 break;
4033 default:
4034 pressure = in.isHovering ? 0 : 1;
4035 break;
4036 }
4037
Jeff Brown65fd2512011-08-18 11:20:58 -07004038 // Tilt and Orientation
4039 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004040 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004041 if (mHaveTilt) {
4042 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4043 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4044 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4045 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4046 } else {
4047 tilt = 0;
4048
4049 switch (mCalibration.orientationCalibration) {
4050 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
4051 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
4052 break;
4053 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4054 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4055 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4056 if (c1 != 0 || c2 != 0) {
4057 orientation = atan2f(c1, c2) * 0.5f;
4058 float confidence = hypotf(c1, c2);
4059 float scale = 1.0f + confidence / 16.0f;
4060 touchMajor *= scale;
4061 touchMinor /= scale;
4062 toolMajor *= scale;
4063 toolMinor /= scale;
4064 } else {
4065 orientation = 0;
4066 }
4067 break;
4068 }
4069 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004070 orientation = 0;
4071 }
Jeff Brownace13b12011-03-09 17:39:48 -08004072 }
4073
Jeff Brown80fd47c2011-05-24 01:07:44 -07004074 // Distance
4075 float distance;
4076 switch (mCalibration.distanceCalibration) {
4077 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004078 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004079 break;
4080 default:
4081 distance = 0;
4082 }
4083
Jeff Brownace13b12011-03-09 17:39:48 -08004084 // X and Y
4085 // Adjust coords for surface orientation.
4086 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004087 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004088 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004089 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
4090 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004091 orientation -= M_PI_2;
4092 if (orientation < - M_PI_2) {
4093 orientation += M_PI;
4094 }
4095 break;
4096 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004097 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
4098 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004099 break;
4100 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004101 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
4102 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004103 orientation += M_PI_2;
4104 if (orientation > M_PI_2) {
4105 orientation -= M_PI;
4106 }
4107 break;
4108 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004109 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
4110 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004111 break;
4112 }
4113
4114 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004115 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004116 out.clear();
4117 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4118 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4119 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4120 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4121 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4122 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4123 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4124 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4125 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004126 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004127 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004128
4129 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004130 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4131 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004132 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004133 properties.id = id;
4134 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004135
Jeff Brownbe1aa822011-07-27 16:04:54 -07004136 // Write id index.
4137 mCurrentCookedPointerData.idToIndex[id] = i;
4138 }
Jeff Brownace13b12011-03-09 17:39:48 -08004139}
4140
Jeff Brown65fd2512011-08-18 11:20:58 -07004141void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4142 PointerUsage pointerUsage) {
4143 if (pointerUsage != mPointerUsage) {
4144 abortPointerUsage(when, policyFlags);
4145 mPointerUsage = pointerUsage;
4146 }
4147
4148 switch (mPointerUsage) {
4149 case POINTER_USAGE_GESTURES:
4150 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4151 break;
4152 case POINTER_USAGE_STYLUS:
4153 dispatchPointerStylus(when, policyFlags);
4154 break;
4155 case POINTER_USAGE_MOUSE:
4156 dispatchPointerMouse(when, policyFlags);
4157 break;
4158 default:
4159 break;
4160 }
4161}
4162
4163void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4164 switch (mPointerUsage) {
4165 case POINTER_USAGE_GESTURES:
4166 abortPointerGestures(when, policyFlags);
4167 break;
4168 case POINTER_USAGE_STYLUS:
4169 abortPointerStylus(when, policyFlags);
4170 break;
4171 case POINTER_USAGE_MOUSE:
4172 abortPointerMouse(when, policyFlags);
4173 break;
4174 default:
4175 break;
4176 }
4177
4178 mPointerUsage = POINTER_USAGE_NONE;
4179}
4180
Jeff Brown79ac9692011-04-19 21:20:10 -07004181void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4182 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004183 // Update current gesture coordinates.
4184 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004185 bool sendEvents = preparePointerGestures(when,
4186 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4187 if (!sendEvents) {
4188 return;
4189 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004190 if (finishPreviousGesture) {
4191 cancelPreviousGesture = false;
4192 }
Jeff Brownace13b12011-03-09 17:39:48 -08004193
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004194 // Update the pointer presentation and spots.
4195 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4196 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4197 if (finishPreviousGesture || cancelPreviousGesture) {
4198 mPointerController->clearSpots();
4199 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004200 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4201 mPointerGesture.currentGestureIdToIndex,
4202 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004203 } else {
4204 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4205 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004206
Jeff Brown538881e2011-05-25 18:23:38 -07004207 // Show or hide the pointer if needed.
4208 switch (mPointerGesture.currentGestureMode) {
4209 case PointerGesture::NEUTRAL:
4210 case PointerGesture::QUIET:
4211 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4212 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4213 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4214 // Remind the user of where the pointer is after finishing a gesture with spots.
4215 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4216 }
4217 break;
4218 case PointerGesture::TAP:
4219 case PointerGesture::TAP_DRAG:
4220 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4221 case PointerGesture::HOVER:
4222 case PointerGesture::PRESS:
4223 // Unfade the pointer when the current gesture manipulates the
4224 // area directly under the pointer.
4225 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4226 break;
4227 case PointerGesture::SWIPE:
4228 case PointerGesture::FREEFORM:
4229 // Fade the pointer when the current gesture manipulates a different
4230 // area and there are spots to guide the user experience.
4231 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4232 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4233 } else {
4234 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4235 }
4236 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004237 }
4238
Jeff Brownace13b12011-03-09 17:39:48 -08004239 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004240 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004241 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004242
4243 // Update last coordinates of pointers that have moved so that we observe the new
4244 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004245 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4246 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4247 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004248 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004249 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4250 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4251 bool moveNeeded = false;
4252 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004253 && !mPointerGesture.lastGestureIdBits.isEmpty()
4254 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004255 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4256 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004257 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004258 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004259 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004260 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4261 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004262 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004263 moveNeeded = true;
4264 }
Jeff Brownace13b12011-03-09 17:39:48 -08004265 }
4266
4267 // Send motion events for all pointers that went up or were canceled.
4268 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4269 if (!dispatchedGestureIdBits.isEmpty()) {
4270 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004271 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004272 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4273 AMOTION_EVENT_EDGE_FLAG_NONE,
4274 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004275 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4276 dispatchedGestureIdBits, -1,
4277 0, 0, mPointerGesture.downTime);
4278
4279 dispatchedGestureIdBits.clear();
4280 } else {
4281 BitSet32 upGestureIdBits;
4282 if (finishPreviousGesture) {
4283 upGestureIdBits = dispatchedGestureIdBits;
4284 } else {
4285 upGestureIdBits.value = dispatchedGestureIdBits.value
4286 & ~mPointerGesture.currentGestureIdBits.value;
4287 }
4288 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004289 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004290
Jeff Brown65fd2512011-08-18 11:20:58 -07004291 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004292 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004293 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4294 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004295 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4296 dispatchedGestureIdBits, id,
4297 0, 0, mPointerGesture.downTime);
4298
4299 dispatchedGestureIdBits.clearBit(id);
4300 }
4301 }
4302 }
4303
4304 // Send motion events for all pointers that moved.
4305 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004306 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004307 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4308 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004309 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4310 dispatchedGestureIdBits, -1,
4311 0, 0, mPointerGesture.downTime);
4312 }
4313
4314 // Send motion events for all pointers that went down.
4315 if (down) {
4316 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4317 & ~dispatchedGestureIdBits.value);
4318 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004319 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004320 dispatchedGestureIdBits.markBit(id);
4321
Jeff Brownace13b12011-03-09 17:39:48 -08004322 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004323 mPointerGesture.downTime = when;
4324 }
4325
Jeff Brown65fd2512011-08-18 11:20:58 -07004326 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004327 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004328 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004329 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4330 dispatchedGestureIdBits, id,
4331 0, 0, mPointerGesture.downTime);
4332 }
4333 }
4334
Jeff Brownace13b12011-03-09 17:39:48 -08004335 // Send motion events for hover.
4336 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004337 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004338 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4339 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4340 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004341 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4342 mPointerGesture.currentGestureIdBits, -1,
4343 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004344 } else if (dispatchedGestureIdBits.isEmpty()
4345 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4346 // Synthesize a hover move event after all pointers go up to indicate that
4347 // the pointer is hovering again even if the user is not currently touching
4348 // the touch pad. This ensures that a view will receive a fresh hover enter
4349 // event after a tap.
4350 float x, y;
4351 mPointerController->getPosition(&x, &y);
4352
4353 PointerProperties pointerProperties;
4354 pointerProperties.clear();
4355 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004356 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004357
4358 PointerCoords pointerCoords;
4359 pointerCoords.clear();
4360 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4361 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4362
Jeff Brown65fd2512011-08-18 11:20:58 -07004363 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004364 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4365 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4366 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004367 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004368 }
4369
4370 // Update state.
4371 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4372 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004373 mPointerGesture.lastGestureIdBits.clear();
4374 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004375 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4376 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004377 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004378 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004379 mPointerGesture.lastGestureProperties[index].copyFrom(
4380 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004381 mPointerGesture.lastGestureCoords[index].copyFrom(
4382 mPointerGesture.currentGestureCoords[index]);
4383 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004384 }
4385 }
4386}
4387
Jeff Brown65fd2512011-08-18 11:20:58 -07004388void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4389 // Cancel previously dispatches pointers.
4390 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4391 int32_t metaState = getContext()->getGlobalMetaState();
4392 int32_t buttonState = mCurrentButtonState;
4393 dispatchMotion(when, policyFlags, mSource,
4394 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4395 AMOTION_EVENT_EDGE_FLAG_NONE,
4396 mPointerGesture.lastGestureProperties,
4397 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4398 mPointerGesture.lastGestureIdBits, -1,
4399 0, 0, mPointerGesture.downTime);
4400 }
4401
4402 // Reset the current pointer gesture.
4403 mPointerGesture.reset();
4404 mPointerVelocityControl.reset();
4405
4406 // Remove any current spots.
4407 if (mPointerController != NULL) {
4408 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4409 mPointerController->clearSpots();
4410 }
4411}
4412
Jeff Brown79ac9692011-04-19 21:20:10 -07004413bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4414 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004415 *outCancelPreviousGesture = false;
4416 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004417
Jeff Brown79ac9692011-04-19 21:20:10 -07004418 // Handle TAP timeout.
4419 if (isTimeout) {
4420#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004421 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004422#endif
4423
4424 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004425 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004426 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004427 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004428 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004429 } else {
4430 // The tap is finished.
4431#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004432 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004433#endif
4434 *outFinishPreviousGesture = true;
4435
4436 mPointerGesture.activeGestureId = -1;
4437 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4438 mPointerGesture.currentGestureIdBits.clear();
4439
Jeff Brown65fd2512011-08-18 11:20:58 -07004440 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004441 return true;
4442 }
4443 }
4444
4445 // We did not handle this timeout.
4446 return false;
4447 }
4448
Jeff Brown65fd2512011-08-18 11:20:58 -07004449 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4450 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4451
Jeff Brownace13b12011-03-09 17:39:48 -08004452 // Update the velocity tracker.
4453 {
4454 VelocityTracker::Position positions[MAX_POINTERS];
4455 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004456 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004457 uint32_t id = idBits.clearFirstMarkedBit();
4458 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004459 positions[count].x = pointer.x * mPointerXMovementScale;
4460 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004461 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004462 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004463 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004464 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004465
Jeff Brownace13b12011-03-09 17:39:48 -08004466 // Pick a new active touch id if needed.
4467 // Choose an arbitrary pointer that just went down, if there is one.
4468 // Otherwise choose an arbitrary remaining pointer.
4469 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004470 // We keep the same active touch id for as long as possible.
4471 bool activeTouchChanged = false;
4472 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4473 int32_t activeTouchId = lastActiveTouchId;
4474 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004475 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004476 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004477 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004478 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004479 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004480 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004481 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004482 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004483 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004484 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004485 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004486 } else {
4487 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004488 }
4489 }
4490
4491 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004492 bool isQuietTime = false;
4493 if (activeTouchId < 0) {
4494 mPointerGesture.resetQuietTime();
4495 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004496 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004497 if (!isQuietTime) {
4498 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4499 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4500 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004501 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004502 // Enter quiet time when exiting swipe or freeform state.
4503 // This is to prevent accidentally entering the hover state and flinging the
4504 // pointer when finishing a swipe and there is still one pointer left onscreen.
4505 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004506 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004507 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004508 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004509 // Enter quiet time when releasing the button and there are still two or more
4510 // fingers down. This may indicate that one finger was used to press the button
4511 // but it has not gone up yet.
4512 isQuietTime = true;
4513 }
4514 if (isQuietTime) {
4515 mPointerGesture.quietTime = when;
4516 }
Jeff Brownace13b12011-03-09 17:39:48 -08004517 }
4518 }
4519
4520 // Switch states based on button and pointer state.
4521 if (isQuietTime) {
4522 // Case 1: Quiet time. (QUIET)
4523#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004524 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004525 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004526#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004527 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4528 *outFinishPreviousGesture = true;
4529 }
Jeff Brownace13b12011-03-09 17:39:48 -08004530
4531 mPointerGesture.activeGestureId = -1;
4532 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004533 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004534
Jeff Brown65fd2512011-08-18 11:20:58 -07004535 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004536 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004537 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004538 // The pointer follows the active touch point.
4539 // Emit DOWN, MOVE, UP events at the pointer location.
4540 //
4541 // Only the active touch matters; other fingers are ignored. This policy helps
4542 // to handle the case where the user places a second finger on the touch pad
4543 // to apply the necessary force to depress an integrated button below the surface.
4544 // We don't want the second finger to be delivered to applications.
4545 //
4546 // For this to work well, we need to make sure to track the pointer that is really
4547 // active. If the user first puts one finger down to click then adds another
4548 // finger to drag then the active pointer should switch to the finger that is
4549 // being dragged.
4550#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004551 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004552 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004553#endif
4554 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004555 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004556 *outFinishPreviousGesture = true;
4557 mPointerGesture.activeGestureId = 0;
4558 }
4559
4560 // Switch pointers if needed.
4561 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004562 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004563 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004564 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004565 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004566 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004567 float vx, vy;
4568 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4569 float speed = hypotf(vx, vy);
4570 if (speed > bestSpeed) {
4571 bestId = id;
4572 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004573 }
Jeff Brown8d608662010-08-30 03:02:23 -07004574 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004575 }
4576 if (bestId >= 0 && bestId != activeTouchId) {
4577 mPointerGesture.activeTouchId = activeTouchId = bestId;
4578 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004579#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004580 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004581 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004582#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004583 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004584 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004585
Jeff Brown65fd2512011-08-18 11:20:58 -07004586 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004587 const RawPointerData::Pointer& currentPointer =
4588 mCurrentRawPointerData.pointerForId(activeTouchId);
4589 const RawPointerData::Pointer& lastPointer =
4590 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004591 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4592 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004593
Jeff Brownbe1aa822011-07-27 16:04:54 -07004594 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004595 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004596
4597 // Move the pointer using a relative motion.
4598 // When using spots, the click will occur at the position of the anchor
4599 // spot and all other spots will move there.
4600 mPointerController->move(deltaX, deltaY);
4601 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004602 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004603 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004604
Jeff Brownace13b12011-03-09 17:39:48 -08004605 float x, y;
4606 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004607
Jeff Brown79ac9692011-04-19 21:20:10 -07004608 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004609 mPointerGesture.currentGestureIdBits.clear();
4610 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4611 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004612 mPointerGesture.currentGestureProperties[0].clear();
4613 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004614 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004615 mPointerGesture.currentGestureCoords[0].clear();
4616 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4617 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4618 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004619 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004620 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004621 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4622 *outFinishPreviousGesture = true;
4623 }
Jeff Brownace13b12011-03-09 17:39:48 -08004624
Jeff Brown79ac9692011-04-19 21:20:10 -07004625 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004626 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004627 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004628 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4629 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004630 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004631 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004632 float x, y;
4633 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004634 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4635 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004636#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004637 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004638#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004639
4640 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004641 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004642 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004643
Jeff Brownace13b12011-03-09 17:39:48 -08004644 mPointerGesture.activeGestureId = 0;
4645 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004646 mPointerGesture.currentGestureIdBits.clear();
4647 mPointerGesture.currentGestureIdBits.markBit(
4648 mPointerGesture.activeGestureId);
4649 mPointerGesture.currentGestureIdToIndex[
4650 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004651 mPointerGesture.currentGestureProperties[0].clear();
4652 mPointerGesture.currentGestureProperties[0].id =
4653 mPointerGesture.activeGestureId;
4654 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004655 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004656 mPointerGesture.currentGestureCoords[0].clear();
4657 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004658 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004659 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004660 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004661 mPointerGesture.currentGestureCoords[0].setAxisValue(
4662 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004663
Jeff Brownace13b12011-03-09 17:39:48 -08004664 tapped = true;
4665 } else {
4666#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004667 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004668 x - mPointerGesture.tapX,
4669 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004670#endif
4671 }
4672 } else {
4673#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004674 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004675 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004676#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004677 }
Jeff Brownace13b12011-03-09 17:39:48 -08004678 }
Jeff Brown2352b972011-04-12 22:39:53 -07004679
Jeff Brown65fd2512011-08-18 11:20:58 -07004680 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004681
Jeff Brownace13b12011-03-09 17:39:48 -08004682 if (!tapped) {
4683#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004684 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004685#endif
4686 mPointerGesture.activeGestureId = -1;
4687 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004688 mPointerGesture.currentGestureIdBits.clear();
4689 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004690 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004691 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004692 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004693 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4694 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004695 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004696
Jeff Brown79ac9692011-04-19 21:20:10 -07004697 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4698 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004699 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004700 float x, y;
4701 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004702 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4703 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004704 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4705 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004706#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004707 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004708 x - mPointerGesture.tapX,
4709 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004710#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004711 }
4712 } else {
4713#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004714 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004715 (when - mPointerGesture.tapUpTime) * 0.000001f);
4716#endif
4717 }
4718 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4719 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4720 }
Jeff Brownace13b12011-03-09 17:39:48 -08004721
Jeff Brown65fd2512011-08-18 11:20:58 -07004722 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004723 const RawPointerData::Pointer& currentPointer =
4724 mCurrentRawPointerData.pointerForId(activeTouchId);
4725 const RawPointerData::Pointer& lastPointer =
4726 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004727 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004728 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004729 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004730 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004731
Jeff Brownbe1aa822011-07-27 16:04:54 -07004732 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004733 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004734
Jeff Brown2352b972011-04-12 22:39:53 -07004735 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004736 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004737 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004738 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004739 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004740 }
4741
Jeff Brown79ac9692011-04-19 21:20:10 -07004742 bool down;
4743 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4744#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004745 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004746#endif
4747 down = true;
4748 } else {
4749#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004750 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004751#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004752 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4753 *outFinishPreviousGesture = true;
4754 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004755 mPointerGesture.activeGestureId = 0;
4756 down = false;
4757 }
Jeff Brownace13b12011-03-09 17:39:48 -08004758
4759 float x, y;
4760 mPointerController->getPosition(&x, &y);
4761
Jeff Brownace13b12011-03-09 17:39:48 -08004762 mPointerGesture.currentGestureIdBits.clear();
4763 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4764 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004765 mPointerGesture.currentGestureProperties[0].clear();
4766 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4767 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004768 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004769 mPointerGesture.currentGestureCoords[0].clear();
4770 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4771 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004772 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4773 down ? 1.0f : 0.0f);
4774
Jeff Brown65fd2512011-08-18 11:20:58 -07004775 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004776 mPointerGesture.resetTap();
4777 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004778 mPointerGesture.tapX = x;
4779 mPointerGesture.tapY = y;
4780 }
Jeff Brownace13b12011-03-09 17:39:48 -08004781 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004782 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4783 // We need to provide feedback for each finger that goes down so we cannot wait
4784 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004785 //
Jeff Brown2352b972011-04-12 22:39:53 -07004786 // The ambiguous case is deciding what to do when there are two fingers down but they
4787 // have not moved enough to determine whether they are part of a drag or part of a
4788 // freeform gesture, or just a press or long-press at the pointer location.
4789 //
4790 // When there are two fingers we start with the PRESS hypothesis and we generate a
4791 // down at the pointer location.
4792 //
4793 // When the two fingers move enough or when additional fingers are added, we make
4794 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004795 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004796
Jeff Brown214eaf42011-05-26 19:17:02 -07004797 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004798 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004799 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004800 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4801 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004802 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004803 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004804 // Additional pointers have gone down but not yet settled.
4805 // Reset the gesture.
4806#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004807 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004808 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004809 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004810 * 0.000001f);
4811#endif
4812 *outCancelPreviousGesture = true;
4813 } else {
4814 // Continue previous gesture.
4815 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4816 }
4817
4818 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004819 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4820 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004821 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004822 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004823
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004824 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004825#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004826 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004827 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004828 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004829 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004830#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004831 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4832 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004833 &mPointerGesture.referenceTouchY);
4834 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4835 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004836 }
Jeff Brownace13b12011-03-09 17:39:48 -08004837
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004838 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004839 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004840 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4841 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004842 mPointerGesture.referenceDeltas[id].dx = 0;
4843 mPointerGesture.referenceDeltas[id].dy = 0;
4844 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004845 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004846
4847 // Add delta for all fingers and calculate a common movement delta.
4848 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004849 BitSet32 commonIdBits(mLastFingerIdBits.value
4850 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004851 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4852 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004853 uint32_t id = idBits.clearFirstMarkedBit();
4854 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4855 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004856 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4857 delta.dx += cpd.x - lpd.x;
4858 delta.dy += cpd.y - lpd.y;
4859
4860 if (first) {
4861 commonDeltaX = delta.dx;
4862 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004863 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004864 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4865 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4866 }
4867 }
Jeff Brownace13b12011-03-09 17:39:48 -08004868
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004869 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4870 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4871 float dist[MAX_POINTER_ID + 1];
4872 int32_t distOverThreshold = 0;
4873 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004874 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004875 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004876 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4877 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004878 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004879 distOverThreshold += 1;
4880 }
4881 }
4882
4883 // Only transition when at least two pointers have moved further than
4884 // the minimum distance threshold.
4885 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004886 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004887 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004888#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004889 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004890 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004891#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004892 *outCancelPreviousGesture = true;
4893 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4894 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004895 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004896 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004897 uint32_t id1 = idBits.clearFirstMarkedBit();
4898 uint32_t id2 = idBits.firstMarkedBit();
4899 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4900 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4901 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4902 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4903 // There are two pointers but they are too far apart for a SWIPE,
4904 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004905#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004906 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004907 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004908#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004909 *outCancelPreviousGesture = true;
4910 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4911 } else {
4912 // There are two pointers. Wait for both pointers to start moving
4913 // before deciding whether this is a SWIPE or FREEFORM gesture.
4914 float dist1 = dist[id1];
4915 float dist2 = dist[id2];
4916 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4917 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4918 // Calculate the dot product of the displacement vectors.
4919 // When the vectors are oriented in approximately the same direction,
4920 // the angle betweeen them is near zero and the cosine of the angle
4921 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4922 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4923 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004924 float dx1 = delta1.dx * mPointerXZoomScale;
4925 float dy1 = delta1.dy * mPointerYZoomScale;
4926 float dx2 = delta2.dx * mPointerXZoomScale;
4927 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004928 float dot = dx1 * dx2 + dy1 * dy2;
4929 float cosine = dot / (dist1 * dist2); // denominator always > 0
4930 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4931 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004932#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004933 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004934 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4935 "cosine %0.3f >= %0.3f",
4936 dist1, mConfig.pointerGestureMultitouchMinDistance,
4937 dist2, mConfig.pointerGestureMultitouchMinDistance,
4938 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004939#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004940 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4941 } else {
4942 // Pointers are moving in different directions. Switch to FREEFORM.
4943#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004944 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004945 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4946 "cosine %0.3f < %0.3f",
4947 dist1, mConfig.pointerGestureMultitouchMinDistance,
4948 dist2, mConfig.pointerGestureMultitouchMinDistance,
4949 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4950#endif
4951 *outCancelPreviousGesture = true;
4952 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4953 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004954 }
Jeff Brownace13b12011-03-09 17:39:48 -08004955 }
4956 }
Jeff Brownace13b12011-03-09 17:39:48 -08004957 }
4958 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004959 // Switch from SWIPE to FREEFORM if additional pointers go down.
4960 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004961 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004962#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004963 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004964 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004965#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004966 *outCancelPreviousGesture = true;
4967 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004968 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004969 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004970
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004971 // Move the reference points based on the overall group motion of the fingers
4972 // except in PRESS mode while waiting for a transition to occur.
4973 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4974 && (commonDeltaX || commonDeltaY)) {
4975 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004976 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004977 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004978 delta.dx = 0;
4979 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004980 }
4981
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004982 mPointerGesture.referenceTouchX += commonDeltaX;
4983 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004984
Jeff Brown65fd2512011-08-18 11:20:58 -07004985 commonDeltaX *= mPointerXMovementScale;
4986 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004987
Jeff Brownbe1aa822011-07-27 16:04:54 -07004988 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004989 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004990
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004991 mPointerGesture.referenceGestureX += commonDeltaX;
4992 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004993 }
4994
4995 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004996 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4997 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4998 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004999#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005000 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005001 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005002 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005003#endif
Steve Blockec193de2012-01-09 18:35:44 +00005004 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005005
5006 mPointerGesture.currentGestureIdBits.clear();
5007 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5008 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005009 mPointerGesture.currentGestureProperties[0].clear();
5010 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5011 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005012 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005013 mPointerGesture.currentGestureCoords[0].clear();
5014 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5015 mPointerGesture.referenceGestureX);
5016 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5017 mPointerGesture.referenceGestureY);
5018 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005019 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5020 // FREEFORM mode.
5021#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005022 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005023 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005024 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005025#endif
Steve Blockec193de2012-01-09 18:35:44 +00005026 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005027
Jeff Brownace13b12011-03-09 17:39:48 -08005028 mPointerGesture.currentGestureIdBits.clear();
5029
5030 BitSet32 mappedTouchIdBits;
5031 BitSet32 usedGestureIdBits;
5032 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5033 // Initially, assign the active gesture id to the active touch point
5034 // if there is one. No other touch id bits are mapped yet.
5035 if (!*outCancelPreviousGesture) {
5036 mappedTouchIdBits.markBit(activeTouchId);
5037 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5038 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5039 mPointerGesture.activeGestureId;
5040 } else {
5041 mPointerGesture.activeGestureId = -1;
5042 }
5043 } else {
5044 // Otherwise, assume we mapped all touches from the previous frame.
5045 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005046 mappedTouchIdBits.value = mLastFingerIdBits.value
5047 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005048 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5049
5050 // Check whether we need to choose a new active gesture id because the
5051 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005052 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5053 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005054 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005055 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005056 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5057 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5058 mPointerGesture.activeGestureId = -1;
5059 break;
5060 }
5061 }
5062 }
5063
5064#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005065 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005066 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5067 "activeGestureId=%d",
5068 mappedTouchIdBits.value, usedGestureIdBits.value,
5069 mPointerGesture.activeGestureId);
5070#endif
5071
Jeff Brown65fd2512011-08-18 11:20:58 -07005072 BitSet32 idBits(mCurrentFingerIdBits);
5073 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005074 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005075 uint32_t gestureId;
5076 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005077 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005078 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5079#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005080 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005081 "new mapping for touch id %d -> gesture id %d",
5082 touchId, gestureId);
5083#endif
5084 } else {
5085 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5086#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005087 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005088 "existing mapping for touch id %d -> gesture id %d",
5089 touchId, gestureId);
5090#endif
5091 }
5092 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5093 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5094
Jeff Brownbe1aa822011-07-27 16:04:54 -07005095 const RawPointerData::Pointer& pointer =
5096 mCurrentRawPointerData.pointerForId(touchId);
5097 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005098 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005099 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005100 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005101 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005102
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005103 mPointerGesture.currentGestureProperties[i].clear();
5104 mPointerGesture.currentGestureProperties[i].id = gestureId;
5105 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005106 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005107 mPointerGesture.currentGestureCoords[i].clear();
5108 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005109 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005110 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005111 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005112 mPointerGesture.currentGestureCoords[i].setAxisValue(
5113 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5114 }
5115
5116 if (mPointerGesture.activeGestureId < 0) {
5117 mPointerGesture.activeGestureId =
5118 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5119#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005120 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005121 "activeGestureId=%d", mPointerGesture.activeGestureId);
5122#endif
5123 }
Jeff Brown2352b972011-04-12 22:39:53 -07005124 }
Jeff Brownace13b12011-03-09 17:39:48 -08005125 }
5126
Jeff Brownbe1aa822011-07-27 16:04:54 -07005127 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005128
Jeff Brownace13b12011-03-09 17:39:48 -08005129#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005130 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005131 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5132 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005133 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005134 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5135 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005136 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005137 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005138 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005139 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005140 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005141 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005142 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5143 id, index, properties.toolType,
5144 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005145 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5146 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5147 }
5148 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005149 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005150 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005151 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005152 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005153 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005154 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5155 id, index, properties.toolType,
5156 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005157 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5158 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5159 }
5160#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005161 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005162}
5163
Jeff Brown65fd2512011-08-18 11:20:58 -07005164void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5165 mPointerSimple.currentCoords.clear();
5166 mPointerSimple.currentProperties.clear();
5167
5168 bool down, hovering;
5169 if (!mCurrentStylusIdBits.isEmpty()) {
5170 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5171 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5172 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5173 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5174 mPointerController->setPosition(x, y);
5175
5176 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5177 down = !hovering;
5178
5179 mPointerController->getPosition(&x, &y);
5180 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5181 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5182 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5183 mPointerSimple.currentProperties.id = 0;
5184 mPointerSimple.currentProperties.toolType =
5185 mCurrentCookedPointerData.pointerProperties[index].toolType;
5186 } else {
5187 down = false;
5188 hovering = false;
5189 }
5190
5191 dispatchPointerSimple(when, policyFlags, down, hovering);
5192}
5193
5194void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5195 abortPointerSimple(when, policyFlags);
5196}
5197
5198void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5199 mPointerSimple.currentCoords.clear();
5200 mPointerSimple.currentProperties.clear();
5201
5202 bool down, hovering;
5203 if (!mCurrentMouseIdBits.isEmpty()) {
5204 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5205 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5206 if (mLastMouseIdBits.hasBit(id)) {
5207 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5208 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5209 - mLastRawPointerData.pointers[lastIndex].x)
5210 * mPointerXMovementScale;
5211 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5212 - mLastRawPointerData.pointers[lastIndex].y)
5213 * mPointerYMovementScale;
5214
5215 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5216 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5217
5218 mPointerController->move(deltaX, deltaY);
5219 } else {
5220 mPointerVelocityControl.reset();
5221 }
5222
5223 down = isPointerDown(mCurrentButtonState);
5224 hovering = !down;
5225
5226 float x, y;
5227 mPointerController->getPosition(&x, &y);
5228 mPointerSimple.currentCoords.copyFrom(
5229 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5230 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5231 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5232 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5233 hovering ? 0.0f : 1.0f);
5234 mPointerSimple.currentProperties.id = 0;
5235 mPointerSimple.currentProperties.toolType =
5236 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5237 } else {
5238 mPointerVelocityControl.reset();
5239
5240 down = false;
5241 hovering = false;
5242 }
5243
5244 dispatchPointerSimple(when, policyFlags, down, hovering);
5245}
5246
5247void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5248 abortPointerSimple(when, policyFlags);
5249
5250 mPointerVelocityControl.reset();
5251}
5252
5253void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5254 bool down, bool hovering) {
5255 int32_t metaState = getContext()->getGlobalMetaState();
5256
5257 if (mPointerController != NULL) {
5258 if (down || hovering) {
5259 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5260 mPointerController->clearSpots();
5261 mPointerController->setButtonState(mCurrentButtonState);
5262 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5263 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5264 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5265 }
5266 }
5267
5268 if (mPointerSimple.down && !down) {
5269 mPointerSimple.down = false;
5270
5271 // Send up.
5272 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5273 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5274 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5275 mOrientedXPrecision, mOrientedYPrecision,
5276 mPointerSimple.downTime);
5277 getListener()->notifyMotion(&args);
5278 }
5279
5280 if (mPointerSimple.hovering && !hovering) {
5281 mPointerSimple.hovering = false;
5282
5283 // Send hover exit.
5284 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5285 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5286 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5287 mOrientedXPrecision, mOrientedYPrecision,
5288 mPointerSimple.downTime);
5289 getListener()->notifyMotion(&args);
5290 }
5291
5292 if (down) {
5293 if (!mPointerSimple.down) {
5294 mPointerSimple.down = true;
5295 mPointerSimple.downTime = when;
5296
5297 // Send down.
5298 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5299 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5300 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5301 mOrientedXPrecision, mOrientedYPrecision,
5302 mPointerSimple.downTime);
5303 getListener()->notifyMotion(&args);
5304 }
5305
5306 // Send move.
5307 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5308 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5309 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5310 mOrientedXPrecision, mOrientedYPrecision,
5311 mPointerSimple.downTime);
5312 getListener()->notifyMotion(&args);
5313 }
5314
5315 if (hovering) {
5316 if (!mPointerSimple.hovering) {
5317 mPointerSimple.hovering = true;
5318
5319 // Send hover enter.
5320 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5321 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5322 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5323 mOrientedXPrecision, mOrientedYPrecision,
5324 mPointerSimple.downTime);
5325 getListener()->notifyMotion(&args);
5326 }
5327
5328 // Send hover move.
5329 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5330 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5331 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5332 mOrientedXPrecision, mOrientedYPrecision,
5333 mPointerSimple.downTime);
5334 getListener()->notifyMotion(&args);
5335 }
5336
5337 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5338 float vscroll = mCurrentRawVScroll;
5339 float hscroll = mCurrentRawHScroll;
5340 mWheelYVelocityControl.move(when, NULL, &vscroll);
5341 mWheelXVelocityControl.move(when, &hscroll, NULL);
5342
5343 // Send scroll.
5344 PointerCoords pointerCoords;
5345 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5346 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5347 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5348
5349 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5350 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5351 1, &mPointerSimple.currentProperties, &pointerCoords,
5352 mOrientedXPrecision, mOrientedYPrecision,
5353 mPointerSimple.downTime);
5354 getListener()->notifyMotion(&args);
5355 }
5356
5357 // Save state.
5358 if (down || hovering) {
5359 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5360 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5361 } else {
5362 mPointerSimple.reset();
5363 }
5364}
5365
5366void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5367 mPointerSimple.currentCoords.clear();
5368 mPointerSimple.currentProperties.clear();
5369
5370 dispatchPointerSimple(when, policyFlags, false, false);
5371}
5372
Jeff Brownace13b12011-03-09 17:39:48 -08005373void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005374 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5375 const PointerProperties* properties, const PointerCoords* coords,
5376 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005377 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5378 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005379 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005380 uint32_t pointerCount = 0;
5381 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005382 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005383 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005384 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005385 pointerCoords[pointerCount].copyFrom(coords[index]);
5386
5387 if (changedId >= 0 && id == uint32_t(changedId)) {
5388 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5389 }
5390
5391 pointerCount += 1;
5392 }
5393
Steve Blockec193de2012-01-09 18:35:44 +00005394 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005395
5396 if (changedId >= 0 && pointerCount == 1) {
5397 // Replace initial down and final up action.
5398 // We can compare the action without masking off the changed pointer index
5399 // because we know the index is 0.
5400 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5401 action = AMOTION_EVENT_ACTION_DOWN;
5402 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5403 action = AMOTION_EVENT_ACTION_UP;
5404 } else {
5405 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005406 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005407 }
5408 }
5409
Jeff Brownbe1aa822011-07-27 16:04:54 -07005410 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005411 action, flags, metaState, buttonState, edgeFlags,
5412 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005413 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005414}
5415
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005416bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005417 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005418 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5419 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005420 bool changed = false;
5421 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005422 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005423 uint32_t inIndex = inIdToIndex[id];
5424 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005425
5426 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005427 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005428 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005429 PointerCoords& curOutCoords = outCoords[outIndex];
5430
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005431 if (curInProperties != curOutProperties) {
5432 curOutProperties.copyFrom(curInProperties);
5433 changed = true;
5434 }
5435
Jeff Brownace13b12011-03-09 17:39:48 -08005436 if (curInCoords != curOutCoords) {
5437 curOutCoords.copyFrom(curInCoords);
5438 changed = true;
5439 }
5440 }
5441 return changed;
5442}
5443
5444void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005445 if (mPointerController != NULL) {
5446 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5447 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005448}
5449
Jeff Brownbe1aa822011-07-27 16:04:54 -07005450bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5451 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5452 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005453}
5454
Jeff Brownbe1aa822011-07-27 16:04:54 -07005455const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005456 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005457 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005458 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005459 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005460
5461#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005462 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005463 "left=%d, top=%d, right=%d, bottom=%d",
5464 x, y,
5465 virtualKey.keyCode, virtualKey.scanCode,
5466 virtualKey.hitLeft, virtualKey.hitTop,
5467 virtualKey.hitRight, virtualKey.hitBottom);
5468#endif
5469
5470 if (virtualKey.isHit(x, y)) {
5471 return & virtualKey;
5472 }
5473 }
5474
5475 return NULL;
5476}
5477
Jeff Brownbe1aa822011-07-27 16:04:54 -07005478void TouchInputMapper::assignPointerIds() {
5479 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5480 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5481
5482 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005483
5484 if (currentPointerCount == 0) {
5485 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005486 return;
5487 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005488
Jeff Brownbe1aa822011-07-27 16:04:54 -07005489 if (lastPointerCount == 0) {
5490 // All pointers are new.
5491 for (uint32_t i = 0; i < currentPointerCount; i++) {
5492 uint32_t id = i;
5493 mCurrentRawPointerData.pointers[i].id = id;
5494 mCurrentRawPointerData.idToIndex[id] = i;
5495 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5496 }
5497 return;
5498 }
5499
5500 if (currentPointerCount == 1 && lastPointerCount == 1
5501 && mCurrentRawPointerData.pointers[0].toolType
5502 == mLastRawPointerData.pointers[0].toolType) {
5503 // Only one pointer and no change in count so it must have the same id as before.
5504 uint32_t id = mLastRawPointerData.pointers[0].id;
5505 mCurrentRawPointerData.pointers[0].id = id;
5506 mCurrentRawPointerData.idToIndex[id] = 0;
5507 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5508 return;
5509 }
5510
5511 // General case.
5512 // We build a heap of squared euclidean distances between current and last pointers
5513 // associated with the current and last pointer indices. Then, we find the best
5514 // match (by distance) for each current pointer.
5515 // The pointers must have the same tool type but it is possible for them to
5516 // transition from hovering to touching or vice-versa while retaining the same id.
5517 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5518
5519 uint32_t heapSize = 0;
5520 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5521 currentPointerIndex++) {
5522 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5523 lastPointerIndex++) {
5524 const RawPointerData::Pointer& currentPointer =
5525 mCurrentRawPointerData.pointers[currentPointerIndex];
5526 const RawPointerData::Pointer& lastPointer =
5527 mLastRawPointerData.pointers[lastPointerIndex];
5528 if (currentPointer.toolType == lastPointer.toolType) {
5529 int64_t deltaX = currentPointer.x - lastPointer.x;
5530 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005531
5532 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5533
5534 // Insert new element into the heap (sift up).
5535 heap[heapSize].currentPointerIndex = currentPointerIndex;
5536 heap[heapSize].lastPointerIndex = lastPointerIndex;
5537 heap[heapSize].distance = distance;
5538 heapSize += 1;
5539 }
5540 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005541 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005542
Jeff Brownbe1aa822011-07-27 16:04:54 -07005543 // Heapify
5544 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5545 startIndex -= 1;
5546 for (uint32_t parentIndex = startIndex; ;) {
5547 uint32_t childIndex = parentIndex * 2 + 1;
5548 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005549 break;
5550 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005551
5552 if (childIndex + 1 < heapSize
5553 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5554 childIndex += 1;
5555 }
5556
5557 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5558 break;
5559 }
5560
5561 swap(heap[parentIndex], heap[childIndex]);
5562 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005563 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005564 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005565
5566#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005567 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005568 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005569 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005570 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5571 heap[i].distance);
5572 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005573#endif
5574
Jeff Brownbe1aa822011-07-27 16:04:54 -07005575 // Pull matches out by increasing order of distance.
5576 // To avoid reassigning pointers that have already been matched, the loop keeps track
5577 // of which last and current pointers have been matched using the matchedXXXBits variables.
5578 // It also tracks the used pointer id bits.
5579 BitSet32 matchedLastBits(0);
5580 BitSet32 matchedCurrentBits(0);
5581 BitSet32 usedIdBits(0);
5582 bool first = true;
5583 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5584 while (heapSize > 0) {
5585 if (first) {
5586 // The first time through the loop, we just consume the root element of
5587 // the heap (the one with smallest distance).
5588 first = false;
5589 } else {
5590 // Previous iterations consumed the root element of the heap.
5591 // Pop root element off of the heap (sift down).
5592 heap[0] = heap[heapSize];
5593 for (uint32_t parentIndex = 0; ;) {
5594 uint32_t childIndex = parentIndex * 2 + 1;
5595 if (childIndex >= heapSize) {
5596 break;
5597 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005598
Jeff Brownbe1aa822011-07-27 16:04:54 -07005599 if (childIndex + 1 < heapSize
5600 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5601 childIndex += 1;
5602 }
5603
5604 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5605 break;
5606 }
5607
5608 swap(heap[parentIndex], heap[childIndex]);
5609 parentIndex = childIndex;
5610 }
5611
5612#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005613 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005614 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005615 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005616 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5617 heap[i].distance);
5618 }
5619#endif
5620 }
5621
5622 heapSize -= 1;
5623
5624 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5625 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5626
5627 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5628 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5629
5630 matchedCurrentBits.markBit(currentPointerIndex);
5631 matchedLastBits.markBit(lastPointerIndex);
5632
5633 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5634 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5635 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5636 mCurrentRawPointerData.markIdBit(id,
5637 mCurrentRawPointerData.isHovering(currentPointerIndex));
5638 usedIdBits.markBit(id);
5639
5640#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005641 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005642 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5643#endif
5644 break;
5645 }
5646 }
5647
5648 // Assign fresh ids to pointers that were not matched in the process.
5649 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5650 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5651 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5652
5653 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5654 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5655 mCurrentRawPointerData.markIdBit(id,
5656 mCurrentRawPointerData.isHovering(currentPointerIndex));
5657
5658#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005659 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005660 currentPointerIndex, id);
5661#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005662 }
5663}
5664
Jeff Brown6d0fec22010-07-23 21:28:06 -07005665int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005666 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5667 return AKEY_STATE_VIRTUAL;
5668 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005669
Jeff Brownbe1aa822011-07-27 16:04:54 -07005670 size_t numVirtualKeys = mVirtualKeys.size();
5671 for (size_t i = 0; i < numVirtualKeys; i++) {
5672 const VirtualKey& virtualKey = mVirtualKeys[i];
5673 if (virtualKey.keyCode == keyCode) {
5674 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005675 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005676 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005677
5678 return AKEY_STATE_UNKNOWN;
5679}
5680
5681int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005682 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5683 return AKEY_STATE_VIRTUAL;
5684 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005685
Jeff Brownbe1aa822011-07-27 16:04:54 -07005686 size_t numVirtualKeys = mVirtualKeys.size();
5687 for (size_t i = 0; i < numVirtualKeys; i++) {
5688 const VirtualKey& virtualKey = mVirtualKeys[i];
5689 if (virtualKey.scanCode == scanCode) {
5690 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005691 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005692 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005693
5694 return AKEY_STATE_UNKNOWN;
5695}
5696
5697bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5698 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005699 size_t numVirtualKeys = mVirtualKeys.size();
5700 for (size_t i = 0; i < numVirtualKeys; i++) {
5701 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005702
Jeff Brownbe1aa822011-07-27 16:04:54 -07005703 for (size_t i = 0; i < numCodes; i++) {
5704 if (virtualKey.keyCode == keyCodes[i]) {
5705 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005706 }
5707 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005708 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005709
5710 return true;
5711}
5712
5713
5714// --- SingleTouchInputMapper ---
5715
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005716SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5717 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005718}
5719
5720SingleTouchInputMapper::~SingleTouchInputMapper() {
5721}
5722
Jeff Brown65fd2512011-08-18 11:20:58 -07005723void SingleTouchInputMapper::reset(nsecs_t when) {
5724 mSingleTouchMotionAccumulator.reset(getDevice());
5725
5726 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005727}
5728
Jeff Brown6d0fec22010-07-23 21:28:06 -07005729void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005730 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005731
Jeff Brown65fd2512011-08-18 11:20:58 -07005732 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005733}
5734
Jeff Brown65fd2512011-08-18 11:20:58 -07005735void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005736 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005737 mCurrentRawPointerData.pointerCount = 1;
5738 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005739
Jeff Brown65fd2512011-08-18 11:20:58 -07005740 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5741 && (mTouchButtonAccumulator.isHovering()
5742 || (mRawPointerAxes.pressure.valid
5743 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005744 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005745
Jeff Brownbe1aa822011-07-27 16:04:54 -07005746 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005747 outPointer.id = 0;
5748 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5749 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5750 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5751 outPointer.touchMajor = 0;
5752 outPointer.touchMinor = 0;
5753 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5754 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5755 outPointer.orientation = 0;
5756 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005757 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5758 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005759 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5760 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5761 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5762 }
5763 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005764 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005765}
5766
Jeff Brownbe1aa822011-07-27 16:04:54 -07005767void SingleTouchInputMapper::configureRawPointerAxes() {
5768 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005769
Jeff Brownbe1aa822011-07-27 16:04:54 -07005770 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5771 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5772 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5773 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5774 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005775 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5776 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005777}
5778
5779
5780// --- MultiTouchInputMapper ---
5781
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005782MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005783 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005784}
5785
5786MultiTouchInputMapper::~MultiTouchInputMapper() {
5787}
5788
Jeff Brown65fd2512011-08-18 11:20:58 -07005789void MultiTouchInputMapper::reset(nsecs_t when) {
5790 mMultiTouchMotionAccumulator.reset(getDevice());
5791
Jeff Brown6894a292011-07-01 17:59:27 -07005792 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005793
Jeff Brown65fd2512011-08-18 11:20:58 -07005794 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005795}
5796
5797void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005798 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005799
Jeff Brown65fd2512011-08-18 11:20:58 -07005800 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005801}
5802
Jeff Brown65fd2512011-08-18 11:20:58 -07005803void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005804 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005805 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005806 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005807
Jeff Brown80fd47c2011-05-24 01:07:44 -07005808 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005809 const MultiTouchMotionAccumulator::Slot* inSlot =
5810 mMultiTouchMotionAccumulator.getSlot(inIndex);
5811 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005812 continue;
5813 }
5814
Jeff Brown80fd47c2011-05-24 01:07:44 -07005815 if (outCount >= MAX_POINTERS) {
5816#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005817 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005818 "ignoring the rest.",
5819 getDeviceName().string(), MAX_POINTERS);
5820#endif
5821 break; // too many fingers!
5822 }
5823
Jeff Brownbe1aa822011-07-27 16:04:54 -07005824 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005825 outPointer.x = inSlot->getX();
5826 outPointer.y = inSlot->getY();
5827 outPointer.pressure = inSlot->getPressure();
5828 outPointer.touchMajor = inSlot->getTouchMajor();
5829 outPointer.touchMinor = inSlot->getTouchMinor();
5830 outPointer.toolMajor = inSlot->getToolMajor();
5831 outPointer.toolMinor = inSlot->getToolMinor();
5832 outPointer.orientation = inSlot->getOrientation();
5833 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005834 outPointer.tiltX = 0;
5835 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005836
Jeff Brown49754db2011-07-01 17:37:58 -07005837 outPointer.toolType = inSlot->getToolType();
5838 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5839 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5840 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5841 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5842 }
Jeff Brown8d608662010-08-30 03:02:23 -07005843 }
5844
Jeff Brown65fd2512011-08-18 11:20:58 -07005845 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5846 && (mTouchButtonAccumulator.isHovering()
5847 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005848 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005849
Jeff Brown8d608662010-08-30 03:02:23 -07005850 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005851 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005852 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005853 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005854 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005855 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005856 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005857 if (mPointerTrackingIdMap[n] == trackingId) {
5858 id = n;
5859 }
5860 }
5861
5862 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005863 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005864 mPointerTrackingIdMap[id] = trackingId;
5865 }
5866 }
5867 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005868 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005869 mCurrentRawPointerData.clearIdBits();
5870 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005871 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005872 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005873 mCurrentRawPointerData.idToIndex[id] = outCount;
5874 mCurrentRawPointerData.markIdBit(id, isHovering);
5875 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005876 }
5877 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005878
Jeff Brown6d0fec22010-07-23 21:28:06 -07005879 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005880 }
5881
Jeff Brownbe1aa822011-07-27 16:04:54 -07005882 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005883 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005884
Jeff Brown65fd2512011-08-18 11:20:58 -07005885 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005886}
5887
Jeff Brownbe1aa822011-07-27 16:04:54 -07005888void MultiTouchInputMapper::configureRawPointerAxes() {
5889 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005890
Jeff Brownbe1aa822011-07-27 16:04:54 -07005891 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5892 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5893 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5894 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5895 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5896 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5897 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5898 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5899 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5900 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5901 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005902
Jeff Brownbe1aa822011-07-27 16:04:54 -07005903 if (mRawPointerAxes.trackingId.valid
5904 && mRawPointerAxes.slot.valid
5905 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5906 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005907 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005908 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005909 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005910 getDeviceName().string(), slotCount, MAX_SLOTS);
5911 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005912 }
Jeff Brown49754db2011-07-01 17:37:58 -07005913 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005914 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005915 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005916 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005917}
5918
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005919
Jeff Browncb1404e2011-01-15 18:14:15 -08005920// --- JoystickInputMapper ---
5921
5922JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5923 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005924}
5925
5926JoystickInputMapper::~JoystickInputMapper() {
5927}
5928
5929uint32_t JoystickInputMapper::getSources() {
5930 return AINPUT_SOURCE_JOYSTICK;
5931}
5932
5933void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5934 InputMapper::populateDeviceInfo(info);
5935
Jeff Brown6f2fba42011-02-19 01:08:02 -08005936 for (size_t i = 0; i < mAxes.size(); i++) {
5937 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005938 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5939 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005940 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005941 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5942 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005943 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005944 }
5945}
5946
5947void JoystickInputMapper::dump(String8& dump) {
5948 dump.append(INDENT2 "Joystick Input Mapper:\n");
5949
Jeff Brown6f2fba42011-02-19 01:08:02 -08005950 dump.append(INDENT3 "Axes:\n");
5951 size_t numAxes = mAxes.size();
5952 for (size_t i = 0; i < numAxes; i++) {
5953 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005954 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005955 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005956 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005957 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005958 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005959 }
Jeff Brown85297452011-03-04 13:07:49 -08005960 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5961 label = getAxisLabel(axis.axisInfo.highAxis);
5962 if (label) {
5963 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5964 } else {
5965 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5966 axis.axisInfo.splitValue);
5967 }
5968 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5969 dump.append(" (invert)");
5970 }
5971
5972 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5973 axis.min, axis.max, axis.flat, axis.fuzz);
5974 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5975 "highScale=%0.5f, highOffset=%0.5f\n",
5976 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005977 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5978 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005979 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005980 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005981 }
5982}
5983
Jeff Brown65fd2512011-08-18 11:20:58 -07005984void JoystickInputMapper::configure(nsecs_t when,
5985 const InputReaderConfiguration* config, uint32_t changes) {
5986 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005987
Jeff Brown474dcb52011-06-14 20:22:50 -07005988 if (!changes) { // first time only
5989 // Collect all axes.
5990 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07005991 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5992 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5993 continue; // axis must be claimed by a different device
5994 }
5995
Jeff Brown474dcb52011-06-14 20:22:50 -07005996 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005997 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005998 if (rawAxisInfo.valid) {
5999 // Map axis.
6000 AxisInfo axisInfo;
6001 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6002 if (!explicitlyMapped) {
6003 // Axis is not explicitly mapped, will choose a generic axis later.
6004 axisInfo.mode = AxisInfo::MODE_NORMAL;
6005 axisInfo.axis = -1;
6006 }
6007
6008 // Apply flat override.
6009 int32_t rawFlat = axisInfo.flatOverride < 0
6010 ? rawAxisInfo.flat : axisInfo.flatOverride;
6011
6012 // Calculate scaling factors and limits.
6013 Axis axis;
6014 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6015 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6016 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6017 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6018 scale, 0.0f, highScale, 0.0f,
6019 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6020 } else if (isCenteredAxis(axisInfo.axis)) {
6021 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6022 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6023 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6024 scale, offset, scale, offset,
6025 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6026 } else {
6027 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6028 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6029 scale, 0.0f, scale, 0.0f,
6030 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6031 }
6032
6033 // To eliminate noise while the joystick is at rest, filter out small variations
6034 // in axis values up front.
6035 axis.filter = axis.flat * 0.25f;
6036
6037 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006038 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006039 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006040
Jeff Brown474dcb52011-06-14 20:22:50 -07006041 // If there are too many axes, start dropping them.
6042 // Prefer to keep explicitly mapped axes.
6043 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006044 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006045 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6046 pruneAxes(true);
6047 pruneAxes(false);
6048 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006049
Jeff Brown474dcb52011-06-14 20:22:50 -07006050 // Assign generic axis ids to remaining axes.
6051 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6052 size_t numAxes = mAxes.size();
6053 for (size_t i = 0; i < numAxes; i++) {
6054 Axis& axis = mAxes.editValueAt(i);
6055 if (axis.axisInfo.axis < 0) {
6056 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6057 && haveAxis(nextGenericAxisId)) {
6058 nextGenericAxisId += 1;
6059 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006060
Jeff Brown474dcb52011-06-14 20:22:50 -07006061 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6062 axis.axisInfo.axis = nextGenericAxisId;
6063 nextGenericAxisId += 1;
6064 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006065 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006066 "have already been assigned to other axes.",
6067 getDeviceName().string(), mAxes.keyAt(i));
6068 mAxes.removeItemsAt(i--);
6069 numAxes -= 1;
6070 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006071 }
6072 }
6073 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006074}
6075
Jeff Brown85297452011-03-04 13:07:49 -08006076bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006077 size_t numAxes = mAxes.size();
6078 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006079 const Axis& axis = mAxes.valueAt(i);
6080 if (axis.axisInfo.axis == axisId
6081 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6082 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006083 return true;
6084 }
6085 }
6086 return false;
6087}
Jeff Browncb1404e2011-01-15 18:14:15 -08006088
Jeff Brown6f2fba42011-02-19 01:08:02 -08006089void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6090 size_t i = mAxes.size();
6091 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6092 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6093 continue;
6094 }
Steve Block6215d3f2012-01-04 20:05:49 +00006095 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006096 getDeviceName().string(), mAxes.keyAt(i));
6097 mAxes.removeItemsAt(i);
6098 }
6099}
6100
6101bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6102 switch (axis) {
6103 case AMOTION_EVENT_AXIS_X:
6104 case AMOTION_EVENT_AXIS_Y:
6105 case AMOTION_EVENT_AXIS_Z:
6106 case AMOTION_EVENT_AXIS_RX:
6107 case AMOTION_EVENT_AXIS_RY:
6108 case AMOTION_EVENT_AXIS_RZ:
6109 case AMOTION_EVENT_AXIS_HAT_X:
6110 case AMOTION_EVENT_AXIS_HAT_Y:
6111 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006112 case AMOTION_EVENT_AXIS_RUDDER:
6113 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006114 return true;
6115 default:
6116 return false;
6117 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006118}
6119
Jeff Brown65fd2512011-08-18 11:20:58 -07006120void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006121 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006122 size_t numAxes = mAxes.size();
6123 for (size_t i = 0; i < numAxes; i++) {
6124 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006125 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006126 }
6127
Jeff Brown65fd2512011-08-18 11:20:58 -07006128 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006129}
6130
6131void JoystickInputMapper::process(const RawEvent* rawEvent) {
6132 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006133 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006134 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006135 if (index >= 0) {
6136 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006137 float newValue, highNewValue;
6138 switch (axis.axisInfo.mode) {
6139 case AxisInfo::MODE_INVERT:
6140 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6141 * axis.scale + axis.offset;
6142 highNewValue = 0.0f;
6143 break;
6144 case AxisInfo::MODE_SPLIT:
6145 if (rawEvent->value < axis.axisInfo.splitValue) {
6146 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6147 * axis.scale + axis.offset;
6148 highNewValue = 0.0f;
6149 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6150 newValue = 0.0f;
6151 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6152 * axis.highScale + axis.highOffset;
6153 } else {
6154 newValue = 0.0f;
6155 highNewValue = 0.0f;
6156 }
6157 break;
6158 default:
6159 newValue = rawEvent->value * axis.scale + axis.offset;
6160 highNewValue = 0.0f;
6161 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006162 }
Jeff Brown85297452011-03-04 13:07:49 -08006163 axis.newValue = newValue;
6164 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006165 }
6166 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006167 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006168
6169 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006170 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006171 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006172 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006173 break;
6174 }
6175 break;
6176 }
6177}
6178
Jeff Brown6f2fba42011-02-19 01:08:02 -08006179void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006180 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006181 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006182 }
6183
6184 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006185 int32_t buttonState = 0;
6186
6187 PointerProperties pointerProperties;
6188 pointerProperties.clear();
6189 pointerProperties.id = 0;
6190 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006191
Jeff Brown6f2fba42011-02-19 01:08:02 -08006192 PointerCoords pointerCoords;
6193 pointerCoords.clear();
6194
6195 size_t numAxes = mAxes.size();
6196 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006197 const Axis& axis = mAxes.valueAt(i);
6198 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6199 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6200 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6201 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006202 }
6203
Jeff Brown56194eb2011-03-02 19:23:13 -08006204 // Moving a joystick axis should not wake the devide because joysticks can
6205 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6206 // button will likely wake the device.
6207 // TODO: Use the input device configuration to control this behavior more finely.
6208 uint32_t policyFlags = 0;
6209
Jeff Brownbe1aa822011-07-27 16:04:54 -07006210 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006211 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
6212 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006213 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006214}
6215
Jeff Brown85297452011-03-04 13:07:49 -08006216bool JoystickInputMapper::filterAxes(bool force) {
6217 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006218 size_t numAxes = mAxes.size();
6219 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006220 Axis& axis = mAxes.editValueAt(i);
6221 if (force || hasValueChangedSignificantly(axis.filter,
6222 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6223 axis.currentValue = axis.newValue;
6224 atLeastOneSignificantChange = true;
6225 }
6226 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6227 if (force || hasValueChangedSignificantly(axis.filter,
6228 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6229 axis.highCurrentValue = axis.highNewValue;
6230 atLeastOneSignificantChange = true;
6231 }
6232 }
6233 }
6234 return atLeastOneSignificantChange;
6235}
6236
6237bool JoystickInputMapper::hasValueChangedSignificantly(
6238 float filter, float newValue, float currentValue, float min, float max) {
6239 if (newValue != currentValue) {
6240 // Filter out small changes in value unless the value is converging on the axis
6241 // bounds or center point. This is intended to reduce the amount of information
6242 // sent to applications by particularly noisy joysticks (such as PS3).
6243 if (fabs(newValue - currentValue) > filter
6244 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6245 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6246 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6247 return true;
6248 }
6249 }
6250 return false;
6251}
6252
6253bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6254 float filter, float newValue, float currentValue, float thresholdValue) {
6255 float newDistance = fabs(newValue - thresholdValue);
6256 if (newDistance < filter) {
6257 float oldDistance = fabs(currentValue - thresholdValue);
6258 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006259 return true;
6260 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006261 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006262 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006263}
6264
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006265} // namespace android