blob: 95e56bf5c7b48348c082f1c613617fca9f0b6b13 [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 Brownfd035822010-06-30 16:10:35 -0700118 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
119 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
120 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
121 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff 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
Jeff Brown6ec6f792012-04-17 16:52:41 -0700965 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
966 sp<KeyCharacterMap> keyboardLayout =
967 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor);
968 if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
969 bumpGeneration();
970 }
971 }
972
Jeff Brown474dcb52011-06-14 20:22:50 -0700973 size_t numMappers = mMappers.size();
974 for (size_t i = 0; i < numMappers; i++) {
975 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700976 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700977 mSources |= mapper->getSources();
978 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700979 }
980}
981
Jeff Brown65fd2512011-08-18 11:20:58 -0700982void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700983 size_t numMappers = mMappers.size();
984 for (size_t i = 0; i < numMappers; i++) {
985 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700986 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700987 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700988
989 mContext->updateGlobalMetaState();
990
991 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700992}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700993
Jeff Brownb7198742011-03-18 18:14:26 -0700994void InputDevice::process(const RawEvent* rawEvents, size_t count) {
995 // Process all of the events in order for each mapper.
996 // We cannot simply ask each mapper to process them in bulk because mappers may
997 // have side-effects that must be interleaved. For example, joystick movement events and
998 // gamepad button presses are handled by different mappers but they should be dispatched
999 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001000 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -07001001 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
1002#if DEBUG_RAW_EVENTS
Jeff Brown49ccac52012-04-11 18:27:33 -07001003 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
1004 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
Jeff Brownb7198742011-03-18 18:14:26 -07001005#endif
1006
Jeff Brown80fd47c2011-05-24 01:07:44 -07001007 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001008 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07001009 mDropUntilNextSync = false;
1010#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +00001011 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -07001012#endif
1013 } else {
1014#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +00001015 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -07001016#endif
1017 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001018 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -07001019 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -07001020 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07001021 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001022 } else {
1023 for (size_t i = 0; i < numMappers; i++) {
1024 InputMapper* mapper = mMappers[i];
1025 mapper->process(rawEvent);
1026 }
Jeff Brownb7198742011-03-18 18:14:26 -07001027 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001028 }
1029}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001030
Jeff Brownaa3855d2011-03-17 01:34:19 -07001031void InputDevice::timeoutExpired(nsecs_t when) {
1032 size_t numMappers = mMappers.size();
1033 for (size_t i = 0; i < numMappers; i++) {
1034 InputMapper* mapper = mMappers[i];
1035 mapper->timeoutExpired(when);
1036 }
1037}
1038
Jeff Brown6d0fec22010-07-23 21:28:06 -07001039void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001040 outDeviceInfo->initialize(mId, mGeneration, mIdentifier.name, mIdentifier.descriptor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001041
1042 size_t numMappers = mMappers.size();
1043 for (size_t i = 0; i < numMappers; i++) {
1044 InputMapper* mapper = mMappers[i];
1045 mapper->populateDeviceInfo(outDeviceInfo);
1046 }
1047}
1048
1049int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1050 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1051}
1052
1053int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1054 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1055}
1056
1057int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1058 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1059}
1060
1061int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1062 int32_t result = AKEY_STATE_UNKNOWN;
1063 size_t numMappers = mMappers.size();
1064 for (size_t i = 0; i < numMappers; i++) {
1065 InputMapper* mapper = mMappers[i];
1066 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001067 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1068 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1069 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1070 if (currentResult >= AKEY_STATE_DOWN) {
1071 return currentResult;
1072 } else if (currentResult == AKEY_STATE_UP) {
1073 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001074 }
1075 }
1076 }
1077 return result;
1078}
1079
1080bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1081 const int32_t* keyCodes, uint8_t* outFlags) {
1082 bool result = false;
1083 size_t numMappers = mMappers.size();
1084 for (size_t i = 0; i < numMappers; i++) {
1085 InputMapper* mapper = mMappers[i];
1086 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1087 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1088 }
1089 }
1090 return result;
1091}
1092
Jeff Browna47425a2012-04-13 04:09:27 -07001093void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1094 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->vibrate(pattern, patternSize, repeat, token);
1099 }
1100}
1101
1102void InputDevice::cancelVibrate(int32_t token) {
1103 size_t numMappers = mMappers.size();
1104 for (size_t i = 0; i < numMappers; i++) {
1105 InputMapper* mapper = mMappers[i];
1106 mapper->cancelVibrate(token);
1107 }
1108}
1109
Jeff Brown6d0fec22010-07-23 21:28:06 -07001110int32_t InputDevice::getMetaState() {
1111 int32_t result = 0;
1112 size_t numMappers = mMappers.size();
1113 for (size_t i = 0; i < numMappers; i++) {
1114 InputMapper* mapper = mMappers[i];
1115 result |= mapper->getMetaState();
1116 }
1117 return result;
1118}
1119
Jeff Brown05dc66a2011-03-02 14:41:58 -08001120void InputDevice::fadePointer() {
1121 size_t numMappers = mMappers.size();
1122 for (size_t i = 0; i < numMappers; i++) {
1123 InputMapper* mapper = mMappers[i];
1124 mapper->fadePointer();
1125 }
1126}
1127
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001128void InputDevice::bumpGeneration() {
1129 mGeneration = mContext->bumpGeneration();
1130}
1131
Jeff Brown65fd2512011-08-18 11:20:58 -07001132void InputDevice::notifyReset(nsecs_t when) {
1133 NotifyDeviceResetArgs args(when, mId);
1134 mContext->getListener()->notifyDeviceReset(&args);
1135}
1136
Jeff Brown6d0fec22010-07-23 21:28:06 -07001137
Jeff Brown49754db2011-07-01 17:37:58 -07001138// --- CursorButtonAccumulator ---
1139
1140CursorButtonAccumulator::CursorButtonAccumulator() {
1141 clearButtons();
1142}
1143
Jeff Brown65fd2512011-08-18 11:20:58 -07001144void CursorButtonAccumulator::reset(InputDevice* device) {
1145 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1146 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1147 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1148 mBtnBack = device->isKeyPressed(BTN_BACK);
1149 mBtnSide = device->isKeyPressed(BTN_SIDE);
1150 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1151 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1152 mBtnTask = device->isKeyPressed(BTN_TASK);
1153}
1154
Jeff Brown49754db2011-07-01 17:37:58 -07001155void CursorButtonAccumulator::clearButtons() {
1156 mBtnLeft = 0;
1157 mBtnRight = 0;
1158 mBtnMiddle = 0;
1159 mBtnBack = 0;
1160 mBtnSide = 0;
1161 mBtnForward = 0;
1162 mBtnExtra = 0;
1163 mBtnTask = 0;
1164}
1165
1166void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1167 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001168 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001169 case BTN_LEFT:
1170 mBtnLeft = rawEvent->value;
1171 break;
1172 case BTN_RIGHT:
1173 mBtnRight = rawEvent->value;
1174 break;
1175 case BTN_MIDDLE:
1176 mBtnMiddle = rawEvent->value;
1177 break;
1178 case BTN_BACK:
1179 mBtnBack = rawEvent->value;
1180 break;
1181 case BTN_SIDE:
1182 mBtnSide = rawEvent->value;
1183 break;
1184 case BTN_FORWARD:
1185 mBtnForward = rawEvent->value;
1186 break;
1187 case BTN_EXTRA:
1188 mBtnExtra = rawEvent->value;
1189 break;
1190 case BTN_TASK:
1191 mBtnTask = rawEvent->value;
1192 break;
1193 }
1194 }
1195}
1196
1197uint32_t CursorButtonAccumulator::getButtonState() const {
1198 uint32_t result = 0;
1199 if (mBtnLeft) {
1200 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1201 }
1202 if (mBtnRight) {
1203 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1204 }
1205 if (mBtnMiddle) {
1206 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1207 }
1208 if (mBtnBack || mBtnSide) {
1209 result |= AMOTION_EVENT_BUTTON_BACK;
1210 }
1211 if (mBtnForward || mBtnExtra) {
1212 result |= AMOTION_EVENT_BUTTON_FORWARD;
1213 }
1214 return result;
1215}
1216
1217
1218// --- CursorMotionAccumulator ---
1219
Jeff Brown65fd2512011-08-18 11:20:58 -07001220CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001221 clearRelativeAxes();
1222}
1223
Jeff Brown65fd2512011-08-18 11:20:58 -07001224void CursorMotionAccumulator::reset(InputDevice* device) {
1225 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001226}
1227
1228void CursorMotionAccumulator::clearRelativeAxes() {
1229 mRelX = 0;
1230 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001231}
1232
1233void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1234 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001235 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001236 case REL_X:
1237 mRelX = rawEvent->value;
1238 break;
1239 case REL_Y:
1240 mRelY = rawEvent->value;
1241 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001242 }
1243 }
1244}
1245
1246void CursorMotionAccumulator::finishSync() {
1247 clearRelativeAxes();
1248}
1249
1250
1251// --- CursorScrollAccumulator ---
1252
1253CursorScrollAccumulator::CursorScrollAccumulator() :
1254 mHaveRelWheel(false), mHaveRelHWheel(false) {
1255 clearRelativeAxes();
1256}
1257
1258void CursorScrollAccumulator::configure(InputDevice* device) {
1259 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1260 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1261}
1262
1263void CursorScrollAccumulator::reset(InputDevice* device) {
1264 clearRelativeAxes();
1265}
1266
1267void CursorScrollAccumulator::clearRelativeAxes() {
1268 mRelWheel = 0;
1269 mRelHWheel = 0;
1270}
1271
1272void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1273 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001274 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001275 case REL_WHEEL:
1276 mRelWheel = rawEvent->value;
1277 break;
1278 case REL_HWHEEL:
1279 mRelHWheel = rawEvent->value;
1280 break;
1281 }
1282 }
1283}
1284
Jeff Brown65fd2512011-08-18 11:20:58 -07001285void CursorScrollAccumulator::finishSync() {
1286 clearRelativeAxes();
1287}
1288
Jeff Brown49754db2011-07-01 17:37:58 -07001289
1290// --- TouchButtonAccumulator ---
1291
1292TouchButtonAccumulator::TouchButtonAccumulator() :
1293 mHaveBtnTouch(false) {
1294 clearButtons();
1295}
1296
1297void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001298 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1299}
1300
1301void TouchButtonAccumulator::reset(InputDevice* device) {
1302 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1303 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1304 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1305 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1306 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1307 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1308 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1309 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1310 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1311 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1312 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001313 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1314 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1315 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001316}
1317
1318void TouchButtonAccumulator::clearButtons() {
1319 mBtnTouch = 0;
1320 mBtnStylus = 0;
1321 mBtnStylus2 = 0;
1322 mBtnToolFinger = 0;
1323 mBtnToolPen = 0;
1324 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001325 mBtnToolBrush = 0;
1326 mBtnToolPencil = 0;
1327 mBtnToolAirbrush = 0;
1328 mBtnToolMouse = 0;
1329 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001330 mBtnToolDoubleTap = 0;
1331 mBtnToolTripleTap = 0;
1332 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001333}
1334
1335void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1336 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001337 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001338 case BTN_TOUCH:
1339 mBtnTouch = rawEvent->value;
1340 break;
1341 case BTN_STYLUS:
1342 mBtnStylus = rawEvent->value;
1343 break;
1344 case BTN_STYLUS2:
1345 mBtnStylus2 = rawEvent->value;
1346 break;
1347 case BTN_TOOL_FINGER:
1348 mBtnToolFinger = rawEvent->value;
1349 break;
1350 case BTN_TOOL_PEN:
1351 mBtnToolPen = rawEvent->value;
1352 break;
1353 case BTN_TOOL_RUBBER:
1354 mBtnToolRubber = rawEvent->value;
1355 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001356 case BTN_TOOL_BRUSH:
1357 mBtnToolBrush = rawEvent->value;
1358 break;
1359 case BTN_TOOL_PENCIL:
1360 mBtnToolPencil = rawEvent->value;
1361 break;
1362 case BTN_TOOL_AIRBRUSH:
1363 mBtnToolAirbrush = rawEvent->value;
1364 break;
1365 case BTN_TOOL_MOUSE:
1366 mBtnToolMouse = rawEvent->value;
1367 break;
1368 case BTN_TOOL_LENS:
1369 mBtnToolLens = rawEvent->value;
1370 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001371 case BTN_TOOL_DOUBLETAP:
1372 mBtnToolDoubleTap = rawEvent->value;
1373 break;
1374 case BTN_TOOL_TRIPLETAP:
1375 mBtnToolTripleTap = rawEvent->value;
1376 break;
1377 case BTN_TOOL_QUADTAP:
1378 mBtnToolQuadTap = rawEvent->value;
1379 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001380 }
1381 }
1382}
1383
1384uint32_t TouchButtonAccumulator::getButtonState() const {
1385 uint32_t result = 0;
1386 if (mBtnStylus) {
1387 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1388 }
1389 if (mBtnStylus2) {
1390 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1391 }
1392 return result;
1393}
1394
1395int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001396 if (mBtnToolMouse || mBtnToolLens) {
1397 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1398 }
Jeff Brown49754db2011-07-01 17:37:58 -07001399 if (mBtnToolRubber) {
1400 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1401 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001402 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001403 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1404 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001405 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001406 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1407 }
1408 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1409}
1410
Jeff Brownd87c6d52011-08-10 14:55:59 -07001411bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001412 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1413 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001414 || mBtnToolMouse || mBtnToolLens
1415 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001416}
1417
1418bool TouchButtonAccumulator::isHovering() const {
1419 return mHaveBtnTouch && !mBtnTouch;
1420}
1421
1422
Jeff Brownbe1aa822011-07-27 16:04:54 -07001423// --- RawPointerAxes ---
1424
1425RawPointerAxes::RawPointerAxes() {
1426 clear();
1427}
1428
1429void RawPointerAxes::clear() {
1430 x.clear();
1431 y.clear();
1432 pressure.clear();
1433 touchMajor.clear();
1434 touchMinor.clear();
1435 toolMajor.clear();
1436 toolMinor.clear();
1437 orientation.clear();
1438 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001439 tiltX.clear();
1440 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001441 trackingId.clear();
1442 slot.clear();
1443}
1444
1445
1446// --- RawPointerData ---
1447
1448RawPointerData::RawPointerData() {
1449 clear();
1450}
1451
1452void RawPointerData::clear() {
1453 pointerCount = 0;
1454 clearIdBits();
1455}
1456
1457void RawPointerData::copyFrom(const RawPointerData& other) {
1458 pointerCount = other.pointerCount;
1459 hoveringIdBits = other.hoveringIdBits;
1460 touchingIdBits = other.touchingIdBits;
1461
1462 for (uint32_t i = 0; i < pointerCount; i++) {
1463 pointers[i] = other.pointers[i];
1464
1465 int id = pointers[i].id;
1466 idToIndex[id] = other.idToIndex[id];
1467 }
1468}
1469
1470void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1471 float x = 0, y = 0;
1472 uint32_t count = touchingIdBits.count();
1473 if (count) {
1474 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1475 uint32_t id = idBits.clearFirstMarkedBit();
1476 const Pointer& pointer = pointerForId(id);
1477 x += pointer.x;
1478 y += pointer.y;
1479 }
1480 x /= count;
1481 y /= count;
1482 }
1483 *outX = x;
1484 *outY = y;
1485}
1486
1487
1488// --- CookedPointerData ---
1489
1490CookedPointerData::CookedPointerData() {
1491 clear();
1492}
1493
1494void CookedPointerData::clear() {
1495 pointerCount = 0;
1496 hoveringIdBits.clear();
1497 touchingIdBits.clear();
1498}
1499
1500void CookedPointerData::copyFrom(const CookedPointerData& other) {
1501 pointerCount = other.pointerCount;
1502 hoveringIdBits = other.hoveringIdBits;
1503 touchingIdBits = other.touchingIdBits;
1504
1505 for (uint32_t i = 0; i < pointerCount; i++) {
1506 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1507 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1508
1509 int id = pointerProperties[i].id;
1510 idToIndex[id] = other.idToIndex[id];
1511 }
1512}
1513
1514
Jeff Brown49754db2011-07-01 17:37:58 -07001515// --- SingleTouchMotionAccumulator ---
1516
1517SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1518 clearAbsoluteAxes();
1519}
1520
Jeff Brown65fd2512011-08-18 11:20:58 -07001521void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1522 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1523 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1524 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1525 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1526 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1527 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1528 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1529}
1530
Jeff Brown49754db2011-07-01 17:37:58 -07001531void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1532 mAbsX = 0;
1533 mAbsY = 0;
1534 mAbsPressure = 0;
1535 mAbsToolWidth = 0;
1536 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001537 mAbsTiltX = 0;
1538 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001539}
1540
1541void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1542 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001543 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001544 case ABS_X:
1545 mAbsX = rawEvent->value;
1546 break;
1547 case ABS_Y:
1548 mAbsY = rawEvent->value;
1549 break;
1550 case ABS_PRESSURE:
1551 mAbsPressure = rawEvent->value;
1552 break;
1553 case ABS_TOOL_WIDTH:
1554 mAbsToolWidth = rawEvent->value;
1555 break;
1556 case ABS_DISTANCE:
1557 mAbsDistance = rawEvent->value;
1558 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001559 case ABS_TILT_X:
1560 mAbsTiltX = rawEvent->value;
1561 break;
1562 case ABS_TILT_Y:
1563 mAbsTiltY = rawEvent->value;
1564 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001565 }
1566 }
1567}
1568
1569
1570// --- MultiTouchMotionAccumulator ---
1571
1572MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1573 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1574}
1575
1576MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1577 delete[] mSlots;
1578}
1579
1580void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1581 mSlotCount = slotCount;
1582 mUsingSlotsProtocol = usingSlotsProtocol;
1583
1584 delete[] mSlots;
1585 mSlots = new Slot[slotCount];
1586}
1587
Jeff Brown65fd2512011-08-18 11:20:58 -07001588void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1589 // Unfortunately there is no way to read the initial contents of the slots.
1590 // So when we reset the accumulator, we must assume they are all zeroes.
1591 if (mUsingSlotsProtocol) {
1592 // Query the driver for the current slot index and use it as the initial slot
1593 // before we start reading events from the device. It is possible that the
1594 // current slot index will not be the same as it was when the first event was
1595 // written into the evdev buffer, which means the input mapper could start
1596 // out of sync with the initial state of the events in the evdev buffer.
1597 // In the extremely unlikely case that this happens, the data from
1598 // two slots will be confused until the next ABS_MT_SLOT event is received.
1599 // This can cause the touch point to "jump", but at least there will be
1600 // no stuck touches.
1601 int32_t initialSlot;
1602 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1603 ABS_MT_SLOT, &initialSlot);
1604 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001605 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001606 initialSlot = -1;
1607 }
1608 clearSlots(initialSlot);
1609 } else {
1610 clearSlots(-1);
1611 }
1612}
1613
Jeff Brown49754db2011-07-01 17:37:58 -07001614void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001615 if (mSlots) {
1616 for (size_t i = 0; i < mSlotCount; i++) {
1617 mSlots[i].clear();
1618 }
Jeff Brown49754db2011-07-01 17:37:58 -07001619 }
1620 mCurrentSlot = initialSlot;
1621}
1622
1623void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1624 if (rawEvent->type == EV_ABS) {
1625 bool newSlot = false;
1626 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001627 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001628 mCurrentSlot = rawEvent->value;
1629 newSlot = true;
1630 }
1631 } else if (mCurrentSlot < 0) {
1632 mCurrentSlot = 0;
1633 }
1634
1635 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1636#if DEBUG_POINTERS
1637 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001638 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001639 "should be between 0 and %d; ignoring this slot.",
1640 mCurrentSlot, mSlotCount - 1);
1641 }
1642#endif
1643 } else {
1644 Slot* slot = &mSlots[mCurrentSlot];
1645
Jeff Brown49ccac52012-04-11 18:27:33 -07001646 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001647 case ABS_MT_POSITION_X:
1648 slot->mInUse = true;
1649 slot->mAbsMTPositionX = rawEvent->value;
1650 break;
1651 case ABS_MT_POSITION_Y:
1652 slot->mInUse = true;
1653 slot->mAbsMTPositionY = rawEvent->value;
1654 break;
1655 case ABS_MT_TOUCH_MAJOR:
1656 slot->mInUse = true;
1657 slot->mAbsMTTouchMajor = rawEvent->value;
1658 break;
1659 case ABS_MT_TOUCH_MINOR:
1660 slot->mInUse = true;
1661 slot->mAbsMTTouchMinor = rawEvent->value;
1662 slot->mHaveAbsMTTouchMinor = true;
1663 break;
1664 case ABS_MT_WIDTH_MAJOR:
1665 slot->mInUse = true;
1666 slot->mAbsMTWidthMajor = rawEvent->value;
1667 break;
1668 case ABS_MT_WIDTH_MINOR:
1669 slot->mInUse = true;
1670 slot->mAbsMTWidthMinor = rawEvent->value;
1671 slot->mHaveAbsMTWidthMinor = true;
1672 break;
1673 case ABS_MT_ORIENTATION:
1674 slot->mInUse = true;
1675 slot->mAbsMTOrientation = rawEvent->value;
1676 break;
1677 case ABS_MT_TRACKING_ID:
1678 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001679 // The slot is no longer in use but it retains its previous contents,
1680 // which may be reused for subsequent touches.
1681 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001682 } else {
1683 slot->mInUse = true;
1684 slot->mAbsMTTrackingId = rawEvent->value;
1685 }
1686 break;
1687 case ABS_MT_PRESSURE:
1688 slot->mInUse = true;
1689 slot->mAbsMTPressure = rawEvent->value;
1690 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001691 case ABS_MT_DISTANCE:
1692 slot->mInUse = true;
1693 slot->mAbsMTDistance = rawEvent->value;
1694 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001695 case ABS_MT_TOOL_TYPE:
1696 slot->mInUse = true;
1697 slot->mAbsMTToolType = rawEvent->value;
1698 slot->mHaveAbsMTToolType = true;
1699 break;
1700 }
1701 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001702 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001703 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1704 mCurrentSlot += 1;
1705 }
1706}
1707
Jeff Brown65fd2512011-08-18 11:20:58 -07001708void MultiTouchMotionAccumulator::finishSync() {
1709 if (!mUsingSlotsProtocol) {
1710 clearSlots(-1);
1711 }
1712}
1713
Jeff Brown49754db2011-07-01 17:37:58 -07001714
1715// --- MultiTouchMotionAccumulator::Slot ---
1716
1717MultiTouchMotionAccumulator::Slot::Slot() {
1718 clear();
1719}
1720
Jeff Brown49754db2011-07-01 17:37:58 -07001721void MultiTouchMotionAccumulator::Slot::clear() {
1722 mInUse = false;
1723 mHaveAbsMTTouchMinor = false;
1724 mHaveAbsMTWidthMinor = false;
1725 mHaveAbsMTToolType = false;
1726 mAbsMTPositionX = 0;
1727 mAbsMTPositionY = 0;
1728 mAbsMTTouchMajor = 0;
1729 mAbsMTTouchMinor = 0;
1730 mAbsMTWidthMajor = 0;
1731 mAbsMTWidthMinor = 0;
1732 mAbsMTOrientation = 0;
1733 mAbsMTTrackingId = -1;
1734 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001735 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001736 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001737}
1738
1739int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1740 if (mHaveAbsMTToolType) {
1741 switch (mAbsMTToolType) {
1742 case MT_TOOL_FINGER:
1743 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1744 case MT_TOOL_PEN:
1745 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1746 }
1747 }
1748 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1749}
1750
1751
Jeff Brown6d0fec22010-07-23 21:28:06 -07001752// --- InputMapper ---
1753
1754InputMapper::InputMapper(InputDevice* device) :
1755 mDevice(device), mContext(device->getContext()) {
1756}
1757
1758InputMapper::~InputMapper() {
1759}
1760
1761void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1762 info->addSource(getSources());
1763}
1764
Jeff Brownef3d7e82010-09-30 14:33:04 -07001765void InputMapper::dump(String8& dump) {
1766}
1767
Jeff Brown65fd2512011-08-18 11:20:58 -07001768void InputMapper::configure(nsecs_t when,
1769 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001770}
1771
Jeff Brown65fd2512011-08-18 11:20:58 -07001772void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001773}
1774
Jeff Brownaa3855d2011-03-17 01:34:19 -07001775void InputMapper::timeoutExpired(nsecs_t when) {
1776}
1777
Jeff Brown6d0fec22010-07-23 21:28:06 -07001778int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1779 return AKEY_STATE_UNKNOWN;
1780}
1781
1782int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1783 return AKEY_STATE_UNKNOWN;
1784}
1785
1786int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1787 return AKEY_STATE_UNKNOWN;
1788}
1789
1790bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1791 const int32_t* keyCodes, uint8_t* outFlags) {
1792 return false;
1793}
1794
Jeff Browna47425a2012-04-13 04:09:27 -07001795void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1796 int32_t token) {
1797}
1798
1799void InputMapper::cancelVibrate(int32_t token) {
1800}
1801
Jeff Brown6d0fec22010-07-23 21:28:06 -07001802int32_t InputMapper::getMetaState() {
1803 return 0;
1804}
1805
Jeff Brown05dc66a2011-03-02 14:41:58 -08001806void InputMapper::fadePointer() {
1807}
1808
Jeff Brownbe1aa822011-07-27 16:04:54 -07001809status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1810 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1811}
1812
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001813void InputMapper::bumpGeneration() {
1814 mDevice->bumpGeneration();
1815}
1816
Jeff Browncb1404e2011-01-15 18:14:15 -08001817void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1818 const RawAbsoluteAxisInfo& axis, const char* name) {
1819 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001820 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1821 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001822 } else {
1823 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1824 }
1825}
1826
Jeff Brown6d0fec22010-07-23 21:28:06 -07001827
1828// --- SwitchInputMapper ---
1829
1830SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1831 InputMapper(device) {
1832}
1833
1834SwitchInputMapper::~SwitchInputMapper() {
1835}
1836
1837uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001838 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001839}
1840
1841void SwitchInputMapper::process(const RawEvent* rawEvent) {
1842 switch (rawEvent->type) {
1843 case EV_SW:
Jeff Brown49ccac52012-04-11 18:27:33 -07001844 processSwitch(rawEvent->when, rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001845 break;
1846 }
1847}
1848
1849void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001850 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1851 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001852}
1853
1854int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1855 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1856}
1857
1858
Jeff Browna47425a2012-04-13 04:09:27 -07001859// --- VibratorInputMapper ---
1860
1861VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1862 InputMapper(device), mVibrating(false) {
1863}
1864
1865VibratorInputMapper::~VibratorInputMapper() {
1866}
1867
1868uint32_t VibratorInputMapper::getSources() {
1869 return 0;
1870}
1871
1872void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1873 InputMapper::populateDeviceInfo(info);
1874
1875 info->setVibrator(true);
1876}
1877
1878void VibratorInputMapper::process(const RawEvent* rawEvent) {
1879 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1880}
1881
1882void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1883 int32_t token) {
1884#if DEBUG_VIBRATOR
1885 String8 patternStr;
1886 for (size_t i = 0; i < patternSize; i++) {
1887 if (i != 0) {
1888 patternStr.append(", ");
1889 }
1890 patternStr.appendFormat("%lld", pattern[i]);
1891 }
1892 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1893 getDeviceId(), patternStr.string(), repeat, token);
1894#endif
1895
1896 mVibrating = true;
1897 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1898 mPatternSize = patternSize;
1899 mRepeat = repeat;
1900 mToken = token;
1901 mIndex = -1;
1902
1903 nextStep();
1904}
1905
1906void VibratorInputMapper::cancelVibrate(int32_t token) {
1907#if DEBUG_VIBRATOR
1908 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1909#endif
1910
1911 if (mVibrating && mToken == token) {
1912 stopVibrating();
1913 }
1914}
1915
1916void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1917 if (mVibrating) {
1918 if (when >= mNextStepTime) {
1919 nextStep();
1920 } else {
1921 getContext()->requestTimeoutAtTime(mNextStepTime);
1922 }
1923 }
1924}
1925
1926void VibratorInputMapper::nextStep() {
1927 mIndex += 1;
1928 if (size_t(mIndex) >= mPatternSize) {
1929 if (mRepeat < 0) {
1930 // We are done.
1931 stopVibrating();
1932 return;
1933 }
1934 mIndex = mRepeat;
1935 }
1936
1937 bool vibratorOn = mIndex & 1;
1938 nsecs_t duration = mPattern[mIndex];
1939 if (vibratorOn) {
1940#if DEBUG_VIBRATOR
1941 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1942 getDeviceId(), duration);
1943#endif
1944 getEventHub()->vibrate(getDeviceId(), duration);
1945 } else {
1946#if DEBUG_VIBRATOR
1947 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1948#endif
1949 getEventHub()->cancelVibrate(getDeviceId());
1950 }
1951 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1952 mNextStepTime = now + duration;
1953 getContext()->requestTimeoutAtTime(mNextStepTime);
1954#if DEBUG_VIBRATOR
1955 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1956#endif
1957}
1958
1959void VibratorInputMapper::stopVibrating() {
1960 mVibrating = false;
1961#if DEBUG_VIBRATOR
1962 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1963#endif
1964 getEventHub()->cancelVibrate(getDeviceId());
1965}
1966
1967void VibratorInputMapper::dump(String8& dump) {
1968 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1969 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1970}
1971
1972
Jeff Brown6d0fec22010-07-23 21:28:06 -07001973// --- KeyboardInputMapper ---
1974
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001975KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001976 uint32_t source, int32_t keyboardType) :
1977 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001978 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001979}
1980
1981KeyboardInputMapper::~KeyboardInputMapper() {
1982}
1983
Jeff Brown6d0fec22010-07-23 21:28:06 -07001984uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001985 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001986}
1987
1988void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1989 InputMapper::populateDeviceInfo(info);
1990
1991 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001992 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001993}
1994
Jeff Brownef3d7e82010-09-30 14:33:04 -07001995void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001996 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1997 dumpParameters(dump);
1998 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001999 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002000 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
2001 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
2002 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002003}
2004
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002005
Jeff Brown65fd2512011-08-18 11:20:58 -07002006void KeyboardInputMapper::configure(nsecs_t when,
2007 const InputReaderConfiguration* config, uint32_t changes) {
2008 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002009
Jeff Brown474dcb52011-06-14 20:22:50 -07002010 if (!changes) { // first time only
2011 // Configure basic parameters.
2012 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002013 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002014
Jeff Brown65fd2512011-08-18 11:20:58 -07002015 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2016 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2017 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2018 false /*external*/, NULL, NULL, &mOrientation)) {
2019 mOrientation = DISPLAY_ORIENTATION_0;
2020 }
2021 } else {
2022 mOrientation = DISPLAY_ORIENTATION_0;
2023 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002024 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002025}
2026
2027void KeyboardInputMapper::configureParameters() {
2028 mParameters.orientationAware = false;
2029 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2030 mParameters.orientationAware);
2031
Jeff Brownbc68a592011-07-25 12:58:12 -07002032 mParameters.associatedDisplayId = -1;
2033 if (mParameters.orientationAware) {
2034 mParameters.associatedDisplayId = 0;
2035 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002036}
2037
2038void KeyboardInputMapper::dumpParameters(String8& dump) {
2039 dump.append(INDENT3 "Parameters:\n");
2040 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2041 mParameters.associatedDisplayId);
2042 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2043 toString(mParameters.orientationAware));
2044}
2045
Jeff Brown65fd2512011-08-18 11:20:58 -07002046void KeyboardInputMapper::reset(nsecs_t when) {
2047 mMetaState = AMETA_NONE;
2048 mDownTime = 0;
2049 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002050 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002051
Jeff Brownbe1aa822011-07-27 16:04:54 -07002052 resetLedState();
2053
Jeff Brown65fd2512011-08-18 11:20:58 -07002054 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002055}
2056
2057void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2058 switch (rawEvent->type) {
2059 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002060 int32_t scanCode = rawEvent->code;
2061 int32_t usageCode = mCurrentHidUsage;
2062 mCurrentHidUsage = 0;
2063
Jeff Brown6d0fec22010-07-23 21:28:06 -07002064 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002065 int32_t keyCode;
2066 uint32_t flags;
2067 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2068 keyCode = AKEYCODE_UNKNOWN;
2069 flags = 0;
2070 }
2071 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002072 }
2073 break;
2074 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002075 case EV_MSC: {
2076 if (rawEvent->code == MSC_SCAN) {
2077 mCurrentHidUsage = rawEvent->value;
2078 }
2079 break;
2080 }
2081 case EV_SYN: {
2082 if (rawEvent->code == SYN_REPORT) {
2083 mCurrentHidUsage = 0;
2084 }
2085 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002086 }
2087}
2088
2089bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2090 return scanCode < BTN_MOUSE
2091 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002092 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002093 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002094}
2095
Jeff Brown6328cdc2010-07-29 18:18:33 -07002096void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2097 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002098
Jeff Brownbe1aa822011-07-27 16:04:54 -07002099 if (down) {
2100 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002101 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002102 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002103 }
Jeff Brownfe508922011-01-18 15:10:10 -08002104
Jeff Brownbe1aa822011-07-27 16:04:54 -07002105 // Add key down.
2106 ssize_t keyDownIndex = findKeyDown(scanCode);
2107 if (keyDownIndex >= 0) {
2108 // key repeat, be sure to use same keycode as before in case of rotation
2109 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002110 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002111 // key down
2112 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2113 && mContext->shouldDropVirtualKey(when,
2114 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002115 return;
2116 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002117
2118 mKeyDowns.push();
2119 KeyDown& keyDown = mKeyDowns.editTop();
2120 keyDown.keyCode = keyCode;
2121 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002122 }
2123
Jeff Brownbe1aa822011-07-27 16:04:54 -07002124 mDownTime = when;
2125 } else {
2126 // Remove key down.
2127 ssize_t keyDownIndex = findKeyDown(scanCode);
2128 if (keyDownIndex >= 0) {
2129 // key up, be sure to use same keycode as before in case of rotation
2130 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2131 mKeyDowns.removeAt(size_t(keyDownIndex));
2132 } else {
2133 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002134 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002135 "keyCode=%d, scanCode=%d",
2136 getDeviceName().string(), keyCode, scanCode);
2137 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002138 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002139 }
Jeff Brownfd035822010-06-30 16:10:35 -07002140
Jeff Brownbe1aa822011-07-27 16:04:54 -07002141 bool metaStateChanged = false;
2142 int32_t oldMetaState = mMetaState;
2143 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2144 if (oldMetaState != newMetaState) {
2145 mMetaState = newMetaState;
2146 metaStateChanged = true;
2147 updateLedState(false);
2148 }
2149
2150 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002151
Jeff Brown56194eb2011-03-02 19:23:13 -08002152 // Key down on external an keyboard should wake the device.
2153 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2154 // For internal keyboards, the key layout file should specify the policy flags for
2155 // each wake key individually.
2156 // TODO: Use the input device configuration to control this behavior more finely.
2157 if (down && getDevice()->isExternal()
2158 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2159 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2160 }
2161
Jeff Brown6328cdc2010-07-29 18:18:33 -07002162 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002163 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002164 }
2165
Jeff Brown05dc66a2011-03-02 14:41:58 -08002166 if (down && !isMetaKey(keyCode)) {
2167 getContext()->fadePointer();
2168 }
2169
Jeff Brownbe1aa822011-07-27 16:04:54 -07002170 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002171 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2172 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002173 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002174}
2175
Jeff Brownbe1aa822011-07-27 16:04:54 -07002176ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2177 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002178 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002179 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002180 return i;
2181 }
2182 }
2183 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002184}
2185
Jeff Brown6d0fec22010-07-23 21:28:06 -07002186int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2187 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2188}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002189
Jeff Brown6d0fec22010-07-23 21:28:06 -07002190int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2191 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2192}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002193
Jeff Brown6d0fec22010-07-23 21:28:06 -07002194bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2195 const int32_t* keyCodes, uint8_t* outFlags) {
2196 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2197}
2198
2199int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002200 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002201}
2202
Jeff Brownbe1aa822011-07-27 16:04:54 -07002203void KeyboardInputMapper::resetLedState() {
2204 initializeLedState(mCapsLockLedState, LED_CAPSL);
2205 initializeLedState(mNumLockLedState, LED_NUML);
2206 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002207
Jeff Brownbe1aa822011-07-27 16:04:54 -07002208 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002209}
2210
Jeff Brownbe1aa822011-07-27 16:04:54 -07002211void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002212 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2213 ledState.on = false;
2214}
2215
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216void KeyboardInputMapper::updateLedState(bool reset) {
2217 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002218 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002219 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002220 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002221 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002222 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002223}
2224
Jeff Brownbe1aa822011-07-27 16:04:54 -07002225void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002226 int32_t led, int32_t modifier, bool reset) {
2227 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002228 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002229 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002230 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2231 ledState.on = desiredState;
2232 }
2233 }
2234}
2235
Jeff Brown6d0fec22010-07-23 21:28:06 -07002236
Jeff Brown83c09682010-12-23 17:50:18 -08002237// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002238
Jeff Brown83c09682010-12-23 17:50:18 -08002239CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002240 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002241}
2242
Jeff Brown83c09682010-12-23 17:50:18 -08002243CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002244}
2245
Jeff Brown83c09682010-12-23 17:50:18 -08002246uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002247 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002248}
2249
Jeff Brown83c09682010-12-23 17:50:18 -08002250void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002251 InputMapper::populateDeviceInfo(info);
2252
Jeff Brown83c09682010-12-23 17:50:18 -08002253 if (mParameters.mode == Parameters::MODE_POINTER) {
2254 float minX, minY, maxX, maxY;
2255 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002256 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2257 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002258 }
2259 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002260 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2261 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002262 }
Jeff Brownefd32662011-03-08 15:13:06 -08002263 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002264
Jeff Brown65fd2512011-08-18 11:20:58 -07002265 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002266 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002267 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002268 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002269 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002270 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002271}
2272
Jeff Brown83c09682010-12-23 17:50:18 -08002273void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002274 dump.append(INDENT2 "Cursor Input Mapper:\n");
2275 dumpParameters(dump);
2276 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2277 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2278 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2279 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2280 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002281 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002282 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002283 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002284 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2285 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002286 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002287 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2288 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2289 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002290}
2291
Jeff Brown65fd2512011-08-18 11:20:58 -07002292void CursorInputMapper::configure(nsecs_t when,
2293 const InputReaderConfiguration* config, uint32_t changes) {
2294 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002295
Jeff Brown474dcb52011-06-14 20:22:50 -07002296 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002297 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002298
Jeff Brown474dcb52011-06-14 20:22:50 -07002299 // Configure basic parameters.
2300 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002301
Jeff Brown474dcb52011-06-14 20:22:50 -07002302 // Configure device mode.
2303 switch (mParameters.mode) {
2304 case Parameters::MODE_POINTER:
2305 mSource = AINPUT_SOURCE_MOUSE;
2306 mXPrecision = 1.0f;
2307 mYPrecision = 1.0f;
2308 mXScale = 1.0f;
2309 mYScale = 1.0f;
2310 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2311 break;
2312 case Parameters::MODE_NAVIGATION:
2313 mSource = AINPUT_SOURCE_TRACKBALL;
2314 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2315 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2316 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2317 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2318 break;
2319 }
2320
2321 mVWheelScale = 1.0f;
2322 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002323 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002324
Jeff Brown474dcb52011-06-14 20:22:50 -07002325 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2326 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2327 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2328 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2329 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002330
2331 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2332 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2333 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2334 false /*external*/, NULL, NULL, &mOrientation)) {
2335 mOrientation = DISPLAY_ORIENTATION_0;
2336 }
2337 } else {
2338 mOrientation = DISPLAY_ORIENTATION_0;
2339 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002340 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002341 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002342}
2343
Jeff Brown83c09682010-12-23 17:50:18 -08002344void CursorInputMapper::configureParameters() {
2345 mParameters.mode = Parameters::MODE_POINTER;
2346 String8 cursorModeString;
2347 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2348 if (cursorModeString == "navigation") {
2349 mParameters.mode = Parameters::MODE_NAVIGATION;
2350 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002351 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002352 }
2353 }
2354
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002355 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002356 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002357 mParameters.orientationAware);
2358
Jeff Brownbc68a592011-07-25 12:58:12 -07002359 mParameters.associatedDisplayId = -1;
2360 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2361 mParameters.associatedDisplayId = 0;
2362 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002363}
2364
Jeff Brown83c09682010-12-23 17:50:18 -08002365void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002366 dump.append(INDENT3 "Parameters:\n");
2367 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2368 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002369
2370 switch (mParameters.mode) {
2371 case Parameters::MODE_POINTER:
2372 dump.append(INDENT4 "Mode: pointer\n");
2373 break;
2374 case Parameters::MODE_NAVIGATION:
2375 dump.append(INDENT4 "Mode: navigation\n");
2376 break;
2377 default:
Steve Blockec193de2012-01-09 18:35:44 +00002378 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002379 }
2380
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002381 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2382 toString(mParameters.orientationAware));
2383}
2384
Jeff Brown65fd2512011-08-18 11:20:58 -07002385void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002386 mButtonState = 0;
2387 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002388
Jeff Brownbe1aa822011-07-27 16:04:54 -07002389 mPointerVelocityControl.reset();
2390 mWheelXVelocityControl.reset();
2391 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002392
Jeff Brown65fd2512011-08-18 11:20:58 -07002393 mCursorButtonAccumulator.reset(getDevice());
2394 mCursorMotionAccumulator.reset(getDevice());
2395 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002396
Jeff Brown65fd2512011-08-18 11:20:58 -07002397 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002398}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002399
Jeff Brown83c09682010-12-23 17:50:18 -08002400void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002401 mCursorButtonAccumulator.process(rawEvent);
2402 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002403 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002404
Jeff Brown49ccac52012-04-11 18:27:33 -07002405 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002406 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002407 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002408}
2409
Jeff Brown83c09682010-12-23 17:50:18 -08002410void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002411 int32_t lastButtonState = mButtonState;
2412 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2413 mButtonState = currentButtonState;
2414
2415 bool wasDown = isPointerDown(lastButtonState);
2416 bool down = isPointerDown(currentButtonState);
2417 bool downChanged;
2418 if (!wasDown && down) {
2419 mDownTime = when;
2420 downChanged = true;
2421 } else if (wasDown && !down) {
2422 downChanged = true;
2423 } else {
2424 downChanged = false;
2425 }
2426 nsecs_t downTime = mDownTime;
2427 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002428 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002429
2430 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2431 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2432 bool moved = deltaX != 0 || deltaY != 0;
2433
Jeff Brown65fd2512011-08-18 11:20:58 -07002434 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002435 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2436 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002437 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002438 }
2439
Jeff Brown65fd2512011-08-18 11:20:58 -07002440 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002441 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002442 pointerProperties.clear();
2443 pointerProperties.id = 0;
2444 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2445
Jeff Brown6328cdc2010-07-29 18:18:33 -07002446 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002447 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002448
Jeff Brown65fd2512011-08-18 11:20:58 -07002449 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2450 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002451 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002452
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 mWheelYVelocityControl.move(when, NULL, &vscroll);
2454 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002455
Jeff Brownbe1aa822011-07-27 16:04:54 -07002456 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002457
Jeff Brownbe1aa822011-07-27 16:04:54 -07002458 if (mPointerController != NULL) {
2459 if (moved || scrolled || buttonsChanged) {
2460 mPointerController->setPresentation(
2461 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002462
Jeff Brownbe1aa822011-07-27 16:04:54 -07002463 if (moved) {
2464 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002465 }
2466
Jeff Brownbe1aa822011-07-27 16:04:54 -07002467 if (buttonsChanged) {
2468 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002469 }
Jeff Brownefd32662011-03-08 15:13:06 -08002470
Jeff Brownbe1aa822011-07-27 16:04:54 -07002471 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002472 }
2473
Jeff Brownbe1aa822011-07-27 16:04:54 -07002474 float x, y;
2475 mPointerController->getPosition(&x, &y);
2476 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2477 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2478 } else {
2479 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2480 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2481 }
2482
2483 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002484
Jeff Brown56194eb2011-03-02 19:23:13 -08002485 // Moving an external trackball or mouse should wake the device.
2486 // We don't do this for internal cursor devices to prevent them from waking up
2487 // the device in your pocket.
2488 // TODO: Use the input device configuration to control this behavior more finely.
2489 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002490 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002491 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2492 }
2493
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002494 // Synthesize key down from buttons if needed.
2495 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2496 policyFlags, lastButtonState, currentButtonState);
2497
2498 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002499 if (downChanged || moved || scrolled || buttonsChanged) {
2500 int32_t metaState = mContext->getGlobalMetaState();
2501 int32_t motionEventAction;
2502 if (downChanged) {
2503 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2504 } else if (down || mPointerController == NULL) {
2505 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2506 } else {
2507 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2508 }
Jeff Brownb6997262010-10-08 22:31:17 -07002509
Jeff Brownbe1aa822011-07-27 16:04:54 -07002510 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2511 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002512 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002513 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002514
Jeff Brownbe1aa822011-07-27 16:04:54 -07002515 // Send hover move after UP to tell the application that the mouse is hovering now.
2516 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2517 && mPointerController != NULL) {
2518 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2519 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2520 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2521 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2522 getListener()->notifyMotion(&hoverArgs);
2523 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002524
Jeff Brownbe1aa822011-07-27 16:04:54 -07002525 // Send scroll events.
2526 if (scrolled) {
2527 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2528 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2529
2530 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2531 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2532 AMOTION_EVENT_EDGE_FLAG_NONE,
2533 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2534 getListener()->notifyMotion(&scrollArgs);
2535 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002536 }
Jeff Browna032cc02011-03-07 16:56:21 -08002537
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002538 // Synthesize key up from buttons if needed.
2539 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2540 policyFlags, lastButtonState, currentButtonState);
2541
Jeff Brown65fd2512011-08-18 11:20:58 -07002542 mCursorMotionAccumulator.finishSync();
2543 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002544}
2545
Jeff Brown83c09682010-12-23 17:50:18 -08002546int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002547 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2548 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2549 } else {
2550 return AKEY_STATE_UNKNOWN;
2551 }
2552}
2553
Jeff Brown05dc66a2011-03-02 14:41:58 -08002554void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002555 if (mPointerController != NULL) {
2556 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2557 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002558}
2559
Jeff Brown6d0fec22010-07-23 21:28:06 -07002560
2561// --- TouchInputMapper ---
2562
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002563TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002564 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002565 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002566 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002567}
2568
2569TouchInputMapper::~TouchInputMapper() {
2570}
2571
2572uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002573 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002574}
2575
2576void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2577 InputMapper::populateDeviceInfo(info);
2578
Jeff Brown65fd2512011-08-18 11:20:58 -07002579 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2580 info->addMotionRange(mOrientedRanges.x);
2581 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002582 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002583
Jeff Brown65fd2512011-08-18 11:20:58 -07002584 if (mOrientedRanges.haveSize) {
2585 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002586 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002587
2588 if (mOrientedRanges.haveTouchSize) {
2589 info->addMotionRange(mOrientedRanges.touchMajor);
2590 info->addMotionRange(mOrientedRanges.touchMinor);
2591 }
2592
2593 if (mOrientedRanges.haveToolSize) {
2594 info->addMotionRange(mOrientedRanges.toolMajor);
2595 info->addMotionRange(mOrientedRanges.toolMinor);
2596 }
2597
2598 if (mOrientedRanges.haveOrientation) {
2599 info->addMotionRange(mOrientedRanges.orientation);
2600 }
2601
2602 if (mOrientedRanges.haveDistance) {
2603 info->addMotionRange(mOrientedRanges.distance);
2604 }
2605
2606 if (mOrientedRanges.haveTilt) {
2607 info->addMotionRange(mOrientedRanges.tilt);
2608 }
2609
2610 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2611 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2612 }
2613 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2614 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2615 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002616 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002617}
2618
Jeff Brownef3d7e82010-09-30 14:33:04 -07002619void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002620 dump.append(INDENT2 "Touch Input Mapper:\n");
2621 dumpParameters(dump);
2622 dumpVirtualKeys(dump);
2623 dumpRawPointerAxes(dump);
2624 dumpCalibration(dump);
2625 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002626
Jeff Brownbe1aa822011-07-27 16:04:54 -07002627 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2628 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2629 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2630 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2631 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2632 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002633 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2634 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002635 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002636 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2637 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002638 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2639 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2640 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2641 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2642 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002643
Jeff Brownbe1aa822011-07-27 16:04:54 -07002644 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002645
Jeff Brownbe1aa822011-07-27 16:04:54 -07002646 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2647 mLastRawPointerData.pointerCount);
2648 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2649 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2650 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2651 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002652 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2653 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002654 pointer.id, pointer.x, pointer.y, pointer.pressure,
2655 pointer.touchMajor, pointer.touchMinor,
2656 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002657 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002658 pointer.toolType, toString(pointer.isHovering));
2659 }
2660
2661 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2662 mLastCookedPointerData.pointerCount);
2663 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2664 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2665 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2666 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2667 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002668 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2669 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002670 pointerProperties.id,
2671 pointerCoords.getX(),
2672 pointerCoords.getY(),
2673 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2674 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2675 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2676 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2677 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2678 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002679 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002680 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2681 pointerProperties.toolType,
2682 toString(mLastCookedPointerData.isHovering(i)));
2683 }
2684
Jeff Brown65fd2512011-08-18 11:20:58 -07002685 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002686 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2687 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002688 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002689 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002690 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002691 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002692 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002693 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002694 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002695 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2696 mPointerGestureMaxSwipeWidth);
2697 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002698}
2699
Jeff Brown65fd2512011-08-18 11:20:58 -07002700void TouchInputMapper::configure(nsecs_t when,
2701 const InputReaderConfiguration* config, uint32_t changes) {
2702 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002703
Jeff Brown474dcb52011-06-14 20:22:50 -07002704 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002705
Jeff Brown474dcb52011-06-14 20:22:50 -07002706 if (!changes) { // first time only
2707 // Configure basic parameters.
2708 configureParameters();
2709
Jeff Brown65fd2512011-08-18 11:20:58 -07002710 // Configure common accumulators.
2711 mCursorScrollAccumulator.configure(getDevice());
2712 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002713
2714 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002715 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002716
2717 // Prepare input device calibration.
2718 parseCalibration();
2719 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002720 }
2721
Jeff Brown474dcb52011-06-14 20:22:50 -07002722 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002723 // Update pointer speed.
2724 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2725 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2726 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002727 }
Jeff Brown8d608662010-08-30 03:02:23 -07002728
Jeff Brown65fd2512011-08-18 11:20:58 -07002729 bool resetNeeded = false;
2730 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002731 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2732 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002733 // Configure device sources, surface dimensions, orientation and
2734 // scaling factors.
2735 configureSurface(when, &resetNeeded);
2736 }
2737
2738 if (changes && resetNeeded) {
2739 // Send reset, unless this is the first time the device has been configured,
2740 // in which case the reader will call reset itself after all mappers are ready.
2741 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002742 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002743}
2744
Jeff Brown8d608662010-08-30 03:02:23 -07002745void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002746 // Use the pointer presentation mode for devices that do not support distinct
2747 // multitouch. The spot-based presentation relies on being able to accurately
2748 // locate two or more fingers on the touch pad.
2749 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2750 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002751
Jeff Brown538881e2011-05-25 18:23:38 -07002752 String8 gestureModeString;
2753 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2754 gestureModeString)) {
2755 if (gestureModeString == "pointer") {
2756 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2757 } else if (gestureModeString == "spots") {
2758 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2759 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002760 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002761 }
2762 }
2763
Jeff Browndeffe072011-08-26 18:38:46 -07002764 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2765 // The device is a touch screen.
2766 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2767 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2768 // The device is a pointing device like a track pad.
2769 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2770 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002771 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2772 // The device is a cursor device with a touch pad attached.
2773 // By default don't use the touch pad to move the pointer.
2774 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2775 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002776 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002777 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2778 }
2779
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002780 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002781 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2782 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002783 if (deviceTypeString == "touchScreen") {
2784 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002785 } else if (deviceTypeString == "touchPad") {
2786 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002787 } else if (deviceTypeString == "pointer") {
2788 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002789 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002790 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002791 }
2792 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002793
Jeff Brownefd32662011-03-08 15:13:06 -08002794 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002795 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2796 mParameters.orientationAware);
2797
Jeff Brownbc68a592011-07-25 12:58:12 -07002798 mParameters.associatedDisplayId = -1;
2799 mParameters.associatedDisplayIsExternal = false;
2800 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002801 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002802 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2803 mParameters.associatedDisplayIsExternal =
2804 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2805 && getDevice()->isExternal();
2806 mParameters.associatedDisplayId = 0;
2807 }
Jeff Brown8d608662010-08-30 03:02:23 -07002808}
2809
Jeff Brownef3d7e82010-09-30 14:33:04 -07002810void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002811 dump.append(INDENT3 "Parameters:\n");
2812
Jeff Brown538881e2011-05-25 18:23:38 -07002813 switch (mParameters.gestureMode) {
2814 case Parameters::GESTURE_MODE_POINTER:
2815 dump.append(INDENT4 "GestureMode: pointer\n");
2816 break;
2817 case Parameters::GESTURE_MODE_SPOTS:
2818 dump.append(INDENT4 "GestureMode: spots\n");
2819 break;
2820 default:
2821 assert(false);
2822 }
2823
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002824 switch (mParameters.deviceType) {
2825 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2826 dump.append(INDENT4 "DeviceType: touchScreen\n");
2827 break;
2828 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2829 dump.append(INDENT4 "DeviceType: touchPad\n");
2830 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002831 case Parameters::DEVICE_TYPE_POINTER:
2832 dump.append(INDENT4 "DeviceType: pointer\n");
2833 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002834 default:
Steve Blockec193de2012-01-09 18:35:44 +00002835 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002836 }
2837
Jeff Brown65fd2512011-08-18 11:20:58 -07002838 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2839 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002840 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2841 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002842}
2843
Jeff Brownbe1aa822011-07-27 16:04:54 -07002844void TouchInputMapper::configureRawPointerAxes() {
2845 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002846}
2847
Jeff Brownbe1aa822011-07-27 16:04:54 -07002848void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2849 dump.append(INDENT3 "Raw Touch Axes:\n");
2850 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2851 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2852 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2853 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2854 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2855 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2856 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2857 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2858 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002859 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2860 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002861 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2862 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002863}
2864
Jeff Brown65fd2512011-08-18 11:20:58 -07002865void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2866 int32_t oldDeviceMode = mDeviceMode;
2867
2868 // Determine device mode.
2869 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2870 && mConfig.pointerGesturesEnabled) {
2871 mSource = AINPUT_SOURCE_MOUSE;
2872 mDeviceMode = DEVICE_MODE_POINTER;
2873 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2874 && mParameters.associatedDisplayId >= 0) {
2875 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2876 mDeviceMode = DEVICE_MODE_DIRECT;
2877 } else {
2878 mSource = AINPUT_SOURCE_TOUCHPAD;
2879 mDeviceMode = DEVICE_MODE_UNSCALED;
2880 }
2881
Jeff Brown9626b142011-03-03 02:09:54 -08002882 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002883 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002884 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002885 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002886 mDeviceMode = DEVICE_MODE_DISABLED;
2887 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002888 }
2889
Jeff Brown65fd2512011-08-18 11:20:58 -07002890 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002891 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002892 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002893 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002894 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2895 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002896 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002897 "display %d. The device will be inoperable until the display size "
2898 "becomes available.",
2899 getDeviceName().string(), mParameters.associatedDisplayId);
2900 mDeviceMode = DEVICE_MODE_DISABLED;
2901 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002902 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002903 }
2904
Jeff Brown65fd2512011-08-18 11:20:58 -07002905 // Configure dimensions.
2906 int32_t width, height, orientation;
2907 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2908 width = mAssociatedDisplayWidth;
2909 height = mAssociatedDisplayHeight;
2910 orientation = mParameters.orientationAware ?
2911 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2912 } else {
2913 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2914 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2915 orientation = DISPLAY_ORIENTATION_0;
2916 }
2917
2918 // If moving between pointer modes, need to reset some state.
2919 bool deviceModeChanged;
2920 if (mDeviceMode != oldDeviceMode) {
2921 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002922 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002923 }
2924
Jeff Browndaf4a122011-08-26 17:14:14 -07002925 // Create pointer controller if needed.
2926 if (mDeviceMode == DEVICE_MODE_POINTER ||
2927 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2928 if (mPointerController == NULL) {
2929 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2930 }
2931 } else {
2932 mPointerController.clear();
2933 }
2934
Jeff Brownbe1aa822011-07-27 16:04:54 -07002935 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002936 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002937 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002938 }
2939
Jeff Brownbe1aa822011-07-27 16:04:54 -07002940 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002941 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002942 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002943 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002944
Jeff Brownbe1aa822011-07-27 16:04:54 -07002945 mSurfaceWidth = width;
2946 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002947
Jeff Brown8d608662010-08-30 03:02:23 -07002948 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002949 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2950 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2951 mXPrecision = 1.0f / mXScale;
2952 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002953
Jeff Brownbe1aa822011-07-27 16:04:54 -07002954 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002955 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002956 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002957 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002958
Jeff Brownbe1aa822011-07-27 16:04:54 -07002959 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002960
Jeff Brown8d608662010-08-30 03:02:23 -07002961 // Scale factor for terms that are not oriented in a particular axis.
2962 // If the pixels are square then xScale == yScale otherwise we fake it
2963 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002964 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002965
Jeff Brown8d608662010-08-30 03:02:23 -07002966 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002967 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002968
Jeff Browna1f89ce2011-08-11 00:05:01 -07002969 // Size factors.
2970 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2971 if (mRawPointerAxes.touchMajor.valid
2972 && mRawPointerAxes.touchMajor.maxValue != 0) {
2973 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2974 } else if (mRawPointerAxes.toolMajor.valid
2975 && mRawPointerAxes.toolMajor.maxValue != 0) {
2976 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2977 } else {
2978 mSizeScale = 0.0f;
2979 }
2980
Jeff Brownbe1aa822011-07-27 16:04:54 -07002981 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002982 mOrientedRanges.haveToolSize = true;
2983 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002984
Jeff Brownbe1aa822011-07-27 16:04:54 -07002985 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002986 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002987 mOrientedRanges.touchMajor.min = 0;
2988 mOrientedRanges.touchMajor.max = diagonalSize;
2989 mOrientedRanges.touchMajor.flat = 0;
2990 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002991
Jeff Brownbe1aa822011-07-27 16:04:54 -07002992 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2993 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002994
Jeff Brownbe1aa822011-07-27 16:04:54 -07002995 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002996 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002997 mOrientedRanges.toolMajor.min = 0;
2998 mOrientedRanges.toolMajor.max = diagonalSize;
2999 mOrientedRanges.toolMajor.flat = 0;
3000 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003001
Jeff Brownbe1aa822011-07-27 16:04:54 -07003002 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3003 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003004
3005 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003006 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003007 mOrientedRanges.size.min = 0;
3008 mOrientedRanges.size.max = 1.0;
3009 mOrientedRanges.size.flat = 0;
3010 mOrientedRanges.size.fuzz = 0;
3011 } else {
3012 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003013 }
3014
3015 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003016 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003017 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3018 || mCalibration.pressureCalibration
3019 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3020 if (mCalibration.havePressureScale) {
3021 mPressureScale = mCalibration.pressureScale;
3022 } else if (mRawPointerAxes.pressure.valid
3023 && mRawPointerAxes.pressure.maxValue != 0) {
3024 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003025 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003026 }
Jeff Brown8d608662010-08-30 03:02:23 -07003027
Jeff Brown65fd2512011-08-18 11:20:58 -07003028 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3029 mOrientedRanges.pressure.source = mSource;
3030 mOrientedRanges.pressure.min = 0;
3031 mOrientedRanges.pressure.max = 1.0;
3032 mOrientedRanges.pressure.flat = 0;
3033 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003034
Jeff Brown65fd2512011-08-18 11:20:58 -07003035 // Tilt
3036 mTiltXCenter = 0;
3037 mTiltXScale = 0;
3038 mTiltYCenter = 0;
3039 mTiltYScale = 0;
3040 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3041 if (mHaveTilt) {
3042 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3043 mRawPointerAxes.tiltX.maxValue);
3044 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3045 mRawPointerAxes.tiltY.maxValue);
3046 mTiltXScale = M_PI / 180;
3047 mTiltYScale = M_PI / 180;
3048
3049 mOrientedRanges.haveTilt = true;
3050
3051 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3052 mOrientedRanges.tilt.source = mSource;
3053 mOrientedRanges.tilt.min = 0;
3054 mOrientedRanges.tilt.max = M_PI_2;
3055 mOrientedRanges.tilt.flat = 0;
3056 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003057 }
3058
Jeff Brown8d608662010-08-30 03:02:23 -07003059 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07003060 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003061 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003062 if (mHaveTilt) {
3063 mOrientedRanges.haveOrientation = true;
3064
3065 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3066 mOrientedRanges.orientation.source = mSource;
3067 mOrientedRanges.orientation.min = -M_PI;
3068 mOrientedRanges.orientation.max = M_PI;
3069 mOrientedRanges.orientation.flat = 0;
3070 mOrientedRanges.orientation.fuzz = 0;
3071 } else if (mCalibration.orientationCalibration !=
3072 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003073 if (mCalibration.orientationCalibration
3074 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003075 if (mRawPointerAxes.orientation.valid) {
3076 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
3077 mRawPointerAxes.orientation.maxValue);
3078 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
3079 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07003080 }
3081 }
3082
Jeff Brownbe1aa822011-07-27 16:04:54 -07003083 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003084
Jeff Brownbe1aa822011-07-27 16:04:54 -07003085 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003086 mOrientedRanges.orientation.source = mSource;
3087 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003088 mOrientedRanges.orientation.max = M_PI_2;
3089 mOrientedRanges.orientation.flat = 0;
3090 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003091 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003092
3093 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003094 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003095 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3096 if (mCalibration.distanceCalibration
3097 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3098 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003099 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003100 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003101 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003102 }
3103 }
3104
Jeff Brownbe1aa822011-07-27 16:04:54 -07003105 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003106
Jeff Brownbe1aa822011-07-27 16:04:54 -07003107 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003108 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003109 mOrientedRanges.distance.min =
3110 mRawPointerAxes.distance.minValue * mDistanceScale;
3111 mOrientedRanges.distance.max =
3112 mRawPointerAxes.distance.minValue * mDistanceScale;
3113 mOrientedRanges.distance.flat = 0;
3114 mOrientedRanges.distance.fuzz =
3115 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003116 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003117 }
3118
Jeff Brown65fd2512011-08-18 11:20:58 -07003119 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08003120 // Compute oriented surface dimensions, precision, scales and ranges.
3121 // Note that the maximum value reported is an inclusive maximum value so it is one
3122 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003123 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003124 case DISPLAY_ORIENTATION_90:
3125 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003126 mOrientedSurfaceWidth = mSurfaceHeight;
3127 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08003128
Jeff Brownbe1aa822011-07-27 16:04:54 -07003129 mOrientedXPrecision = mYPrecision;
3130 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003131
Jeff Brownbe1aa822011-07-27 16:04:54 -07003132 mOrientedRanges.x.min = 0;
3133 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3134 * mYScale;
3135 mOrientedRanges.x.flat = 0;
3136 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003137
Jeff Brownbe1aa822011-07-27 16:04:54 -07003138 mOrientedRanges.y.min = 0;
3139 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3140 * mXScale;
3141 mOrientedRanges.y.flat = 0;
3142 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003143 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003144
Jeff Brown6d0fec22010-07-23 21:28:06 -07003145 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003146 mOrientedSurfaceWidth = mSurfaceWidth;
3147 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08003148
Jeff Brownbe1aa822011-07-27 16:04:54 -07003149 mOrientedXPrecision = mXPrecision;
3150 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003151
Jeff Brownbe1aa822011-07-27 16:04:54 -07003152 mOrientedRanges.x.min = 0;
3153 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3154 * mXScale;
3155 mOrientedRanges.x.flat = 0;
3156 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003157
Jeff Brownbe1aa822011-07-27 16:04:54 -07003158 mOrientedRanges.y.min = 0;
3159 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3160 * mYScale;
3161 mOrientedRanges.y.flat = 0;
3162 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003163 break;
3164 }
Jeff Brownace13b12011-03-09 17:39:48 -08003165
3166 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003167 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003168 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3169 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07003170 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003171 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
3172 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003173
Jeff Brown2352b972011-04-12 22:39:53 -07003174 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003175 // given area relative to the diagonal size of the display when no acceleration
3176 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003177 // Assume that the touch pad has a square aspect ratio such that movements in
3178 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003179 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003180 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003181 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003182
3183 // Scale zooms to cover a smaller range of the display than movements do.
3184 // This value determines the area around the pointer that is affected by freeform
3185 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003186 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003187 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003188 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003189
Jeff Brown2352b972011-04-12 22:39:53 -07003190 // Max width between pointers to detect a swipe gesture is more than some fraction
3191 // of the diagonal axis of the touch pad. Touches that are wider than this are
3192 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003193 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003194 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003195 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003196
Jeff Brown65fd2512011-08-18 11:20:58 -07003197 // Abort current pointer usages because the state has changed.
3198 abortPointerUsage(when, 0 /*policyFlags*/);
3199
3200 // Inform the dispatcher about the changes.
3201 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003202 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003203 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003204}
3205
Jeff Brownbe1aa822011-07-27 16:04:54 -07003206void TouchInputMapper::dumpSurface(String8& dump) {
3207 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3208 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
3209 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003210}
3211
Jeff Brownbe1aa822011-07-27 16:04:54 -07003212void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003213 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003214 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003215
Jeff Brownbe1aa822011-07-27 16:04:54 -07003216 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003217
Jeff Brown6328cdc2010-07-29 18:18:33 -07003218 if (virtualKeyDefinitions.size() == 0) {
3219 return;
3220 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003221
Jeff Brownbe1aa822011-07-27 16:04:54 -07003222 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003223
Jeff Brownbe1aa822011-07-27 16:04:54 -07003224 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3225 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3226 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3227 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003228
3229 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003230 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003231 virtualKeyDefinitions[i];
3232
Jeff Brownbe1aa822011-07-27 16:04:54 -07003233 mVirtualKeys.add();
3234 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003235
3236 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3237 int32_t keyCode;
3238 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003239 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003240 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003241 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003242 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003243 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003244 }
3245
Jeff Brown6328cdc2010-07-29 18:18:33 -07003246 virtualKey.keyCode = keyCode;
3247 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003248
Jeff Brown6328cdc2010-07-29 18:18:33 -07003249 // convert the key definition's display coordinates into touch coordinates for a hit box
3250 int32_t halfWidth = virtualKeyDefinition.width / 2;
3251 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003252
Jeff Brown6328cdc2010-07-29 18:18:33 -07003253 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003254 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003255 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003256 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003257 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003258 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003259 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003260 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003261 }
3262}
3263
Jeff Brownbe1aa822011-07-27 16:04:54 -07003264void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3265 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003266 dump.append(INDENT3 "Virtual Keys:\n");
3267
Jeff Brownbe1aa822011-07-27 16:04:54 -07003268 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3269 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003270 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3271 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3272 i, virtualKey.scanCode, virtualKey.keyCode,
3273 virtualKey.hitLeft, virtualKey.hitRight,
3274 virtualKey.hitTop, virtualKey.hitBottom);
3275 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003276 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003277}
3278
Jeff Brown8d608662010-08-30 03:02:23 -07003279void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003280 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003281 Calibration& out = mCalibration;
3282
Jeff Browna1f89ce2011-08-11 00:05:01 -07003283 // Size
3284 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3285 String8 sizeCalibrationString;
3286 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3287 if (sizeCalibrationString == "none") {
3288 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3289 } else if (sizeCalibrationString == "geometric") {
3290 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3291 } else if (sizeCalibrationString == "diameter") {
3292 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3293 } else if (sizeCalibrationString == "area") {
3294 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3295 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003296 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003297 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003298 }
3299 }
3300
Jeff Browna1f89ce2011-08-11 00:05:01 -07003301 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3302 out.sizeScale);
3303 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3304 out.sizeBias);
3305 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3306 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003307
3308 // Pressure
3309 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3310 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003311 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003312 if (pressureCalibrationString == "none") {
3313 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3314 } else if (pressureCalibrationString == "physical") {
3315 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3316 } else if (pressureCalibrationString == "amplitude") {
3317 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3318 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003319 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003320 pressureCalibrationString.string());
3321 }
3322 }
3323
Jeff Brown8d608662010-08-30 03:02:23 -07003324 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3325 out.pressureScale);
3326
Jeff Brown8d608662010-08-30 03:02:23 -07003327 // Orientation
3328 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3329 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003330 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003331 if (orientationCalibrationString == "none") {
3332 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3333 } else if (orientationCalibrationString == "interpolated") {
3334 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003335 } else if (orientationCalibrationString == "vector") {
3336 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003337 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003338 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003339 orientationCalibrationString.string());
3340 }
3341 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003342
3343 // Distance
3344 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3345 String8 distanceCalibrationString;
3346 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3347 if (distanceCalibrationString == "none") {
3348 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3349 } else if (distanceCalibrationString == "scaled") {
3350 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3351 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003352 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003353 distanceCalibrationString.string());
3354 }
3355 }
3356
3357 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3358 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003359}
3360
3361void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003362 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003363 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3364 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3365 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003366 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003367 } else {
3368 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3369 }
Jeff Brown8d608662010-08-30 03:02:23 -07003370
Jeff Browna1f89ce2011-08-11 00:05:01 -07003371 // Pressure
3372 if (mRawPointerAxes.pressure.valid) {
3373 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3374 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3375 }
3376 } else {
3377 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003378 }
3379
3380 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003381 if (mRawPointerAxes.orientation.valid) {
3382 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003383 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003384 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003385 } else {
3386 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003387 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003388
3389 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003390 if (mRawPointerAxes.distance.valid) {
3391 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003392 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003393 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003394 } else {
3395 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003396 }
Jeff Brown8d608662010-08-30 03:02:23 -07003397}
3398
Jeff Brownef3d7e82010-09-30 14:33:04 -07003399void TouchInputMapper::dumpCalibration(String8& dump) {
3400 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003401
Jeff Browna1f89ce2011-08-11 00:05:01 -07003402 // Size
3403 switch (mCalibration.sizeCalibration) {
3404 case Calibration::SIZE_CALIBRATION_NONE:
3405 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003406 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003407 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3408 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003409 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003410 case Calibration::SIZE_CALIBRATION_DIAMETER:
3411 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3412 break;
3413 case Calibration::SIZE_CALIBRATION_AREA:
3414 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003415 break;
3416 default:
Steve Blockec193de2012-01-09 18:35:44 +00003417 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003418 }
3419
Jeff Browna1f89ce2011-08-11 00:05:01 -07003420 if (mCalibration.haveSizeScale) {
3421 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3422 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003423 }
3424
Jeff Browna1f89ce2011-08-11 00:05:01 -07003425 if (mCalibration.haveSizeBias) {
3426 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3427 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003428 }
3429
Jeff Browna1f89ce2011-08-11 00:05:01 -07003430 if (mCalibration.haveSizeIsSummed) {
3431 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3432 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003433 }
3434
3435 // Pressure
3436 switch (mCalibration.pressureCalibration) {
3437 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003438 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003439 break;
3440 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003441 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003442 break;
3443 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003444 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003445 break;
3446 default:
Steve Blockec193de2012-01-09 18:35:44 +00003447 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003448 }
3449
Jeff Brown8d608662010-08-30 03:02:23 -07003450 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003451 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3452 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003453 }
3454
Jeff Brown8d608662010-08-30 03:02:23 -07003455 // Orientation
3456 switch (mCalibration.orientationCalibration) {
3457 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003458 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003459 break;
3460 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003461 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003462 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003463 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3464 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3465 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003466 default:
Steve Blockec193de2012-01-09 18:35:44 +00003467 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003468 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003469
3470 // Distance
3471 switch (mCalibration.distanceCalibration) {
3472 case Calibration::DISTANCE_CALIBRATION_NONE:
3473 dump.append(INDENT4 "touch.distance.calibration: none\n");
3474 break;
3475 case Calibration::DISTANCE_CALIBRATION_SCALED:
3476 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3477 break;
3478 default:
Steve Blockec193de2012-01-09 18:35:44 +00003479 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003480 }
3481
3482 if (mCalibration.haveDistanceScale) {
3483 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3484 mCalibration.distanceScale);
3485 }
Jeff Brown8d608662010-08-30 03:02:23 -07003486}
3487
Jeff Brown65fd2512011-08-18 11:20:58 -07003488void TouchInputMapper::reset(nsecs_t when) {
3489 mCursorButtonAccumulator.reset(getDevice());
3490 mCursorScrollAccumulator.reset(getDevice());
3491 mTouchButtonAccumulator.reset(getDevice());
3492
3493 mPointerVelocityControl.reset();
3494 mWheelXVelocityControl.reset();
3495 mWheelYVelocityControl.reset();
3496
Jeff Brownbe1aa822011-07-27 16:04:54 -07003497 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003498 mLastRawPointerData.clear();
3499 mCurrentCookedPointerData.clear();
3500 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003501 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003502 mLastButtonState = 0;
3503 mCurrentRawVScroll = 0;
3504 mCurrentRawHScroll = 0;
3505 mCurrentFingerIdBits.clear();
3506 mLastFingerIdBits.clear();
3507 mCurrentStylusIdBits.clear();
3508 mLastStylusIdBits.clear();
3509 mCurrentMouseIdBits.clear();
3510 mLastMouseIdBits.clear();
3511 mPointerUsage = POINTER_USAGE_NONE;
3512 mSentHoverEnter = false;
3513 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003514
Jeff Brown65fd2512011-08-18 11:20:58 -07003515 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003516
Jeff Brown65fd2512011-08-18 11:20:58 -07003517 mPointerGesture.reset();
3518 mPointerSimple.reset();
3519
3520 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003521 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3522 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003523 }
3524
Jeff Brown65fd2512011-08-18 11:20:58 -07003525 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003526}
3527
Jeff Brown65fd2512011-08-18 11:20:58 -07003528void TouchInputMapper::process(const RawEvent* rawEvent) {
3529 mCursorButtonAccumulator.process(rawEvent);
3530 mCursorScrollAccumulator.process(rawEvent);
3531 mTouchButtonAccumulator.process(rawEvent);
3532
Jeff Brown49ccac52012-04-11 18:27:33 -07003533 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003534 sync(rawEvent->when);
3535 }
3536}
3537
3538void TouchInputMapper::sync(nsecs_t when) {
3539 // Sync button state.
3540 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3541 | mCursorButtonAccumulator.getButtonState();
3542
3543 // Sync scroll state.
3544 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3545 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3546 mCursorScrollAccumulator.finishSync();
3547
3548 // Sync touch state.
3549 bool havePointerIds = true;
3550 mCurrentRawPointerData.clear();
3551 syncTouch(when, &havePointerIds);
3552
Jeff Brownaa3855d2011-03-17 01:34:19 -07003553#if DEBUG_RAW_EVENTS
3554 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003555 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003556 mLastRawPointerData.pointerCount,
3557 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003558 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003559 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003560 "hovering ids 0x%08x -> 0x%08x",
3561 mLastRawPointerData.pointerCount,
3562 mCurrentRawPointerData.pointerCount,
3563 mLastRawPointerData.touchingIdBits.value,
3564 mCurrentRawPointerData.touchingIdBits.value,
3565 mLastRawPointerData.hoveringIdBits.value,
3566 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003567 }
3568#endif
3569
Jeff Brown65fd2512011-08-18 11:20:58 -07003570 // Reset state that we will compute below.
3571 mCurrentFingerIdBits.clear();
3572 mCurrentStylusIdBits.clear();
3573 mCurrentMouseIdBits.clear();
3574 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003575
Jeff Brown65fd2512011-08-18 11:20:58 -07003576 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3577 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003578 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003579 mCurrentButtonState = 0;
3580 } else {
3581 // Preprocess pointer data.
3582 if (!havePointerIds) {
3583 assignPointerIds();
3584 }
3585
3586 // Handle policy on initial down or hover events.
3587 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003588 bool initialDown = mLastRawPointerData.pointerCount == 0
3589 && mCurrentRawPointerData.pointerCount != 0;
3590 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3591 if (initialDown || buttonsPressed) {
3592 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003593 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003594 getContext()->fadePointer();
3595 }
3596
3597 // Initial downs on external touch devices should wake the device.
3598 // We don't do this for internal touch screens to prevent them from waking
3599 // up in your pocket.
3600 // TODO: Use the input device configuration to control this behavior more finely.
3601 if (getDevice()->isExternal()) {
3602 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3603 }
3604 }
3605
3606 // Synthesize key down from raw buttons if needed.
3607 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3608 policyFlags, mLastButtonState, mCurrentButtonState);
3609
3610 // Consume raw off-screen touches before cooking pointer data.
3611 // If touches are consumed, subsequent code will not receive any pointer data.
3612 if (consumeRawTouches(when, policyFlags)) {
3613 mCurrentRawPointerData.clear();
3614 }
3615
3616 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3617 // with cooked pointer data that has the same ids and indices as the raw data.
3618 // The following code can use either the raw or cooked data, as needed.
3619 cookPointerData();
3620
3621 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003622 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003623 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3624 uint32_t id = idBits.clearFirstMarkedBit();
3625 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3626 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3627 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3628 mCurrentStylusIdBits.markBit(id);
3629 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3630 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3631 mCurrentFingerIdBits.markBit(id);
3632 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3633 mCurrentMouseIdBits.markBit(id);
3634 }
3635 }
3636 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3637 uint32_t id = idBits.clearFirstMarkedBit();
3638 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3639 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3640 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3641 mCurrentStylusIdBits.markBit(id);
3642 }
3643 }
3644
3645 // Stylus takes precedence over all tools, then mouse, then finger.
3646 PointerUsage pointerUsage = mPointerUsage;
3647 if (!mCurrentStylusIdBits.isEmpty()) {
3648 mCurrentMouseIdBits.clear();
3649 mCurrentFingerIdBits.clear();
3650 pointerUsage = POINTER_USAGE_STYLUS;
3651 } else if (!mCurrentMouseIdBits.isEmpty()) {
3652 mCurrentFingerIdBits.clear();
3653 pointerUsage = POINTER_USAGE_MOUSE;
3654 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3655 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003656 }
3657
3658 dispatchPointerUsage(when, policyFlags, pointerUsage);
3659 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003660 if (mDeviceMode == DEVICE_MODE_DIRECT
3661 && mConfig.showTouches && mPointerController != NULL) {
3662 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3663 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3664
3665 mPointerController->setButtonState(mCurrentButtonState);
3666 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3667 mCurrentCookedPointerData.idToIndex,
3668 mCurrentCookedPointerData.touchingIdBits);
3669 }
3670
Jeff Brown65fd2512011-08-18 11:20:58 -07003671 dispatchHoverExit(when, policyFlags);
3672 dispatchTouches(when, policyFlags);
3673 dispatchHoverEnterAndMove(when, policyFlags);
3674 }
3675
3676 // Synthesize key up from raw buttons if needed.
3677 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3678 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003679 }
3680
Jeff Brown6328cdc2010-07-29 18:18:33 -07003681 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003682 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3683 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3684 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003685 mLastFingerIdBits = mCurrentFingerIdBits;
3686 mLastStylusIdBits = mCurrentStylusIdBits;
3687 mLastMouseIdBits = mCurrentMouseIdBits;
3688
3689 // Clear some transient state.
3690 mCurrentRawVScroll = 0;
3691 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003692}
3693
Jeff Brown79ac9692011-04-19 21:20:10 -07003694void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003695 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003696 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3697 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3698 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003699 }
3700}
3701
Jeff Brownbe1aa822011-07-27 16:04:54 -07003702bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3703 // Check for release of a virtual key.
3704 if (mCurrentVirtualKey.down) {
3705 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3706 // Pointer went up while virtual key was down.
3707 mCurrentVirtualKey.down = false;
3708 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003709#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003710 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003711 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003712#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003713 dispatchVirtualKey(when, policyFlags,
3714 AKEY_EVENT_ACTION_UP,
3715 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003716 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003717 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003718 }
3719
Jeff Brownbe1aa822011-07-27 16:04:54 -07003720 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3721 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3722 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3723 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3724 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3725 // Pointer is still within the space of the virtual key.
3726 return true;
3727 }
3728 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003729
Jeff Brownbe1aa822011-07-27 16:04:54 -07003730 // Pointer left virtual key area or another pointer also went down.
3731 // Send key cancellation but do not consume the touch yet.
3732 // This is useful when the user swipes through from the virtual key area
3733 // into the main display surface.
3734 mCurrentVirtualKey.down = false;
3735 if (!mCurrentVirtualKey.ignored) {
3736#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003737 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003738 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3739#endif
3740 dispatchVirtualKey(when, policyFlags,
3741 AKEY_EVENT_ACTION_UP,
3742 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3743 | AKEY_EVENT_FLAG_CANCELED);
3744 }
3745 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003746
Jeff Brownbe1aa822011-07-27 16:04:54 -07003747 if (mLastRawPointerData.touchingIdBits.isEmpty()
3748 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3749 // Pointer just went down. Check for virtual key press or off-screen touches.
3750 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3751 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3752 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3753 // If exactly one pointer went down, check for virtual key hit.
3754 // Otherwise we will drop the entire stroke.
3755 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3756 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3757 if (virtualKey) {
3758 mCurrentVirtualKey.down = true;
3759 mCurrentVirtualKey.downTime = when;
3760 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3761 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3762 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3763 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3764
3765 if (!mCurrentVirtualKey.ignored) {
3766#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003767 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003768 mCurrentVirtualKey.keyCode,
3769 mCurrentVirtualKey.scanCode);
3770#endif
3771 dispatchVirtualKey(when, policyFlags,
3772 AKEY_EVENT_ACTION_DOWN,
3773 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3774 }
3775 }
3776 }
3777 return true;
3778 }
3779 }
3780
Jeff Brownfe508922011-01-18 15:10:10 -08003781 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003782 // most recent touch within the screen area. The idea is to filter out stray
3783 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003784 //
3785 // Problems we're trying to solve:
3786 //
3787 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3788 // virtual key area that is implemented by a separate touch panel and accidentally
3789 // triggers a virtual key.
3790 //
3791 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3792 // area and accidentally triggers a virtual key. This often happens when virtual keys
3793 // are layed out below the screen near to where the on screen keyboard's space bar
3794 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003795 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003796 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003797 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003798 return false;
3799}
3800
3801void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3802 int32_t keyEventAction, int32_t keyEventFlags) {
3803 int32_t keyCode = mCurrentVirtualKey.keyCode;
3804 int32_t scanCode = mCurrentVirtualKey.scanCode;
3805 nsecs_t downTime = mCurrentVirtualKey.downTime;
3806 int32_t metaState = mContext->getGlobalMetaState();
3807 policyFlags |= POLICY_FLAG_VIRTUAL;
3808
3809 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3810 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3811 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003812}
3813
Jeff Brown6d0fec22010-07-23 21:28:06 -07003814void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003815 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3816 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003817 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003818 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003819
3820 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003821 if (!currentIdBits.isEmpty()) {
3822 // No pointer id changes so this is a move event.
3823 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003824 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003825 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3826 AMOTION_EVENT_EDGE_FLAG_NONE,
3827 mCurrentCookedPointerData.pointerProperties,
3828 mCurrentCookedPointerData.pointerCoords,
3829 mCurrentCookedPointerData.idToIndex,
3830 currentIdBits, -1,
3831 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3832 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003833 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003834 // There may be pointers going up and pointers going down and pointers moving
3835 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003836 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3837 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003838 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003839 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003840
Jeff Brownace13b12011-03-09 17:39:48 -08003841 // Update last coordinates of pointers that have moved so that we observe the new
3842 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003843 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003844 mCurrentCookedPointerData.pointerProperties,
3845 mCurrentCookedPointerData.pointerCoords,
3846 mCurrentCookedPointerData.idToIndex,
3847 mLastCookedPointerData.pointerProperties,
3848 mLastCookedPointerData.pointerCoords,
3849 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003850 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003851 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003852 moveNeeded = true;
3853 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003854
Jeff Brownace13b12011-03-09 17:39:48 -08003855 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003856 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003857 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003858
Jeff Brown65fd2512011-08-18 11:20:58 -07003859 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003860 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003861 mLastCookedPointerData.pointerProperties,
3862 mLastCookedPointerData.pointerCoords,
3863 mLastCookedPointerData.idToIndex,
3864 dispatchedIdBits, upId,
3865 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003866 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003867 }
3868
Jeff Brownc3db8582010-10-20 15:33:38 -07003869 // Dispatch move events if any of the remaining pointers moved from their old locations.
3870 // Although applications receive new locations as part of individual pointer up
3871 // events, they do not generally handle them except when presented in a move event.
3872 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003873 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003874 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003875 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003876 mCurrentCookedPointerData.pointerProperties,
3877 mCurrentCookedPointerData.pointerCoords,
3878 mCurrentCookedPointerData.idToIndex,
3879 dispatchedIdBits, -1,
3880 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003881 }
3882
3883 // Dispatch pointer down events using the new pointer locations.
3884 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003885 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003886 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003887
Jeff Brownace13b12011-03-09 17:39:48 -08003888 if (dispatchedIdBits.count() == 1) {
3889 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003890 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003891 }
3892
Jeff Brown65fd2512011-08-18 11:20:58 -07003893 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003894 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003895 mCurrentCookedPointerData.pointerProperties,
3896 mCurrentCookedPointerData.pointerCoords,
3897 mCurrentCookedPointerData.idToIndex,
3898 dispatchedIdBits, downId,
3899 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003900 }
3901 }
Jeff Brownace13b12011-03-09 17:39:48 -08003902}
3903
Jeff Brownbe1aa822011-07-27 16:04:54 -07003904void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3905 if (mSentHoverEnter &&
3906 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3907 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3908 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003909 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003910 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3911 mLastCookedPointerData.pointerProperties,
3912 mLastCookedPointerData.pointerCoords,
3913 mLastCookedPointerData.idToIndex,
3914 mLastCookedPointerData.hoveringIdBits, -1,
3915 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3916 mSentHoverEnter = false;
3917 }
3918}
Jeff Brownace13b12011-03-09 17:39:48 -08003919
Jeff Brownbe1aa822011-07-27 16:04:54 -07003920void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3921 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3922 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3923 int32_t metaState = getContext()->getGlobalMetaState();
3924 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003925 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003926 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3927 mCurrentCookedPointerData.pointerProperties,
3928 mCurrentCookedPointerData.pointerCoords,
3929 mCurrentCookedPointerData.idToIndex,
3930 mCurrentCookedPointerData.hoveringIdBits, -1,
3931 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3932 mSentHoverEnter = true;
3933 }
Jeff Brownace13b12011-03-09 17:39:48 -08003934
Jeff Brown65fd2512011-08-18 11:20:58 -07003935 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003936 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3937 mCurrentCookedPointerData.pointerProperties,
3938 mCurrentCookedPointerData.pointerCoords,
3939 mCurrentCookedPointerData.idToIndex,
3940 mCurrentCookedPointerData.hoveringIdBits, -1,
3941 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3942 }
3943}
3944
3945void TouchInputMapper::cookPointerData() {
3946 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3947
3948 mCurrentCookedPointerData.clear();
3949 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3950 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3951 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3952
3953 // Walk through the the active pointers and map device coordinates onto
3954 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003955 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003956 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003957
Jeff Browna1f89ce2011-08-11 00:05:01 -07003958 // Size
3959 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3960 switch (mCalibration.sizeCalibration) {
3961 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3962 case Calibration::SIZE_CALIBRATION_DIAMETER:
3963 case Calibration::SIZE_CALIBRATION_AREA:
3964 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3965 touchMajor = in.touchMajor;
3966 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3967 toolMajor = in.toolMajor;
3968 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3969 size = mRawPointerAxes.touchMinor.valid
3970 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3971 } else if (mRawPointerAxes.touchMajor.valid) {
3972 toolMajor = touchMajor = in.touchMajor;
3973 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3974 ? in.touchMinor : in.touchMajor;
3975 size = mRawPointerAxes.touchMinor.valid
3976 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3977 } else if (mRawPointerAxes.toolMajor.valid) {
3978 touchMajor = toolMajor = in.toolMajor;
3979 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3980 ? in.toolMinor : in.toolMajor;
3981 size = mRawPointerAxes.toolMinor.valid
3982 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003983 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003984 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003985 "Size calibration should have been resolved to NONE.");
3986 touchMajor = 0;
3987 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003988 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003989 toolMinor = 0;
3990 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003991 }
Jeff Brownace13b12011-03-09 17:39:48 -08003992
Jeff Browna1f89ce2011-08-11 00:05:01 -07003993 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3994 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3995 if (touchingCount > 1) {
3996 touchMajor /= touchingCount;
3997 touchMinor /= touchingCount;
3998 toolMajor /= touchingCount;
3999 toolMinor /= touchingCount;
4000 size /= touchingCount;
4001 }
4002 }
Jeff Brownace13b12011-03-09 17:39:48 -08004003
Jeff Browna1f89ce2011-08-11 00:05:01 -07004004 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4005 touchMajor *= mGeometricScale;
4006 touchMinor *= mGeometricScale;
4007 toolMajor *= mGeometricScale;
4008 toolMinor *= mGeometricScale;
4009 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4010 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004011 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004012 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4013 toolMinor = toolMajor;
4014 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4015 touchMinor = touchMajor;
4016 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004017 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004018
4019 mCalibration.applySizeScaleAndBias(&touchMajor);
4020 mCalibration.applySizeScaleAndBias(&touchMinor);
4021 mCalibration.applySizeScaleAndBias(&toolMajor);
4022 mCalibration.applySizeScaleAndBias(&toolMinor);
4023 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004024 break;
4025 default:
4026 touchMajor = 0;
4027 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004028 toolMajor = 0;
4029 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004030 size = 0;
4031 break;
4032 }
4033
Jeff Browna1f89ce2011-08-11 00:05:01 -07004034 // Pressure
4035 float pressure;
4036 switch (mCalibration.pressureCalibration) {
4037 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4038 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4039 pressure = in.pressure * mPressureScale;
4040 break;
4041 default:
4042 pressure = in.isHovering ? 0 : 1;
4043 break;
4044 }
4045
Jeff Brown65fd2512011-08-18 11:20:58 -07004046 // Tilt and Orientation
4047 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004048 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004049 if (mHaveTilt) {
4050 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4051 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4052 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4053 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4054 } else {
4055 tilt = 0;
4056
4057 switch (mCalibration.orientationCalibration) {
4058 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
4059 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
4060 break;
4061 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4062 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4063 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4064 if (c1 != 0 || c2 != 0) {
4065 orientation = atan2f(c1, c2) * 0.5f;
4066 float confidence = hypotf(c1, c2);
4067 float scale = 1.0f + confidence / 16.0f;
4068 touchMajor *= scale;
4069 touchMinor /= scale;
4070 toolMajor *= scale;
4071 toolMinor /= scale;
4072 } else {
4073 orientation = 0;
4074 }
4075 break;
4076 }
4077 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004078 orientation = 0;
4079 }
Jeff Brownace13b12011-03-09 17:39:48 -08004080 }
4081
Jeff Brown80fd47c2011-05-24 01:07:44 -07004082 // Distance
4083 float distance;
4084 switch (mCalibration.distanceCalibration) {
4085 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004086 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004087 break;
4088 default:
4089 distance = 0;
4090 }
4091
Jeff Brownace13b12011-03-09 17:39:48 -08004092 // X and Y
4093 // Adjust coords for surface orientation.
4094 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004095 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004096 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004097 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
4098 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004099 orientation -= M_PI_2;
4100 if (orientation < - M_PI_2) {
4101 orientation += M_PI;
4102 }
4103 break;
4104 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004105 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
4106 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004107 break;
4108 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004109 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
4110 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004111 orientation += M_PI_2;
4112 if (orientation > M_PI_2) {
4113 orientation -= M_PI;
4114 }
4115 break;
4116 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004117 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
4118 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004119 break;
4120 }
4121
4122 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004123 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004124 out.clear();
4125 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4126 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4127 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4128 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4129 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4130 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4131 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4132 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4133 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004134 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004135 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004136
4137 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004138 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4139 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004140 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004141 properties.id = id;
4142 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004143
Jeff Brownbe1aa822011-07-27 16:04:54 -07004144 // Write id index.
4145 mCurrentCookedPointerData.idToIndex[id] = i;
4146 }
Jeff Brownace13b12011-03-09 17:39:48 -08004147}
4148
Jeff Brown65fd2512011-08-18 11:20:58 -07004149void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4150 PointerUsage pointerUsage) {
4151 if (pointerUsage != mPointerUsage) {
4152 abortPointerUsage(when, policyFlags);
4153 mPointerUsage = pointerUsage;
4154 }
4155
4156 switch (mPointerUsage) {
4157 case POINTER_USAGE_GESTURES:
4158 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4159 break;
4160 case POINTER_USAGE_STYLUS:
4161 dispatchPointerStylus(when, policyFlags);
4162 break;
4163 case POINTER_USAGE_MOUSE:
4164 dispatchPointerMouse(when, policyFlags);
4165 break;
4166 default:
4167 break;
4168 }
4169}
4170
4171void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4172 switch (mPointerUsage) {
4173 case POINTER_USAGE_GESTURES:
4174 abortPointerGestures(when, policyFlags);
4175 break;
4176 case POINTER_USAGE_STYLUS:
4177 abortPointerStylus(when, policyFlags);
4178 break;
4179 case POINTER_USAGE_MOUSE:
4180 abortPointerMouse(when, policyFlags);
4181 break;
4182 default:
4183 break;
4184 }
4185
4186 mPointerUsage = POINTER_USAGE_NONE;
4187}
4188
Jeff Brown79ac9692011-04-19 21:20:10 -07004189void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4190 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004191 // Update current gesture coordinates.
4192 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004193 bool sendEvents = preparePointerGestures(when,
4194 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4195 if (!sendEvents) {
4196 return;
4197 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004198 if (finishPreviousGesture) {
4199 cancelPreviousGesture = false;
4200 }
Jeff Brownace13b12011-03-09 17:39:48 -08004201
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004202 // Update the pointer presentation and spots.
4203 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4204 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4205 if (finishPreviousGesture || cancelPreviousGesture) {
4206 mPointerController->clearSpots();
4207 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004208 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4209 mPointerGesture.currentGestureIdToIndex,
4210 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004211 } else {
4212 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4213 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004214
Jeff Brown538881e2011-05-25 18:23:38 -07004215 // Show or hide the pointer if needed.
4216 switch (mPointerGesture.currentGestureMode) {
4217 case PointerGesture::NEUTRAL:
4218 case PointerGesture::QUIET:
4219 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4220 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4221 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4222 // Remind the user of where the pointer is after finishing a gesture with spots.
4223 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4224 }
4225 break;
4226 case PointerGesture::TAP:
4227 case PointerGesture::TAP_DRAG:
4228 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4229 case PointerGesture::HOVER:
4230 case PointerGesture::PRESS:
4231 // Unfade the pointer when the current gesture manipulates the
4232 // area directly under the pointer.
4233 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4234 break;
4235 case PointerGesture::SWIPE:
4236 case PointerGesture::FREEFORM:
4237 // Fade the pointer when the current gesture manipulates a different
4238 // area and there are spots to guide the user experience.
4239 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4240 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4241 } else {
4242 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4243 }
4244 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004245 }
4246
Jeff Brownace13b12011-03-09 17:39:48 -08004247 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004248 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004249 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004250
4251 // Update last coordinates of pointers that have moved so that we observe the new
4252 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004253 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4254 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4255 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004256 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004257 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4258 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4259 bool moveNeeded = false;
4260 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004261 && !mPointerGesture.lastGestureIdBits.isEmpty()
4262 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004263 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4264 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004265 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004266 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004267 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004268 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4269 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004270 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004271 moveNeeded = true;
4272 }
Jeff Brownace13b12011-03-09 17:39:48 -08004273 }
4274
4275 // Send motion events for all pointers that went up or were canceled.
4276 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4277 if (!dispatchedGestureIdBits.isEmpty()) {
4278 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004279 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004280 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4281 AMOTION_EVENT_EDGE_FLAG_NONE,
4282 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004283 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4284 dispatchedGestureIdBits, -1,
4285 0, 0, mPointerGesture.downTime);
4286
4287 dispatchedGestureIdBits.clear();
4288 } else {
4289 BitSet32 upGestureIdBits;
4290 if (finishPreviousGesture) {
4291 upGestureIdBits = dispatchedGestureIdBits;
4292 } else {
4293 upGestureIdBits.value = dispatchedGestureIdBits.value
4294 & ~mPointerGesture.currentGestureIdBits.value;
4295 }
4296 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004297 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004298
Jeff Brown65fd2512011-08-18 11:20:58 -07004299 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004300 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004301 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4302 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004303 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4304 dispatchedGestureIdBits, id,
4305 0, 0, mPointerGesture.downTime);
4306
4307 dispatchedGestureIdBits.clearBit(id);
4308 }
4309 }
4310 }
4311
4312 // Send motion events for all pointers that moved.
4313 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004314 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004315 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4316 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004317 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4318 dispatchedGestureIdBits, -1,
4319 0, 0, mPointerGesture.downTime);
4320 }
4321
4322 // Send motion events for all pointers that went down.
4323 if (down) {
4324 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4325 & ~dispatchedGestureIdBits.value);
4326 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004327 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004328 dispatchedGestureIdBits.markBit(id);
4329
Jeff Brownace13b12011-03-09 17:39:48 -08004330 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004331 mPointerGesture.downTime = when;
4332 }
4333
Jeff Brown65fd2512011-08-18 11:20:58 -07004334 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004335 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004336 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004337 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4338 dispatchedGestureIdBits, id,
4339 0, 0, mPointerGesture.downTime);
4340 }
4341 }
4342
Jeff Brownace13b12011-03-09 17:39:48 -08004343 // Send motion events for hover.
4344 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004345 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004346 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4347 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4348 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004349 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4350 mPointerGesture.currentGestureIdBits, -1,
4351 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004352 } else if (dispatchedGestureIdBits.isEmpty()
4353 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4354 // Synthesize a hover move event after all pointers go up to indicate that
4355 // the pointer is hovering again even if the user is not currently touching
4356 // the touch pad. This ensures that a view will receive a fresh hover enter
4357 // event after a tap.
4358 float x, y;
4359 mPointerController->getPosition(&x, &y);
4360
4361 PointerProperties pointerProperties;
4362 pointerProperties.clear();
4363 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004364 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004365
4366 PointerCoords pointerCoords;
4367 pointerCoords.clear();
4368 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4369 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4370
Jeff Brown65fd2512011-08-18 11:20:58 -07004371 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004372 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4373 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4374 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004375 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004376 }
4377
4378 // Update state.
4379 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4380 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004381 mPointerGesture.lastGestureIdBits.clear();
4382 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004383 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4384 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004385 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004386 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004387 mPointerGesture.lastGestureProperties[index].copyFrom(
4388 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004389 mPointerGesture.lastGestureCoords[index].copyFrom(
4390 mPointerGesture.currentGestureCoords[index]);
4391 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004392 }
4393 }
4394}
4395
Jeff Brown65fd2512011-08-18 11:20:58 -07004396void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4397 // Cancel previously dispatches pointers.
4398 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4399 int32_t metaState = getContext()->getGlobalMetaState();
4400 int32_t buttonState = mCurrentButtonState;
4401 dispatchMotion(when, policyFlags, mSource,
4402 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4403 AMOTION_EVENT_EDGE_FLAG_NONE,
4404 mPointerGesture.lastGestureProperties,
4405 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4406 mPointerGesture.lastGestureIdBits, -1,
4407 0, 0, mPointerGesture.downTime);
4408 }
4409
4410 // Reset the current pointer gesture.
4411 mPointerGesture.reset();
4412 mPointerVelocityControl.reset();
4413
4414 // Remove any current spots.
4415 if (mPointerController != NULL) {
4416 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4417 mPointerController->clearSpots();
4418 }
4419}
4420
Jeff Brown79ac9692011-04-19 21:20:10 -07004421bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4422 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004423 *outCancelPreviousGesture = false;
4424 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004425
Jeff Brown79ac9692011-04-19 21:20:10 -07004426 // Handle TAP timeout.
4427 if (isTimeout) {
4428#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004429 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004430#endif
4431
4432 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004433 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004434 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004435 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004436 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004437 } else {
4438 // The tap is finished.
4439#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004440 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004441#endif
4442 *outFinishPreviousGesture = true;
4443
4444 mPointerGesture.activeGestureId = -1;
4445 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4446 mPointerGesture.currentGestureIdBits.clear();
4447
Jeff Brown65fd2512011-08-18 11:20:58 -07004448 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004449 return true;
4450 }
4451 }
4452
4453 // We did not handle this timeout.
4454 return false;
4455 }
4456
Jeff Brown65fd2512011-08-18 11:20:58 -07004457 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4458 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4459
Jeff Brownace13b12011-03-09 17:39:48 -08004460 // Update the velocity tracker.
4461 {
4462 VelocityTracker::Position positions[MAX_POINTERS];
4463 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004464 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004465 uint32_t id = idBits.clearFirstMarkedBit();
4466 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004467 positions[count].x = pointer.x * mPointerXMovementScale;
4468 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004469 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004470 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004471 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004472 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004473
Jeff Brownace13b12011-03-09 17:39:48 -08004474 // Pick a new active touch id if needed.
4475 // Choose an arbitrary pointer that just went down, if there is one.
4476 // Otherwise choose an arbitrary remaining pointer.
4477 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004478 // We keep the same active touch id for as long as possible.
4479 bool activeTouchChanged = false;
4480 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4481 int32_t activeTouchId = lastActiveTouchId;
4482 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004483 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004484 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004485 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004486 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004487 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004488 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004489 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004490 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004491 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004492 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004493 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004494 } else {
4495 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004496 }
4497 }
4498
4499 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004500 bool isQuietTime = false;
4501 if (activeTouchId < 0) {
4502 mPointerGesture.resetQuietTime();
4503 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004504 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004505 if (!isQuietTime) {
4506 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4507 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4508 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004509 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004510 // Enter quiet time when exiting swipe or freeform state.
4511 // This is to prevent accidentally entering the hover state and flinging the
4512 // pointer when finishing a swipe and there is still one pointer left onscreen.
4513 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004514 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004515 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004516 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004517 // Enter quiet time when releasing the button and there are still two or more
4518 // fingers down. This may indicate that one finger was used to press the button
4519 // but it has not gone up yet.
4520 isQuietTime = true;
4521 }
4522 if (isQuietTime) {
4523 mPointerGesture.quietTime = when;
4524 }
Jeff Brownace13b12011-03-09 17:39:48 -08004525 }
4526 }
4527
4528 // Switch states based on button and pointer state.
4529 if (isQuietTime) {
4530 // Case 1: Quiet time. (QUIET)
4531#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004532 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004533 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004534#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004535 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4536 *outFinishPreviousGesture = true;
4537 }
Jeff Brownace13b12011-03-09 17:39:48 -08004538
4539 mPointerGesture.activeGestureId = -1;
4540 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004541 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004542
Jeff Brown65fd2512011-08-18 11:20:58 -07004543 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004544 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004545 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004546 // The pointer follows the active touch point.
4547 // Emit DOWN, MOVE, UP events at the pointer location.
4548 //
4549 // Only the active touch matters; other fingers are ignored. This policy helps
4550 // to handle the case where the user places a second finger on the touch pad
4551 // to apply the necessary force to depress an integrated button below the surface.
4552 // We don't want the second finger to be delivered to applications.
4553 //
4554 // For this to work well, we need to make sure to track the pointer that is really
4555 // active. If the user first puts one finger down to click then adds another
4556 // finger to drag then the active pointer should switch to the finger that is
4557 // being dragged.
4558#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004559 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004560 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004561#endif
4562 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004563 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004564 *outFinishPreviousGesture = true;
4565 mPointerGesture.activeGestureId = 0;
4566 }
4567
4568 // Switch pointers if needed.
4569 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004570 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004571 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004572 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004573 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004574 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004575 float vx, vy;
4576 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4577 float speed = hypotf(vx, vy);
4578 if (speed > bestSpeed) {
4579 bestId = id;
4580 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004581 }
Jeff Brown8d608662010-08-30 03:02:23 -07004582 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004583 }
4584 if (bestId >= 0 && bestId != activeTouchId) {
4585 mPointerGesture.activeTouchId = activeTouchId = bestId;
4586 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004587#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004588 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004589 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004590#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004591 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004592 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004593
Jeff Brown65fd2512011-08-18 11:20:58 -07004594 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004595 const RawPointerData::Pointer& currentPointer =
4596 mCurrentRawPointerData.pointerForId(activeTouchId);
4597 const RawPointerData::Pointer& lastPointer =
4598 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004599 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4600 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004601
Jeff Brownbe1aa822011-07-27 16:04:54 -07004602 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004603 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004604
4605 // Move the pointer using a relative motion.
4606 // When using spots, the click will occur at the position of the anchor
4607 // spot and all other spots will move there.
4608 mPointerController->move(deltaX, deltaY);
4609 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004610 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004611 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004612
Jeff Brownace13b12011-03-09 17:39:48 -08004613 float x, y;
4614 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004615
Jeff Brown79ac9692011-04-19 21:20:10 -07004616 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004617 mPointerGesture.currentGestureIdBits.clear();
4618 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4619 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004620 mPointerGesture.currentGestureProperties[0].clear();
4621 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004622 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004623 mPointerGesture.currentGestureCoords[0].clear();
4624 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4625 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4626 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004627 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004628 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004629 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4630 *outFinishPreviousGesture = true;
4631 }
Jeff Brownace13b12011-03-09 17:39:48 -08004632
Jeff Brown79ac9692011-04-19 21:20:10 -07004633 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004634 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004635 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004636 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4637 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004638 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004639 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004640 float x, y;
4641 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004642 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4643 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004644#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004645 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004646#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004647
4648 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004649 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004650 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004651
Jeff Brownace13b12011-03-09 17:39:48 -08004652 mPointerGesture.activeGestureId = 0;
4653 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004654 mPointerGesture.currentGestureIdBits.clear();
4655 mPointerGesture.currentGestureIdBits.markBit(
4656 mPointerGesture.activeGestureId);
4657 mPointerGesture.currentGestureIdToIndex[
4658 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004659 mPointerGesture.currentGestureProperties[0].clear();
4660 mPointerGesture.currentGestureProperties[0].id =
4661 mPointerGesture.activeGestureId;
4662 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004663 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004664 mPointerGesture.currentGestureCoords[0].clear();
4665 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004666 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004667 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004668 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004669 mPointerGesture.currentGestureCoords[0].setAxisValue(
4670 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004671
Jeff Brownace13b12011-03-09 17:39:48 -08004672 tapped = true;
4673 } else {
4674#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004675 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004676 x - mPointerGesture.tapX,
4677 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004678#endif
4679 }
4680 } else {
4681#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004682 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004683 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004684#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004685 }
Jeff Brownace13b12011-03-09 17:39:48 -08004686 }
Jeff Brown2352b972011-04-12 22:39:53 -07004687
Jeff Brown65fd2512011-08-18 11:20:58 -07004688 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004689
Jeff Brownace13b12011-03-09 17:39:48 -08004690 if (!tapped) {
4691#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004692 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004693#endif
4694 mPointerGesture.activeGestureId = -1;
4695 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004696 mPointerGesture.currentGestureIdBits.clear();
4697 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004698 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004699 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004700 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004701 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4702 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004703 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004704
Jeff Brown79ac9692011-04-19 21:20:10 -07004705 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4706 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004707 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004708 float x, y;
4709 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004710 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4711 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004712 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4713 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004714#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004715 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004716 x - mPointerGesture.tapX,
4717 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004718#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004719 }
4720 } else {
4721#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004722 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004723 (when - mPointerGesture.tapUpTime) * 0.000001f);
4724#endif
4725 }
4726 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4727 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4728 }
Jeff Brownace13b12011-03-09 17:39:48 -08004729
Jeff Brown65fd2512011-08-18 11:20:58 -07004730 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004731 const RawPointerData::Pointer& currentPointer =
4732 mCurrentRawPointerData.pointerForId(activeTouchId);
4733 const RawPointerData::Pointer& lastPointer =
4734 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004735 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004736 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004737 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004738 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004739
Jeff Brownbe1aa822011-07-27 16:04:54 -07004740 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004741 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004742
Jeff Brown2352b972011-04-12 22:39:53 -07004743 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004744 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004745 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004746 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004747 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004748 }
4749
Jeff Brown79ac9692011-04-19 21:20:10 -07004750 bool down;
4751 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4752#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004753 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004754#endif
4755 down = true;
4756 } else {
4757#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004758 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004759#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004760 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4761 *outFinishPreviousGesture = true;
4762 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004763 mPointerGesture.activeGestureId = 0;
4764 down = false;
4765 }
Jeff Brownace13b12011-03-09 17:39:48 -08004766
4767 float x, y;
4768 mPointerController->getPosition(&x, &y);
4769
Jeff Brownace13b12011-03-09 17:39:48 -08004770 mPointerGesture.currentGestureIdBits.clear();
4771 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4772 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004773 mPointerGesture.currentGestureProperties[0].clear();
4774 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4775 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004776 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004777 mPointerGesture.currentGestureCoords[0].clear();
4778 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4779 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004780 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4781 down ? 1.0f : 0.0f);
4782
Jeff Brown65fd2512011-08-18 11:20:58 -07004783 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004784 mPointerGesture.resetTap();
4785 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004786 mPointerGesture.tapX = x;
4787 mPointerGesture.tapY = y;
4788 }
Jeff Brownace13b12011-03-09 17:39:48 -08004789 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004790 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4791 // We need to provide feedback for each finger that goes down so we cannot wait
4792 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004793 //
Jeff Brown2352b972011-04-12 22:39:53 -07004794 // The ambiguous case is deciding what to do when there are two fingers down but they
4795 // have not moved enough to determine whether they are part of a drag or part of a
4796 // freeform gesture, or just a press or long-press at the pointer location.
4797 //
4798 // When there are two fingers we start with the PRESS hypothesis and we generate a
4799 // down at the pointer location.
4800 //
4801 // When the two fingers move enough or when additional fingers are added, we make
4802 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004803 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004804
Jeff Brown214eaf42011-05-26 19:17:02 -07004805 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004806 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004807 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004808 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4809 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004810 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004811 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004812 // Additional pointers have gone down but not yet settled.
4813 // Reset the gesture.
4814#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004815 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004816 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004817 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004818 * 0.000001f);
4819#endif
4820 *outCancelPreviousGesture = true;
4821 } else {
4822 // Continue previous gesture.
4823 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4824 }
4825
4826 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004827 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4828 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004829 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004830 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004831
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004832 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004833#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004834 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004835 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004836 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004837 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004838#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004839 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4840 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004841 &mPointerGesture.referenceTouchY);
4842 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4843 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004844 }
Jeff Brownace13b12011-03-09 17:39:48 -08004845
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004846 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004847 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004848 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4849 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004850 mPointerGesture.referenceDeltas[id].dx = 0;
4851 mPointerGesture.referenceDeltas[id].dy = 0;
4852 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004853 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004854
4855 // Add delta for all fingers and calculate a common movement delta.
4856 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004857 BitSet32 commonIdBits(mLastFingerIdBits.value
4858 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004859 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4860 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004861 uint32_t id = idBits.clearFirstMarkedBit();
4862 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4863 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004864 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4865 delta.dx += cpd.x - lpd.x;
4866 delta.dy += cpd.y - lpd.y;
4867
4868 if (first) {
4869 commonDeltaX = delta.dx;
4870 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004871 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004872 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4873 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4874 }
4875 }
Jeff Brownace13b12011-03-09 17:39:48 -08004876
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004877 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4878 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4879 float dist[MAX_POINTER_ID + 1];
4880 int32_t distOverThreshold = 0;
4881 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004882 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004883 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004884 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4885 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004886 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004887 distOverThreshold += 1;
4888 }
4889 }
4890
4891 // Only transition when at least two pointers have moved further than
4892 // the minimum distance threshold.
4893 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004894 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004895 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004896#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004897 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004898 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004899#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004900 *outCancelPreviousGesture = true;
4901 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4902 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004903 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004904 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004905 uint32_t id1 = idBits.clearFirstMarkedBit();
4906 uint32_t id2 = idBits.firstMarkedBit();
4907 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4908 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4909 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4910 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4911 // There are two pointers but they are too far apart for a SWIPE,
4912 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004913#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004914 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004915 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004916#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004917 *outCancelPreviousGesture = true;
4918 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4919 } else {
4920 // There are two pointers. Wait for both pointers to start moving
4921 // before deciding whether this is a SWIPE or FREEFORM gesture.
4922 float dist1 = dist[id1];
4923 float dist2 = dist[id2];
4924 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4925 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4926 // Calculate the dot product of the displacement vectors.
4927 // When the vectors are oriented in approximately the same direction,
4928 // the angle betweeen them is near zero and the cosine of the angle
4929 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4930 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4931 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004932 float dx1 = delta1.dx * mPointerXZoomScale;
4933 float dy1 = delta1.dy * mPointerYZoomScale;
4934 float dx2 = delta2.dx * mPointerXZoomScale;
4935 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004936 float dot = dx1 * dx2 + dy1 * dy2;
4937 float cosine = dot / (dist1 * dist2); // denominator always > 0
4938 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4939 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004940#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004941 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004942 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4943 "cosine %0.3f >= %0.3f",
4944 dist1, mConfig.pointerGestureMultitouchMinDistance,
4945 dist2, mConfig.pointerGestureMultitouchMinDistance,
4946 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004947#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004948 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4949 } else {
4950 // Pointers are moving in different directions. Switch to FREEFORM.
4951#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004952 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004953 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4954 "cosine %0.3f < %0.3f",
4955 dist1, mConfig.pointerGestureMultitouchMinDistance,
4956 dist2, mConfig.pointerGestureMultitouchMinDistance,
4957 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4958#endif
4959 *outCancelPreviousGesture = true;
4960 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4961 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004962 }
Jeff Brownace13b12011-03-09 17:39:48 -08004963 }
4964 }
Jeff Brownace13b12011-03-09 17:39:48 -08004965 }
4966 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004967 // Switch from SWIPE to FREEFORM if additional pointers go down.
4968 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004969 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004970#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004971 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004972 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004973#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004974 *outCancelPreviousGesture = true;
4975 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004976 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004977 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004978
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004979 // Move the reference points based on the overall group motion of the fingers
4980 // except in PRESS mode while waiting for a transition to occur.
4981 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4982 && (commonDeltaX || commonDeltaY)) {
4983 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004984 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004985 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004986 delta.dx = 0;
4987 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004988 }
4989
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004990 mPointerGesture.referenceTouchX += commonDeltaX;
4991 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004992
Jeff Brown65fd2512011-08-18 11:20:58 -07004993 commonDeltaX *= mPointerXMovementScale;
4994 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004995
Jeff Brownbe1aa822011-07-27 16:04:54 -07004996 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004997 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004998
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004999 mPointerGesture.referenceGestureX += commonDeltaX;
5000 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005001 }
5002
5003 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005004 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5005 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5006 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005007#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005008 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005009 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005010 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005011#endif
Steve Blockec193de2012-01-09 18:35:44 +00005012 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005013
5014 mPointerGesture.currentGestureIdBits.clear();
5015 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5016 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005017 mPointerGesture.currentGestureProperties[0].clear();
5018 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5019 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005020 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005021 mPointerGesture.currentGestureCoords[0].clear();
5022 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5023 mPointerGesture.referenceGestureX);
5024 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5025 mPointerGesture.referenceGestureY);
5026 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005027 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5028 // FREEFORM mode.
5029#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005030 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005031 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005032 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005033#endif
Steve Blockec193de2012-01-09 18:35:44 +00005034 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005035
Jeff Brownace13b12011-03-09 17:39:48 -08005036 mPointerGesture.currentGestureIdBits.clear();
5037
5038 BitSet32 mappedTouchIdBits;
5039 BitSet32 usedGestureIdBits;
5040 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5041 // Initially, assign the active gesture id to the active touch point
5042 // if there is one. No other touch id bits are mapped yet.
5043 if (!*outCancelPreviousGesture) {
5044 mappedTouchIdBits.markBit(activeTouchId);
5045 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5046 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5047 mPointerGesture.activeGestureId;
5048 } else {
5049 mPointerGesture.activeGestureId = -1;
5050 }
5051 } else {
5052 // Otherwise, assume we mapped all touches from the previous frame.
5053 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005054 mappedTouchIdBits.value = mLastFingerIdBits.value
5055 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005056 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5057
5058 // Check whether we need to choose a new active gesture id because the
5059 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005060 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5061 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005062 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005063 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005064 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5065 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5066 mPointerGesture.activeGestureId = -1;
5067 break;
5068 }
5069 }
5070 }
5071
5072#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005073 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005074 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5075 "activeGestureId=%d",
5076 mappedTouchIdBits.value, usedGestureIdBits.value,
5077 mPointerGesture.activeGestureId);
5078#endif
5079
Jeff Brown65fd2512011-08-18 11:20:58 -07005080 BitSet32 idBits(mCurrentFingerIdBits);
5081 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005082 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005083 uint32_t gestureId;
5084 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005085 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005086 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5087#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005088 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005089 "new mapping for touch id %d -> gesture id %d",
5090 touchId, gestureId);
5091#endif
5092 } else {
5093 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5094#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005095 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005096 "existing mapping for touch id %d -> gesture id %d",
5097 touchId, gestureId);
5098#endif
5099 }
5100 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5101 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5102
Jeff Brownbe1aa822011-07-27 16:04:54 -07005103 const RawPointerData::Pointer& pointer =
5104 mCurrentRawPointerData.pointerForId(touchId);
5105 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005106 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005107 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005108 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005109 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005110
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005111 mPointerGesture.currentGestureProperties[i].clear();
5112 mPointerGesture.currentGestureProperties[i].id = gestureId;
5113 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005114 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005115 mPointerGesture.currentGestureCoords[i].clear();
5116 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005117 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005118 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005119 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005120 mPointerGesture.currentGestureCoords[i].setAxisValue(
5121 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5122 }
5123
5124 if (mPointerGesture.activeGestureId < 0) {
5125 mPointerGesture.activeGestureId =
5126 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5127#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005128 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005129 "activeGestureId=%d", mPointerGesture.activeGestureId);
5130#endif
5131 }
Jeff Brown2352b972011-04-12 22:39:53 -07005132 }
Jeff Brownace13b12011-03-09 17:39:48 -08005133 }
5134
Jeff Brownbe1aa822011-07-27 16:04:54 -07005135 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005136
Jeff Brownace13b12011-03-09 17:39:48 -08005137#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005138 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005139 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5140 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005141 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005142 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5143 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005144 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005145 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005146 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005147 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005148 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005149 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005150 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5151 id, index, properties.toolType,
5152 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005153 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5154 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5155 }
5156 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005157 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005158 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005159 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005160 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005161 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005162 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5163 id, index, properties.toolType,
5164 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005165 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5166 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5167 }
5168#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005169 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005170}
5171
Jeff Brown65fd2512011-08-18 11:20:58 -07005172void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5173 mPointerSimple.currentCoords.clear();
5174 mPointerSimple.currentProperties.clear();
5175
5176 bool down, hovering;
5177 if (!mCurrentStylusIdBits.isEmpty()) {
5178 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5179 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5180 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5181 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5182 mPointerController->setPosition(x, y);
5183
5184 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5185 down = !hovering;
5186
5187 mPointerController->getPosition(&x, &y);
5188 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5189 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5190 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5191 mPointerSimple.currentProperties.id = 0;
5192 mPointerSimple.currentProperties.toolType =
5193 mCurrentCookedPointerData.pointerProperties[index].toolType;
5194 } else {
5195 down = false;
5196 hovering = false;
5197 }
5198
5199 dispatchPointerSimple(when, policyFlags, down, hovering);
5200}
5201
5202void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5203 abortPointerSimple(when, policyFlags);
5204}
5205
5206void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5207 mPointerSimple.currentCoords.clear();
5208 mPointerSimple.currentProperties.clear();
5209
5210 bool down, hovering;
5211 if (!mCurrentMouseIdBits.isEmpty()) {
5212 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5213 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5214 if (mLastMouseIdBits.hasBit(id)) {
5215 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5216 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5217 - mLastRawPointerData.pointers[lastIndex].x)
5218 * mPointerXMovementScale;
5219 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5220 - mLastRawPointerData.pointers[lastIndex].y)
5221 * mPointerYMovementScale;
5222
5223 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5224 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5225
5226 mPointerController->move(deltaX, deltaY);
5227 } else {
5228 mPointerVelocityControl.reset();
5229 }
5230
5231 down = isPointerDown(mCurrentButtonState);
5232 hovering = !down;
5233
5234 float x, y;
5235 mPointerController->getPosition(&x, &y);
5236 mPointerSimple.currentCoords.copyFrom(
5237 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5238 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5239 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5240 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5241 hovering ? 0.0f : 1.0f);
5242 mPointerSimple.currentProperties.id = 0;
5243 mPointerSimple.currentProperties.toolType =
5244 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5245 } else {
5246 mPointerVelocityControl.reset();
5247
5248 down = false;
5249 hovering = false;
5250 }
5251
5252 dispatchPointerSimple(when, policyFlags, down, hovering);
5253}
5254
5255void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5256 abortPointerSimple(when, policyFlags);
5257
5258 mPointerVelocityControl.reset();
5259}
5260
5261void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5262 bool down, bool hovering) {
5263 int32_t metaState = getContext()->getGlobalMetaState();
5264
5265 if (mPointerController != NULL) {
5266 if (down || hovering) {
5267 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5268 mPointerController->clearSpots();
5269 mPointerController->setButtonState(mCurrentButtonState);
5270 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5271 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5272 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5273 }
5274 }
5275
5276 if (mPointerSimple.down && !down) {
5277 mPointerSimple.down = false;
5278
5279 // Send up.
5280 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5281 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5282 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5283 mOrientedXPrecision, mOrientedYPrecision,
5284 mPointerSimple.downTime);
5285 getListener()->notifyMotion(&args);
5286 }
5287
5288 if (mPointerSimple.hovering && !hovering) {
5289 mPointerSimple.hovering = false;
5290
5291 // Send hover exit.
5292 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5293 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5294 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5295 mOrientedXPrecision, mOrientedYPrecision,
5296 mPointerSimple.downTime);
5297 getListener()->notifyMotion(&args);
5298 }
5299
5300 if (down) {
5301 if (!mPointerSimple.down) {
5302 mPointerSimple.down = true;
5303 mPointerSimple.downTime = when;
5304
5305 // Send down.
5306 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5307 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5308 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5309 mOrientedXPrecision, mOrientedYPrecision,
5310 mPointerSimple.downTime);
5311 getListener()->notifyMotion(&args);
5312 }
5313
5314 // Send move.
5315 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5316 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5317 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5318 mOrientedXPrecision, mOrientedYPrecision,
5319 mPointerSimple.downTime);
5320 getListener()->notifyMotion(&args);
5321 }
5322
5323 if (hovering) {
5324 if (!mPointerSimple.hovering) {
5325 mPointerSimple.hovering = true;
5326
5327 // Send hover enter.
5328 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5329 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5330 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5331 mOrientedXPrecision, mOrientedYPrecision,
5332 mPointerSimple.downTime);
5333 getListener()->notifyMotion(&args);
5334 }
5335
5336 // Send hover move.
5337 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5338 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5339 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5340 mOrientedXPrecision, mOrientedYPrecision,
5341 mPointerSimple.downTime);
5342 getListener()->notifyMotion(&args);
5343 }
5344
5345 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5346 float vscroll = mCurrentRawVScroll;
5347 float hscroll = mCurrentRawHScroll;
5348 mWheelYVelocityControl.move(when, NULL, &vscroll);
5349 mWheelXVelocityControl.move(when, &hscroll, NULL);
5350
5351 // Send scroll.
5352 PointerCoords pointerCoords;
5353 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5354 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5355 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5356
5357 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5358 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5359 1, &mPointerSimple.currentProperties, &pointerCoords,
5360 mOrientedXPrecision, mOrientedYPrecision,
5361 mPointerSimple.downTime);
5362 getListener()->notifyMotion(&args);
5363 }
5364
5365 // Save state.
5366 if (down || hovering) {
5367 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5368 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5369 } else {
5370 mPointerSimple.reset();
5371 }
5372}
5373
5374void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5375 mPointerSimple.currentCoords.clear();
5376 mPointerSimple.currentProperties.clear();
5377
5378 dispatchPointerSimple(when, policyFlags, false, false);
5379}
5380
Jeff Brownace13b12011-03-09 17:39:48 -08005381void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005382 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5383 const PointerProperties* properties, const PointerCoords* coords,
5384 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005385 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5386 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005387 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005388 uint32_t pointerCount = 0;
5389 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005390 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005391 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005392 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005393 pointerCoords[pointerCount].copyFrom(coords[index]);
5394
5395 if (changedId >= 0 && id == uint32_t(changedId)) {
5396 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5397 }
5398
5399 pointerCount += 1;
5400 }
5401
Steve Blockec193de2012-01-09 18:35:44 +00005402 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005403
5404 if (changedId >= 0 && pointerCount == 1) {
5405 // Replace initial down and final up action.
5406 // We can compare the action without masking off the changed pointer index
5407 // because we know the index is 0.
5408 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5409 action = AMOTION_EVENT_ACTION_DOWN;
5410 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5411 action = AMOTION_EVENT_ACTION_UP;
5412 } else {
5413 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005414 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005415 }
5416 }
5417
Jeff Brownbe1aa822011-07-27 16:04:54 -07005418 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005419 action, flags, metaState, buttonState, edgeFlags,
5420 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005421 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005422}
5423
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005424bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005425 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005426 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5427 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005428 bool changed = false;
5429 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005430 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005431 uint32_t inIndex = inIdToIndex[id];
5432 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005433
5434 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005435 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005436 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005437 PointerCoords& curOutCoords = outCoords[outIndex];
5438
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005439 if (curInProperties != curOutProperties) {
5440 curOutProperties.copyFrom(curInProperties);
5441 changed = true;
5442 }
5443
Jeff Brownace13b12011-03-09 17:39:48 -08005444 if (curInCoords != curOutCoords) {
5445 curOutCoords.copyFrom(curInCoords);
5446 changed = true;
5447 }
5448 }
5449 return changed;
5450}
5451
5452void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005453 if (mPointerController != NULL) {
5454 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5455 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005456}
5457
Jeff Brownbe1aa822011-07-27 16:04:54 -07005458bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5459 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5460 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005461}
5462
Jeff Brownbe1aa822011-07-27 16:04:54 -07005463const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005464 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005465 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005466 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005467 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005468
5469#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005470 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005471 "left=%d, top=%d, right=%d, bottom=%d",
5472 x, y,
5473 virtualKey.keyCode, virtualKey.scanCode,
5474 virtualKey.hitLeft, virtualKey.hitTop,
5475 virtualKey.hitRight, virtualKey.hitBottom);
5476#endif
5477
5478 if (virtualKey.isHit(x, y)) {
5479 return & virtualKey;
5480 }
5481 }
5482
5483 return NULL;
5484}
5485
Jeff Brownbe1aa822011-07-27 16:04:54 -07005486void TouchInputMapper::assignPointerIds() {
5487 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5488 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5489
5490 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005491
5492 if (currentPointerCount == 0) {
5493 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005494 return;
5495 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005496
Jeff Brownbe1aa822011-07-27 16:04:54 -07005497 if (lastPointerCount == 0) {
5498 // All pointers are new.
5499 for (uint32_t i = 0; i < currentPointerCount; i++) {
5500 uint32_t id = i;
5501 mCurrentRawPointerData.pointers[i].id = id;
5502 mCurrentRawPointerData.idToIndex[id] = i;
5503 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5504 }
5505 return;
5506 }
5507
5508 if (currentPointerCount == 1 && lastPointerCount == 1
5509 && mCurrentRawPointerData.pointers[0].toolType
5510 == mLastRawPointerData.pointers[0].toolType) {
5511 // Only one pointer and no change in count so it must have the same id as before.
5512 uint32_t id = mLastRawPointerData.pointers[0].id;
5513 mCurrentRawPointerData.pointers[0].id = id;
5514 mCurrentRawPointerData.idToIndex[id] = 0;
5515 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5516 return;
5517 }
5518
5519 // General case.
5520 // We build a heap of squared euclidean distances between current and last pointers
5521 // associated with the current and last pointer indices. Then, we find the best
5522 // match (by distance) for each current pointer.
5523 // The pointers must have the same tool type but it is possible for them to
5524 // transition from hovering to touching or vice-versa while retaining the same id.
5525 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5526
5527 uint32_t heapSize = 0;
5528 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5529 currentPointerIndex++) {
5530 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5531 lastPointerIndex++) {
5532 const RawPointerData::Pointer& currentPointer =
5533 mCurrentRawPointerData.pointers[currentPointerIndex];
5534 const RawPointerData::Pointer& lastPointer =
5535 mLastRawPointerData.pointers[lastPointerIndex];
5536 if (currentPointer.toolType == lastPointer.toolType) {
5537 int64_t deltaX = currentPointer.x - lastPointer.x;
5538 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005539
5540 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5541
5542 // Insert new element into the heap (sift up).
5543 heap[heapSize].currentPointerIndex = currentPointerIndex;
5544 heap[heapSize].lastPointerIndex = lastPointerIndex;
5545 heap[heapSize].distance = distance;
5546 heapSize += 1;
5547 }
5548 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005549 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005550
Jeff Brownbe1aa822011-07-27 16:04:54 -07005551 // Heapify
5552 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5553 startIndex -= 1;
5554 for (uint32_t parentIndex = startIndex; ;) {
5555 uint32_t childIndex = parentIndex * 2 + 1;
5556 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005557 break;
5558 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005559
5560 if (childIndex + 1 < heapSize
5561 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5562 childIndex += 1;
5563 }
5564
5565 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5566 break;
5567 }
5568
5569 swap(heap[parentIndex], heap[childIndex]);
5570 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005571 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005572 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005573
5574#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005575 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005576 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005577 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005578 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5579 heap[i].distance);
5580 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005581#endif
5582
Jeff Brownbe1aa822011-07-27 16:04:54 -07005583 // Pull matches out by increasing order of distance.
5584 // To avoid reassigning pointers that have already been matched, the loop keeps track
5585 // of which last and current pointers have been matched using the matchedXXXBits variables.
5586 // It also tracks the used pointer id bits.
5587 BitSet32 matchedLastBits(0);
5588 BitSet32 matchedCurrentBits(0);
5589 BitSet32 usedIdBits(0);
5590 bool first = true;
5591 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5592 while (heapSize > 0) {
5593 if (first) {
5594 // The first time through the loop, we just consume the root element of
5595 // the heap (the one with smallest distance).
5596 first = false;
5597 } else {
5598 // Previous iterations consumed the root element of the heap.
5599 // Pop root element off of the heap (sift down).
5600 heap[0] = heap[heapSize];
5601 for (uint32_t parentIndex = 0; ;) {
5602 uint32_t childIndex = parentIndex * 2 + 1;
5603 if (childIndex >= heapSize) {
5604 break;
5605 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005606
Jeff Brownbe1aa822011-07-27 16:04:54 -07005607 if (childIndex + 1 < heapSize
5608 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5609 childIndex += 1;
5610 }
5611
5612 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5613 break;
5614 }
5615
5616 swap(heap[parentIndex], heap[childIndex]);
5617 parentIndex = childIndex;
5618 }
5619
5620#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005621 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005622 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005623 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005624 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5625 heap[i].distance);
5626 }
5627#endif
5628 }
5629
5630 heapSize -= 1;
5631
5632 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5633 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5634
5635 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5636 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5637
5638 matchedCurrentBits.markBit(currentPointerIndex);
5639 matchedLastBits.markBit(lastPointerIndex);
5640
5641 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5642 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5643 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5644 mCurrentRawPointerData.markIdBit(id,
5645 mCurrentRawPointerData.isHovering(currentPointerIndex));
5646 usedIdBits.markBit(id);
5647
5648#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005649 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005650 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5651#endif
5652 break;
5653 }
5654 }
5655
5656 // Assign fresh ids to pointers that were not matched in the process.
5657 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5658 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5659 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5660
5661 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5662 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5663 mCurrentRawPointerData.markIdBit(id,
5664 mCurrentRawPointerData.isHovering(currentPointerIndex));
5665
5666#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005667 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005668 currentPointerIndex, id);
5669#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005670 }
5671}
5672
Jeff Brown6d0fec22010-07-23 21:28:06 -07005673int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005674 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5675 return AKEY_STATE_VIRTUAL;
5676 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005677
Jeff Brownbe1aa822011-07-27 16:04:54 -07005678 size_t numVirtualKeys = mVirtualKeys.size();
5679 for (size_t i = 0; i < numVirtualKeys; i++) {
5680 const VirtualKey& virtualKey = mVirtualKeys[i];
5681 if (virtualKey.keyCode == keyCode) {
5682 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005683 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005684 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005685
5686 return AKEY_STATE_UNKNOWN;
5687}
5688
5689int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005690 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5691 return AKEY_STATE_VIRTUAL;
5692 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005693
Jeff Brownbe1aa822011-07-27 16:04:54 -07005694 size_t numVirtualKeys = mVirtualKeys.size();
5695 for (size_t i = 0; i < numVirtualKeys; i++) {
5696 const VirtualKey& virtualKey = mVirtualKeys[i];
5697 if (virtualKey.scanCode == scanCode) {
5698 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005699 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005700 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005701
5702 return AKEY_STATE_UNKNOWN;
5703}
5704
5705bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5706 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005707 size_t numVirtualKeys = mVirtualKeys.size();
5708 for (size_t i = 0; i < numVirtualKeys; i++) {
5709 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005710
Jeff Brownbe1aa822011-07-27 16:04:54 -07005711 for (size_t i = 0; i < numCodes; i++) {
5712 if (virtualKey.keyCode == keyCodes[i]) {
5713 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005714 }
5715 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005716 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005717
5718 return true;
5719}
5720
5721
5722// --- SingleTouchInputMapper ---
5723
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005724SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5725 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005726}
5727
5728SingleTouchInputMapper::~SingleTouchInputMapper() {
5729}
5730
Jeff Brown65fd2512011-08-18 11:20:58 -07005731void SingleTouchInputMapper::reset(nsecs_t when) {
5732 mSingleTouchMotionAccumulator.reset(getDevice());
5733
5734 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005735}
5736
Jeff Brown6d0fec22010-07-23 21:28:06 -07005737void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005738 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005739
Jeff Brown65fd2512011-08-18 11:20:58 -07005740 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005741}
5742
Jeff Brown65fd2512011-08-18 11:20:58 -07005743void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005744 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005745 mCurrentRawPointerData.pointerCount = 1;
5746 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005747
Jeff Brown65fd2512011-08-18 11:20:58 -07005748 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5749 && (mTouchButtonAccumulator.isHovering()
5750 || (mRawPointerAxes.pressure.valid
5751 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005752 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005753
Jeff Brownbe1aa822011-07-27 16:04:54 -07005754 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005755 outPointer.id = 0;
5756 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5757 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5758 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5759 outPointer.touchMajor = 0;
5760 outPointer.touchMinor = 0;
5761 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5762 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5763 outPointer.orientation = 0;
5764 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005765 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5766 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005767 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5768 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5769 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5770 }
5771 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005772 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005773}
5774
Jeff Brownbe1aa822011-07-27 16:04:54 -07005775void SingleTouchInputMapper::configureRawPointerAxes() {
5776 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005777
Jeff Brownbe1aa822011-07-27 16:04:54 -07005778 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5779 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5780 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5781 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5782 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005783 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5784 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005785}
5786
5787
5788// --- MultiTouchInputMapper ---
5789
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005790MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005791 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005792}
5793
5794MultiTouchInputMapper::~MultiTouchInputMapper() {
5795}
5796
Jeff Brown65fd2512011-08-18 11:20:58 -07005797void MultiTouchInputMapper::reset(nsecs_t when) {
5798 mMultiTouchMotionAccumulator.reset(getDevice());
5799
Jeff Brown6894a292011-07-01 17:59:27 -07005800 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005801
Jeff Brown65fd2512011-08-18 11:20:58 -07005802 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005803}
5804
5805void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005806 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005807
Jeff Brown65fd2512011-08-18 11:20:58 -07005808 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005809}
5810
Jeff Brown65fd2512011-08-18 11:20:58 -07005811void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005812 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005813 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005814 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005815
Jeff Brown80fd47c2011-05-24 01:07:44 -07005816 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005817 const MultiTouchMotionAccumulator::Slot* inSlot =
5818 mMultiTouchMotionAccumulator.getSlot(inIndex);
5819 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005820 continue;
5821 }
5822
Jeff Brown80fd47c2011-05-24 01:07:44 -07005823 if (outCount >= MAX_POINTERS) {
5824#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005825 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005826 "ignoring the rest.",
5827 getDeviceName().string(), MAX_POINTERS);
5828#endif
5829 break; // too many fingers!
5830 }
5831
Jeff Brownbe1aa822011-07-27 16:04:54 -07005832 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005833 outPointer.x = inSlot->getX();
5834 outPointer.y = inSlot->getY();
5835 outPointer.pressure = inSlot->getPressure();
5836 outPointer.touchMajor = inSlot->getTouchMajor();
5837 outPointer.touchMinor = inSlot->getTouchMinor();
5838 outPointer.toolMajor = inSlot->getToolMajor();
5839 outPointer.toolMinor = inSlot->getToolMinor();
5840 outPointer.orientation = inSlot->getOrientation();
5841 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005842 outPointer.tiltX = 0;
5843 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005844
Jeff Brown49754db2011-07-01 17:37:58 -07005845 outPointer.toolType = inSlot->getToolType();
5846 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5847 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5848 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5849 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5850 }
Jeff Brown8d608662010-08-30 03:02:23 -07005851 }
5852
Jeff Brown65fd2512011-08-18 11:20:58 -07005853 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5854 && (mTouchButtonAccumulator.isHovering()
5855 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005856 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005857
Jeff Brown8d608662010-08-30 03:02:23 -07005858 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005859 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005860 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005861 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005862 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005863 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005864 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005865 if (mPointerTrackingIdMap[n] == trackingId) {
5866 id = n;
5867 }
5868 }
5869
5870 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005871 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005872 mPointerTrackingIdMap[id] = trackingId;
5873 }
5874 }
5875 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005876 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005877 mCurrentRawPointerData.clearIdBits();
5878 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005879 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005880 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005881 mCurrentRawPointerData.idToIndex[id] = outCount;
5882 mCurrentRawPointerData.markIdBit(id, isHovering);
5883 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005884 }
5885 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005886
Jeff Brown6d0fec22010-07-23 21:28:06 -07005887 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005888 }
5889
Jeff Brownbe1aa822011-07-27 16:04:54 -07005890 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005891 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005892
Jeff Brown65fd2512011-08-18 11:20:58 -07005893 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005894}
5895
Jeff Brownbe1aa822011-07-27 16:04:54 -07005896void MultiTouchInputMapper::configureRawPointerAxes() {
5897 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005898
Jeff Brownbe1aa822011-07-27 16:04:54 -07005899 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5900 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5901 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5902 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5903 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5904 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5905 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5906 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5907 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5908 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5909 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005910
Jeff Brownbe1aa822011-07-27 16:04:54 -07005911 if (mRawPointerAxes.trackingId.valid
5912 && mRawPointerAxes.slot.valid
5913 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5914 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005915 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005916 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005917 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005918 getDeviceName().string(), slotCount, MAX_SLOTS);
5919 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005920 }
Jeff Brown49754db2011-07-01 17:37:58 -07005921 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005922 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005923 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005924 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005925}
5926
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005927
Jeff Browncb1404e2011-01-15 18:14:15 -08005928// --- JoystickInputMapper ---
5929
5930JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5931 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005932}
5933
5934JoystickInputMapper::~JoystickInputMapper() {
5935}
5936
5937uint32_t JoystickInputMapper::getSources() {
5938 return AINPUT_SOURCE_JOYSTICK;
5939}
5940
5941void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5942 InputMapper::populateDeviceInfo(info);
5943
Jeff Brown6f2fba42011-02-19 01:08:02 -08005944 for (size_t i = 0; i < mAxes.size(); i++) {
5945 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005946 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5947 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005948 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005949 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5950 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005951 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005952 }
5953}
5954
5955void JoystickInputMapper::dump(String8& dump) {
5956 dump.append(INDENT2 "Joystick Input Mapper:\n");
5957
Jeff Brown6f2fba42011-02-19 01:08:02 -08005958 dump.append(INDENT3 "Axes:\n");
5959 size_t numAxes = mAxes.size();
5960 for (size_t i = 0; i < numAxes; i++) {
5961 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005962 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005963 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005964 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005965 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005966 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005967 }
Jeff Brown85297452011-03-04 13:07:49 -08005968 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5969 label = getAxisLabel(axis.axisInfo.highAxis);
5970 if (label) {
5971 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5972 } else {
5973 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5974 axis.axisInfo.splitValue);
5975 }
5976 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5977 dump.append(" (invert)");
5978 }
5979
5980 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5981 axis.min, axis.max, axis.flat, axis.fuzz);
5982 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5983 "highScale=%0.5f, highOffset=%0.5f\n",
5984 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005985 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5986 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005987 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005988 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005989 }
5990}
5991
Jeff Brown65fd2512011-08-18 11:20:58 -07005992void JoystickInputMapper::configure(nsecs_t when,
5993 const InputReaderConfiguration* config, uint32_t changes) {
5994 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005995
Jeff Brown474dcb52011-06-14 20:22:50 -07005996 if (!changes) { // first time only
5997 // Collect all axes.
5998 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07005999 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6000 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6001 continue; // axis must be claimed by a different device
6002 }
6003
Jeff Brown474dcb52011-06-14 20:22:50 -07006004 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006005 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006006 if (rawAxisInfo.valid) {
6007 // Map axis.
6008 AxisInfo axisInfo;
6009 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6010 if (!explicitlyMapped) {
6011 // Axis is not explicitly mapped, will choose a generic axis later.
6012 axisInfo.mode = AxisInfo::MODE_NORMAL;
6013 axisInfo.axis = -1;
6014 }
6015
6016 // Apply flat override.
6017 int32_t rawFlat = axisInfo.flatOverride < 0
6018 ? rawAxisInfo.flat : axisInfo.flatOverride;
6019
6020 // Calculate scaling factors and limits.
6021 Axis axis;
6022 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6023 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6024 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6025 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6026 scale, 0.0f, highScale, 0.0f,
6027 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6028 } else if (isCenteredAxis(axisInfo.axis)) {
6029 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6030 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6031 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6032 scale, offset, scale, offset,
6033 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6034 } else {
6035 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6036 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6037 scale, 0.0f, scale, 0.0f,
6038 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6039 }
6040
6041 // To eliminate noise while the joystick is at rest, filter out small variations
6042 // in axis values up front.
6043 axis.filter = axis.flat * 0.25f;
6044
6045 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006046 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006047 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006048
Jeff Brown474dcb52011-06-14 20:22:50 -07006049 // If there are too many axes, start dropping them.
6050 // Prefer to keep explicitly mapped axes.
6051 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006052 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006053 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6054 pruneAxes(true);
6055 pruneAxes(false);
6056 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006057
Jeff Brown474dcb52011-06-14 20:22:50 -07006058 // Assign generic axis ids to remaining axes.
6059 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6060 size_t numAxes = mAxes.size();
6061 for (size_t i = 0; i < numAxes; i++) {
6062 Axis& axis = mAxes.editValueAt(i);
6063 if (axis.axisInfo.axis < 0) {
6064 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6065 && haveAxis(nextGenericAxisId)) {
6066 nextGenericAxisId += 1;
6067 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006068
Jeff Brown474dcb52011-06-14 20:22:50 -07006069 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6070 axis.axisInfo.axis = nextGenericAxisId;
6071 nextGenericAxisId += 1;
6072 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006073 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006074 "have already been assigned to other axes.",
6075 getDeviceName().string(), mAxes.keyAt(i));
6076 mAxes.removeItemsAt(i--);
6077 numAxes -= 1;
6078 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006079 }
6080 }
6081 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006082}
6083
Jeff Brown85297452011-03-04 13:07:49 -08006084bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006085 size_t numAxes = mAxes.size();
6086 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006087 const Axis& axis = mAxes.valueAt(i);
6088 if (axis.axisInfo.axis == axisId
6089 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6090 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006091 return true;
6092 }
6093 }
6094 return false;
6095}
Jeff Browncb1404e2011-01-15 18:14:15 -08006096
Jeff Brown6f2fba42011-02-19 01:08:02 -08006097void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6098 size_t i = mAxes.size();
6099 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6100 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6101 continue;
6102 }
Steve Block6215d3f2012-01-04 20:05:49 +00006103 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006104 getDeviceName().string(), mAxes.keyAt(i));
6105 mAxes.removeItemsAt(i);
6106 }
6107}
6108
6109bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6110 switch (axis) {
6111 case AMOTION_EVENT_AXIS_X:
6112 case AMOTION_EVENT_AXIS_Y:
6113 case AMOTION_EVENT_AXIS_Z:
6114 case AMOTION_EVENT_AXIS_RX:
6115 case AMOTION_EVENT_AXIS_RY:
6116 case AMOTION_EVENT_AXIS_RZ:
6117 case AMOTION_EVENT_AXIS_HAT_X:
6118 case AMOTION_EVENT_AXIS_HAT_Y:
6119 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006120 case AMOTION_EVENT_AXIS_RUDDER:
6121 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006122 return true;
6123 default:
6124 return false;
6125 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006126}
6127
Jeff Brown65fd2512011-08-18 11:20:58 -07006128void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006129 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006130 size_t numAxes = mAxes.size();
6131 for (size_t i = 0; i < numAxes; i++) {
6132 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006133 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006134 }
6135
Jeff Brown65fd2512011-08-18 11:20:58 -07006136 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006137}
6138
6139void JoystickInputMapper::process(const RawEvent* rawEvent) {
6140 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006141 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006142 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006143 if (index >= 0) {
6144 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006145 float newValue, highNewValue;
6146 switch (axis.axisInfo.mode) {
6147 case AxisInfo::MODE_INVERT:
6148 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6149 * axis.scale + axis.offset;
6150 highNewValue = 0.0f;
6151 break;
6152 case AxisInfo::MODE_SPLIT:
6153 if (rawEvent->value < axis.axisInfo.splitValue) {
6154 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6155 * axis.scale + axis.offset;
6156 highNewValue = 0.0f;
6157 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6158 newValue = 0.0f;
6159 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6160 * axis.highScale + axis.highOffset;
6161 } else {
6162 newValue = 0.0f;
6163 highNewValue = 0.0f;
6164 }
6165 break;
6166 default:
6167 newValue = rawEvent->value * axis.scale + axis.offset;
6168 highNewValue = 0.0f;
6169 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006170 }
Jeff Brown85297452011-03-04 13:07:49 -08006171 axis.newValue = newValue;
6172 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006173 }
6174 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006175 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006176
6177 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006178 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006179 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006180 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006181 break;
6182 }
6183 break;
6184 }
6185}
6186
Jeff Brown6f2fba42011-02-19 01:08:02 -08006187void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006188 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006189 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006190 }
6191
6192 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006193 int32_t buttonState = 0;
6194
6195 PointerProperties pointerProperties;
6196 pointerProperties.clear();
6197 pointerProperties.id = 0;
6198 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006199
Jeff Brown6f2fba42011-02-19 01:08:02 -08006200 PointerCoords pointerCoords;
6201 pointerCoords.clear();
6202
6203 size_t numAxes = mAxes.size();
6204 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006205 const Axis& axis = mAxes.valueAt(i);
6206 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6207 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6208 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6209 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006210 }
6211
Jeff Brown56194eb2011-03-02 19:23:13 -08006212 // Moving a joystick axis should not wake the devide because joysticks can
6213 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6214 // button will likely wake the device.
6215 // TODO: Use the input device configuration to control this behavior more finely.
6216 uint32_t policyFlags = 0;
6217
Jeff Brownbe1aa822011-07-27 16:04:54 -07006218 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006219 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
6220 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006221 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006222}
6223
Jeff Brown85297452011-03-04 13:07:49 -08006224bool JoystickInputMapper::filterAxes(bool force) {
6225 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006226 size_t numAxes = mAxes.size();
6227 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006228 Axis& axis = mAxes.editValueAt(i);
6229 if (force || hasValueChangedSignificantly(axis.filter,
6230 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6231 axis.currentValue = axis.newValue;
6232 atLeastOneSignificantChange = true;
6233 }
6234 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6235 if (force || hasValueChangedSignificantly(axis.filter,
6236 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6237 axis.highCurrentValue = axis.highNewValue;
6238 atLeastOneSignificantChange = true;
6239 }
6240 }
6241 }
6242 return atLeastOneSignificantChange;
6243}
6244
6245bool JoystickInputMapper::hasValueChangedSignificantly(
6246 float filter, float newValue, float currentValue, float min, float max) {
6247 if (newValue != currentValue) {
6248 // Filter out small changes in value unless the value is converging on the axis
6249 // bounds or center point. This is intended to reduce the amount of information
6250 // sent to applications by particularly noisy joysticks (such as PS3).
6251 if (fabs(newValue - currentValue) > filter
6252 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6253 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6254 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6255 return true;
6256 }
6257 }
6258 return false;
6259}
6260
6261bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6262 float filter, float newValue, float currentValue, float thresholdValue) {
6263 float newDistance = fabs(newValue - thresholdValue);
6264 if (newDistance < filter) {
6265 float oldDistance = fabs(currentValue - thresholdValue);
6266 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006267 return true;
6268 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006269 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006270 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006271}
6272
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006273} // namespace android