blob: 6d63998cc93e698f38916437407539ed064c3b55 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Browna47425a2012-04-13 04:09:27 -070039// Log debug messages about the vibrator.
40#define DEBUG_VIBRATOR 0
41
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042#include "InputReader.h"
43
Jeff Brown46b9ac02010-04-22 18:58:52 -070044#include <cutils/log.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080045#include <androidfw/Keyboard.h>
46#include <androidfw/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070047
48#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070049#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070050#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070051#include <errno.h>
52#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070053#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070054
Jeff Brown8d608662010-08-30 03:02:23 -070055#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070056#define INDENT2 " "
57#define INDENT3 " "
58#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070059#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070060
Jeff Brown46b9ac02010-04-22 18:58:52 -070061namespace android {
62
Jeff Brownace13b12011-03-09 17:39:48 -080063// --- Constants ---
64
Jeff Brown80fd47c2011-05-24 01:07:44 -070065// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
66static const size_t MAX_SLOTS = 32;
67
Jeff Brown46b9ac02010-04-22 18:58:52 -070068// --- Static Functions ---
69
70template<typename T>
71inline static T abs(const T& value) {
72 return value < 0 ? - value : value;
73}
74
75template<typename T>
76inline static T min(const T& a, const T& b) {
77 return a < b ? a : b;
78}
79
Jeff Brown5c225b12010-06-16 01:53:36 -070080template<typename T>
81inline static void swap(T& a, T& b) {
82 T temp = a;
83 a = b;
84 b = temp;
85}
86
Jeff Brown8d608662010-08-30 03:02:23 -070087inline static float avg(float x, float y) {
88 return (x + y) / 2;
89}
90
Jeff Brown2352b972011-04-12 22:39:53 -070091inline static float distance(float x1, float y1, float x2, float y2) {
92 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080093}
94
Jeff Brown517bb4c2011-01-14 19:09:23 -080095inline static int32_t signExtendNybble(int32_t value) {
96 return value >= 8 ? value - 16 : value;
97}
98
Jeff Brownef3d7e82010-09-30 14:33:04 -070099static inline const char* toString(bool value) {
100 return value ? "true" : "false";
101}
102
Jeff Brown9626b142011-03-03 02:09:54 -0800103static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
104 const int32_t map[][4], size_t mapSize) {
105 if (orientation != DISPLAY_ORIENTATION_0) {
106 for (size_t i = 0; i < mapSize; i++) {
107 if (value == map[i][0]) {
108 return map[i][orientation];
109 }
110 }
111 }
112 return value;
113}
114
Jeff Brown46b9ac02010-04-22 18:58:52 -0700115static const int32_t keyCodeRotationMap[][4] = {
116 // key codes enumerated counter-clockwise with the original (unrotated) key first
117 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -0700118 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
119 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
120 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
121 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac02010-04-22 18:58:52 -0700122};
Jeff Brown9626b142011-03-03 02:09:54 -0800123static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-04-22 18:58:52 -0700124 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
125
Jeff Brown60691392011-07-15 19:08:26 -0700126static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800127 return rotateValueUsingRotationMap(keyCode, orientation,
128 keyCodeRotationMap, keyCodeRotationMapSize);
129}
130
Jeff Brown612891e2011-07-15 20:44:17 -0700131static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
132 float temp;
133 switch (orientation) {
134 case DISPLAY_ORIENTATION_90:
135 temp = *deltaX;
136 *deltaX = *deltaY;
137 *deltaY = -temp;
138 break;
139
140 case DISPLAY_ORIENTATION_180:
141 *deltaX = -*deltaX;
142 *deltaY = -*deltaY;
143 break;
144
145 case DISPLAY_ORIENTATION_270:
146 temp = *deltaX;
147 *deltaX = -*deltaY;
148 *deltaY = temp;
149 break;
150 }
151}
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
154 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
155}
156
Jeff Brownefd32662011-03-08 15:13:06 -0800157// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700158// button states. This determines whether the event is reported as a touch event.
159static bool isPointerDown(int32_t buttonState) {
160 return buttonState &
161 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700162 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800163}
164
Jeff Brown2352b972011-04-12 22:39:53 -0700165static float calculateCommonVector(float a, float b) {
166 if (a > 0 && b > 0) {
167 return a < b ? a : b;
168 } else if (a < 0 && b < 0) {
169 return a > b ? a : b;
170 } else {
171 return 0;
172 }
173}
174
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700175static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
176 nsecs_t when, int32_t deviceId, uint32_t source,
177 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
178 int32_t buttonState, int32_t keyCode) {
179 if (
180 (action == AKEY_EVENT_ACTION_DOWN
181 && !(lastButtonState & buttonState)
182 && (currentButtonState & buttonState))
183 || (action == AKEY_EVENT_ACTION_UP
184 && (lastButtonState & buttonState)
185 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700186 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700187 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700188 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700189 }
190}
191
192static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
193 nsecs_t when, int32_t deviceId, uint32_t source,
194 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
198 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
199 lastButtonState, currentButtonState,
200 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
201}
202
Jeff Brown46b9ac02010-04-22 18:58:52 -0700203
Jeff Brown65fd2512011-08-18 11:20:58 -0700204// --- InputReaderConfiguration ---
205
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 Brown46b9ac02010-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();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700253 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700254}
255
256InputReader::~InputReader() {
257 for (size_t i = 0; i < mDevices.size(); i++) {
258 delete mDevices.valueAt(i);
259 }
260}
261
262void InputReader::loopOnce() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700263 int32_t oldGeneration;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700264 int32_t timeoutMillis;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700265 bool inputDevicesChanged = false;
266 Vector<InputDeviceInfo> inputDevices;
Jeff Brown474dcb52011-06-14 20:22:50 -0700267 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700268 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700269
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700270 oldGeneration = mGeneration;
271 timeoutMillis = -1;
272
Jeff Brownbe1aa822011-07-27 16:04:54 -0700273 uint32_t changes = mConfigurationChangesToRefresh;
274 if (changes) {
275 mConfigurationChangesToRefresh = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700276 timeoutMillis = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700277 refreshConfigurationLocked(changes);
Jeff Browna47425a2012-04-13 04:09:27 -0700278 } else if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700279 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
280 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
281 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700282 } // release lock
283
Jeff Brownb7198742011-03-18 18:14:26 -0700284 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700285
286 { // acquire lock
287 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800288 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700289
290 if (count) {
291 processEventsLocked(mEventBuffer, count);
292 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700293
294 if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700295 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800296 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700297#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800298 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700299#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800300 mNextTimeout = LLONG_MAX;
301 timeoutExpiredLocked(now);
302 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700303 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700304
305 if (oldGeneration != mGeneration) {
306 inputDevicesChanged = true;
307 getInputDevicesLocked(inputDevices);
308 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700309 } // release lock
310
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700311 // Send out a message that the describes the changed input devices.
312 if (inputDevicesChanged) {
313 mPolicy->notifyInputDevicesChanged(inputDevices);
314 }
315
Jeff Brownbe1aa822011-07-27 16:04:54 -0700316 // Flush queued events out to the listener.
317 // This must happen outside of the lock because the listener could potentially call
318 // back into the InputReader's methods, such as getScanCodeState, or become blocked
319 // on another thread similarly waiting to acquire the InputReader lock thereby
320 // resulting in a deadlock. This situation is actually quite plausible because the
321 // listener is actually the input dispatcher, which calls into the window manager,
322 // which occasionally calls into the input reader.
323 mQueuedListener->flush();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700324}
325
Jeff Brownbe1aa822011-07-27 16:04:54 -0700326void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700327 for (const RawEvent* rawEvent = rawEvents; count;) {
328 int32_t type = rawEvent->type;
329 size_t batchSize = 1;
330 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
331 int32_t deviceId = rawEvent->deviceId;
332 while (batchSize < count) {
333 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
334 || rawEvent[batchSize].deviceId != deviceId) {
335 break;
336 }
337 batchSize += 1;
338 }
339#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000340 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700341#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700342 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700343 } else {
344 switch (rawEvent->type) {
345 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700346 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700347 break;
348 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700349 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700350 break;
351 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700352 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700353 break;
354 default:
Steve Blockec193de2012-01-09 18:35:44 +0000355 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700356 break;
357 }
358 }
359 count -= batchSize;
360 rawEvent += batchSize;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700361 }
362}
363
Jeff Brown65fd2512011-08-18 11:20:58 -0700364void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700365 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
366 if (deviceIndex >= 0) {
367 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
368 return;
369 }
370
Jeff Browne38fdfa2012-04-06 14:51:01 -0700371 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
373
Jeff Browne38fdfa2012-04-06 14:51:01 -0700374 InputDevice* device = createDeviceLocked(deviceId, identifier, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700375 device->configure(when, &mConfig, 0);
376 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377
Jeff Brown8d608662010-08-30 03:02:23 -0700378 if (device->isIgnored()) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700379 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
380 identifier.name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700381 } else {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700382 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
383 identifier.name.string(), device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700384 }
385
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700386 mDevices.add(deviceId, device);
387 bumpGenerationLocked();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700388}
389
Jeff Brown65fd2512011-08-18 11:20:58 -0700390void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700391 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700392 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700393 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000394 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700395 return;
396 }
397
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700398 device = mDevices.valueAt(deviceIndex);
399 mDevices.removeItemsAt(deviceIndex, 1);
400 bumpGenerationLocked();
401
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000403 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700404 device->getId(), device->getName().string());
405 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000406 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 device->getId(), device->getName().string(), device->getSources());
408 }
409
Jeff Brown65fd2512011-08-18 11:20:58 -0700410 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700411 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700412}
413
Jeff Brownbe1aa822011-07-27 16:04:54 -0700414InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700415 const InputDeviceIdentifier& identifier, uint32_t classes) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700416 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
417 identifier, classes);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700418
Jeff Brown56194eb2011-03-02 19:23:13 -0800419 // External devices.
420 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
421 device->setExternal(true);
422 }
423
Jeff Brown6d0fec22010-07-23 21:28:06 -0700424 // Switch-like devices.
425 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
426 device->addMapper(new SwitchInputMapper(device));
427 }
428
Jeff Browna47425a2012-04-13 04:09:27 -0700429 // Vibrator-like devices.
430 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
431 device->addMapper(new VibratorInputMapper(device));
432 }
433
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800435 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700436 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
437 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800438 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439 }
440 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
441 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
442 }
443 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800444 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700445 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800446 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800447 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800448 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449
Jeff Brownefd32662011-03-08 15:13:06 -0800450 if (keyboardSource != 0) {
451 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452 }
453
Jeff Brown83c09682010-12-23 17:50:18 -0800454 // Cursor-like devices.
455 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
456 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700457 }
458
Jeff Brown58a2da82011-01-25 16:02:22 -0800459 // Touchscreens and touchpad devices.
460 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800461 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800462 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800463 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700464 }
465
Jeff Browncb1404e2011-01-15 18:14:15 -0800466 // Joystick-like devices.
467 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
468 device->addMapper(new JoystickInputMapper(device));
469 }
470
Jeff Brown6d0fec22010-07-23 21:28:06 -0700471 return device;
472}
473
Jeff Brownbe1aa822011-07-27 16:04:54 -0700474void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700475 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700476 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
477 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000478 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700479 return;
480 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481
Jeff Brownbe1aa822011-07-27 16:04:54 -0700482 InputDevice* device = mDevices.valueAt(deviceIndex);
483 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000484 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700485 return;
486 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487
Jeff Brownbe1aa822011-07-27 16:04:54 -0700488 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700489}
490
Jeff Brownbe1aa822011-07-27 16:04:54 -0700491void InputReader::timeoutExpiredLocked(nsecs_t when) {
492 for (size_t i = 0; i < mDevices.size(); i++) {
493 InputDevice* device = mDevices.valueAt(i);
494 if (!device->isIgnored()) {
495 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700496 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700497 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700498}
499
Jeff Brownbe1aa822011-07-27 16:04:54 -0700500void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700501 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700502 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700503
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700505 NotifyConfigurationChangedArgs args(when);
506 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507}
508
Jeff Brownbe1aa822011-07-27 16:04:54 -0700509void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700510 mPolicy->getReaderConfiguration(&mConfig);
511 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
512
Jeff Brown474dcb52011-06-14 20:22:50 -0700513 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000514 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700515 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700516
517 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
518 mEventHub->requestReopenDevices();
519 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700520 for (size_t i = 0; i < mDevices.size(); i++) {
521 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700522 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700523 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700524 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700525 }
526}
527
Jeff Brownbe1aa822011-07-27 16:04:54 -0700528void InputReader::updateGlobalMetaStateLocked() {
529 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700530
Jeff Brownbe1aa822011-07-27 16:04:54 -0700531 for (size_t i = 0; i < mDevices.size(); i++) {
532 InputDevice* device = mDevices.valueAt(i);
533 mGlobalMetaState |= device->getMetaState();
534 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700535}
536
Jeff Brownbe1aa822011-07-27 16:04:54 -0700537int32_t InputReader::getGlobalMetaStateLocked() {
538 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700539}
540
Jeff Brownbe1aa822011-07-27 16:04:54 -0700541void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800542 mDisableVirtualKeysTimeout = time;
543}
544
Jeff Brownbe1aa822011-07-27 16:04:54 -0700545bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800546 InputDevice* device, int32_t keyCode, int32_t scanCode) {
547 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000548 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800549 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
550 device->getName().string(),
551 (mDisableVirtualKeysTimeout - now) * 0.000001,
552 keyCode, scanCode);
553 return true;
554 } else {
555 return false;
556 }
557}
558
Jeff Brownbe1aa822011-07-27 16:04:54 -0700559void InputReader::fadePointerLocked() {
560 for (size_t i = 0; i < mDevices.size(); i++) {
561 InputDevice* device = mDevices.valueAt(i);
562 device->fadePointer();
563 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800564}
565
Jeff Brownbe1aa822011-07-27 16:04:54 -0700566void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700567 if (when < mNextTimeout) {
568 mNextTimeout = when;
Jeff Browna47425a2012-04-13 04:09:27 -0700569 mEventHub->wake();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700570 }
571}
572
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700573int32_t InputReader::bumpGenerationLocked() {
574 return ++mGeneration;
575}
576
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700577void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700578 AutoMutex _l(mLock);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700579 getInputDevicesLocked(outInputDevices);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580}
581
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700582void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) {
583 outInputDevices.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700584
Jeff Brownbe1aa822011-07-27 16:04:54 -0700585 size_t numDevices = mDevices.size();
586 for (size_t i = 0; i < numDevices; i++) {
587 InputDevice* device = mDevices.valueAt(i);
588 if (!device->isIgnored()) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700589 outInputDevices.push();
590 device->getDeviceInfo(&outInputDevices.editTop());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700591 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700592 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700593}
594
595int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
596 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700597 AutoMutex _l(mLock);
598
599 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600}
601
602int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
603 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700604 AutoMutex _l(mLock);
605
606 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700607}
608
609int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700610 AutoMutex _l(mLock);
611
612 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700613}
614
Jeff Brownbe1aa822011-07-27 16:04:54 -0700615int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700617 int32_t result = AKEY_STATE_UNKNOWN;
618 if (deviceId >= 0) {
619 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
620 if (deviceIndex >= 0) {
621 InputDevice* device = mDevices.valueAt(deviceIndex);
622 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
623 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700624 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700625 }
626 } else {
627 size_t numDevices = mDevices.size();
628 for (size_t i = 0; i < numDevices; i++) {
629 InputDevice* device = mDevices.valueAt(i);
630 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800631 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
632 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
633 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
634 if (currentResult >= AKEY_STATE_DOWN) {
635 return currentResult;
636 } else if (currentResult == AKEY_STATE_UP) {
637 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700638 }
639 }
640 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700641 }
642 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700643}
644
645bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
646 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700647 AutoMutex _l(mLock);
648
Jeff Brown6d0fec22010-07-23 21:28:06 -0700649 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700650 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700651}
652
Jeff Brownbe1aa822011-07-27 16:04:54 -0700653bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
654 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
655 bool result = false;
656 if (deviceId >= 0) {
657 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
658 if (deviceIndex >= 0) {
659 InputDevice* device = mDevices.valueAt(deviceIndex);
660 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
661 result = device->markSupportedKeyCodes(sourceMask,
662 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700663 }
664 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700665 } else {
666 size_t numDevices = mDevices.size();
667 for (size_t i = 0; i < numDevices; i++) {
668 InputDevice* device = mDevices.valueAt(i);
669 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
670 result |= device->markSupportedKeyCodes(sourceMask,
671 numCodes, keyCodes, outFlags);
672 }
673 }
674 }
675 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700676}
677
Jeff Brown474dcb52011-06-14 20:22:50 -0700678void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700679 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700680
Jeff Brownbe1aa822011-07-27 16:04:54 -0700681 if (changes) {
682 bool needWake = !mConfigurationChangesToRefresh;
683 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700684
685 if (needWake) {
686 mEventHub->wake();
687 }
688 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700689}
690
Jeff Browna47425a2012-04-13 04:09:27 -0700691void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
692 ssize_t repeat, int32_t token) {
693 AutoMutex _l(mLock);
694
695 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
696 if (deviceIndex >= 0) {
697 InputDevice* device = mDevices.valueAt(deviceIndex);
698 device->vibrate(pattern, patternSize, repeat, token);
699 }
700}
701
702void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
703 AutoMutex _l(mLock);
704
705 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
706 if (deviceIndex >= 0) {
707 InputDevice* device = mDevices.valueAt(deviceIndex);
708 device->cancelVibrate(token);
709 }
710}
711
Jeff Brownb88102f2010-09-08 11:49:43 -0700712void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700713 AutoMutex _l(mLock);
714
Jeff Brownf2f48712010-10-01 17:46:21 -0700715 mEventHub->dump(dump);
716 dump.append("\n");
717
718 dump.append("Input Reader State:\n");
719
Jeff Brownbe1aa822011-07-27 16:04:54 -0700720 for (size_t i = 0; i < mDevices.size(); i++) {
721 mDevices.valueAt(i)->dump(dump);
722 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700723
724 dump.append(INDENT "Configuration:\n");
725 dump.append(INDENT2 "ExcludedDeviceNames: [");
726 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
727 if (i != 0) {
728 dump.append(", ");
729 }
730 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
731 }
732 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700733 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
734 mConfig.virtualKeyQuietTime * 0.000001f);
735
Jeff Brown19c97d462011-06-01 12:33:19 -0700736 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
737 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
738 mConfig.pointerVelocityControlParameters.scale,
739 mConfig.pointerVelocityControlParameters.lowThreshold,
740 mConfig.pointerVelocityControlParameters.highThreshold,
741 mConfig.pointerVelocityControlParameters.acceleration);
742
743 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
744 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
745 mConfig.wheelVelocityControlParameters.scale,
746 mConfig.wheelVelocityControlParameters.lowThreshold,
747 mConfig.wheelVelocityControlParameters.highThreshold,
748 mConfig.wheelVelocityControlParameters.acceleration);
749
Jeff Brown214eaf42011-05-26 19:17:02 -0700750 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700751 dump.appendFormat(INDENT3 "Enabled: %s\n",
752 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700753 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
754 mConfig.pointerGestureQuietInterval * 0.000001f);
755 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
756 mConfig.pointerGestureDragMinSwitchSpeed);
757 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
758 mConfig.pointerGestureTapInterval * 0.000001f);
759 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
760 mConfig.pointerGestureTapDragInterval * 0.000001f);
761 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
762 mConfig.pointerGestureTapSlop);
763 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
764 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700765 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
766 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700767 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
768 mConfig.pointerGestureSwipeTransitionAngleCosine);
769 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
770 mConfig.pointerGestureSwipeMaxWidthRatio);
771 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
772 mConfig.pointerGestureMovementSpeedRatio);
773 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
774 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700775}
776
Jeff Brown89ef0722011-08-10 16:25:21 -0700777void InputReader::monitor() {
778 // Acquire and release the lock to ensure that the reader has not deadlocked.
779 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800780 mEventHub->wake();
781 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700782 mLock.unlock();
783
784 // Check the EventHub
785 mEventHub->monitor();
786}
787
Jeff Brown6d0fec22010-07-23 21:28:06 -0700788
Jeff Brownbe1aa822011-07-27 16:04:54 -0700789// --- InputReader::ContextImpl ---
790
791InputReader::ContextImpl::ContextImpl(InputReader* reader) :
792 mReader(reader) {
793}
794
795void InputReader::ContextImpl::updateGlobalMetaState() {
796 // lock is already held by the input loop
797 mReader->updateGlobalMetaStateLocked();
798}
799
800int32_t InputReader::ContextImpl::getGlobalMetaState() {
801 // lock is already held by the input loop
802 return mReader->getGlobalMetaStateLocked();
803}
804
805void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
806 // lock is already held by the input loop
807 mReader->disableVirtualKeysUntilLocked(time);
808}
809
810bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
811 InputDevice* device, int32_t keyCode, int32_t scanCode) {
812 // lock is already held by the input loop
813 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
814}
815
816void InputReader::ContextImpl::fadePointer() {
817 // lock is already held by the input loop
818 mReader->fadePointerLocked();
819}
820
821void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
822 // lock is already held by the input loop
823 mReader->requestTimeoutAtTimeLocked(when);
824}
825
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700826int32_t InputReader::ContextImpl::bumpGeneration() {
827 // lock is already held by the input loop
828 return mReader->bumpGenerationLocked();
829}
830
Jeff Brownbe1aa822011-07-27 16:04:54 -0700831InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
832 return mReader->mPolicy.get();
833}
834
835InputListenerInterface* InputReader::ContextImpl::getListener() {
836 return mReader->mQueuedListener.get();
837}
838
839EventHubInterface* InputReader::ContextImpl::getEventHub() {
840 return mReader->mEventHub.get();
841}
842
843
Jeff Brown6d0fec22010-07-23 21:28:06 -0700844// --- InputReaderThread ---
845
846InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
847 Thread(/*canCallJava*/ true), mReader(reader) {
848}
849
850InputReaderThread::~InputReaderThread() {
851}
852
853bool InputReaderThread::threadLoop() {
854 mReader->loopOnce();
855 return true;
856}
857
858
859// --- InputDevice ---
860
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700861InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700862 const InputDeviceIdentifier& identifier, uint32_t classes) :
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700863 mContext(context), mId(id), mGeneration(generation),
864 mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285a2011-08-31 12:56:34 -0700865 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700866}
867
868InputDevice::~InputDevice() {
869 size_t numMappers = mMappers.size();
870 for (size_t i = 0; i < numMappers; i++) {
871 delete mMappers[i];
872 }
873 mMappers.clear();
874}
875
Jeff Brownef3d7e82010-09-30 14:33:04 -0700876void InputDevice::dump(String8& dump) {
877 InputDeviceInfo deviceInfo;
878 getDeviceInfo(& deviceInfo);
879
Jeff Brown90655042010-12-02 13:50:46 -0800880 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700881 deviceInfo.getDisplayName().string());
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700882 dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration);
Jeff Brown56194eb2011-03-02 19:23:13 -0800883 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700884 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
885 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800886
Jeff Brownefd32662011-03-08 15:13:06 -0800887 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800888 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700889 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800890 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800891 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
892 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800893 char name[32];
894 if (label) {
895 strncpy(name, label, sizeof(name));
896 name[sizeof(name) - 1] = '\0';
897 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800898 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800899 }
Jeff Brownefd32662011-03-08 15:13:06 -0800900 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
901 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
902 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800903 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700904 }
905
906 size_t numMappers = mMappers.size();
907 for (size_t i = 0; i < numMappers; i++) {
908 InputMapper* mapper = mMappers[i];
909 mapper->dump(dump);
910 }
911}
912
Jeff Brown6d0fec22010-07-23 21:28:06 -0700913void InputDevice::addMapper(InputMapper* mapper) {
914 mMappers.add(mapper);
915}
916
Jeff Brown65fd2512011-08-18 11:20:58 -0700917void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700918 mSources = 0;
919
Jeff Brown474dcb52011-06-14 20:22:50 -0700920 if (!isIgnored()) {
921 if (!changes) { // first time only
922 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
923 }
924
Jeff Brown6ec6f792012-04-17 16:52:41 -0700925 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Jeff Brown61c08242012-04-19 11:14:33 -0700926 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
927 sp<KeyCharacterMap> keyboardLayout =
928 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor);
929 if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
930 bumpGeneration();
931 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700932 }
933 }
934
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700935 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
936 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
937 String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
938 if (mAlias != alias) {
939 mAlias = alias;
940 bumpGeneration();
941 }
942 }
943 }
944
Jeff Brown474dcb52011-06-14 20:22:50 -0700945 size_t numMappers = mMappers.size();
946 for (size_t i = 0; i < numMappers; i++) {
947 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700948 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700949 mSources |= mapper->getSources();
950 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700951 }
952}
953
Jeff Brown65fd2512011-08-18 11:20:58 -0700954void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700955 size_t numMappers = mMappers.size();
956 for (size_t i = 0; i < numMappers; i++) {
957 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700958 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700959 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700960
961 mContext->updateGlobalMetaState();
962
963 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700964}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700965
Jeff Brownb7198742011-03-18 18:14:26 -0700966void InputDevice::process(const RawEvent* rawEvents, size_t count) {
967 // Process all of the events in order for each mapper.
968 // We cannot simply ask each mapper to process them in bulk because mappers may
969 // have side-effects that must be interleaved. For example, joystick movement events and
970 // gamepad button presses are handled by different mappers but they should be dispatched
971 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700972 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700973 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
974#if DEBUG_RAW_EVENTS
Jeff Brown49ccac52012-04-11 18:27:33 -0700975 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
976 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
Jeff Brownb7198742011-03-18 18:14:26 -0700977#endif
978
Jeff Brown80fd47c2011-05-24 01:07:44 -0700979 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700980 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700981 mDropUntilNextSync = false;
982#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000983 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700984#endif
985 } else {
986#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000987 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700988#endif
989 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700990 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700991 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700992 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700993 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700994 } else {
995 for (size_t i = 0; i < numMappers; i++) {
996 InputMapper* mapper = mMappers[i];
997 mapper->process(rawEvent);
998 }
Jeff Brownb7198742011-03-18 18:14:26 -0700999 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001000 }
1001}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001002
Jeff Brownaa3855d2011-03-17 01:34:19 -07001003void InputDevice::timeoutExpired(nsecs_t when) {
1004 size_t numMappers = mMappers.size();
1005 for (size_t i = 0; i < numMappers; i++) {
1006 InputMapper* mapper = mMappers[i];
1007 mapper->timeoutExpired(when);
1008 }
1009}
1010
Jeff Brown6d0fec22010-07-23 21:28:06 -07001011void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Browndaa37532012-05-01 15:54:03 -07001012 outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001013
1014 size_t numMappers = mMappers.size();
1015 for (size_t i = 0; i < numMappers; i++) {
1016 InputMapper* mapper = mMappers[i];
1017 mapper->populateDeviceInfo(outDeviceInfo);
1018 }
1019}
1020
1021int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1022 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1023}
1024
1025int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1026 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1027}
1028
1029int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1030 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1031}
1032
1033int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1034 int32_t result = AKEY_STATE_UNKNOWN;
1035 size_t numMappers = mMappers.size();
1036 for (size_t i = 0; i < numMappers; i++) {
1037 InputMapper* mapper = mMappers[i];
1038 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001039 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1040 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1041 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1042 if (currentResult >= AKEY_STATE_DOWN) {
1043 return currentResult;
1044 } else if (currentResult == AKEY_STATE_UP) {
1045 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001046 }
1047 }
1048 }
1049 return result;
1050}
1051
1052bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1053 const int32_t* keyCodes, uint8_t* outFlags) {
1054 bool result = false;
1055 size_t numMappers = mMappers.size();
1056 for (size_t i = 0; i < numMappers; i++) {
1057 InputMapper* mapper = mMappers[i];
1058 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1059 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1060 }
1061 }
1062 return result;
1063}
1064
Jeff Browna47425a2012-04-13 04:09:27 -07001065void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1066 int32_t token) {
1067 size_t numMappers = mMappers.size();
1068 for (size_t i = 0; i < numMappers; i++) {
1069 InputMapper* mapper = mMappers[i];
1070 mapper->vibrate(pattern, patternSize, repeat, token);
1071 }
1072}
1073
1074void InputDevice::cancelVibrate(int32_t token) {
1075 size_t numMappers = mMappers.size();
1076 for (size_t i = 0; i < numMappers; i++) {
1077 InputMapper* mapper = mMappers[i];
1078 mapper->cancelVibrate(token);
1079 }
1080}
1081
Jeff Brown6d0fec22010-07-23 21:28:06 -07001082int32_t InputDevice::getMetaState() {
1083 int32_t result = 0;
1084 size_t numMappers = mMappers.size();
1085 for (size_t i = 0; i < numMappers; i++) {
1086 InputMapper* mapper = mMappers[i];
1087 result |= mapper->getMetaState();
1088 }
1089 return result;
1090}
1091
Jeff Brown05dc66a2011-03-02 14:41:58 -08001092void InputDevice::fadePointer() {
1093 size_t numMappers = mMappers.size();
1094 for (size_t i = 0; i < numMappers; i++) {
1095 InputMapper* mapper = mMappers[i];
1096 mapper->fadePointer();
1097 }
1098}
1099
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001100void InputDevice::bumpGeneration() {
1101 mGeneration = mContext->bumpGeneration();
1102}
1103
Jeff Brown65fd2512011-08-18 11:20:58 -07001104void InputDevice::notifyReset(nsecs_t when) {
1105 NotifyDeviceResetArgs args(when, mId);
1106 mContext->getListener()->notifyDeviceReset(&args);
1107}
1108
Jeff Brown6d0fec22010-07-23 21:28:06 -07001109
Jeff Brown49754db2011-07-01 17:37:58 -07001110// --- CursorButtonAccumulator ---
1111
1112CursorButtonAccumulator::CursorButtonAccumulator() {
1113 clearButtons();
1114}
1115
Jeff Brown65fd2512011-08-18 11:20:58 -07001116void CursorButtonAccumulator::reset(InputDevice* device) {
1117 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1118 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1119 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1120 mBtnBack = device->isKeyPressed(BTN_BACK);
1121 mBtnSide = device->isKeyPressed(BTN_SIDE);
1122 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1123 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1124 mBtnTask = device->isKeyPressed(BTN_TASK);
1125}
1126
Jeff Brown49754db2011-07-01 17:37:58 -07001127void CursorButtonAccumulator::clearButtons() {
1128 mBtnLeft = 0;
1129 mBtnRight = 0;
1130 mBtnMiddle = 0;
1131 mBtnBack = 0;
1132 mBtnSide = 0;
1133 mBtnForward = 0;
1134 mBtnExtra = 0;
1135 mBtnTask = 0;
1136}
1137
1138void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1139 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001140 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001141 case BTN_LEFT:
1142 mBtnLeft = rawEvent->value;
1143 break;
1144 case BTN_RIGHT:
1145 mBtnRight = rawEvent->value;
1146 break;
1147 case BTN_MIDDLE:
1148 mBtnMiddle = rawEvent->value;
1149 break;
1150 case BTN_BACK:
1151 mBtnBack = rawEvent->value;
1152 break;
1153 case BTN_SIDE:
1154 mBtnSide = rawEvent->value;
1155 break;
1156 case BTN_FORWARD:
1157 mBtnForward = rawEvent->value;
1158 break;
1159 case BTN_EXTRA:
1160 mBtnExtra = rawEvent->value;
1161 break;
1162 case BTN_TASK:
1163 mBtnTask = rawEvent->value;
1164 break;
1165 }
1166 }
1167}
1168
1169uint32_t CursorButtonAccumulator::getButtonState() const {
1170 uint32_t result = 0;
1171 if (mBtnLeft) {
1172 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1173 }
1174 if (mBtnRight) {
1175 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1176 }
1177 if (mBtnMiddle) {
1178 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1179 }
1180 if (mBtnBack || mBtnSide) {
1181 result |= AMOTION_EVENT_BUTTON_BACK;
1182 }
1183 if (mBtnForward || mBtnExtra) {
1184 result |= AMOTION_EVENT_BUTTON_FORWARD;
1185 }
1186 return result;
1187}
1188
1189
1190// --- CursorMotionAccumulator ---
1191
Jeff Brown65fd2512011-08-18 11:20:58 -07001192CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001193 clearRelativeAxes();
1194}
1195
Jeff Brown65fd2512011-08-18 11:20:58 -07001196void CursorMotionAccumulator::reset(InputDevice* device) {
1197 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001198}
1199
1200void CursorMotionAccumulator::clearRelativeAxes() {
1201 mRelX = 0;
1202 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001203}
1204
1205void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1206 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001207 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001208 case REL_X:
1209 mRelX = rawEvent->value;
1210 break;
1211 case REL_Y:
1212 mRelY = rawEvent->value;
1213 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001214 }
1215 }
1216}
1217
1218void CursorMotionAccumulator::finishSync() {
1219 clearRelativeAxes();
1220}
1221
1222
1223// --- CursorScrollAccumulator ---
1224
1225CursorScrollAccumulator::CursorScrollAccumulator() :
1226 mHaveRelWheel(false), mHaveRelHWheel(false) {
1227 clearRelativeAxes();
1228}
1229
1230void CursorScrollAccumulator::configure(InputDevice* device) {
1231 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1232 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1233}
1234
1235void CursorScrollAccumulator::reset(InputDevice* device) {
1236 clearRelativeAxes();
1237}
1238
1239void CursorScrollAccumulator::clearRelativeAxes() {
1240 mRelWheel = 0;
1241 mRelHWheel = 0;
1242}
1243
1244void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1245 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001246 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001247 case REL_WHEEL:
1248 mRelWheel = rawEvent->value;
1249 break;
1250 case REL_HWHEEL:
1251 mRelHWheel = rawEvent->value;
1252 break;
1253 }
1254 }
1255}
1256
Jeff Brown65fd2512011-08-18 11:20:58 -07001257void CursorScrollAccumulator::finishSync() {
1258 clearRelativeAxes();
1259}
1260
Jeff Brown49754db2011-07-01 17:37:58 -07001261
1262// --- TouchButtonAccumulator ---
1263
1264TouchButtonAccumulator::TouchButtonAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001265 mHaveBtnTouch(false), mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001266 clearButtons();
1267}
1268
1269void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001270 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
Jeff Brown00710e92012-04-19 15:18:26 -07001271 mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1272 || device->hasKey(BTN_TOOL_RUBBER)
1273 || device->hasKey(BTN_TOOL_BRUSH)
1274 || device->hasKey(BTN_TOOL_PENCIL)
1275 || device->hasKey(BTN_TOOL_AIRBRUSH);
Jeff Brown65fd2512011-08-18 11:20:58 -07001276}
1277
1278void TouchButtonAccumulator::reset(InputDevice* device) {
1279 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1280 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1281 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1282 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1283 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1284 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1285 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1286 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1287 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1288 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1289 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001290 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1291 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1292 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001293}
1294
1295void TouchButtonAccumulator::clearButtons() {
1296 mBtnTouch = 0;
1297 mBtnStylus = 0;
1298 mBtnStylus2 = 0;
1299 mBtnToolFinger = 0;
1300 mBtnToolPen = 0;
1301 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001302 mBtnToolBrush = 0;
1303 mBtnToolPencil = 0;
1304 mBtnToolAirbrush = 0;
1305 mBtnToolMouse = 0;
1306 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001307 mBtnToolDoubleTap = 0;
1308 mBtnToolTripleTap = 0;
1309 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001310}
1311
1312void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1313 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001314 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001315 case BTN_TOUCH:
1316 mBtnTouch = rawEvent->value;
1317 break;
1318 case BTN_STYLUS:
1319 mBtnStylus = rawEvent->value;
1320 break;
1321 case BTN_STYLUS2:
1322 mBtnStylus2 = rawEvent->value;
1323 break;
1324 case BTN_TOOL_FINGER:
1325 mBtnToolFinger = rawEvent->value;
1326 break;
1327 case BTN_TOOL_PEN:
1328 mBtnToolPen = rawEvent->value;
1329 break;
1330 case BTN_TOOL_RUBBER:
1331 mBtnToolRubber = rawEvent->value;
1332 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001333 case BTN_TOOL_BRUSH:
1334 mBtnToolBrush = rawEvent->value;
1335 break;
1336 case BTN_TOOL_PENCIL:
1337 mBtnToolPencil = rawEvent->value;
1338 break;
1339 case BTN_TOOL_AIRBRUSH:
1340 mBtnToolAirbrush = rawEvent->value;
1341 break;
1342 case BTN_TOOL_MOUSE:
1343 mBtnToolMouse = rawEvent->value;
1344 break;
1345 case BTN_TOOL_LENS:
1346 mBtnToolLens = rawEvent->value;
1347 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001348 case BTN_TOOL_DOUBLETAP:
1349 mBtnToolDoubleTap = rawEvent->value;
1350 break;
1351 case BTN_TOOL_TRIPLETAP:
1352 mBtnToolTripleTap = rawEvent->value;
1353 break;
1354 case BTN_TOOL_QUADTAP:
1355 mBtnToolQuadTap = rawEvent->value;
1356 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001357 }
1358 }
1359}
1360
1361uint32_t TouchButtonAccumulator::getButtonState() const {
1362 uint32_t result = 0;
1363 if (mBtnStylus) {
1364 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1365 }
1366 if (mBtnStylus2) {
1367 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1368 }
1369 return result;
1370}
1371
1372int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001373 if (mBtnToolMouse || mBtnToolLens) {
1374 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1375 }
Jeff Brown49754db2011-07-01 17:37:58 -07001376 if (mBtnToolRubber) {
1377 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1378 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001379 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001380 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1381 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001382 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001383 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1384 }
1385 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1386}
1387
Jeff Brownd87c6d52011-08-10 14:55:59 -07001388bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001389 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1390 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001391 || mBtnToolMouse || mBtnToolLens
1392 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001393}
1394
1395bool TouchButtonAccumulator::isHovering() const {
1396 return mHaveBtnTouch && !mBtnTouch;
1397}
1398
Jeff Brown00710e92012-04-19 15:18:26 -07001399bool TouchButtonAccumulator::hasStylus() const {
1400 return mHaveStylus;
1401}
1402
Jeff Brown49754db2011-07-01 17:37:58 -07001403
Jeff Brownbe1aa822011-07-27 16:04:54 -07001404// --- RawPointerAxes ---
1405
1406RawPointerAxes::RawPointerAxes() {
1407 clear();
1408}
1409
1410void RawPointerAxes::clear() {
1411 x.clear();
1412 y.clear();
1413 pressure.clear();
1414 touchMajor.clear();
1415 touchMinor.clear();
1416 toolMajor.clear();
1417 toolMinor.clear();
1418 orientation.clear();
1419 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001420 tiltX.clear();
1421 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001422 trackingId.clear();
1423 slot.clear();
1424}
1425
1426
1427// --- RawPointerData ---
1428
1429RawPointerData::RawPointerData() {
1430 clear();
1431}
1432
1433void RawPointerData::clear() {
1434 pointerCount = 0;
1435 clearIdBits();
1436}
1437
1438void RawPointerData::copyFrom(const RawPointerData& other) {
1439 pointerCount = other.pointerCount;
1440 hoveringIdBits = other.hoveringIdBits;
1441 touchingIdBits = other.touchingIdBits;
1442
1443 for (uint32_t i = 0; i < pointerCount; i++) {
1444 pointers[i] = other.pointers[i];
1445
1446 int id = pointers[i].id;
1447 idToIndex[id] = other.idToIndex[id];
1448 }
1449}
1450
1451void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1452 float x = 0, y = 0;
1453 uint32_t count = touchingIdBits.count();
1454 if (count) {
1455 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1456 uint32_t id = idBits.clearFirstMarkedBit();
1457 const Pointer& pointer = pointerForId(id);
1458 x += pointer.x;
1459 y += pointer.y;
1460 }
1461 x /= count;
1462 y /= count;
1463 }
1464 *outX = x;
1465 *outY = y;
1466}
1467
1468
1469// --- CookedPointerData ---
1470
1471CookedPointerData::CookedPointerData() {
1472 clear();
1473}
1474
1475void CookedPointerData::clear() {
1476 pointerCount = 0;
1477 hoveringIdBits.clear();
1478 touchingIdBits.clear();
1479}
1480
1481void CookedPointerData::copyFrom(const CookedPointerData& other) {
1482 pointerCount = other.pointerCount;
1483 hoveringIdBits = other.hoveringIdBits;
1484 touchingIdBits = other.touchingIdBits;
1485
1486 for (uint32_t i = 0; i < pointerCount; i++) {
1487 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1488 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1489
1490 int id = pointerProperties[i].id;
1491 idToIndex[id] = other.idToIndex[id];
1492 }
1493}
1494
1495
Jeff Brown49754db2011-07-01 17:37:58 -07001496// --- SingleTouchMotionAccumulator ---
1497
1498SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1499 clearAbsoluteAxes();
1500}
1501
Jeff Brown65fd2512011-08-18 11:20:58 -07001502void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1503 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1504 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1505 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1506 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1507 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1508 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1509 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1510}
1511
Jeff Brown49754db2011-07-01 17:37:58 -07001512void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1513 mAbsX = 0;
1514 mAbsY = 0;
1515 mAbsPressure = 0;
1516 mAbsToolWidth = 0;
1517 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001518 mAbsTiltX = 0;
1519 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001520}
1521
1522void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1523 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001524 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001525 case ABS_X:
1526 mAbsX = rawEvent->value;
1527 break;
1528 case ABS_Y:
1529 mAbsY = rawEvent->value;
1530 break;
1531 case ABS_PRESSURE:
1532 mAbsPressure = rawEvent->value;
1533 break;
1534 case ABS_TOOL_WIDTH:
1535 mAbsToolWidth = rawEvent->value;
1536 break;
1537 case ABS_DISTANCE:
1538 mAbsDistance = rawEvent->value;
1539 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001540 case ABS_TILT_X:
1541 mAbsTiltX = rawEvent->value;
1542 break;
1543 case ABS_TILT_Y:
1544 mAbsTiltY = rawEvent->value;
1545 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001546 }
1547 }
1548}
1549
1550
1551// --- MultiTouchMotionAccumulator ---
1552
1553MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001554 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1555 mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001556}
1557
1558MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1559 delete[] mSlots;
1560}
1561
Jeff Brown00710e92012-04-19 15:18:26 -07001562void MultiTouchMotionAccumulator::configure(InputDevice* device,
1563 size_t slotCount, bool usingSlotsProtocol) {
Jeff Brown49754db2011-07-01 17:37:58 -07001564 mSlotCount = slotCount;
1565 mUsingSlotsProtocol = usingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -07001566 mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Jeff Brown49754db2011-07-01 17:37:58 -07001567
1568 delete[] mSlots;
1569 mSlots = new Slot[slotCount];
1570}
1571
Jeff Brown65fd2512011-08-18 11:20:58 -07001572void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1573 // Unfortunately there is no way to read the initial contents of the slots.
1574 // So when we reset the accumulator, we must assume they are all zeroes.
1575 if (mUsingSlotsProtocol) {
1576 // Query the driver for the current slot index and use it as the initial slot
1577 // before we start reading events from the device. It is possible that the
1578 // current slot index will not be the same as it was when the first event was
1579 // written into the evdev buffer, which means the input mapper could start
1580 // out of sync with the initial state of the events in the evdev buffer.
1581 // In the extremely unlikely case that this happens, the data from
1582 // two slots will be confused until the next ABS_MT_SLOT event is received.
1583 // This can cause the touch point to "jump", but at least there will be
1584 // no stuck touches.
1585 int32_t initialSlot;
1586 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1587 ABS_MT_SLOT, &initialSlot);
1588 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001589 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001590 initialSlot = -1;
1591 }
1592 clearSlots(initialSlot);
1593 } else {
1594 clearSlots(-1);
1595 }
1596}
1597
Jeff Brown49754db2011-07-01 17:37:58 -07001598void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001599 if (mSlots) {
1600 for (size_t i = 0; i < mSlotCount; i++) {
1601 mSlots[i].clear();
1602 }
Jeff Brown49754db2011-07-01 17:37:58 -07001603 }
1604 mCurrentSlot = initialSlot;
1605}
1606
1607void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1608 if (rawEvent->type == EV_ABS) {
1609 bool newSlot = false;
1610 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001611 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001612 mCurrentSlot = rawEvent->value;
1613 newSlot = true;
1614 }
1615 } else if (mCurrentSlot < 0) {
1616 mCurrentSlot = 0;
1617 }
1618
1619 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1620#if DEBUG_POINTERS
1621 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001622 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001623 "should be between 0 and %d; ignoring this slot.",
1624 mCurrentSlot, mSlotCount - 1);
1625 }
1626#endif
1627 } else {
1628 Slot* slot = &mSlots[mCurrentSlot];
1629
Jeff Brown49ccac52012-04-11 18:27:33 -07001630 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001631 case ABS_MT_POSITION_X:
1632 slot->mInUse = true;
1633 slot->mAbsMTPositionX = rawEvent->value;
1634 break;
1635 case ABS_MT_POSITION_Y:
1636 slot->mInUse = true;
1637 slot->mAbsMTPositionY = rawEvent->value;
1638 break;
1639 case ABS_MT_TOUCH_MAJOR:
1640 slot->mInUse = true;
1641 slot->mAbsMTTouchMajor = rawEvent->value;
1642 break;
1643 case ABS_MT_TOUCH_MINOR:
1644 slot->mInUse = true;
1645 slot->mAbsMTTouchMinor = rawEvent->value;
1646 slot->mHaveAbsMTTouchMinor = true;
1647 break;
1648 case ABS_MT_WIDTH_MAJOR:
1649 slot->mInUse = true;
1650 slot->mAbsMTWidthMajor = rawEvent->value;
1651 break;
1652 case ABS_MT_WIDTH_MINOR:
1653 slot->mInUse = true;
1654 slot->mAbsMTWidthMinor = rawEvent->value;
1655 slot->mHaveAbsMTWidthMinor = true;
1656 break;
1657 case ABS_MT_ORIENTATION:
1658 slot->mInUse = true;
1659 slot->mAbsMTOrientation = rawEvent->value;
1660 break;
1661 case ABS_MT_TRACKING_ID:
1662 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001663 // The slot is no longer in use but it retains its previous contents,
1664 // which may be reused for subsequent touches.
1665 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001666 } else {
1667 slot->mInUse = true;
1668 slot->mAbsMTTrackingId = rawEvent->value;
1669 }
1670 break;
1671 case ABS_MT_PRESSURE:
1672 slot->mInUse = true;
1673 slot->mAbsMTPressure = rawEvent->value;
1674 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001675 case ABS_MT_DISTANCE:
1676 slot->mInUse = true;
1677 slot->mAbsMTDistance = rawEvent->value;
1678 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001679 case ABS_MT_TOOL_TYPE:
1680 slot->mInUse = true;
1681 slot->mAbsMTToolType = rawEvent->value;
1682 slot->mHaveAbsMTToolType = true;
1683 break;
1684 }
1685 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001686 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001687 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1688 mCurrentSlot += 1;
1689 }
1690}
1691
Jeff Brown65fd2512011-08-18 11:20:58 -07001692void MultiTouchMotionAccumulator::finishSync() {
1693 if (!mUsingSlotsProtocol) {
1694 clearSlots(-1);
1695 }
1696}
1697
Jeff Brown00710e92012-04-19 15:18:26 -07001698bool MultiTouchMotionAccumulator::hasStylus() const {
1699 return mHaveStylus;
1700}
1701
Jeff Brown49754db2011-07-01 17:37:58 -07001702
1703// --- MultiTouchMotionAccumulator::Slot ---
1704
1705MultiTouchMotionAccumulator::Slot::Slot() {
1706 clear();
1707}
1708
Jeff Brown49754db2011-07-01 17:37:58 -07001709void MultiTouchMotionAccumulator::Slot::clear() {
1710 mInUse = false;
1711 mHaveAbsMTTouchMinor = false;
1712 mHaveAbsMTWidthMinor = false;
1713 mHaveAbsMTToolType = false;
1714 mAbsMTPositionX = 0;
1715 mAbsMTPositionY = 0;
1716 mAbsMTTouchMajor = 0;
1717 mAbsMTTouchMinor = 0;
1718 mAbsMTWidthMajor = 0;
1719 mAbsMTWidthMinor = 0;
1720 mAbsMTOrientation = 0;
1721 mAbsMTTrackingId = -1;
1722 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001723 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001724 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001725}
1726
1727int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1728 if (mHaveAbsMTToolType) {
1729 switch (mAbsMTToolType) {
1730 case MT_TOOL_FINGER:
1731 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1732 case MT_TOOL_PEN:
1733 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1734 }
1735 }
1736 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1737}
1738
1739
Jeff Brown6d0fec22010-07-23 21:28:06 -07001740// --- InputMapper ---
1741
1742InputMapper::InputMapper(InputDevice* device) :
1743 mDevice(device), mContext(device->getContext()) {
1744}
1745
1746InputMapper::~InputMapper() {
1747}
1748
1749void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1750 info->addSource(getSources());
1751}
1752
Jeff Brownef3d7e82010-09-30 14:33:04 -07001753void InputMapper::dump(String8& dump) {
1754}
1755
Jeff Brown65fd2512011-08-18 11:20:58 -07001756void InputMapper::configure(nsecs_t when,
1757 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001758}
1759
Jeff Brown65fd2512011-08-18 11:20:58 -07001760void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001761}
1762
Jeff Brownaa3855d2011-03-17 01:34:19 -07001763void InputMapper::timeoutExpired(nsecs_t when) {
1764}
1765
Jeff Brown6d0fec22010-07-23 21:28:06 -07001766int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1767 return AKEY_STATE_UNKNOWN;
1768}
1769
1770int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1771 return AKEY_STATE_UNKNOWN;
1772}
1773
1774int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1775 return AKEY_STATE_UNKNOWN;
1776}
1777
1778bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1779 const int32_t* keyCodes, uint8_t* outFlags) {
1780 return false;
1781}
1782
Jeff Browna47425a2012-04-13 04:09:27 -07001783void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1784 int32_t token) {
1785}
1786
1787void InputMapper::cancelVibrate(int32_t token) {
1788}
1789
Jeff Brown6d0fec22010-07-23 21:28:06 -07001790int32_t InputMapper::getMetaState() {
1791 return 0;
1792}
1793
Jeff Brown05dc66a2011-03-02 14:41:58 -08001794void InputMapper::fadePointer() {
1795}
1796
Jeff Brownbe1aa822011-07-27 16:04:54 -07001797status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1798 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1799}
1800
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001801void InputMapper::bumpGeneration() {
1802 mDevice->bumpGeneration();
1803}
1804
Jeff Browncb1404e2011-01-15 18:14:15 -08001805void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1806 const RawAbsoluteAxisInfo& axis, const char* name) {
1807 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001808 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1809 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001810 } else {
1811 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1812 }
1813}
1814
Jeff Brown6d0fec22010-07-23 21:28:06 -07001815
1816// --- SwitchInputMapper ---
1817
1818SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1819 InputMapper(device) {
1820}
1821
1822SwitchInputMapper::~SwitchInputMapper() {
1823}
1824
1825uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001826 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001827}
1828
1829void SwitchInputMapper::process(const RawEvent* rawEvent) {
1830 switch (rawEvent->type) {
1831 case EV_SW:
Jeff Brown49ccac52012-04-11 18:27:33 -07001832 processSwitch(rawEvent->when, rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001833 break;
1834 }
1835}
1836
1837void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001838 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1839 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001840}
1841
1842int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1843 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1844}
1845
1846
Jeff Browna47425a2012-04-13 04:09:27 -07001847// --- VibratorInputMapper ---
1848
1849VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1850 InputMapper(device), mVibrating(false) {
1851}
1852
1853VibratorInputMapper::~VibratorInputMapper() {
1854}
1855
1856uint32_t VibratorInputMapper::getSources() {
1857 return 0;
1858}
1859
1860void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1861 InputMapper::populateDeviceInfo(info);
1862
1863 info->setVibrator(true);
1864}
1865
1866void VibratorInputMapper::process(const RawEvent* rawEvent) {
1867 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1868}
1869
1870void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1871 int32_t token) {
1872#if DEBUG_VIBRATOR
1873 String8 patternStr;
1874 for (size_t i = 0; i < patternSize; i++) {
1875 if (i != 0) {
1876 patternStr.append(", ");
1877 }
1878 patternStr.appendFormat("%lld", pattern[i]);
1879 }
1880 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1881 getDeviceId(), patternStr.string(), repeat, token);
1882#endif
1883
1884 mVibrating = true;
1885 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1886 mPatternSize = patternSize;
1887 mRepeat = repeat;
1888 mToken = token;
1889 mIndex = -1;
1890
1891 nextStep();
1892}
1893
1894void VibratorInputMapper::cancelVibrate(int32_t token) {
1895#if DEBUG_VIBRATOR
1896 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1897#endif
1898
1899 if (mVibrating && mToken == token) {
1900 stopVibrating();
1901 }
1902}
1903
1904void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1905 if (mVibrating) {
1906 if (when >= mNextStepTime) {
1907 nextStep();
1908 } else {
1909 getContext()->requestTimeoutAtTime(mNextStepTime);
1910 }
1911 }
1912}
1913
1914void VibratorInputMapper::nextStep() {
1915 mIndex += 1;
1916 if (size_t(mIndex) >= mPatternSize) {
1917 if (mRepeat < 0) {
1918 // We are done.
1919 stopVibrating();
1920 return;
1921 }
1922 mIndex = mRepeat;
1923 }
1924
1925 bool vibratorOn = mIndex & 1;
1926 nsecs_t duration = mPattern[mIndex];
1927 if (vibratorOn) {
1928#if DEBUG_VIBRATOR
1929 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1930 getDeviceId(), duration);
1931#endif
1932 getEventHub()->vibrate(getDeviceId(), duration);
1933 } else {
1934#if DEBUG_VIBRATOR
1935 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1936#endif
1937 getEventHub()->cancelVibrate(getDeviceId());
1938 }
1939 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1940 mNextStepTime = now + duration;
1941 getContext()->requestTimeoutAtTime(mNextStepTime);
1942#if DEBUG_VIBRATOR
1943 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1944#endif
1945}
1946
1947void VibratorInputMapper::stopVibrating() {
1948 mVibrating = false;
1949#if DEBUG_VIBRATOR
1950 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1951#endif
1952 getEventHub()->cancelVibrate(getDeviceId());
1953}
1954
1955void VibratorInputMapper::dump(String8& dump) {
1956 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1957 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1958}
1959
1960
Jeff Brown6d0fec22010-07-23 21:28:06 -07001961// --- KeyboardInputMapper ---
1962
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001963KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001964 uint32_t source, int32_t keyboardType) :
1965 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001966 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001967}
1968
1969KeyboardInputMapper::~KeyboardInputMapper() {
1970}
1971
Jeff Brown6d0fec22010-07-23 21:28:06 -07001972uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001973 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001974}
1975
1976void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1977 InputMapper::populateDeviceInfo(info);
1978
1979 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001980 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001981}
1982
Jeff Brownef3d7e82010-09-30 14:33:04 -07001983void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001984 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1985 dumpParameters(dump);
1986 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001987 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001988 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1989 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1990 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001991}
1992
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001993
Jeff Brown65fd2512011-08-18 11:20:58 -07001994void KeyboardInputMapper::configure(nsecs_t when,
1995 const InputReaderConfiguration* config, uint32_t changes) {
1996 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001997
Jeff Brown474dcb52011-06-14 20:22:50 -07001998 if (!changes) { // first time only
1999 // Configure basic parameters.
2000 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002001 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002002
Jeff Brown65fd2512011-08-18 11:20:58 -07002003 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2004 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2005 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2006 false /*external*/, NULL, NULL, &mOrientation)) {
2007 mOrientation = DISPLAY_ORIENTATION_0;
2008 }
2009 } else {
2010 mOrientation = DISPLAY_ORIENTATION_0;
2011 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002012 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002013}
2014
2015void KeyboardInputMapper::configureParameters() {
2016 mParameters.orientationAware = false;
2017 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2018 mParameters.orientationAware);
2019
Jeff Brownbc68a592011-07-25 12:58:12 -07002020 mParameters.associatedDisplayId = -1;
2021 if (mParameters.orientationAware) {
2022 mParameters.associatedDisplayId = 0;
2023 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002024}
2025
2026void KeyboardInputMapper::dumpParameters(String8& dump) {
2027 dump.append(INDENT3 "Parameters:\n");
2028 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2029 mParameters.associatedDisplayId);
2030 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2031 toString(mParameters.orientationAware));
2032}
2033
Jeff Brown65fd2512011-08-18 11:20:58 -07002034void KeyboardInputMapper::reset(nsecs_t when) {
2035 mMetaState = AMETA_NONE;
2036 mDownTime = 0;
2037 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002038 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002039
Jeff Brownbe1aa822011-07-27 16:04:54 -07002040 resetLedState();
2041
Jeff Brown65fd2512011-08-18 11:20:58 -07002042 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002043}
2044
2045void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2046 switch (rawEvent->type) {
2047 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002048 int32_t scanCode = rawEvent->code;
2049 int32_t usageCode = mCurrentHidUsage;
2050 mCurrentHidUsage = 0;
2051
Jeff Brown6d0fec22010-07-23 21:28:06 -07002052 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002053 int32_t keyCode;
2054 uint32_t flags;
2055 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2056 keyCode = AKEYCODE_UNKNOWN;
2057 flags = 0;
2058 }
2059 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002060 }
2061 break;
2062 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002063 case EV_MSC: {
2064 if (rawEvent->code == MSC_SCAN) {
2065 mCurrentHidUsage = rawEvent->value;
2066 }
2067 break;
2068 }
2069 case EV_SYN: {
2070 if (rawEvent->code == SYN_REPORT) {
2071 mCurrentHidUsage = 0;
2072 }
2073 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002074 }
2075}
2076
2077bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2078 return scanCode < BTN_MOUSE
2079 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002080 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002081 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002082}
2083
Jeff Brown6328cdc2010-07-29 18:18:33 -07002084void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2085 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002086
Jeff Brownbe1aa822011-07-27 16:04:54 -07002087 if (down) {
2088 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002089 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002090 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002091 }
Jeff Brownfe508922011-01-18 15:10:10 -08002092
Jeff Brownbe1aa822011-07-27 16:04:54 -07002093 // Add key down.
2094 ssize_t keyDownIndex = findKeyDown(scanCode);
2095 if (keyDownIndex >= 0) {
2096 // key repeat, be sure to use same keycode as before in case of rotation
2097 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002098 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002099 // key down
2100 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2101 && mContext->shouldDropVirtualKey(when,
2102 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002103 return;
2104 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002105
2106 mKeyDowns.push();
2107 KeyDown& keyDown = mKeyDowns.editTop();
2108 keyDown.keyCode = keyCode;
2109 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002110 }
2111
Jeff Brownbe1aa822011-07-27 16:04:54 -07002112 mDownTime = when;
2113 } else {
2114 // Remove key down.
2115 ssize_t keyDownIndex = findKeyDown(scanCode);
2116 if (keyDownIndex >= 0) {
2117 // key up, be sure to use same keycode as before in case of rotation
2118 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2119 mKeyDowns.removeAt(size_t(keyDownIndex));
2120 } else {
2121 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002122 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002123 "keyCode=%d, scanCode=%d",
2124 getDeviceName().string(), keyCode, scanCode);
2125 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002126 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002127 }
Jeff Brownfd035822010-06-30 16:10:35 -07002128
Jeff Brownbe1aa822011-07-27 16:04:54 -07002129 bool metaStateChanged = false;
2130 int32_t oldMetaState = mMetaState;
2131 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
2132 if (oldMetaState != newMetaState) {
2133 mMetaState = newMetaState;
2134 metaStateChanged = true;
2135 updateLedState(false);
2136 }
2137
2138 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002139
Jeff Brown56194eb2011-03-02 19:23:13 -08002140 // Key down on external an keyboard should wake the device.
2141 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2142 // For internal keyboards, the key layout file should specify the policy flags for
2143 // each wake key individually.
2144 // TODO: Use the input device configuration to control this behavior more finely.
2145 if (down && getDevice()->isExternal()
2146 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2147 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2148 }
2149
Jeff Brown6328cdc2010-07-29 18:18:33 -07002150 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002151 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002152 }
2153
Jeff Brown05dc66a2011-03-02 14:41:58 -08002154 if (down && !isMetaKey(keyCode)) {
2155 getContext()->fadePointer();
2156 }
2157
Jeff Brownbe1aa822011-07-27 16:04:54 -07002158 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002159 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2160 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002161 getListener()->notifyKey(&args);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002162}
2163
Jeff Brownbe1aa822011-07-27 16:04:54 -07002164ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2165 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002166 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002167 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002168 return i;
2169 }
2170 }
2171 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002172}
2173
Jeff Brown6d0fec22010-07-23 21:28:06 -07002174int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2175 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2176}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002177
Jeff Brown6d0fec22010-07-23 21:28:06 -07002178int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2179 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2180}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002181
Jeff Brown6d0fec22010-07-23 21:28:06 -07002182bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2183 const int32_t* keyCodes, uint8_t* outFlags) {
2184 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2185}
2186
2187int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002188 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002189}
2190
Jeff Brownbe1aa822011-07-27 16:04:54 -07002191void KeyboardInputMapper::resetLedState() {
2192 initializeLedState(mCapsLockLedState, LED_CAPSL);
2193 initializeLedState(mNumLockLedState, LED_NUML);
2194 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002195
Jeff Brownbe1aa822011-07-27 16:04:54 -07002196 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002197}
2198
Jeff Brownbe1aa822011-07-27 16:04:54 -07002199void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002200 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2201 ledState.on = false;
2202}
2203
Jeff Brownbe1aa822011-07-27 16:04:54 -07002204void KeyboardInputMapper::updateLedState(bool reset) {
2205 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002206 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002207 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002208 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002209 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002210 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002211}
2212
Jeff Brownbe1aa822011-07-27 16:04:54 -07002213void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002214 int32_t led, int32_t modifier, bool reset) {
2215 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002217 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002218 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2219 ledState.on = desiredState;
2220 }
2221 }
2222}
2223
Jeff Brown6d0fec22010-07-23 21:28:06 -07002224
Jeff Brown83c09682010-12-23 17:50:18 -08002225// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002226
Jeff Brown83c09682010-12-23 17:50:18 -08002227CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002228 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002229}
2230
Jeff Brown83c09682010-12-23 17:50:18 -08002231CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002232}
2233
Jeff Brown83c09682010-12-23 17:50:18 -08002234uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002235 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002236}
2237
Jeff Brown83c09682010-12-23 17:50:18 -08002238void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002239 InputMapper::populateDeviceInfo(info);
2240
Jeff Brown83c09682010-12-23 17:50:18 -08002241 if (mParameters.mode == Parameters::MODE_POINTER) {
2242 float minX, minY, maxX, maxY;
2243 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002244 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2245 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002246 }
2247 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002248 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2249 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002250 }
Jeff Brownefd32662011-03-08 15:13:06 -08002251 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002252
Jeff Brown65fd2512011-08-18 11:20:58 -07002253 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002254 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002255 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002256 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002257 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002258 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002259}
2260
Jeff Brown83c09682010-12-23 17:50:18 -08002261void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002262 dump.append(INDENT2 "Cursor Input Mapper:\n");
2263 dumpParameters(dump);
2264 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2265 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2266 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2267 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2268 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002269 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002270 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002271 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002272 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2273 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002274 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002275 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2276 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2277 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002278}
2279
Jeff Brown65fd2512011-08-18 11:20:58 -07002280void CursorInputMapper::configure(nsecs_t when,
2281 const InputReaderConfiguration* config, uint32_t changes) {
2282 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002283
Jeff Brown474dcb52011-06-14 20:22:50 -07002284 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002285 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002286
Jeff Brown474dcb52011-06-14 20:22:50 -07002287 // Configure basic parameters.
2288 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002289
Jeff Brown474dcb52011-06-14 20:22:50 -07002290 // Configure device mode.
2291 switch (mParameters.mode) {
2292 case Parameters::MODE_POINTER:
2293 mSource = AINPUT_SOURCE_MOUSE;
2294 mXPrecision = 1.0f;
2295 mYPrecision = 1.0f;
2296 mXScale = 1.0f;
2297 mYScale = 1.0f;
2298 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2299 break;
2300 case Parameters::MODE_NAVIGATION:
2301 mSource = AINPUT_SOURCE_TRACKBALL;
2302 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2303 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2304 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2305 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2306 break;
2307 }
2308
2309 mVWheelScale = 1.0f;
2310 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002311 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002312
Jeff Brown474dcb52011-06-14 20:22:50 -07002313 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2314 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2315 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2316 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2317 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002318
2319 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2320 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2321 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2322 false /*external*/, NULL, NULL, &mOrientation)) {
2323 mOrientation = DISPLAY_ORIENTATION_0;
2324 }
2325 } else {
2326 mOrientation = DISPLAY_ORIENTATION_0;
2327 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002328 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002329 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002330}
2331
Jeff Brown83c09682010-12-23 17:50:18 -08002332void CursorInputMapper::configureParameters() {
2333 mParameters.mode = Parameters::MODE_POINTER;
2334 String8 cursorModeString;
2335 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2336 if (cursorModeString == "navigation") {
2337 mParameters.mode = Parameters::MODE_NAVIGATION;
2338 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002339 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002340 }
2341 }
2342
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002343 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002344 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002345 mParameters.orientationAware);
2346
Jeff Brownbc68a592011-07-25 12:58:12 -07002347 mParameters.associatedDisplayId = -1;
2348 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2349 mParameters.associatedDisplayId = 0;
2350 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002351}
2352
Jeff Brown83c09682010-12-23 17:50:18 -08002353void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002354 dump.append(INDENT3 "Parameters:\n");
2355 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2356 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002357
2358 switch (mParameters.mode) {
2359 case Parameters::MODE_POINTER:
2360 dump.append(INDENT4 "Mode: pointer\n");
2361 break;
2362 case Parameters::MODE_NAVIGATION:
2363 dump.append(INDENT4 "Mode: navigation\n");
2364 break;
2365 default:
Steve Blockec193de2012-01-09 18:35:44 +00002366 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002367 }
2368
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002369 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2370 toString(mParameters.orientationAware));
2371}
2372
Jeff Brown65fd2512011-08-18 11:20:58 -07002373void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002374 mButtonState = 0;
2375 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002376
Jeff Brownbe1aa822011-07-27 16:04:54 -07002377 mPointerVelocityControl.reset();
2378 mWheelXVelocityControl.reset();
2379 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002380
Jeff Brown65fd2512011-08-18 11:20:58 -07002381 mCursorButtonAccumulator.reset(getDevice());
2382 mCursorMotionAccumulator.reset(getDevice());
2383 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002384
Jeff Brown65fd2512011-08-18 11:20:58 -07002385 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002386}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002387
Jeff Brown83c09682010-12-23 17:50:18 -08002388void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002389 mCursorButtonAccumulator.process(rawEvent);
2390 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002391 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002392
Jeff Brown49ccac52012-04-11 18:27:33 -07002393 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002394 sync(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002395 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002396}
2397
Jeff Brown83c09682010-12-23 17:50:18 -08002398void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002399 int32_t lastButtonState = mButtonState;
2400 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2401 mButtonState = currentButtonState;
2402
2403 bool wasDown = isPointerDown(lastButtonState);
2404 bool down = isPointerDown(currentButtonState);
2405 bool downChanged;
2406 if (!wasDown && down) {
2407 mDownTime = when;
2408 downChanged = true;
2409 } else if (wasDown && !down) {
2410 downChanged = true;
2411 } else {
2412 downChanged = false;
2413 }
2414 nsecs_t downTime = mDownTime;
2415 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002416 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002417
2418 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2419 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2420 bool moved = deltaX != 0 || deltaY != 0;
2421
Jeff Brown65fd2512011-08-18 11:20:58 -07002422 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002423 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2424 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002425 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002426 }
2427
Jeff Brown65fd2512011-08-18 11:20:58 -07002428 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002429 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002430 pointerProperties.clear();
2431 pointerProperties.id = 0;
2432 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2433
Jeff Brown6328cdc2010-07-29 18:18:33 -07002434 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002435 pointerCoords.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002436
Jeff Brown65fd2512011-08-18 11:20:58 -07002437 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2438 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002439 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002440
Jeff Brownbe1aa822011-07-27 16:04:54 -07002441 mWheelYVelocityControl.move(when, NULL, &vscroll);
2442 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002443
Jeff Brownbe1aa822011-07-27 16:04:54 -07002444 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002445
Jeff Brownbe1aa822011-07-27 16:04:54 -07002446 if (mPointerController != NULL) {
2447 if (moved || scrolled || buttonsChanged) {
2448 mPointerController->setPresentation(
2449 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002450
Jeff Brownbe1aa822011-07-27 16:04:54 -07002451 if (moved) {
2452 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002453 }
2454
Jeff Brownbe1aa822011-07-27 16:04:54 -07002455 if (buttonsChanged) {
2456 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002457 }
Jeff Brownefd32662011-03-08 15:13:06 -08002458
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002460 }
2461
Jeff Brownbe1aa822011-07-27 16:04:54 -07002462 float x, y;
2463 mPointerController->getPosition(&x, &y);
2464 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2465 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2466 } else {
2467 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2468 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2469 }
2470
2471 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002472
Jeff Brown56194eb2011-03-02 19:23:13 -08002473 // Moving an external trackball or mouse should wake the device.
2474 // We don't do this for internal cursor devices to prevent them from waking up
2475 // the device in your pocket.
2476 // TODO: Use the input device configuration to control this behavior more finely.
2477 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002478 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002479 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2480 }
2481
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002482 // Synthesize key down from buttons if needed.
2483 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2484 policyFlags, lastButtonState, currentButtonState);
2485
2486 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002487 if (downChanged || moved || scrolled || buttonsChanged) {
2488 int32_t metaState = mContext->getGlobalMetaState();
2489 int32_t motionEventAction;
2490 if (downChanged) {
2491 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2492 } else if (down || mPointerController == NULL) {
2493 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2494 } else {
2495 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2496 }
Jeff Brownb6997262010-10-08 22:31:17 -07002497
Jeff Brownbe1aa822011-07-27 16:04:54 -07002498 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2499 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002500 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002501 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002502
Jeff Brownbe1aa822011-07-27 16:04:54 -07002503 // Send hover move after UP to tell the application that the mouse is hovering now.
2504 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2505 && mPointerController != NULL) {
2506 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2507 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2508 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2509 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2510 getListener()->notifyMotion(&hoverArgs);
2511 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002512
Jeff Brownbe1aa822011-07-27 16:04:54 -07002513 // Send scroll events.
2514 if (scrolled) {
2515 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2516 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2517
2518 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2519 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2520 AMOTION_EVENT_EDGE_FLAG_NONE,
2521 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2522 getListener()->notifyMotion(&scrollArgs);
2523 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002524 }
Jeff Browna032cc02011-03-07 16:56:21 -08002525
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002526 // Synthesize key up from buttons if needed.
2527 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2528 policyFlags, lastButtonState, currentButtonState);
2529
Jeff Brown65fd2512011-08-18 11:20:58 -07002530 mCursorMotionAccumulator.finishSync();
2531 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002532}
2533
Jeff Brown83c09682010-12-23 17:50:18 -08002534int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002535 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2536 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2537 } else {
2538 return AKEY_STATE_UNKNOWN;
2539 }
2540}
2541
Jeff Brown05dc66a2011-03-02 14:41:58 -08002542void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002543 if (mPointerController != NULL) {
2544 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2545 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002546}
2547
Jeff Brown6d0fec22010-07-23 21:28:06 -07002548
2549// --- TouchInputMapper ---
2550
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002551TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002552 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002553 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002554 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002555}
2556
2557TouchInputMapper::~TouchInputMapper() {
2558}
2559
2560uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002561 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002562}
2563
2564void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2565 InputMapper::populateDeviceInfo(info);
2566
Jeff Brown65fd2512011-08-18 11:20:58 -07002567 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2568 info->addMotionRange(mOrientedRanges.x);
2569 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002570 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002571
Jeff Brown65fd2512011-08-18 11:20:58 -07002572 if (mOrientedRanges.haveSize) {
2573 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002574 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002575
2576 if (mOrientedRanges.haveTouchSize) {
2577 info->addMotionRange(mOrientedRanges.touchMajor);
2578 info->addMotionRange(mOrientedRanges.touchMinor);
2579 }
2580
2581 if (mOrientedRanges.haveToolSize) {
2582 info->addMotionRange(mOrientedRanges.toolMajor);
2583 info->addMotionRange(mOrientedRanges.toolMinor);
2584 }
2585
2586 if (mOrientedRanges.haveOrientation) {
2587 info->addMotionRange(mOrientedRanges.orientation);
2588 }
2589
2590 if (mOrientedRanges.haveDistance) {
2591 info->addMotionRange(mOrientedRanges.distance);
2592 }
2593
2594 if (mOrientedRanges.haveTilt) {
2595 info->addMotionRange(mOrientedRanges.tilt);
2596 }
2597
2598 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2599 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2600 }
2601 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2602 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2603 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002604 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002605}
2606
Jeff Brownef3d7e82010-09-30 14:33:04 -07002607void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002608 dump.append(INDENT2 "Touch Input Mapper:\n");
2609 dumpParameters(dump);
2610 dumpVirtualKeys(dump);
2611 dumpRawPointerAxes(dump);
2612 dumpCalibration(dump);
2613 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002614
Jeff Brownbe1aa822011-07-27 16:04:54 -07002615 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2616 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2617 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2618 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2619 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2620 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002621 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2622 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2623 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2624 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002625 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2626 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2627 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2628 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2629 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002630
Jeff Brownbe1aa822011-07-27 16:04:54 -07002631 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002632
Jeff Brownbe1aa822011-07-27 16:04:54 -07002633 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2634 mLastRawPointerData.pointerCount);
2635 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2636 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2637 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2638 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002639 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2640 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002641 pointer.id, pointer.x, pointer.y, pointer.pressure,
2642 pointer.touchMajor, pointer.touchMinor,
2643 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002644 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002645 pointer.toolType, toString(pointer.isHovering));
2646 }
2647
2648 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2649 mLastCookedPointerData.pointerCount);
2650 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2651 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2652 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2653 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2654 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002655 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2656 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002657 pointerProperties.id,
2658 pointerCoords.getX(),
2659 pointerCoords.getY(),
2660 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2661 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2662 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2663 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2664 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2665 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002666 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002667 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2668 pointerProperties.toolType,
2669 toString(mLastCookedPointerData.isHovering(i)));
2670 }
2671
Jeff Brown65fd2512011-08-18 11:20:58 -07002672 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002673 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2674 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002675 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002676 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002677 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002678 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002679 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002680 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002681 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002682 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2683 mPointerGestureMaxSwipeWidth);
2684 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002685}
2686
Jeff Brown65fd2512011-08-18 11:20:58 -07002687void TouchInputMapper::configure(nsecs_t when,
2688 const InputReaderConfiguration* config, uint32_t changes) {
2689 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002690
Jeff Brown474dcb52011-06-14 20:22:50 -07002691 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002692
Jeff Brown474dcb52011-06-14 20:22:50 -07002693 if (!changes) { // first time only
2694 // Configure basic parameters.
2695 configureParameters();
2696
Jeff Brown65fd2512011-08-18 11:20:58 -07002697 // Configure common accumulators.
2698 mCursorScrollAccumulator.configure(getDevice());
2699 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002700
2701 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002702 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002703
2704 // Prepare input device calibration.
2705 parseCalibration();
2706 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002707 }
2708
Jeff Brown474dcb52011-06-14 20:22:50 -07002709 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002710 // Update pointer speed.
2711 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2712 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2713 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002714 }
Jeff Brown8d608662010-08-30 03:02:23 -07002715
Jeff Brown65fd2512011-08-18 11:20:58 -07002716 bool resetNeeded = false;
2717 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002718 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2719 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002720 // Configure device sources, surface dimensions, orientation and
2721 // scaling factors.
2722 configureSurface(when, &resetNeeded);
2723 }
2724
2725 if (changes && resetNeeded) {
2726 // Send reset, unless this is the first time the device has been configured,
2727 // in which case the reader will call reset itself after all mappers are ready.
2728 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002729 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002730}
2731
Jeff Brown8d608662010-08-30 03:02:23 -07002732void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002733 // Use the pointer presentation mode for devices that do not support distinct
2734 // multitouch. The spot-based presentation relies on being able to accurately
2735 // locate two or more fingers on the touch pad.
2736 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2737 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002738
Jeff Brown538881e2011-05-25 18:23:38 -07002739 String8 gestureModeString;
2740 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2741 gestureModeString)) {
2742 if (gestureModeString == "pointer") {
2743 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2744 } else if (gestureModeString == "spots") {
2745 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2746 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002747 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002748 }
2749 }
2750
Jeff Browndeffe072011-08-26 18:38:46 -07002751 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2752 // The device is a touch screen.
2753 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2754 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2755 // The device is a pointing device like a track pad.
2756 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2757 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002758 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2759 // The device is a cursor device with a touch pad attached.
2760 // By default don't use the touch pad to move the pointer.
2761 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2762 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002763 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002764 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2765 }
2766
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002767 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002768 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2769 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002770 if (deviceTypeString == "touchScreen") {
2771 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002772 } else if (deviceTypeString == "touchPad") {
2773 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002774 } else if (deviceTypeString == "pointer") {
2775 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002776 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002777 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002778 }
2779 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002780
Jeff Brownefd32662011-03-08 15:13:06 -08002781 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002782 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2783 mParameters.orientationAware);
2784
Jeff Brownbc68a592011-07-25 12:58:12 -07002785 mParameters.associatedDisplayId = -1;
2786 mParameters.associatedDisplayIsExternal = false;
2787 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002788 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002789 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2790 mParameters.associatedDisplayIsExternal =
2791 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2792 && getDevice()->isExternal();
2793 mParameters.associatedDisplayId = 0;
2794 }
Jeff Brown8d608662010-08-30 03:02:23 -07002795}
2796
Jeff Brownef3d7e82010-09-30 14:33:04 -07002797void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002798 dump.append(INDENT3 "Parameters:\n");
2799
Jeff Brown538881e2011-05-25 18:23:38 -07002800 switch (mParameters.gestureMode) {
2801 case Parameters::GESTURE_MODE_POINTER:
2802 dump.append(INDENT4 "GestureMode: pointer\n");
2803 break;
2804 case Parameters::GESTURE_MODE_SPOTS:
2805 dump.append(INDENT4 "GestureMode: spots\n");
2806 break;
2807 default:
2808 assert(false);
2809 }
2810
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002811 switch (mParameters.deviceType) {
2812 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2813 dump.append(INDENT4 "DeviceType: touchScreen\n");
2814 break;
2815 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2816 dump.append(INDENT4 "DeviceType: touchPad\n");
2817 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002818 case Parameters::DEVICE_TYPE_POINTER:
2819 dump.append(INDENT4 "DeviceType: pointer\n");
2820 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002821 default:
Steve Blockec193de2012-01-09 18:35:44 +00002822 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002823 }
2824
Jeff Brown65fd2512011-08-18 11:20:58 -07002825 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2826 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002827 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2828 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002829}
2830
Jeff Brownbe1aa822011-07-27 16:04:54 -07002831void TouchInputMapper::configureRawPointerAxes() {
2832 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002833}
2834
Jeff Brownbe1aa822011-07-27 16:04:54 -07002835void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2836 dump.append(INDENT3 "Raw Touch Axes:\n");
2837 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2838 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2839 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2840 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2841 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2842 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2843 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2844 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2845 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002846 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2847 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002848 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2849 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002850}
2851
Jeff Brown65fd2512011-08-18 11:20:58 -07002852void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2853 int32_t oldDeviceMode = mDeviceMode;
2854
2855 // Determine device mode.
2856 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2857 && mConfig.pointerGesturesEnabled) {
2858 mSource = AINPUT_SOURCE_MOUSE;
2859 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002860 if (hasStylus()) {
2861 mSource |= AINPUT_SOURCE_STYLUS;
2862 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002863 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2864 && mParameters.associatedDisplayId >= 0) {
2865 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2866 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002867 if (hasStylus()) {
2868 mSource |= AINPUT_SOURCE_STYLUS;
2869 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002870 } else {
2871 mSource = AINPUT_SOURCE_TOUCHPAD;
2872 mDeviceMode = DEVICE_MODE_UNSCALED;
2873 }
2874
Jeff Brown9626b142011-03-03 02:09:54 -08002875 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002876 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002877 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002878 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002879 mDeviceMode = DEVICE_MODE_DISABLED;
2880 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002881 }
2882
Jeff Brown65fd2512011-08-18 11:20:58 -07002883 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002884 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002885 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002886 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002887 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2888 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002889 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002890 "display %d. The device will be inoperable until the display size "
2891 "becomes available.",
2892 getDeviceName().string(), mParameters.associatedDisplayId);
2893 mDeviceMode = DEVICE_MODE_DISABLED;
2894 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002895 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002896 }
2897
Jeff Brown65fd2512011-08-18 11:20:58 -07002898 // Configure dimensions.
2899 int32_t width, height, orientation;
2900 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2901 width = mAssociatedDisplayWidth;
2902 height = mAssociatedDisplayHeight;
2903 orientation = mParameters.orientationAware ?
2904 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2905 } else {
2906 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2907 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2908 orientation = DISPLAY_ORIENTATION_0;
2909 }
2910
2911 // If moving between pointer modes, need to reset some state.
2912 bool deviceModeChanged;
2913 if (mDeviceMode != oldDeviceMode) {
2914 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002915 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002916 }
2917
Jeff Browndaf4a122011-08-26 17:14:14 -07002918 // Create pointer controller if needed.
2919 if (mDeviceMode == DEVICE_MODE_POINTER ||
2920 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2921 if (mPointerController == NULL) {
2922 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2923 }
2924 } else {
2925 mPointerController.clear();
2926 }
2927
Jeff Brownbe1aa822011-07-27 16:04:54 -07002928 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002929 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002930 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002931 }
2932
Jeff Brownbe1aa822011-07-27 16:04:54 -07002933 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002934 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002935 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002936 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002937
Jeff Brownbe1aa822011-07-27 16:04:54 -07002938 mSurfaceWidth = width;
2939 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002940
Jeff Brown8d608662010-08-30 03:02:23 -07002941 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002942 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2943 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2944 mXPrecision = 1.0f / mXScale;
2945 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002946
Jeff Brownbe1aa822011-07-27 16:04:54 -07002947 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002948 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002949 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002950 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002951
Jeff Brownbe1aa822011-07-27 16:04:54 -07002952 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002953
Jeff Brown8d608662010-08-30 03:02:23 -07002954 // Scale factor for terms that are not oriented in a particular axis.
2955 // If the pixels are square then xScale == yScale otherwise we fake it
2956 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002957 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002958
Jeff Brown8d608662010-08-30 03:02:23 -07002959 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002960 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002961
Jeff Browna1f89ce2011-08-11 00:05:01 -07002962 // Size factors.
2963 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2964 if (mRawPointerAxes.touchMajor.valid
2965 && mRawPointerAxes.touchMajor.maxValue != 0) {
2966 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2967 } else if (mRawPointerAxes.toolMajor.valid
2968 && mRawPointerAxes.toolMajor.maxValue != 0) {
2969 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2970 } else {
2971 mSizeScale = 0.0f;
2972 }
2973
Jeff Brownbe1aa822011-07-27 16:04:54 -07002974 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002975 mOrientedRanges.haveToolSize = true;
2976 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002977
Jeff Brownbe1aa822011-07-27 16:04:54 -07002978 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002979 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002980 mOrientedRanges.touchMajor.min = 0;
2981 mOrientedRanges.touchMajor.max = diagonalSize;
2982 mOrientedRanges.touchMajor.flat = 0;
2983 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002984
Jeff Brownbe1aa822011-07-27 16:04:54 -07002985 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2986 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002987
Jeff Brownbe1aa822011-07-27 16:04:54 -07002988 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002989 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002990 mOrientedRanges.toolMajor.min = 0;
2991 mOrientedRanges.toolMajor.max = diagonalSize;
2992 mOrientedRanges.toolMajor.flat = 0;
2993 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002994
Jeff Brownbe1aa822011-07-27 16:04:54 -07002995 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2996 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002997
2998 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002999 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003000 mOrientedRanges.size.min = 0;
3001 mOrientedRanges.size.max = 1.0;
3002 mOrientedRanges.size.flat = 0;
3003 mOrientedRanges.size.fuzz = 0;
3004 } else {
3005 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003006 }
3007
3008 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003009 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003010 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3011 || mCalibration.pressureCalibration
3012 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3013 if (mCalibration.havePressureScale) {
3014 mPressureScale = mCalibration.pressureScale;
3015 } else if (mRawPointerAxes.pressure.valid
3016 && mRawPointerAxes.pressure.maxValue != 0) {
3017 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003018 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003019 }
Jeff Brown8d608662010-08-30 03:02:23 -07003020
Jeff Brown65fd2512011-08-18 11:20:58 -07003021 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3022 mOrientedRanges.pressure.source = mSource;
3023 mOrientedRanges.pressure.min = 0;
3024 mOrientedRanges.pressure.max = 1.0;
3025 mOrientedRanges.pressure.flat = 0;
3026 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003027
Jeff Brown65fd2512011-08-18 11:20:58 -07003028 // Tilt
3029 mTiltXCenter = 0;
3030 mTiltXScale = 0;
3031 mTiltYCenter = 0;
3032 mTiltYScale = 0;
3033 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3034 if (mHaveTilt) {
3035 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3036 mRawPointerAxes.tiltX.maxValue);
3037 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3038 mRawPointerAxes.tiltY.maxValue);
3039 mTiltXScale = M_PI / 180;
3040 mTiltYScale = M_PI / 180;
3041
3042 mOrientedRanges.haveTilt = true;
3043
3044 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3045 mOrientedRanges.tilt.source = mSource;
3046 mOrientedRanges.tilt.min = 0;
3047 mOrientedRanges.tilt.max = M_PI_2;
3048 mOrientedRanges.tilt.flat = 0;
3049 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003050 }
3051
Jeff Brown8d608662010-08-30 03:02:23 -07003052 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07003053 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003054 if (mHaveTilt) {
3055 mOrientedRanges.haveOrientation = true;
3056
3057 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3058 mOrientedRanges.orientation.source = mSource;
3059 mOrientedRanges.orientation.min = -M_PI;
3060 mOrientedRanges.orientation.max = M_PI;
3061 mOrientedRanges.orientation.flat = 0;
3062 mOrientedRanges.orientation.fuzz = 0;
3063 } else if (mCalibration.orientationCalibration !=
3064 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003065 if (mCalibration.orientationCalibration
3066 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003067 if (mRawPointerAxes.orientation.valid) {
Jeff Brown037f7272012-06-25 17:31:23 -07003068 if (mRawPointerAxes.orientation.maxValue > 0) {
3069 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
3070 } else if (mRawPointerAxes.orientation.minValue < 0) {
3071 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
3072 } else {
3073 mOrientationScale = 0;
3074 }
Jeff Brown8d608662010-08-30 03:02:23 -07003075 }
3076 }
3077
Jeff Brownbe1aa822011-07-27 16:04:54 -07003078 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003079
Jeff Brownbe1aa822011-07-27 16:04:54 -07003080 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003081 mOrientedRanges.orientation.source = mSource;
3082 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003083 mOrientedRanges.orientation.max = M_PI_2;
3084 mOrientedRanges.orientation.flat = 0;
3085 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003086 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003087
3088 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003089 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003090 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3091 if (mCalibration.distanceCalibration
3092 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3093 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003094 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003095 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003096 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003097 }
3098 }
3099
Jeff Brownbe1aa822011-07-27 16:04:54 -07003100 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003101
Jeff Brownbe1aa822011-07-27 16:04:54 -07003102 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003103 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003104 mOrientedRanges.distance.min =
3105 mRawPointerAxes.distance.minValue * mDistanceScale;
3106 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003107 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003108 mOrientedRanges.distance.flat = 0;
3109 mOrientedRanges.distance.fuzz =
3110 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003111 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003112 }
3113
Jeff Brown65fd2512011-08-18 11:20:58 -07003114 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08003115 // Compute oriented surface dimensions, precision, scales and ranges.
3116 // Note that the maximum value reported is an inclusive maximum value so it is one
3117 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003118 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003119 case DISPLAY_ORIENTATION_90:
3120 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003121 mOrientedSurfaceWidth = mSurfaceHeight;
3122 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08003123
Jeff Brownbe1aa822011-07-27 16:04:54 -07003124 mOrientedXPrecision = mYPrecision;
3125 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003126
Jeff Brownbe1aa822011-07-27 16:04:54 -07003127 mOrientedRanges.x.min = 0;
3128 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3129 * mYScale;
3130 mOrientedRanges.x.flat = 0;
3131 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003132
Jeff Brownbe1aa822011-07-27 16:04:54 -07003133 mOrientedRanges.y.min = 0;
3134 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3135 * mXScale;
3136 mOrientedRanges.y.flat = 0;
3137 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003138 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003139
Jeff Brown6d0fec22010-07-23 21:28:06 -07003140 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003141 mOrientedSurfaceWidth = mSurfaceWidth;
3142 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08003143
Jeff Brownbe1aa822011-07-27 16:04:54 -07003144 mOrientedXPrecision = mXPrecision;
3145 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003146
Jeff Brownbe1aa822011-07-27 16:04:54 -07003147 mOrientedRanges.x.min = 0;
3148 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3149 * mXScale;
3150 mOrientedRanges.x.flat = 0;
3151 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003152
Jeff Brownbe1aa822011-07-27 16:04:54 -07003153 mOrientedRanges.y.min = 0;
3154 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3155 * mYScale;
3156 mOrientedRanges.y.flat = 0;
3157 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003158 break;
3159 }
Jeff Brownace13b12011-03-09 17:39:48 -08003160
3161 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003162 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003163 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3164 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07003165 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003166 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
3167 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003168
Jeff Brown2352b972011-04-12 22:39:53 -07003169 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003170 // given area relative to the diagonal size of the display when no acceleration
3171 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003172 // Assume that the touch pad has a square aspect ratio such that movements in
3173 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003174 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003175 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003176 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003177
3178 // Scale zooms to cover a smaller range of the display than movements do.
3179 // This value determines the area around the pointer that is affected by freeform
3180 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003181 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003182 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003183 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003184
Jeff Brown2352b972011-04-12 22:39:53 -07003185 // Max width between pointers to detect a swipe gesture is more than some fraction
3186 // of the diagonal axis of the touch pad. Touches that are wider than this are
3187 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003188 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003189 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003190 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003191
Jeff Brown65fd2512011-08-18 11:20:58 -07003192 // Abort current pointer usages because the state has changed.
3193 abortPointerUsage(when, 0 /*policyFlags*/);
3194
3195 // Inform the dispatcher about the changes.
3196 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003197 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003198 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003199}
3200
Jeff Brownbe1aa822011-07-27 16:04:54 -07003201void TouchInputMapper::dumpSurface(String8& dump) {
3202 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3203 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
3204 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003205}
3206
Jeff Brownbe1aa822011-07-27 16:04:54 -07003207void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003208 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003209 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003210
Jeff Brownbe1aa822011-07-27 16:04:54 -07003211 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003212
Jeff Brown6328cdc2010-07-29 18:18:33 -07003213 if (virtualKeyDefinitions.size() == 0) {
3214 return;
3215 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003216
Jeff Brownbe1aa822011-07-27 16:04:54 -07003217 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003218
Jeff Brownbe1aa822011-07-27 16:04:54 -07003219 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3220 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3221 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3222 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003223
3224 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003225 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003226 virtualKeyDefinitions[i];
3227
Jeff Brownbe1aa822011-07-27 16:04:54 -07003228 mVirtualKeys.add();
3229 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003230
3231 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3232 int32_t keyCode;
3233 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003234 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003235 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003236 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003237 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003238 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003239 }
3240
Jeff Brown6328cdc2010-07-29 18:18:33 -07003241 virtualKey.keyCode = keyCode;
3242 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003243
Jeff Brown6328cdc2010-07-29 18:18:33 -07003244 // convert the key definition's display coordinates into touch coordinates for a hit box
3245 int32_t halfWidth = virtualKeyDefinition.width / 2;
3246 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003247
Jeff Brown6328cdc2010-07-29 18:18:33 -07003248 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003249 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003250 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003251 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003252 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003253 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003254 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003255 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003256 }
3257}
3258
Jeff Brownbe1aa822011-07-27 16:04:54 -07003259void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3260 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003261 dump.append(INDENT3 "Virtual Keys:\n");
3262
Jeff Brownbe1aa822011-07-27 16:04:54 -07003263 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3264 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003265 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3266 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3267 i, virtualKey.scanCode, virtualKey.keyCode,
3268 virtualKey.hitLeft, virtualKey.hitRight,
3269 virtualKey.hitTop, virtualKey.hitBottom);
3270 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003271 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003272}
3273
Jeff Brown8d608662010-08-30 03:02:23 -07003274void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003275 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003276 Calibration& out = mCalibration;
3277
Jeff Browna1f89ce2011-08-11 00:05:01 -07003278 // Size
3279 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3280 String8 sizeCalibrationString;
3281 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3282 if (sizeCalibrationString == "none") {
3283 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3284 } else if (sizeCalibrationString == "geometric") {
3285 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3286 } else if (sizeCalibrationString == "diameter") {
3287 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
Jeff Brown037f7272012-06-25 17:31:23 -07003288 } else if (sizeCalibrationString == "box") {
3289 out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003290 } else if (sizeCalibrationString == "area") {
3291 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3292 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003293 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003294 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003295 }
3296 }
3297
Jeff Browna1f89ce2011-08-11 00:05:01 -07003298 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3299 out.sizeScale);
3300 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3301 out.sizeBias);
3302 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3303 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003304
3305 // Pressure
3306 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3307 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003308 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003309 if (pressureCalibrationString == "none") {
3310 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3311 } else if (pressureCalibrationString == "physical") {
3312 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3313 } else if (pressureCalibrationString == "amplitude") {
3314 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3315 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003316 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003317 pressureCalibrationString.string());
3318 }
3319 }
3320
Jeff Brown8d608662010-08-30 03:02:23 -07003321 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3322 out.pressureScale);
3323
Jeff Brown8d608662010-08-30 03:02:23 -07003324 // Orientation
3325 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3326 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003327 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003328 if (orientationCalibrationString == "none") {
3329 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3330 } else if (orientationCalibrationString == "interpolated") {
3331 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003332 } else if (orientationCalibrationString == "vector") {
3333 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003334 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003335 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003336 orientationCalibrationString.string());
3337 }
3338 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003339
3340 // Distance
3341 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3342 String8 distanceCalibrationString;
3343 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3344 if (distanceCalibrationString == "none") {
3345 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3346 } else if (distanceCalibrationString == "scaled") {
3347 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3348 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003349 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003350 distanceCalibrationString.string());
3351 }
3352 }
3353
3354 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3355 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003356}
3357
3358void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003359 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003360 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3361 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3362 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003363 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003364 } else {
3365 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3366 }
Jeff Brown8d608662010-08-30 03:02:23 -07003367
Jeff Browna1f89ce2011-08-11 00:05:01 -07003368 // Pressure
3369 if (mRawPointerAxes.pressure.valid) {
3370 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3371 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3372 }
3373 } else {
3374 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003375 }
3376
3377 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003378 if (mRawPointerAxes.orientation.valid) {
3379 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003380 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003381 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003382 } else {
3383 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003384 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003385
3386 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003387 if (mRawPointerAxes.distance.valid) {
3388 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003389 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003390 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003391 } else {
3392 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003393 }
Jeff Brown8d608662010-08-30 03:02:23 -07003394}
3395
Jeff Brownef3d7e82010-09-30 14:33:04 -07003396void TouchInputMapper::dumpCalibration(String8& dump) {
3397 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003398
Jeff Browna1f89ce2011-08-11 00:05:01 -07003399 // Size
3400 switch (mCalibration.sizeCalibration) {
3401 case Calibration::SIZE_CALIBRATION_NONE:
3402 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003403 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003404 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3405 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003406 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003407 case Calibration::SIZE_CALIBRATION_DIAMETER:
3408 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3409 break;
Jeff Brown037f7272012-06-25 17:31:23 -07003410 case Calibration::SIZE_CALIBRATION_BOX:
3411 dump.append(INDENT4 "touch.size.calibration: box\n");
3412 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003413 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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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:
Jeff Brown037f7272012-06-25 17:31:23 -07003963 case Calibration::SIZE_CALIBRATION_BOX:
Jeff Browna1f89ce2011-08-11 00:05:01 -07003964 case Calibration::SIZE_CALIBRATION_AREA:
3965 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3966 touchMajor = in.touchMajor;
3967 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3968 toolMajor = in.toolMajor;
3969 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3970 size = mRawPointerAxes.touchMinor.valid
3971 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3972 } else if (mRawPointerAxes.touchMajor.valid) {
3973 toolMajor = touchMajor = in.touchMajor;
3974 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3975 ? in.touchMinor : in.touchMajor;
3976 size = mRawPointerAxes.touchMinor.valid
3977 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3978 } else if (mRawPointerAxes.toolMajor.valid) {
3979 touchMajor = toolMajor = in.toolMajor;
3980 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3981 ? in.toolMinor : in.toolMajor;
3982 size = mRawPointerAxes.toolMinor.valid
3983 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003984 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003985 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003986 "Size calibration should have been resolved to NONE.");
3987 touchMajor = 0;
3988 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003989 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003990 toolMinor = 0;
3991 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003992 }
Jeff Brownace13b12011-03-09 17:39:48 -08003993
Jeff Browna1f89ce2011-08-11 00:05:01 -07003994 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3995 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3996 if (touchingCount > 1) {
3997 touchMajor /= touchingCount;
3998 touchMinor /= touchingCount;
3999 toolMajor /= touchingCount;
4000 toolMinor /= touchingCount;
4001 size /= touchingCount;
4002 }
4003 }
Jeff Brownace13b12011-03-09 17:39:48 -08004004
Jeff Browna1f89ce2011-08-11 00:05:01 -07004005 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4006 touchMajor *= mGeometricScale;
4007 touchMinor *= mGeometricScale;
4008 toolMajor *= mGeometricScale;
4009 toolMinor *= mGeometricScale;
4010 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4011 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004012 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004013 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4014 toolMinor = toolMajor;
4015 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4016 touchMinor = touchMajor;
4017 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004018 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004019
4020 mCalibration.applySizeScaleAndBias(&touchMajor);
4021 mCalibration.applySizeScaleAndBias(&touchMinor);
4022 mCalibration.applySizeScaleAndBias(&toolMajor);
4023 mCalibration.applySizeScaleAndBias(&toolMinor);
4024 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004025 break;
4026 default:
4027 touchMajor = 0;
4028 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004029 toolMajor = 0;
4030 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004031 size = 0;
4032 break;
4033 }
4034
Jeff Browna1f89ce2011-08-11 00:05:01 -07004035 // Pressure
4036 float pressure;
4037 switch (mCalibration.pressureCalibration) {
4038 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4039 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4040 pressure = in.pressure * mPressureScale;
4041 break;
4042 default:
4043 pressure = in.isHovering ? 0 : 1;
4044 break;
4045 }
4046
Jeff Brown65fd2512011-08-18 11:20:58 -07004047 // Tilt and Orientation
4048 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004049 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004050 if (mHaveTilt) {
4051 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4052 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4053 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4054 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4055 } else {
4056 tilt = 0;
4057
4058 switch (mCalibration.orientationCalibration) {
4059 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown037f7272012-06-25 17:31:23 -07004060 orientation = in.orientation * mOrientationScale;
Jeff Brown65fd2512011-08-18 11:20:58 -07004061 break;
4062 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4063 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4064 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4065 if (c1 != 0 || c2 != 0) {
4066 orientation = atan2f(c1, c2) * 0.5f;
4067 float confidence = hypotf(c1, c2);
4068 float scale = 1.0f + confidence / 16.0f;
4069 touchMajor *= scale;
4070 touchMinor /= scale;
4071 toolMajor *= scale;
4072 toolMinor /= scale;
4073 } else {
4074 orientation = 0;
4075 }
4076 break;
4077 }
4078 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004079 orientation = 0;
4080 }
Jeff Brownace13b12011-03-09 17:39:48 -08004081 }
4082
Jeff Brown80fd47c2011-05-24 01:07:44 -07004083 // Distance
4084 float distance;
4085 switch (mCalibration.distanceCalibration) {
4086 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004087 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004088 break;
4089 default:
4090 distance = 0;
4091 }
4092
Jeff Brownace13b12011-03-09 17:39:48 -08004093 // X and Y
4094 // Adjust coords for surface orientation.
4095 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004096 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004097 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004098 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
4099 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004100 orientation -= M_PI_2;
4101 if (orientation < - M_PI_2) {
4102 orientation += M_PI;
4103 }
4104 break;
4105 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004106 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
4107 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004108 break;
4109 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004110 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
4111 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004112 orientation += M_PI_2;
4113 if (orientation > M_PI_2) {
4114 orientation -= M_PI;
4115 }
4116 break;
4117 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004118 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
4119 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004120 break;
4121 }
4122
4123 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004124 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004125 out.clear();
4126 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4127 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4128 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4129 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4130 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4131 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4132 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4133 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4134 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004135 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004136 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004137
4138 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004139 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4140 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004141 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004142 properties.id = id;
4143 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004144
Jeff Brownbe1aa822011-07-27 16:04:54 -07004145 // Write id index.
4146 mCurrentCookedPointerData.idToIndex[id] = i;
4147 }
Jeff Brownace13b12011-03-09 17:39:48 -08004148}
4149
Jeff Brown65fd2512011-08-18 11:20:58 -07004150void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4151 PointerUsage pointerUsage) {
4152 if (pointerUsage != mPointerUsage) {
4153 abortPointerUsage(when, policyFlags);
4154 mPointerUsage = pointerUsage;
4155 }
4156
4157 switch (mPointerUsage) {
4158 case POINTER_USAGE_GESTURES:
4159 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4160 break;
4161 case POINTER_USAGE_STYLUS:
4162 dispatchPointerStylus(when, policyFlags);
4163 break;
4164 case POINTER_USAGE_MOUSE:
4165 dispatchPointerMouse(when, policyFlags);
4166 break;
4167 default:
4168 break;
4169 }
4170}
4171
4172void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4173 switch (mPointerUsage) {
4174 case POINTER_USAGE_GESTURES:
4175 abortPointerGestures(when, policyFlags);
4176 break;
4177 case POINTER_USAGE_STYLUS:
4178 abortPointerStylus(when, policyFlags);
4179 break;
4180 case POINTER_USAGE_MOUSE:
4181 abortPointerMouse(when, policyFlags);
4182 break;
4183 default:
4184 break;
4185 }
4186
4187 mPointerUsage = POINTER_USAGE_NONE;
4188}
4189
Jeff Brown79ac9692011-04-19 21:20:10 -07004190void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4191 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004192 // Update current gesture coordinates.
4193 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004194 bool sendEvents = preparePointerGestures(when,
4195 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4196 if (!sendEvents) {
4197 return;
4198 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004199 if (finishPreviousGesture) {
4200 cancelPreviousGesture = false;
4201 }
Jeff Brownace13b12011-03-09 17:39:48 -08004202
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004203 // Update the pointer presentation and spots.
4204 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4205 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4206 if (finishPreviousGesture || cancelPreviousGesture) {
4207 mPointerController->clearSpots();
4208 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004209 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4210 mPointerGesture.currentGestureIdToIndex,
4211 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004212 } else {
4213 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4214 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004215
Jeff Brown538881e2011-05-25 18:23:38 -07004216 // Show or hide the pointer if needed.
4217 switch (mPointerGesture.currentGestureMode) {
4218 case PointerGesture::NEUTRAL:
4219 case PointerGesture::QUIET:
4220 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4221 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4222 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4223 // Remind the user of where the pointer is after finishing a gesture with spots.
4224 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4225 }
4226 break;
4227 case PointerGesture::TAP:
4228 case PointerGesture::TAP_DRAG:
4229 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4230 case PointerGesture::HOVER:
4231 case PointerGesture::PRESS:
4232 // Unfade the pointer when the current gesture manipulates the
4233 // area directly under the pointer.
4234 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4235 break;
4236 case PointerGesture::SWIPE:
4237 case PointerGesture::FREEFORM:
4238 // Fade the pointer when the current gesture manipulates a different
4239 // area and there are spots to guide the user experience.
4240 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4241 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4242 } else {
4243 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4244 }
4245 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004246 }
4247
Jeff Brownace13b12011-03-09 17:39:48 -08004248 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004249 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004250 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004251
4252 // Update last coordinates of pointers that have moved so that we observe the new
4253 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004254 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4255 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4256 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004257 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004258 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4259 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4260 bool moveNeeded = false;
4261 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004262 && !mPointerGesture.lastGestureIdBits.isEmpty()
4263 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004264 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4265 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004266 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004267 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004268 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004269 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4270 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004271 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004272 moveNeeded = true;
4273 }
Jeff Brownace13b12011-03-09 17:39:48 -08004274 }
4275
4276 // Send motion events for all pointers that went up or were canceled.
4277 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4278 if (!dispatchedGestureIdBits.isEmpty()) {
4279 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004280 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004281 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4282 AMOTION_EVENT_EDGE_FLAG_NONE,
4283 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004284 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4285 dispatchedGestureIdBits, -1,
4286 0, 0, mPointerGesture.downTime);
4287
4288 dispatchedGestureIdBits.clear();
4289 } else {
4290 BitSet32 upGestureIdBits;
4291 if (finishPreviousGesture) {
4292 upGestureIdBits = dispatchedGestureIdBits;
4293 } else {
4294 upGestureIdBits.value = dispatchedGestureIdBits.value
4295 & ~mPointerGesture.currentGestureIdBits.value;
4296 }
4297 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004298 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004299
Jeff Brown65fd2512011-08-18 11:20:58 -07004300 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004301 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004302 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4303 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004304 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4305 dispatchedGestureIdBits, id,
4306 0, 0, mPointerGesture.downTime);
4307
4308 dispatchedGestureIdBits.clearBit(id);
4309 }
4310 }
4311 }
4312
4313 // Send motion events for all pointers that moved.
4314 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004315 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004316 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4317 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004318 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4319 dispatchedGestureIdBits, -1,
4320 0, 0, mPointerGesture.downTime);
4321 }
4322
4323 // Send motion events for all pointers that went down.
4324 if (down) {
4325 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4326 & ~dispatchedGestureIdBits.value);
4327 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004328 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004329 dispatchedGestureIdBits.markBit(id);
4330
Jeff Brownace13b12011-03-09 17:39:48 -08004331 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004332 mPointerGesture.downTime = when;
4333 }
4334
Jeff Brown65fd2512011-08-18 11:20:58 -07004335 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004336 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004337 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004338 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4339 dispatchedGestureIdBits, id,
4340 0, 0, mPointerGesture.downTime);
4341 }
4342 }
4343
Jeff Brownace13b12011-03-09 17:39:48 -08004344 // Send motion events for hover.
4345 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004346 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004347 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4348 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4349 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004350 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4351 mPointerGesture.currentGestureIdBits, -1,
4352 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004353 } else if (dispatchedGestureIdBits.isEmpty()
4354 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4355 // Synthesize a hover move event after all pointers go up to indicate that
4356 // the pointer is hovering again even if the user is not currently touching
4357 // the touch pad. This ensures that a view will receive a fresh hover enter
4358 // event after a tap.
4359 float x, y;
4360 mPointerController->getPosition(&x, &y);
4361
4362 PointerProperties pointerProperties;
4363 pointerProperties.clear();
4364 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004365 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004366
4367 PointerCoords pointerCoords;
4368 pointerCoords.clear();
4369 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4370 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4371
Jeff Brown65fd2512011-08-18 11:20:58 -07004372 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004373 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4374 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4375 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004376 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004377 }
4378
4379 // Update state.
4380 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4381 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004382 mPointerGesture.lastGestureIdBits.clear();
4383 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004384 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4385 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004386 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004387 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004388 mPointerGesture.lastGestureProperties[index].copyFrom(
4389 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004390 mPointerGesture.lastGestureCoords[index].copyFrom(
4391 mPointerGesture.currentGestureCoords[index]);
4392 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004393 }
4394 }
4395}
4396
Jeff Brown65fd2512011-08-18 11:20:58 -07004397void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4398 // Cancel previously dispatches pointers.
4399 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4400 int32_t metaState = getContext()->getGlobalMetaState();
4401 int32_t buttonState = mCurrentButtonState;
4402 dispatchMotion(when, policyFlags, mSource,
4403 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4404 AMOTION_EVENT_EDGE_FLAG_NONE,
4405 mPointerGesture.lastGestureProperties,
4406 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4407 mPointerGesture.lastGestureIdBits, -1,
4408 0, 0, mPointerGesture.downTime);
4409 }
4410
4411 // Reset the current pointer gesture.
4412 mPointerGesture.reset();
4413 mPointerVelocityControl.reset();
4414
4415 // Remove any current spots.
4416 if (mPointerController != NULL) {
4417 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4418 mPointerController->clearSpots();
4419 }
4420}
4421
Jeff Brown79ac9692011-04-19 21:20:10 -07004422bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4423 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004424 *outCancelPreviousGesture = false;
4425 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004426
Jeff Brown79ac9692011-04-19 21:20:10 -07004427 // Handle TAP timeout.
4428 if (isTimeout) {
4429#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004430 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004431#endif
4432
4433 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004434 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004435 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004436 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004437 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004438 } else {
4439 // The tap is finished.
4440#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004441 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004442#endif
4443 *outFinishPreviousGesture = true;
4444
4445 mPointerGesture.activeGestureId = -1;
4446 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4447 mPointerGesture.currentGestureIdBits.clear();
4448
Jeff Brown65fd2512011-08-18 11:20:58 -07004449 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004450 return true;
4451 }
4452 }
4453
4454 // We did not handle this timeout.
4455 return false;
4456 }
4457
Jeff Brown65fd2512011-08-18 11:20:58 -07004458 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4459 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4460
Jeff Brownace13b12011-03-09 17:39:48 -08004461 // Update the velocity tracker.
4462 {
4463 VelocityTracker::Position positions[MAX_POINTERS];
4464 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004465 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004466 uint32_t id = idBits.clearFirstMarkedBit();
4467 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004468 positions[count].x = pointer.x * mPointerXMovementScale;
4469 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004470 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004471 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004472 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004473 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004474
Jeff Brownace13b12011-03-09 17:39:48 -08004475 // Pick a new active touch id if needed.
4476 // Choose an arbitrary pointer that just went down, if there is one.
4477 // Otherwise choose an arbitrary remaining pointer.
4478 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004479 // We keep the same active touch id for as long as possible.
4480 bool activeTouchChanged = false;
4481 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4482 int32_t activeTouchId = lastActiveTouchId;
4483 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004484 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004485 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004486 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004487 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004488 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004489 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004490 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004491 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004492 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004493 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004494 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004495 } else {
4496 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004497 }
4498 }
4499
4500 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004501 bool isQuietTime = false;
4502 if (activeTouchId < 0) {
4503 mPointerGesture.resetQuietTime();
4504 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004505 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004506 if (!isQuietTime) {
4507 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4508 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4509 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004510 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004511 // Enter quiet time when exiting swipe or freeform state.
4512 // This is to prevent accidentally entering the hover state and flinging the
4513 // pointer when finishing a swipe and there is still one pointer left onscreen.
4514 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004515 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004516 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004517 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004518 // Enter quiet time when releasing the button and there are still two or more
4519 // fingers down. This may indicate that one finger was used to press the button
4520 // but it has not gone up yet.
4521 isQuietTime = true;
4522 }
4523 if (isQuietTime) {
4524 mPointerGesture.quietTime = when;
4525 }
Jeff Brownace13b12011-03-09 17:39:48 -08004526 }
4527 }
4528
4529 // Switch states based on button and pointer state.
4530 if (isQuietTime) {
4531 // Case 1: Quiet time. (QUIET)
4532#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004533 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004534 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004535#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004536 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4537 *outFinishPreviousGesture = true;
4538 }
Jeff Brownace13b12011-03-09 17:39:48 -08004539
4540 mPointerGesture.activeGestureId = -1;
4541 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004542 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004543
Jeff Brown65fd2512011-08-18 11:20:58 -07004544 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004545 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004546 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004547 // The pointer follows the active touch point.
4548 // Emit DOWN, MOVE, UP events at the pointer location.
4549 //
4550 // Only the active touch matters; other fingers are ignored. This policy helps
4551 // to handle the case where the user places a second finger on the touch pad
4552 // to apply the necessary force to depress an integrated button below the surface.
4553 // We don't want the second finger to be delivered to applications.
4554 //
4555 // For this to work well, we need to make sure to track the pointer that is really
4556 // active. If the user first puts one finger down to click then adds another
4557 // finger to drag then the active pointer should switch to the finger that is
4558 // being dragged.
4559#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004560 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004561 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004562#endif
4563 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004564 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004565 *outFinishPreviousGesture = true;
4566 mPointerGesture.activeGestureId = 0;
4567 }
4568
4569 // Switch pointers if needed.
4570 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004571 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004572 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004573 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004574 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004575 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004576 float vx, vy;
4577 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4578 float speed = hypotf(vx, vy);
4579 if (speed > bestSpeed) {
4580 bestId = id;
4581 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004582 }
Jeff Brown8d608662010-08-30 03:02:23 -07004583 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004584 }
4585 if (bestId >= 0 && bestId != activeTouchId) {
4586 mPointerGesture.activeTouchId = activeTouchId = bestId;
4587 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004588#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004589 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004590 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004591#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004592 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004593 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004594
Jeff Brown65fd2512011-08-18 11:20:58 -07004595 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004596 const RawPointerData::Pointer& currentPointer =
4597 mCurrentRawPointerData.pointerForId(activeTouchId);
4598 const RawPointerData::Pointer& lastPointer =
4599 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004600 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4601 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004602
Jeff Brownbe1aa822011-07-27 16:04:54 -07004603 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004604 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004605
4606 // Move the pointer using a relative motion.
4607 // When using spots, the click will occur at the position of the anchor
4608 // spot and all other spots will move there.
4609 mPointerController->move(deltaX, deltaY);
4610 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004611 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004612 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004613
Jeff Brownace13b12011-03-09 17:39:48 -08004614 float x, y;
4615 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004616
Jeff Brown79ac9692011-04-19 21:20:10 -07004617 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004618 mPointerGesture.currentGestureIdBits.clear();
4619 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4620 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004621 mPointerGesture.currentGestureProperties[0].clear();
4622 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004623 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004624 mPointerGesture.currentGestureCoords[0].clear();
4625 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4626 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4627 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004628 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004629 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004630 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4631 *outFinishPreviousGesture = true;
4632 }
Jeff Brownace13b12011-03-09 17:39:48 -08004633
Jeff Brown79ac9692011-04-19 21:20:10 -07004634 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004635 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004636 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004637 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4638 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004639 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004640 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004641 float x, y;
4642 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004643 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4644 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004645#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004646 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004647#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004648
4649 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004650 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004651 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004652
Jeff Brownace13b12011-03-09 17:39:48 -08004653 mPointerGesture.activeGestureId = 0;
4654 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004655 mPointerGesture.currentGestureIdBits.clear();
4656 mPointerGesture.currentGestureIdBits.markBit(
4657 mPointerGesture.activeGestureId);
4658 mPointerGesture.currentGestureIdToIndex[
4659 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004660 mPointerGesture.currentGestureProperties[0].clear();
4661 mPointerGesture.currentGestureProperties[0].id =
4662 mPointerGesture.activeGestureId;
4663 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004664 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004665 mPointerGesture.currentGestureCoords[0].clear();
4666 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004667 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004668 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004669 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004670 mPointerGesture.currentGestureCoords[0].setAxisValue(
4671 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004672
Jeff Brownace13b12011-03-09 17:39:48 -08004673 tapped = true;
4674 } else {
4675#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004676 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004677 x - mPointerGesture.tapX,
4678 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004679#endif
4680 }
4681 } else {
4682#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004683 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004684 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004685#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004686 }
Jeff Brownace13b12011-03-09 17:39:48 -08004687 }
Jeff Brown2352b972011-04-12 22:39:53 -07004688
Jeff Brown65fd2512011-08-18 11:20:58 -07004689 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004690
Jeff Brownace13b12011-03-09 17:39:48 -08004691 if (!tapped) {
4692#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004693 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004694#endif
4695 mPointerGesture.activeGestureId = -1;
4696 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004697 mPointerGesture.currentGestureIdBits.clear();
4698 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004699 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004700 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004701 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004702 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4703 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004704 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004705
Jeff Brown79ac9692011-04-19 21:20:10 -07004706 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4707 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004708 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004709 float x, y;
4710 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004711 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4712 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004713 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4714 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004715#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004716 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004717 x - mPointerGesture.tapX,
4718 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004719#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004720 }
4721 } else {
4722#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004723 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004724 (when - mPointerGesture.tapUpTime) * 0.000001f);
4725#endif
4726 }
4727 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4728 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4729 }
Jeff Brownace13b12011-03-09 17:39:48 -08004730
Jeff Brown65fd2512011-08-18 11:20:58 -07004731 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004732 const RawPointerData::Pointer& currentPointer =
4733 mCurrentRawPointerData.pointerForId(activeTouchId);
4734 const RawPointerData::Pointer& lastPointer =
4735 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004736 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004737 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004738 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004739 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004740
Jeff Brownbe1aa822011-07-27 16:04:54 -07004741 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004742 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004743
Jeff Brown2352b972011-04-12 22:39:53 -07004744 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004745 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004746 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004747 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004748 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004749 }
4750
Jeff Brown79ac9692011-04-19 21:20:10 -07004751 bool down;
4752 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4753#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004754 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004755#endif
4756 down = true;
4757 } else {
4758#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004759 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004760#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004761 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4762 *outFinishPreviousGesture = true;
4763 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004764 mPointerGesture.activeGestureId = 0;
4765 down = false;
4766 }
Jeff Brownace13b12011-03-09 17:39:48 -08004767
4768 float x, y;
4769 mPointerController->getPosition(&x, &y);
4770
Jeff Brownace13b12011-03-09 17:39:48 -08004771 mPointerGesture.currentGestureIdBits.clear();
4772 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4773 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004774 mPointerGesture.currentGestureProperties[0].clear();
4775 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4776 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004777 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004778 mPointerGesture.currentGestureCoords[0].clear();
4779 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4780 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004781 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4782 down ? 1.0f : 0.0f);
4783
Jeff Brown65fd2512011-08-18 11:20:58 -07004784 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004785 mPointerGesture.resetTap();
4786 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004787 mPointerGesture.tapX = x;
4788 mPointerGesture.tapY = y;
4789 }
Jeff Brownace13b12011-03-09 17:39:48 -08004790 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004791 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4792 // We need to provide feedback for each finger that goes down so we cannot wait
4793 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004794 //
Jeff Brown2352b972011-04-12 22:39:53 -07004795 // The ambiguous case is deciding what to do when there are two fingers down but they
4796 // have not moved enough to determine whether they are part of a drag or part of a
4797 // freeform gesture, or just a press or long-press at the pointer location.
4798 //
4799 // When there are two fingers we start with the PRESS hypothesis and we generate a
4800 // down at the pointer location.
4801 //
4802 // When the two fingers move enough or when additional fingers are added, we make
4803 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004804 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004805
Jeff Brown214eaf42011-05-26 19:17:02 -07004806 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004807 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004808 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004809 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4810 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004811 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004812 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004813 // Additional pointers have gone down but not yet settled.
4814 // Reset the gesture.
4815#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004816 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004817 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004818 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004819 * 0.000001f);
4820#endif
4821 *outCancelPreviousGesture = true;
4822 } else {
4823 // Continue previous gesture.
4824 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4825 }
4826
4827 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004828 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4829 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004830 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004831 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004832
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004833 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004834#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004835 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004836 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004837 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004838 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004839#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004840 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4841 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004842 &mPointerGesture.referenceTouchY);
4843 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4844 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004845 }
Jeff Brownace13b12011-03-09 17:39:48 -08004846
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004847 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004848 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004849 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4850 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004851 mPointerGesture.referenceDeltas[id].dx = 0;
4852 mPointerGesture.referenceDeltas[id].dy = 0;
4853 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004854 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004855
4856 // Add delta for all fingers and calculate a common movement delta.
4857 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004858 BitSet32 commonIdBits(mLastFingerIdBits.value
4859 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004860 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4861 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004862 uint32_t id = idBits.clearFirstMarkedBit();
4863 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4864 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004865 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4866 delta.dx += cpd.x - lpd.x;
4867 delta.dy += cpd.y - lpd.y;
4868
4869 if (first) {
4870 commonDeltaX = delta.dx;
4871 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004872 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004873 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4874 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4875 }
4876 }
Jeff Brownace13b12011-03-09 17:39:48 -08004877
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004878 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4879 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4880 float dist[MAX_POINTER_ID + 1];
4881 int32_t distOverThreshold = 0;
4882 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004883 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004884 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004885 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4886 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004887 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004888 distOverThreshold += 1;
4889 }
4890 }
4891
4892 // Only transition when at least two pointers have moved further than
4893 // the minimum distance threshold.
4894 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004895 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004896 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004897#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004898 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004899 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004900#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004901 *outCancelPreviousGesture = true;
4902 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4903 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004904 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004905 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004906 uint32_t id1 = idBits.clearFirstMarkedBit();
4907 uint32_t id2 = idBits.firstMarkedBit();
4908 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4909 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4910 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4911 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4912 // There are two pointers but they are too far apart for a SWIPE,
4913 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004914#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004915 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004916 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004917#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004918 *outCancelPreviousGesture = true;
4919 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4920 } else {
4921 // There are two pointers. Wait for both pointers to start moving
4922 // before deciding whether this is a SWIPE or FREEFORM gesture.
4923 float dist1 = dist[id1];
4924 float dist2 = dist[id2];
4925 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4926 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4927 // Calculate the dot product of the displacement vectors.
4928 // When the vectors are oriented in approximately the same direction,
4929 // the angle betweeen them is near zero and the cosine of the angle
4930 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4931 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4932 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004933 float dx1 = delta1.dx * mPointerXZoomScale;
4934 float dy1 = delta1.dy * mPointerYZoomScale;
4935 float dx2 = delta2.dx * mPointerXZoomScale;
4936 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004937 float dot = dx1 * dx2 + dy1 * dy2;
4938 float cosine = dot / (dist1 * dist2); // denominator always > 0
4939 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4940 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004941#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004942 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004943 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4944 "cosine %0.3f >= %0.3f",
4945 dist1, mConfig.pointerGestureMultitouchMinDistance,
4946 dist2, mConfig.pointerGestureMultitouchMinDistance,
4947 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004948#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004949 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4950 } else {
4951 // Pointers are moving in different directions. Switch to FREEFORM.
4952#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004953 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004954 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4955 "cosine %0.3f < %0.3f",
4956 dist1, mConfig.pointerGestureMultitouchMinDistance,
4957 dist2, mConfig.pointerGestureMultitouchMinDistance,
4958 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4959#endif
4960 *outCancelPreviousGesture = true;
4961 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4962 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004963 }
Jeff Brownace13b12011-03-09 17:39:48 -08004964 }
4965 }
Jeff Brownace13b12011-03-09 17:39:48 -08004966 }
4967 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004968 // Switch from SWIPE to FREEFORM if additional pointers go down.
4969 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004970 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004971#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004972 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004973 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004974#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004975 *outCancelPreviousGesture = true;
4976 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004977 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004978 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004979
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004980 // Move the reference points based on the overall group motion of the fingers
4981 // except in PRESS mode while waiting for a transition to occur.
4982 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4983 && (commonDeltaX || commonDeltaY)) {
4984 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004985 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004986 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004987 delta.dx = 0;
4988 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004989 }
4990
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004991 mPointerGesture.referenceTouchX += commonDeltaX;
4992 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004993
Jeff Brown65fd2512011-08-18 11:20:58 -07004994 commonDeltaX *= mPointerXMovementScale;
4995 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004996
Jeff Brownbe1aa822011-07-27 16:04:54 -07004997 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004998 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004999
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005000 mPointerGesture.referenceGestureX += commonDeltaX;
5001 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005002 }
5003
5004 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005005 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5006 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5007 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005008#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005009 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005010 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005011 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005012#endif
Steve Blockec193de2012-01-09 18:35:44 +00005013 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005014
5015 mPointerGesture.currentGestureIdBits.clear();
5016 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5017 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005018 mPointerGesture.currentGestureProperties[0].clear();
5019 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5020 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005021 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005022 mPointerGesture.currentGestureCoords[0].clear();
5023 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5024 mPointerGesture.referenceGestureX);
5025 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5026 mPointerGesture.referenceGestureY);
5027 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005028 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5029 // FREEFORM mode.
5030#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005031 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005032 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005033 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005034#endif
Steve Blockec193de2012-01-09 18:35:44 +00005035 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005036
Jeff Brownace13b12011-03-09 17:39:48 -08005037 mPointerGesture.currentGestureIdBits.clear();
5038
5039 BitSet32 mappedTouchIdBits;
5040 BitSet32 usedGestureIdBits;
5041 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5042 // Initially, assign the active gesture id to the active touch point
5043 // if there is one. No other touch id bits are mapped yet.
5044 if (!*outCancelPreviousGesture) {
5045 mappedTouchIdBits.markBit(activeTouchId);
5046 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5047 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5048 mPointerGesture.activeGestureId;
5049 } else {
5050 mPointerGesture.activeGestureId = -1;
5051 }
5052 } else {
5053 // Otherwise, assume we mapped all touches from the previous frame.
5054 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005055 mappedTouchIdBits.value = mLastFingerIdBits.value
5056 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005057 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5058
5059 // Check whether we need to choose a new active gesture id because the
5060 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005061 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5062 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005063 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005064 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005065 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5066 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5067 mPointerGesture.activeGestureId = -1;
5068 break;
5069 }
5070 }
5071 }
5072
5073#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005074 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005075 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5076 "activeGestureId=%d",
5077 mappedTouchIdBits.value, usedGestureIdBits.value,
5078 mPointerGesture.activeGestureId);
5079#endif
5080
Jeff Brown65fd2512011-08-18 11:20:58 -07005081 BitSet32 idBits(mCurrentFingerIdBits);
5082 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005083 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005084 uint32_t gestureId;
5085 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005086 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005087 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5088#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005089 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005090 "new mapping for touch id %d -> gesture id %d",
5091 touchId, gestureId);
5092#endif
5093 } else {
5094 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5095#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005096 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005097 "existing mapping for touch id %d -> gesture id %d",
5098 touchId, gestureId);
5099#endif
5100 }
5101 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5102 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5103
Jeff Brownbe1aa822011-07-27 16:04:54 -07005104 const RawPointerData::Pointer& pointer =
5105 mCurrentRawPointerData.pointerForId(touchId);
5106 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005107 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005108 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005109 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005110 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005111
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005112 mPointerGesture.currentGestureProperties[i].clear();
5113 mPointerGesture.currentGestureProperties[i].id = gestureId;
5114 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005115 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005116 mPointerGesture.currentGestureCoords[i].clear();
5117 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005118 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005119 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005120 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005121 mPointerGesture.currentGestureCoords[i].setAxisValue(
5122 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5123 }
5124
5125 if (mPointerGesture.activeGestureId < 0) {
5126 mPointerGesture.activeGestureId =
5127 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5128#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005129 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005130 "activeGestureId=%d", mPointerGesture.activeGestureId);
5131#endif
5132 }
Jeff Brown2352b972011-04-12 22:39:53 -07005133 }
Jeff Brownace13b12011-03-09 17:39:48 -08005134 }
5135
Jeff Brownbe1aa822011-07-27 16:04:54 -07005136 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005137
Jeff Brownace13b12011-03-09 17:39:48 -08005138#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005139 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005140 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5141 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005142 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005143 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5144 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005145 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005146 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005147 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005148 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005149 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005150 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005151 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5152 id, index, properties.toolType,
5153 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005154 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5155 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5156 }
5157 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005158 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005159 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005160 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005161 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005162 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005163 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5164 id, index, properties.toolType,
5165 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005166 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5167 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5168 }
5169#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005170 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005171}
5172
Jeff Brown65fd2512011-08-18 11:20:58 -07005173void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5174 mPointerSimple.currentCoords.clear();
5175 mPointerSimple.currentProperties.clear();
5176
5177 bool down, hovering;
5178 if (!mCurrentStylusIdBits.isEmpty()) {
5179 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5180 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5181 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5182 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5183 mPointerController->setPosition(x, y);
5184
5185 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5186 down = !hovering;
5187
5188 mPointerController->getPosition(&x, &y);
5189 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5190 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5191 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5192 mPointerSimple.currentProperties.id = 0;
5193 mPointerSimple.currentProperties.toolType =
5194 mCurrentCookedPointerData.pointerProperties[index].toolType;
5195 } else {
5196 down = false;
5197 hovering = false;
5198 }
5199
5200 dispatchPointerSimple(when, policyFlags, down, hovering);
5201}
5202
5203void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5204 abortPointerSimple(when, policyFlags);
5205}
5206
5207void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5208 mPointerSimple.currentCoords.clear();
5209 mPointerSimple.currentProperties.clear();
5210
5211 bool down, hovering;
5212 if (!mCurrentMouseIdBits.isEmpty()) {
5213 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5214 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5215 if (mLastMouseIdBits.hasBit(id)) {
5216 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5217 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5218 - mLastRawPointerData.pointers[lastIndex].x)
5219 * mPointerXMovementScale;
5220 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5221 - mLastRawPointerData.pointers[lastIndex].y)
5222 * mPointerYMovementScale;
5223
5224 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5225 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5226
5227 mPointerController->move(deltaX, deltaY);
5228 } else {
5229 mPointerVelocityControl.reset();
5230 }
5231
5232 down = isPointerDown(mCurrentButtonState);
5233 hovering = !down;
5234
5235 float x, y;
5236 mPointerController->getPosition(&x, &y);
5237 mPointerSimple.currentCoords.copyFrom(
5238 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5239 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5240 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5241 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5242 hovering ? 0.0f : 1.0f);
5243 mPointerSimple.currentProperties.id = 0;
5244 mPointerSimple.currentProperties.toolType =
5245 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5246 } else {
5247 mPointerVelocityControl.reset();
5248
5249 down = false;
5250 hovering = false;
5251 }
5252
5253 dispatchPointerSimple(when, policyFlags, down, hovering);
5254}
5255
5256void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5257 abortPointerSimple(when, policyFlags);
5258
5259 mPointerVelocityControl.reset();
5260}
5261
5262void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5263 bool down, bool hovering) {
5264 int32_t metaState = getContext()->getGlobalMetaState();
5265
5266 if (mPointerController != NULL) {
5267 if (down || hovering) {
5268 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5269 mPointerController->clearSpots();
5270 mPointerController->setButtonState(mCurrentButtonState);
5271 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5272 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5273 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5274 }
5275 }
5276
5277 if (mPointerSimple.down && !down) {
5278 mPointerSimple.down = false;
5279
5280 // Send up.
5281 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5282 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5283 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5284 mOrientedXPrecision, mOrientedYPrecision,
5285 mPointerSimple.downTime);
5286 getListener()->notifyMotion(&args);
5287 }
5288
5289 if (mPointerSimple.hovering && !hovering) {
5290 mPointerSimple.hovering = false;
5291
5292 // Send hover exit.
5293 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5294 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5295 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5296 mOrientedXPrecision, mOrientedYPrecision,
5297 mPointerSimple.downTime);
5298 getListener()->notifyMotion(&args);
5299 }
5300
5301 if (down) {
5302 if (!mPointerSimple.down) {
5303 mPointerSimple.down = true;
5304 mPointerSimple.downTime = when;
5305
5306 // Send down.
5307 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5308 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5309 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5310 mOrientedXPrecision, mOrientedYPrecision,
5311 mPointerSimple.downTime);
5312 getListener()->notifyMotion(&args);
5313 }
5314
5315 // Send move.
5316 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5317 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5318 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5319 mOrientedXPrecision, mOrientedYPrecision,
5320 mPointerSimple.downTime);
5321 getListener()->notifyMotion(&args);
5322 }
5323
5324 if (hovering) {
5325 if (!mPointerSimple.hovering) {
5326 mPointerSimple.hovering = true;
5327
5328 // Send hover enter.
5329 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5330 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5331 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5332 mOrientedXPrecision, mOrientedYPrecision,
5333 mPointerSimple.downTime);
5334 getListener()->notifyMotion(&args);
5335 }
5336
5337 // Send hover move.
5338 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5339 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5340 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5341 mOrientedXPrecision, mOrientedYPrecision,
5342 mPointerSimple.downTime);
5343 getListener()->notifyMotion(&args);
5344 }
5345
5346 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5347 float vscroll = mCurrentRawVScroll;
5348 float hscroll = mCurrentRawHScroll;
5349 mWheelYVelocityControl.move(when, NULL, &vscroll);
5350 mWheelXVelocityControl.move(when, &hscroll, NULL);
5351
5352 // Send scroll.
5353 PointerCoords pointerCoords;
5354 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5355 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5356 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5357
5358 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5359 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5360 1, &mPointerSimple.currentProperties, &pointerCoords,
5361 mOrientedXPrecision, mOrientedYPrecision,
5362 mPointerSimple.downTime);
5363 getListener()->notifyMotion(&args);
5364 }
5365
5366 // Save state.
5367 if (down || hovering) {
5368 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5369 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5370 } else {
5371 mPointerSimple.reset();
5372 }
5373}
5374
5375void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5376 mPointerSimple.currentCoords.clear();
5377 mPointerSimple.currentProperties.clear();
5378
5379 dispatchPointerSimple(when, policyFlags, false, false);
5380}
5381
Jeff Brownace13b12011-03-09 17:39:48 -08005382void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005383 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5384 const PointerProperties* properties, const PointerCoords* coords,
5385 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005386 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5387 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005388 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005389 uint32_t pointerCount = 0;
5390 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005391 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005392 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005393 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005394 pointerCoords[pointerCount].copyFrom(coords[index]);
5395
5396 if (changedId >= 0 && id == uint32_t(changedId)) {
5397 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5398 }
5399
5400 pointerCount += 1;
5401 }
5402
Steve Blockec193de2012-01-09 18:35:44 +00005403 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005404
5405 if (changedId >= 0 && pointerCount == 1) {
5406 // Replace initial down and final up action.
5407 // We can compare the action without masking off the changed pointer index
5408 // because we know the index is 0.
5409 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5410 action = AMOTION_EVENT_ACTION_DOWN;
5411 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5412 action = AMOTION_EVENT_ACTION_UP;
5413 } else {
5414 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005415 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005416 }
5417 }
5418
Jeff Brownbe1aa822011-07-27 16:04:54 -07005419 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005420 action, flags, metaState, buttonState, edgeFlags,
5421 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005422 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005423}
5424
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005425bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005426 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005427 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5428 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005429 bool changed = false;
5430 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005431 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005432 uint32_t inIndex = inIdToIndex[id];
5433 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005434
5435 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005436 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005437 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005438 PointerCoords& curOutCoords = outCoords[outIndex];
5439
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005440 if (curInProperties != curOutProperties) {
5441 curOutProperties.copyFrom(curInProperties);
5442 changed = true;
5443 }
5444
Jeff Brownace13b12011-03-09 17:39:48 -08005445 if (curInCoords != curOutCoords) {
5446 curOutCoords.copyFrom(curInCoords);
5447 changed = true;
5448 }
5449 }
5450 return changed;
5451}
5452
5453void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005454 if (mPointerController != NULL) {
5455 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5456 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005457}
5458
Jeff Brownbe1aa822011-07-27 16:04:54 -07005459bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5460 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5461 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005462}
5463
Jeff Brownbe1aa822011-07-27 16:04:54 -07005464const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005465 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005466 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005467 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005468 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005469
5470#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005471 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005472 "left=%d, top=%d, right=%d, bottom=%d",
5473 x, y,
5474 virtualKey.keyCode, virtualKey.scanCode,
5475 virtualKey.hitLeft, virtualKey.hitTop,
5476 virtualKey.hitRight, virtualKey.hitBottom);
5477#endif
5478
5479 if (virtualKey.isHit(x, y)) {
5480 return & virtualKey;
5481 }
5482 }
5483
5484 return NULL;
5485}
5486
Jeff Brownbe1aa822011-07-27 16:04:54 -07005487void TouchInputMapper::assignPointerIds() {
5488 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5489 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5490
5491 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005492
5493 if (currentPointerCount == 0) {
5494 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005495 return;
5496 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005497
Jeff Brownbe1aa822011-07-27 16:04:54 -07005498 if (lastPointerCount == 0) {
5499 // All pointers are new.
5500 for (uint32_t i = 0; i < currentPointerCount; i++) {
5501 uint32_t id = i;
5502 mCurrentRawPointerData.pointers[i].id = id;
5503 mCurrentRawPointerData.idToIndex[id] = i;
5504 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5505 }
5506 return;
5507 }
5508
5509 if (currentPointerCount == 1 && lastPointerCount == 1
5510 && mCurrentRawPointerData.pointers[0].toolType
5511 == mLastRawPointerData.pointers[0].toolType) {
5512 // Only one pointer and no change in count so it must have the same id as before.
5513 uint32_t id = mLastRawPointerData.pointers[0].id;
5514 mCurrentRawPointerData.pointers[0].id = id;
5515 mCurrentRawPointerData.idToIndex[id] = 0;
5516 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5517 return;
5518 }
5519
5520 // General case.
5521 // We build a heap of squared euclidean distances between current and last pointers
5522 // associated with the current and last pointer indices. Then, we find the best
5523 // match (by distance) for each current pointer.
5524 // The pointers must have the same tool type but it is possible for them to
5525 // transition from hovering to touching or vice-versa while retaining the same id.
5526 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5527
5528 uint32_t heapSize = 0;
5529 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5530 currentPointerIndex++) {
5531 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5532 lastPointerIndex++) {
5533 const RawPointerData::Pointer& currentPointer =
5534 mCurrentRawPointerData.pointers[currentPointerIndex];
5535 const RawPointerData::Pointer& lastPointer =
5536 mLastRawPointerData.pointers[lastPointerIndex];
5537 if (currentPointer.toolType == lastPointer.toolType) {
5538 int64_t deltaX = currentPointer.x - lastPointer.x;
5539 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005540
5541 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5542
5543 // Insert new element into the heap (sift up).
5544 heap[heapSize].currentPointerIndex = currentPointerIndex;
5545 heap[heapSize].lastPointerIndex = lastPointerIndex;
5546 heap[heapSize].distance = distance;
5547 heapSize += 1;
5548 }
5549 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005550 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005551
Jeff Brownbe1aa822011-07-27 16:04:54 -07005552 // Heapify
5553 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5554 startIndex -= 1;
5555 for (uint32_t parentIndex = startIndex; ;) {
5556 uint32_t childIndex = parentIndex * 2 + 1;
5557 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005558 break;
5559 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005560
5561 if (childIndex + 1 < heapSize
5562 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5563 childIndex += 1;
5564 }
5565
5566 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5567 break;
5568 }
5569
5570 swap(heap[parentIndex], heap[childIndex]);
5571 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005572 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005573 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005574
5575#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005576 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005577 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005578 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005579 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5580 heap[i].distance);
5581 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005582#endif
5583
Jeff Brownbe1aa822011-07-27 16:04:54 -07005584 // Pull matches out by increasing order of distance.
5585 // To avoid reassigning pointers that have already been matched, the loop keeps track
5586 // of which last and current pointers have been matched using the matchedXXXBits variables.
5587 // It also tracks the used pointer id bits.
5588 BitSet32 matchedLastBits(0);
5589 BitSet32 matchedCurrentBits(0);
5590 BitSet32 usedIdBits(0);
5591 bool first = true;
5592 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5593 while (heapSize > 0) {
5594 if (first) {
5595 // The first time through the loop, we just consume the root element of
5596 // the heap (the one with smallest distance).
5597 first = false;
5598 } else {
5599 // Previous iterations consumed the root element of the heap.
5600 // Pop root element off of the heap (sift down).
5601 heap[0] = heap[heapSize];
5602 for (uint32_t parentIndex = 0; ;) {
5603 uint32_t childIndex = parentIndex * 2 + 1;
5604 if (childIndex >= heapSize) {
5605 break;
5606 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005607
Jeff Brownbe1aa822011-07-27 16:04:54 -07005608 if (childIndex + 1 < heapSize
5609 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5610 childIndex += 1;
5611 }
5612
5613 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5614 break;
5615 }
5616
5617 swap(heap[parentIndex], heap[childIndex]);
5618 parentIndex = childIndex;
5619 }
5620
5621#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005622 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005623 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005624 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005625 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5626 heap[i].distance);
5627 }
5628#endif
5629 }
5630
5631 heapSize -= 1;
5632
5633 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5634 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5635
5636 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5637 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5638
5639 matchedCurrentBits.markBit(currentPointerIndex);
5640 matchedLastBits.markBit(lastPointerIndex);
5641
5642 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5643 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5644 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5645 mCurrentRawPointerData.markIdBit(id,
5646 mCurrentRawPointerData.isHovering(currentPointerIndex));
5647 usedIdBits.markBit(id);
5648
5649#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005650 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005651 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5652#endif
5653 break;
5654 }
5655 }
5656
5657 // Assign fresh ids to pointers that were not matched in the process.
5658 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5659 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5660 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5661
5662 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5663 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5664 mCurrentRawPointerData.markIdBit(id,
5665 mCurrentRawPointerData.isHovering(currentPointerIndex));
5666
5667#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005668 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005669 currentPointerIndex, id);
5670#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005671 }
5672}
5673
Jeff Brown6d0fec22010-07-23 21:28:06 -07005674int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005675 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5676 return AKEY_STATE_VIRTUAL;
5677 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005678
Jeff Brownbe1aa822011-07-27 16:04:54 -07005679 size_t numVirtualKeys = mVirtualKeys.size();
5680 for (size_t i = 0; i < numVirtualKeys; i++) {
5681 const VirtualKey& virtualKey = mVirtualKeys[i];
5682 if (virtualKey.keyCode == keyCode) {
5683 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005684 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005685 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005686
5687 return AKEY_STATE_UNKNOWN;
5688}
5689
5690int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005691 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5692 return AKEY_STATE_VIRTUAL;
5693 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005694
Jeff Brownbe1aa822011-07-27 16:04:54 -07005695 size_t numVirtualKeys = mVirtualKeys.size();
5696 for (size_t i = 0; i < numVirtualKeys; i++) {
5697 const VirtualKey& virtualKey = mVirtualKeys[i];
5698 if (virtualKey.scanCode == scanCode) {
5699 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005700 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005701 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005702
5703 return AKEY_STATE_UNKNOWN;
5704}
5705
5706bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5707 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005708 size_t numVirtualKeys = mVirtualKeys.size();
5709 for (size_t i = 0; i < numVirtualKeys; i++) {
5710 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005711
Jeff Brownbe1aa822011-07-27 16:04:54 -07005712 for (size_t i = 0; i < numCodes; i++) {
5713 if (virtualKey.keyCode == keyCodes[i]) {
5714 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005715 }
5716 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005717 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005718
5719 return true;
5720}
5721
5722
5723// --- SingleTouchInputMapper ---
5724
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005725SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5726 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005727}
5728
5729SingleTouchInputMapper::~SingleTouchInputMapper() {
5730}
5731
Jeff Brown65fd2512011-08-18 11:20:58 -07005732void SingleTouchInputMapper::reset(nsecs_t when) {
5733 mSingleTouchMotionAccumulator.reset(getDevice());
5734
5735 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005736}
5737
Jeff Brown6d0fec22010-07-23 21:28:06 -07005738void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005739 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005740
Jeff Brown65fd2512011-08-18 11:20:58 -07005741 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005742}
5743
Jeff Brown65fd2512011-08-18 11:20:58 -07005744void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005745 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005746 mCurrentRawPointerData.pointerCount = 1;
5747 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005748
Jeff Brown65fd2512011-08-18 11:20:58 -07005749 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5750 && (mTouchButtonAccumulator.isHovering()
5751 || (mRawPointerAxes.pressure.valid
5752 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005753 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005754
Jeff Brownbe1aa822011-07-27 16:04:54 -07005755 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005756 outPointer.id = 0;
5757 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5758 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5759 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5760 outPointer.touchMajor = 0;
5761 outPointer.touchMinor = 0;
5762 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5763 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5764 outPointer.orientation = 0;
5765 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005766 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5767 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005768 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5769 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5770 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5771 }
5772 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005773 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005774}
5775
Jeff Brownbe1aa822011-07-27 16:04:54 -07005776void SingleTouchInputMapper::configureRawPointerAxes() {
5777 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005778
Jeff Brownbe1aa822011-07-27 16:04:54 -07005779 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5780 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5781 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5782 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5783 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005784 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5785 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005786}
5787
Jeff Brown00710e92012-04-19 15:18:26 -07005788bool SingleTouchInputMapper::hasStylus() const {
5789 return mTouchButtonAccumulator.hasStylus();
5790}
5791
Jeff Brown6d0fec22010-07-23 21:28:06 -07005792
5793// --- MultiTouchInputMapper ---
5794
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005795MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005796 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005797}
5798
5799MultiTouchInputMapper::~MultiTouchInputMapper() {
5800}
5801
Jeff Brown65fd2512011-08-18 11:20:58 -07005802void MultiTouchInputMapper::reset(nsecs_t when) {
5803 mMultiTouchMotionAccumulator.reset(getDevice());
5804
Jeff Brown6894a292011-07-01 17:59:27 -07005805 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005806
Jeff Brown65fd2512011-08-18 11:20:58 -07005807 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005808}
5809
5810void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005811 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005812
Jeff Brown65fd2512011-08-18 11:20:58 -07005813 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005814}
5815
Jeff Brown65fd2512011-08-18 11:20:58 -07005816void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005817 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005818 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005819 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005820
Jeff Brown80fd47c2011-05-24 01:07:44 -07005821 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005822 const MultiTouchMotionAccumulator::Slot* inSlot =
5823 mMultiTouchMotionAccumulator.getSlot(inIndex);
5824 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005825 continue;
5826 }
5827
Jeff Brown80fd47c2011-05-24 01:07:44 -07005828 if (outCount >= MAX_POINTERS) {
5829#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005830 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005831 "ignoring the rest.",
5832 getDeviceName().string(), MAX_POINTERS);
5833#endif
5834 break; // too many fingers!
5835 }
5836
Jeff Brownbe1aa822011-07-27 16:04:54 -07005837 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005838 outPointer.x = inSlot->getX();
5839 outPointer.y = inSlot->getY();
5840 outPointer.pressure = inSlot->getPressure();
5841 outPointer.touchMajor = inSlot->getTouchMajor();
5842 outPointer.touchMinor = inSlot->getTouchMinor();
5843 outPointer.toolMajor = inSlot->getToolMajor();
5844 outPointer.toolMinor = inSlot->getToolMinor();
5845 outPointer.orientation = inSlot->getOrientation();
5846 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005847 outPointer.tiltX = 0;
5848 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005849
Jeff Brown49754db2011-07-01 17:37:58 -07005850 outPointer.toolType = inSlot->getToolType();
5851 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5852 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5853 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5854 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5855 }
Jeff Brown8d608662010-08-30 03:02:23 -07005856 }
5857
Jeff Brown65fd2512011-08-18 11:20:58 -07005858 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5859 && (mTouchButtonAccumulator.isHovering()
5860 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005861 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005862
Jeff Brown8d608662010-08-30 03:02:23 -07005863 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005864 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005865 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005866 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005867 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005868 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005869 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005870 if (mPointerTrackingIdMap[n] == trackingId) {
5871 id = n;
5872 }
5873 }
5874
5875 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005876 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005877 mPointerTrackingIdMap[id] = trackingId;
5878 }
5879 }
5880 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005881 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005882 mCurrentRawPointerData.clearIdBits();
5883 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005884 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005885 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005886 mCurrentRawPointerData.idToIndex[id] = outCount;
5887 mCurrentRawPointerData.markIdBit(id, isHovering);
5888 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005889 }
5890 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005891
Jeff Brown6d0fec22010-07-23 21:28:06 -07005892 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005893 }
5894
Jeff Brownbe1aa822011-07-27 16:04:54 -07005895 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005896 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005897
Jeff Brown65fd2512011-08-18 11:20:58 -07005898 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005899}
5900
Jeff Brownbe1aa822011-07-27 16:04:54 -07005901void MultiTouchInputMapper::configureRawPointerAxes() {
5902 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005903
Jeff Brownbe1aa822011-07-27 16:04:54 -07005904 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5905 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5906 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5907 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5908 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5909 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5910 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5911 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5912 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5913 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5914 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005915
Jeff Brownbe1aa822011-07-27 16:04:54 -07005916 if (mRawPointerAxes.trackingId.valid
5917 && mRawPointerAxes.slot.valid
5918 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5919 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005920 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005921 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005922 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005923 getDeviceName().string(), slotCount, MAX_SLOTS);
5924 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005925 }
Jeff Brown00710e92012-04-19 15:18:26 -07005926 mMultiTouchMotionAccumulator.configure(getDevice(),
5927 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005928 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07005929 mMultiTouchMotionAccumulator.configure(getDevice(),
5930 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005931 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005932}
5933
Jeff Brown00710e92012-04-19 15:18:26 -07005934bool MultiTouchInputMapper::hasStylus() const {
5935 return mMultiTouchMotionAccumulator.hasStylus()
5936 || mTouchButtonAccumulator.hasStylus();
5937}
5938
Jeff Brown46b9ac02010-04-22 18:58:52 -07005939
Jeff Browncb1404e2011-01-15 18:14:15 -08005940// --- JoystickInputMapper ---
5941
5942JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5943 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005944}
5945
5946JoystickInputMapper::~JoystickInputMapper() {
5947}
5948
5949uint32_t JoystickInputMapper::getSources() {
5950 return AINPUT_SOURCE_JOYSTICK;
5951}
5952
5953void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5954 InputMapper::populateDeviceInfo(info);
5955
Jeff Brown6f2fba42011-02-19 01:08:02 -08005956 for (size_t i = 0; i < mAxes.size(); i++) {
5957 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005958 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5959 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005960 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005961 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5962 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005963 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005964 }
5965}
5966
5967void JoystickInputMapper::dump(String8& dump) {
5968 dump.append(INDENT2 "Joystick Input Mapper:\n");
5969
Jeff Brown6f2fba42011-02-19 01:08:02 -08005970 dump.append(INDENT3 "Axes:\n");
5971 size_t numAxes = mAxes.size();
5972 for (size_t i = 0; i < numAxes; i++) {
5973 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005974 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005975 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005976 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005977 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005978 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005979 }
Jeff Brown85297452011-03-04 13:07:49 -08005980 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5981 label = getAxisLabel(axis.axisInfo.highAxis);
5982 if (label) {
5983 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5984 } else {
5985 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5986 axis.axisInfo.splitValue);
5987 }
5988 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5989 dump.append(" (invert)");
5990 }
5991
5992 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5993 axis.min, axis.max, axis.flat, axis.fuzz);
5994 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5995 "highScale=%0.5f, highOffset=%0.5f\n",
5996 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005997 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5998 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005999 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006000 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006001 }
6002}
6003
Jeff Brown65fd2512011-08-18 11:20:58 -07006004void JoystickInputMapper::configure(nsecs_t when,
6005 const InputReaderConfiguration* config, uint32_t changes) {
6006 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006007
Jeff Brown474dcb52011-06-14 20:22:50 -07006008 if (!changes) { // first time only
6009 // Collect all axes.
6010 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285a2011-08-31 12:56:34 -07006011 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6012 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6013 continue; // axis must be claimed by a different device
6014 }
6015
Jeff Brown474dcb52011-06-14 20:22:50 -07006016 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006017 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006018 if (rawAxisInfo.valid) {
6019 // Map axis.
6020 AxisInfo axisInfo;
6021 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6022 if (!explicitlyMapped) {
6023 // Axis is not explicitly mapped, will choose a generic axis later.
6024 axisInfo.mode = AxisInfo::MODE_NORMAL;
6025 axisInfo.axis = -1;
6026 }
6027
6028 // Apply flat override.
6029 int32_t rawFlat = axisInfo.flatOverride < 0
6030 ? rawAxisInfo.flat : axisInfo.flatOverride;
6031
6032 // Calculate scaling factors and limits.
6033 Axis axis;
6034 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6035 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6036 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6037 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6038 scale, 0.0f, highScale, 0.0f,
6039 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6040 } else if (isCenteredAxis(axisInfo.axis)) {
6041 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6042 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6043 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6044 scale, offset, scale, offset,
6045 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6046 } else {
6047 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6048 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6049 scale, 0.0f, scale, 0.0f,
6050 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6051 }
6052
6053 // To eliminate noise while the joystick is at rest, filter out small variations
6054 // in axis values up front.
6055 axis.filter = axis.flat * 0.25f;
6056
6057 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006058 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006059 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006060
Jeff Brown474dcb52011-06-14 20:22:50 -07006061 // If there are too many axes, start dropping them.
6062 // Prefer to keep explicitly mapped axes.
6063 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006064 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006065 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6066 pruneAxes(true);
6067 pruneAxes(false);
6068 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006069
Jeff Brown474dcb52011-06-14 20:22:50 -07006070 // Assign generic axis ids to remaining axes.
6071 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6072 size_t numAxes = mAxes.size();
6073 for (size_t i = 0; i < numAxes; i++) {
6074 Axis& axis = mAxes.editValueAt(i);
6075 if (axis.axisInfo.axis < 0) {
6076 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6077 && haveAxis(nextGenericAxisId)) {
6078 nextGenericAxisId += 1;
6079 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006080
Jeff Brown474dcb52011-06-14 20:22:50 -07006081 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6082 axis.axisInfo.axis = nextGenericAxisId;
6083 nextGenericAxisId += 1;
6084 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006085 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006086 "have already been assigned to other axes.",
6087 getDeviceName().string(), mAxes.keyAt(i));
6088 mAxes.removeItemsAt(i--);
6089 numAxes -= 1;
6090 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006091 }
6092 }
6093 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006094}
6095
Jeff Brown85297452011-03-04 13:07:49 -08006096bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006097 size_t numAxes = mAxes.size();
6098 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006099 const Axis& axis = mAxes.valueAt(i);
6100 if (axis.axisInfo.axis == axisId
6101 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6102 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006103 return true;
6104 }
6105 }
6106 return false;
6107}
Jeff Browncb1404e2011-01-15 18:14:15 -08006108
Jeff Brown6f2fba42011-02-19 01:08:02 -08006109void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6110 size_t i = mAxes.size();
6111 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6112 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6113 continue;
6114 }
Steve Block6215d3f2012-01-04 20:05:49 +00006115 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006116 getDeviceName().string(), mAxes.keyAt(i));
6117 mAxes.removeItemsAt(i);
6118 }
6119}
6120
6121bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6122 switch (axis) {
6123 case AMOTION_EVENT_AXIS_X:
6124 case AMOTION_EVENT_AXIS_Y:
6125 case AMOTION_EVENT_AXIS_Z:
6126 case AMOTION_EVENT_AXIS_RX:
6127 case AMOTION_EVENT_AXIS_RY:
6128 case AMOTION_EVENT_AXIS_RZ:
6129 case AMOTION_EVENT_AXIS_HAT_X:
6130 case AMOTION_EVENT_AXIS_HAT_Y:
6131 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006132 case AMOTION_EVENT_AXIS_RUDDER:
6133 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006134 return true;
6135 default:
6136 return false;
6137 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006138}
6139
Jeff Brown65fd2512011-08-18 11:20:58 -07006140void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006141 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006142 size_t numAxes = mAxes.size();
6143 for (size_t i = 0; i < numAxes; i++) {
6144 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006145 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006146 }
6147
Jeff Brown65fd2512011-08-18 11:20:58 -07006148 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006149}
6150
6151void JoystickInputMapper::process(const RawEvent* rawEvent) {
6152 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006153 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006154 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006155 if (index >= 0) {
6156 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006157 float newValue, highNewValue;
6158 switch (axis.axisInfo.mode) {
6159 case AxisInfo::MODE_INVERT:
6160 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6161 * axis.scale + axis.offset;
6162 highNewValue = 0.0f;
6163 break;
6164 case AxisInfo::MODE_SPLIT:
6165 if (rawEvent->value < axis.axisInfo.splitValue) {
6166 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6167 * axis.scale + axis.offset;
6168 highNewValue = 0.0f;
6169 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6170 newValue = 0.0f;
6171 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6172 * axis.highScale + axis.highOffset;
6173 } else {
6174 newValue = 0.0f;
6175 highNewValue = 0.0f;
6176 }
6177 break;
6178 default:
6179 newValue = rawEvent->value * axis.scale + axis.offset;
6180 highNewValue = 0.0f;
6181 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006182 }
Jeff Brown85297452011-03-04 13:07:49 -08006183 axis.newValue = newValue;
6184 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006185 }
6186 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006187 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006188
6189 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006190 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006191 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006192 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006193 break;
6194 }
6195 break;
6196 }
6197}
6198
Jeff Brown6f2fba42011-02-19 01:08:02 -08006199void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006200 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006201 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006202 }
6203
6204 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006205 int32_t buttonState = 0;
6206
6207 PointerProperties pointerProperties;
6208 pointerProperties.clear();
6209 pointerProperties.id = 0;
6210 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006211
Jeff Brown6f2fba42011-02-19 01:08:02 -08006212 PointerCoords pointerCoords;
6213 pointerCoords.clear();
6214
6215 size_t numAxes = mAxes.size();
6216 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006217 const Axis& axis = mAxes.valueAt(i);
6218 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6219 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6220 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6221 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006222 }
6223
Jeff Brown56194eb2011-03-02 19:23:13 -08006224 // Moving a joystick axis should not wake the devide because joysticks can
6225 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6226 // button will likely wake the device.
6227 // TODO: Use the input device configuration to control this behavior more finely.
6228 uint32_t policyFlags = 0;
6229
Jeff Brownbe1aa822011-07-27 16:04:54 -07006230 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006231 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
6232 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006233 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006234}
6235
Jeff Brown85297452011-03-04 13:07:49 -08006236bool JoystickInputMapper::filterAxes(bool force) {
6237 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006238 size_t numAxes = mAxes.size();
6239 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006240 Axis& axis = mAxes.editValueAt(i);
6241 if (force || hasValueChangedSignificantly(axis.filter,
6242 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6243 axis.currentValue = axis.newValue;
6244 atLeastOneSignificantChange = true;
6245 }
6246 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6247 if (force || hasValueChangedSignificantly(axis.filter,
6248 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6249 axis.highCurrentValue = axis.highNewValue;
6250 atLeastOneSignificantChange = true;
6251 }
6252 }
6253 }
6254 return atLeastOneSignificantChange;
6255}
6256
6257bool JoystickInputMapper::hasValueChangedSignificantly(
6258 float filter, float newValue, float currentValue, float min, float max) {
6259 if (newValue != currentValue) {
6260 // Filter out small changes in value unless the value is converging on the axis
6261 // bounds or center point. This is intended to reduce the amount of information
6262 // sent to applications by particularly noisy joysticks (such as PS3).
6263 if (fabs(newValue - currentValue) > filter
6264 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6265 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6266 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6267 return true;
6268 }
6269 }
6270 return false;
6271}
6272
6273bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6274 float filter, float newValue, float currentValue, float thresholdValue) {
6275 float newDistance = fabs(newValue - thresholdValue);
6276 if (newDistance < filter) {
6277 float oldDistance = fabs(currentValue - thresholdValue);
6278 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006279 return true;
6280 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006281 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006282 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006283}
6284
Jeff Brown46b9ac02010-04-22 18:58:52 -07006285} // namespace android