blob: cd6a2ec551f69a04a0fc399e0a4180ad8b10fbe2 [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);
Jeff Brown65fd2512011-08-18 11:20:58 -07002623 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002624 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2625 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002626 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2627 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2628 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2629 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2630 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002631
Jeff Brownbe1aa822011-07-27 16:04:54 -07002632 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002633
Jeff Brownbe1aa822011-07-27 16:04:54 -07002634 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2635 mLastRawPointerData.pointerCount);
2636 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2637 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2638 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2639 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002640 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2641 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002642 pointer.id, pointer.x, pointer.y, pointer.pressure,
2643 pointer.touchMajor, pointer.touchMinor,
2644 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002645 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002646 pointer.toolType, toString(pointer.isHovering));
2647 }
2648
2649 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2650 mLastCookedPointerData.pointerCount);
2651 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2652 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2653 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2654 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2655 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002656 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2657 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002658 pointerProperties.id,
2659 pointerCoords.getX(),
2660 pointerCoords.getY(),
2661 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2662 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2663 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2664 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2665 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2666 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002667 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002668 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2669 pointerProperties.toolType,
2670 toString(mLastCookedPointerData.isHovering(i)));
2671 }
2672
Jeff Brown65fd2512011-08-18 11:20:58 -07002673 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002674 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2675 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002676 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002677 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002678 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002679 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002680 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002681 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002682 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002683 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2684 mPointerGestureMaxSwipeWidth);
2685 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002686}
2687
Jeff Brown65fd2512011-08-18 11:20:58 -07002688void TouchInputMapper::configure(nsecs_t when,
2689 const InputReaderConfiguration* config, uint32_t changes) {
2690 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002691
Jeff Brown474dcb52011-06-14 20:22:50 -07002692 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002693
Jeff Brown474dcb52011-06-14 20:22:50 -07002694 if (!changes) { // first time only
2695 // Configure basic parameters.
2696 configureParameters();
2697
Jeff Brown65fd2512011-08-18 11:20:58 -07002698 // Configure common accumulators.
2699 mCursorScrollAccumulator.configure(getDevice());
2700 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002701
2702 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002703 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002704
2705 // Prepare input device calibration.
2706 parseCalibration();
2707 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002708 }
2709
Jeff Brown474dcb52011-06-14 20:22:50 -07002710 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002711 // Update pointer speed.
2712 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2713 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2714 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002715 }
Jeff Brown8d608662010-08-30 03:02:23 -07002716
Jeff Brown65fd2512011-08-18 11:20:58 -07002717 bool resetNeeded = false;
2718 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002719 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2720 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002721 // Configure device sources, surface dimensions, orientation and
2722 // scaling factors.
2723 configureSurface(when, &resetNeeded);
2724 }
2725
2726 if (changes && resetNeeded) {
2727 // Send reset, unless this is the first time the device has been configured,
2728 // in which case the reader will call reset itself after all mappers are ready.
2729 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002730 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002731}
2732
Jeff Brown8d608662010-08-30 03:02:23 -07002733void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002734 // Use the pointer presentation mode for devices that do not support distinct
2735 // multitouch. The spot-based presentation relies on being able to accurately
2736 // locate two or more fingers on the touch pad.
2737 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2738 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002739
Jeff Brown538881e2011-05-25 18:23:38 -07002740 String8 gestureModeString;
2741 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2742 gestureModeString)) {
2743 if (gestureModeString == "pointer") {
2744 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2745 } else if (gestureModeString == "spots") {
2746 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2747 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002748 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002749 }
2750 }
2751
Jeff Browndeffe072011-08-26 18:38:46 -07002752 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2753 // The device is a touch screen.
2754 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2755 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2756 // The device is a pointing device like a track pad.
2757 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2758 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002759 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2760 // The device is a cursor device with a touch pad attached.
2761 // By default don't use the touch pad to move the pointer.
2762 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2763 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002764 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002765 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2766 }
2767
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002768 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002769 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2770 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002771 if (deviceTypeString == "touchScreen") {
2772 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002773 } else if (deviceTypeString == "touchPad") {
2774 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002775 } else if (deviceTypeString == "pointer") {
2776 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002777 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002778 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002779 }
2780 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002781
Jeff Brownefd32662011-03-08 15:13:06 -08002782 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002783 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2784 mParameters.orientationAware);
2785
Jeff Brownbc68a592011-07-25 12:58:12 -07002786 mParameters.associatedDisplayId = -1;
2787 mParameters.associatedDisplayIsExternal = false;
2788 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002789 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002790 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2791 mParameters.associatedDisplayIsExternal =
2792 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2793 && getDevice()->isExternal();
2794 mParameters.associatedDisplayId = 0;
2795 }
Jeff Brown8d608662010-08-30 03:02:23 -07002796}
2797
Jeff Brownef3d7e82010-09-30 14:33:04 -07002798void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002799 dump.append(INDENT3 "Parameters:\n");
2800
Jeff Brown538881e2011-05-25 18:23:38 -07002801 switch (mParameters.gestureMode) {
2802 case Parameters::GESTURE_MODE_POINTER:
2803 dump.append(INDENT4 "GestureMode: pointer\n");
2804 break;
2805 case Parameters::GESTURE_MODE_SPOTS:
2806 dump.append(INDENT4 "GestureMode: spots\n");
2807 break;
2808 default:
2809 assert(false);
2810 }
2811
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002812 switch (mParameters.deviceType) {
2813 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2814 dump.append(INDENT4 "DeviceType: touchScreen\n");
2815 break;
2816 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2817 dump.append(INDENT4 "DeviceType: touchPad\n");
2818 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002819 case Parameters::DEVICE_TYPE_POINTER:
2820 dump.append(INDENT4 "DeviceType: pointer\n");
2821 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002822 default:
Steve Blockec193de2012-01-09 18:35:44 +00002823 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002824 }
2825
Jeff Brown65fd2512011-08-18 11:20:58 -07002826 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2827 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002828 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2829 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002830}
2831
Jeff Brownbe1aa822011-07-27 16:04:54 -07002832void TouchInputMapper::configureRawPointerAxes() {
2833 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002834}
2835
Jeff Brownbe1aa822011-07-27 16:04:54 -07002836void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2837 dump.append(INDENT3 "Raw Touch Axes:\n");
2838 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2839 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2840 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2841 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2842 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2843 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2844 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2845 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2846 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002847 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2848 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002849 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2850 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002851}
2852
Jeff Brown65fd2512011-08-18 11:20:58 -07002853void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2854 int32_t oldDeviceMode = mDeviceMode;
2855
2856 // Determine device mode.
2857 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2858 && mConfig.pointerGesturesEnabled) {
2859 mSource = AINPUT_SOURCE_MOUSE;
2860 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002861 if (hasStylus()) {
2862 mSource |= AINPUT_SOURCE_STYLUS;
2863 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002864 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2865 && mParameters.associatedDisplayId >= 0) {
2866 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2867 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002868 if (hasStylus()) {
2869 mSource |= AINPUT_SOURCE_STYLUS;
2870 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002871 } else {
2872 mSource = AINPUT_SOURCE_TOUCHPAD;
2873 mDeviceMode = DEVICE_MODE_UNSCALED;
2874 }
2875
Jeff Brown9626b142011-03-03 02:09:54 -08002876 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002877 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002878 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002879 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002880 mDeviceMode = DEVICE_MODE_DISABLED;
2881 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002882 }
2883
Jeff Brown65fd2512011-08-18 11:20:58 -07002884 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002885 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002886 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002887 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002888 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2889 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002890 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002891 "display %d. The device will be inoperable until the display size "
2892 "becomes available.",
2893 getDeviceName().string(), mParameters.associatedDisplayId);
2894 mDeviceMode = DEVICE_MODE_DISABLED;
2895 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002896 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002897 }
2898
Jeff Brown65fd2512011-08-18 11:20:58 -07002899 // Configure dimensions.
2900 int32_t width, height, orientation;
2901 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2902 width = mAssociatedDisplayWidth;
2903 height = mAssociatedDisplayHeight;
2904 orientation = mParameters.orientationAware ?
2905 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2906 } else {
2907 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2908 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2909 orientation = DISPLAY_ORIENTATION_0;
2910 }
2911
2912 // If moving between pointer modes, need to reset some state.
2913 bool deviceModeChanged;
2914 if (mDeviceMode != oldDeviceMode) {
2915 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002916 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002917 }
2918
Jeff Browndaf4a122011-08-26 17:14:14 -07002919 // Create pointer controller if needed.
2920 if (mDeviceMode == DEVICE_MODE_POINTER ||
2921 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2922 if (mPointerController == NULL) {
2923 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2924 }
2925 } else {
2926 mPointerController.clear();
2927 }
2928
Jeff Brownbe1aa822011-07-27 16:04:54 -07002929 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002930 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002931 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002932 }
2933
Jeff Brownbe1aa822011-07-27 16:04:54 -07002934 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002935 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002936 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002937 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002938
Jeff Brownbe1aa822011-07-27 16:04:54 -07002939 mSurfaceWidth = width;
2940 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002941
Jeff Brown8d608662010-08-30 03:02:23 -07002942 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002943 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2944 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2945 mXPrecision = 1.0f / mXScale;
2946 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002947
Jeff Brownbe1aa822011-07-27 16:04:54 -07002948 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002949 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002950 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002951 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002952
Jeff Brownbe1aa822011-07-27 16:04:54 -07002953 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002954
Jeff Brown8d608662010-08-30 03:02:23 -07002955 // Scale factor for terms that are not oriented in a particular axis.
2956 // If the pixels are square then xScale == yScale otherwise we fake it
2957 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002958 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002959
Jeff Brown8d608662010-08-30 03:02:23 -07002960 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002961 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002962
Jeff Browna1f89ce2011-08-11 00:05:01 -07002963 // Size factors.
2964 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2965 if (mRawPointerAxes.touchMajor.valid
2966 && mRawPointerAxes.touchMajor.maxValue != 0) {
2967 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2968 } else if (mRawPointerAxes.toolMajor.valid
2969 && mRawPointerAxes.toolMajor.maxValue != 0) {
2970 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2971 } else {
2972 mSizeScale = 0.0f;
2973 }
2974
Jeff Brownbe1aa822011-07-27 16:04:54 -07002975 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002976 mOrientedRanges.haveToolSize = true;
2977 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002978
Jeff Brownbe1aa822011-07-27 16:04:54 -07002979 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002980 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002981 mOrientedRanges.touchMajor.min = 0;
2982 mOrientedRanges.touchMajor.max = diagonalSize;
2983 mOrientedRanges.touchMajor.flat = 0;
2984 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002985
Jeff Brownbe1aa822011-07-27 16:04:54 -07002986 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2987 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002988
Jeff Brownbe1aa822011-07-27 16:04:54 -07002989 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002990 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002991 mOrientedRanges.toolMajor.min = 0;
2992 mOrientedRanges.toolMajor.max = diagonalSize;
2993 mOrientedRanges.toolMajor.flat = 0;
2994 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002995
Jeff Brownbe1aa822011-07-27 16:04:54 -07002996 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2997 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002998
2999 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003000 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003001 mOrientedRanges.size.min = 0;
3002 mOrientedRanges.size.max = 1.0;
3003 mOrientedRanges.size.flat = 0;
3004 mOrientedRanges.size.fuzz = 0;
3005 } else {
3006 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003007 }
3008
3009 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003010 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003011 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3012 || mCalibration.pressureCalibration
3013 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3014 if (mCalibration.havePressureScale) {
3015 mPressureScale = mCalibration.pressureScale;
3016 } else if (mRawPointerAxes.pressure.valid
3017 && mRawPointerAxes.pressure.maxValue != 0) {
3018 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003019 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003020 }
Jeff Brown8d608662010-08-30 03:02:23 -07003021
Jeff Brown65fd2512011-08-18 11:20:58 -07003022 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3023 mOrientedRanges.pressure.source = mSource;
3024 mOrientedRanges.pressure.min = 0;
3025 mOrientedRanges.pressure.max = 1.0;
3026 mOrientedRanges.pressure.flat = 0;
3027 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003028
Jeff Brown65fd2512011-08-18 11:20:58 -07003029 // Tilt
3030 mTiltXCenter = 0;
3031 mTiltXScale = 0;
3032 mTiltYCenter = 0;
3033 mTiltYScale = 0;
3034 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3035 if (mHaveTilt) {
3036 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3037 mRawPointerAxes.tiltX.maxValue);
3038 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3039 mRawPointerAxes.tiltY.maxValue);
3040 mTiltXScale = M_PI / 180;
3041 mTiltYScale = M_PI / 180;
3042
3043 mOrientedRanges.haveTilt = true;
3044
3045 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3046 mOrientedRanges.tilt.source = mSource;
3047 mOrientedRanges.tilt.min = 0;
3048 mOrientedRanges.tilt.max = M_PI_2;
3049 mOrientedRanges.tilt.flat = 0;
3050 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003051 }
3052
Jeff Brown8d608662010-08-30 03:02:23 -07003053 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07003054 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003055 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003056 if (mHaveTilt) {
3057 mOrientedRanges.haveOrientation = true;
3058
3059 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3060 mOrientedRanges.orientation.source = mSource;
3061 mOrientedRanges.orientation.min = -M_PI;
3062 mOrientedRanges.orientation.max = M_PI;
3063 mOrientedRanges.orientation.flat = 0;
3064 mOrientedRanges.orientation.fuzz = 0;
3065 } else if (mCalibration.orientationCalibration !=
3066 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003067 if (mCalibration.orientationCalibration
3068 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003069 if (mRawPointerAxes.orientation.valid) {
3070 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
3071 mRawPointerAxes.orientation.maxValue);
3072 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
3073 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07003074 }
3075 }
3076
Jeff Brownbe1aa822011-07-27 16:04:54 -07003077 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003078
Jeff Brownbe1aa822011-07-27 16:04:54 -07003079 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003080 mOrientedRanges.orientation.source = mSource;
3081 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003082 mOrientedRanges.orientation.max = M_PI_2;
3083 mOrientedRanges.orientation.flat = 0;
3084 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003085 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003086
3087 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003088 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003089 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3090 if (mCalibration.distanceCalibration
3091 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3092 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003093 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003094 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003095 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003096 }
3097 }
3098
Jeff Brownbe1aa822011-07-27 16:04:54 -07003099 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003100
Jeff Brownbe1aa822011-07-27 16:04:54 -07003101 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003102 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003103 mOrientedRanges.distance.min =
3104 mRawPointerAxes.distance.minValue * mDistanceScale;
3105 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003106 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003107 mOrientedRanges.distance.flat = 0;
3108 mOrientedRanges.distance.fuzz =
3109 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003110 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003111 }
3112
Jeff Brown65fd2512011-08-18 11:20:58 -07003113 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08003114 // Compute oriented surface dimensions, precision, scales and ranges.
3115 // Note that the maximum value reported is an inclusive maximum value so it is one
3116 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003117 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003118 case DISPLAY_ORIENTATION_90:
3119 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003120 mOrientedSurfaceWidth = mSurfaceHeight;
3121 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08003122
Jeff Brownbe1aa822011-07-27 16:04:54 -07003123 mOrientedXPrecision = mYPrecision;
3124 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003125
Jeff Brownbe1aa822011-07-27 16:04:54 -07003126 mOrientedRanges.x.min = 0;
3127 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3128 * mYScale;
3129 mOrientedRanges.x.flat = 0;
3130 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003131
Jeff Brownbe1aa822011-07-27 16:04:54 -07003132 mOrientedRanges.y.min = 0;
3133 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3134 * mXScale;
3135 mOrientedRanges.y.flat = 0;
3136 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003137 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003138
Jeff Brown6d0fec22010-07-23 21:28:06 -07003139 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003140 mOrientedSurfaceWidth = mSurfaceWidth;
3141 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08003142
Jeff Brownbe1aa822011-07-27 16:04:54 -07003143 mOrientedXPrecision = mXPrecision;
3144 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003145
Jeff Brownbe1aa822011-07-27 16:04:54 -07003146 mOrientedRanges.x.min = 0;
3147 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
3148 * mXScale;
3149 mOrientedRanges.x.flat = 0;
3150 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003151
Jeff Brownbe1aa822011-07-27 16:04:54 -07003152 mOrientedRanges.y.min = 0;
3153 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
3154 * mYScale;
3155 mOrientedRanges.y.flat = 0;
3156 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003157 break;
3158 }
Jeff Brownace13b12011-03-09 17:39:48 -08003159
3160 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07003161 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003162 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3163 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07003164 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003165 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
3166 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003167
Jeff Brown2352b972011-04-12 22:39:53 -07003168 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003169 // given area relative to the diagonal size of the display when no acceleration
3170 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003171 // Assume that the touch pad has a square aspect ratio such that movements in
3172 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003173 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003174 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003175 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003176
3177 // Scale zooms to cover a smaller range of the display than movements do.
3178 // This value determines the area around the pointer that is affected by freeform
3179 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003180 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003181 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003182 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003183
Jeff Brown2352b972011-04-12 22:39:53 -07003184 // Max width between pointers to detect a swipe gesture is more than some fraction
3185 // of the diagonal axis of the touch pad. Touches that are wider than this are
3186 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003187 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003188 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003189 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003190
Jeff Brown65fd2512011-08-18 11:20:58 -07003191 // Abort current pointer usages because the state has changed.
3192 abortPointerUsage(when, 0 /*policyFlags*/);
3193
3194 // Inform the dispatcher about the changes.
3195 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003196 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003197 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003198}
3199
Jeff Brownbe1aa822011-07-27 16:04:54 -07003200void TouchInputMapper::dumpSurface(String8& dump) {
3201 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3202 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
3203 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003204}
3205
Jeff Brownbe1aa822011-07-27 16:04:54 -07003206void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003207 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003208 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003209
Jeff Brownbe1aa822011-07-27 16:04:54 -07003210 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003211
Jeff Brown6328cdc2010-07-29 18:18:33 -07003212 if (virtualKeyDefinitions.size() == 0) {
3213 return;
3214 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003215
Jeff Brownbe1aa822011-07-27 16:04:54 -07003216 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003217
Jeff Brownbe1aa822011-07-27 16:04:54 -07003218 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3219 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3220 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3221 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003222
3223 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003224 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003225 virtualKeyDefinitions[i];
3226
Jeff Brownbe1aa822011-07-27 16:04:54 -07003227 mVirtualKeys.add();
3228 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003229
3230 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3231 int32_t keyCode;
3232 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003233 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003234 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003235 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003236 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003237 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003238 }
3239
Jeff Brown6328cdc2010-07-29 18:18:33 -07003240 virtualKey.keyCode = keyCode;
3241 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003242
Jeff Brown6328cdc2010-07-29 18:18:33 -07003243 // convert the key definition's display coordinates into touch coordinates for a hit box
3244 int32_t halfWidth = virtualKeyDefinition.width / 2;
3245 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003246
Jeff Brown6328cdc2010-07-29 18:18:33 -07003247 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003248 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003249 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003250 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003251 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003252 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003253 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003254 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003255 }
3256}
3257
Jeff Brownbe1aa822011-07-27 16:04:54 -07003258void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3259 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003260 dump.append(INDENT3 "Virtual Keys:\n");
3261
Jeff Brownbe1aa822011-07-27 16:04:54 -07003262 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3263 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003264 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3265 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3266 i, virtualKey.scanCode, virtualKey.keyCode,
3267 virtualKey.hitLeft, virtualKey.hitRight,
3268 virtualKey.hitTop, virtualKey.hitBottom);
3269 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003270 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003271}
3272
Jeff Brown8d608662010-08-30 03:02:23 -07003273void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003274 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003275 Calibration& out = mCalibration;
3276
Jeff Browna1f89ce2011-08-11 00:05:01 -07003277 // Size
3278 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3279 String8 sizeCalibrationString;
3280 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3281 if (sizeCalibrationString == "none") {
3282 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3283 } else if (sizeCalibrationString == "geometric") {
3284 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3285 } else if (sizeCalibrationString == "diameter") {
3286 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3287 } else if (sizeCalibrationString == "area") {
3288 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3289 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003290 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003291 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003292 }
3293 }
3294
Jeff Browna1f89ce2011-08-11 00:05:01 -07003295 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3296 out.sizeScale);
3297 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3298 out.sizeBias);
3299 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3300 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003301
3302 // Pressure
3303 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3304 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003305 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003306 if (pressureCalibrationString == "none") {
3307 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3308 } else if (pressureCalibrationString == "physical") {
3309 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3310 } else if (pressureCalibrationString == "amplitude") {
3311 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3312 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003313 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003314 pressureCalibrationString.string());
3315 }
3316 }
3317
Jeff Brown8d608662010-08-30 03:02:23 -07003318 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3319 out.pressureScale);
3320
Jeff Brown8d608662010-08-30 03:02:23 -07003321 // Orientation
3322 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3323 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003324 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003325 if (orientationCalibrationString == "none") {
3326 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3327 } else if (orientationCalibrationString == "interpolated") {
3328 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003329 } else if (orientationCalibrationString == "vector") {
3330 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003331 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003332 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003333 orientationCalibrationString.string());
3334 }
3335 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003336
3337 // Distance
3338 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3339 String8 distanceCalibrationString;
3340 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3341 if (distanceCalibrationString == "none") {
3342 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3343 } else if (distanceCalibrationString == "scaled") {
3344 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3345 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003346 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003347 distanceCalibrationString.string());
3348 }
3349 }
3350
3351 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3352 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003353}
3354
3355void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003356 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003357 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3358 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3359 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003360 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003361 } else {
3362 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3363 }
Jeff Brown8d608662010-08-30 03:02:23 -07003364
Jeff Browna1f89ce2011-08-11 00:05:01 -07003365 // Pressure
3366 if (mRawPointerAxes.pressure.valid) {
3367 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3368 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3369 }
3370 } else {
3371 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003372 }
3373
3374 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003375 if (mRawPointerAxes.orientation.valid) {
3376 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003377 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003378 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003379 } else {
3380 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003381 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003382
3383 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003384 if (mRawPointerAxes.distance.valid) {
3385 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003386 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003387 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003388 } else {
3389 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003390 }
Jeff Brown8d608662010-08-30 03:02:23 -07003391}
3392
Jeff Brownef3d7e82010-09-30 14:33:04 -07003393void TouchInputMapper::dumpCalibration(String8& dump) {
3394 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003395
Jeff Browna1f89ce2011-08-11 00:05:01 -07003396 // Size
3397 switch (mCalibration.sizeCalibration) {
3398 case Calibration::SIZE_CALIBRATION_NONE:
3399 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003400 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003401 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3402 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003403 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003404 case Calibration::SIZE_CALIBRATION_DIAMETER:
3405 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3406 break;
3407 case Calibration::SIZE_CALIBRATION_AREA:
3408 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003409 break;
3410 default:
Steve Blockec193de2012-01-09 18:35:44 +00003411 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003412 }
3413
Jeff Browna1f89ce2011-08-11 00:05:01 -07003414 if (mCalibration.haveSizeScale) {
3415 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3416 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003417 }
3418
Jeff Browna1f89ce2011-08-11 00:05:01 -07003419 if (mCalibration.haveSizeBias) {
3420 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3421 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003422 }
3423
Jeff Browna1f89ce2011-08-11 00:05:01 -07003424 if (mCalibration.haveSizeIsSummed) {
3425 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3426 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003427 }
3428
3429 // Pressure
3430 switch (mCalibration.pressureCalibration) {
3431 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003432 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003433 break;
3434 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003435 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003436 break;
3437 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003438 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003439 break;
3440 default:
Steve Blockec193de2012-01-09 18:35:44 +00003441 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003442 }
3443
Jeff Brown8d608662010-08-30 03:02:23 -07003444 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003445 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3446 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003447 }
3448
Jeff Brown8d608662010-08-30 03:02:23 -07003449 // Orientation
3450 switch (mCalibration.orientationCalibration) {
3451 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003452 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003453 break;
3454 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003455 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003456 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003457 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3458 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3459 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003460 default:
Steve Blockec193de2012-01-09 18:35:44 +00003461 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003462 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003463
3464 // Distance
3465 switch (mCalibration.distanceCalibration) {
3466 case Calibration::DISTANCE_CALIBRATION_NONE:
3467 dump.append(INDENT4 "touch.distance.calibration: none\n");
3468 break;
3469 case Calibration::DISTANCE_CALIBRATION_SCALED:
3470 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3471 break;
3472 default:
Steve Blockec193de2012-01-09 18:35:44 +00003473 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003474 }
3475
3476 if (mCalibration.haveDistanceScale) {
3477 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3478 mCalibration.distanceScale);
3479 }
Jeff Brown8d608662010-08-30 03:02:23 -07003480}
3481
Jeff Brown65fd2512011-08-18 11:20:58 -07003482void TouchInputMapper::reset(nsecs_t when) {
3483 mCursorButtonAccumulator.reset(getDevice());
3484 mCursorScrollAccumulator.reset(getDevice());
3485 mTouchButtonAccumulator.reset(getDevice());
3486
3487 mPointerVelocityControl.reset();
3488 mWheelXVelocityControl.reset();
3489 mWheelYVelocityControl.reset();
3490
Jeff Brownbe1aa822011-07-27 16:04:54 -07003491 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003492 mLastRawPointerData.clear();
3493 mCurrentCookedPointerData.clear();
3494 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003495 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003496 mLastButtonState = 0;
3497 mCurrentRawVScroll = 0;
3498 mCurrentRawHScroll = 0;
3499 mCurrentFingerIdBits.clear();
3500 mLastFingerIdBits.clear();
3501 mCurrentStylusIdBits.clear();
3502 mLastStylusIdBits.clear();
3503 mCurrentMouseIdBits.clear();
3504 mLastMouseIdBits.clear();
3505 mPointerUsage = POINTER_USAGE_NONE;
3506 mSentHoverEnter = false;
3507 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003508
Jeff Brown65fd2512011-08-18 11:20:58 -07003509 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003510
Jeff Brown65fd2512011-08-18 11:20:58 -07003511 mPointerGesture.reset();
3512 mPointerSimple.reset();
3513
3514 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003515 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3516 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003517 }
3518
Jeff Brown65fd2512011-08-18 11:20:58 -07003519 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003520}
3521
Jeff Brown65fd2512011-08-18 11:20:58 -07003522void TouchInputMapper::process(const RawEvent* rawEvent) {
3523 mCursorButtonAccumulator.process(rawEvent);
3524 mCursorScrollAccumulator.process(rawEvent);
3525 mTouchButtonAccumulator.process(rawEvent);
3526
Jeff Brown49ccac52012-04-11 18:27:33 -07003527 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003528 sync(rawEvent->when);
3529 }
3530}
3531
3532void TouchInputMapper::sync(nsecs_t when) {
3533 // Sync button state.
3534 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3535 | mCursorButtonAccumulator.getButtonState();
3536
3537 // Sync scroll state.
3538 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3539 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3540 mCursorScrollAccumulator.finishSync();
3541
3542 // Sync touch state.
3543 bool havePointerIds = true;
3544 mCurrentRawPointerData.clear();
3545 syncTouch(when, &havePointerIds);
3546
Jeff Brownaa3855d2011-03-17 01:34:19 -07003547#if DEBUG_RAW_EVENTS
3548 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003549 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003550 mLastRawPointerData.pointerCount,
3551 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003552 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003553 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003554 "hovering ids 0x%08x -> 0x%08x",
3555 mLastRawPointerData.pointerCount,
3556 mCurrentRawPointerData.pointerCount,
3557 mLastRawPointerData.touchingIdBits.value,
3558 mCurrentRawPointerData.touchingIdBits.value,
3559 mLastRawPointerData.hoveringIdBits.value,
3560 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003561 }
3562#endif
3563
Jeff Brown65fd2512011-08-18 11:20:58 -07003564 // Reset state that we will compute below.
3565 mCurrentFingerIdBits.clear();
3566 mCurrentStylusIdBits.clear();
3567 mCurrentMouseIdBits.clear();
3568 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003569
Jeff Brown65fd2512011-08-18 11:20:58 -07003570 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3571 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003572 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003573 mCurrentButtonState = 0;
3574 } else {
3575 // Preprocess pointer data.
3576 if (!havePointerIds) {
3577 assignPointerIds();
3578 }
3579
3580 // Handle policy on initial down or hover events.
3581 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003582 bool initialDown = mLastRawPointerData.pointerCount == 0
3583 && mCurrentRawPointerData.pointerCount != 0;
3584 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3585 if (initialDown || buttonsPressed) {
3586 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003587 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003588 getContext()->fadePointer();
3589 }
3590
3591 // Initial downs on external touch devices should wake the device.
3592 // We don't do this for internal touch screens to prevent them from waking
3593 // up in your pocket.
3594 // TODO: Use the input device configuration to control this behavior more finely.
3595 if (getDevice()->isExternal()) {
3596 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3597 }
3598 }
3599
3600 // Synthesize key down from raw buttons if needed.
3601 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3602 policyFlags, mLastButtonState, mCurrentButtonState);
3603
3604 // Consume raw off-screen touches before cooking pointer data.
3605 // If touches are consumed, subsequent code will not receive any pointer data.
3606 if (consumeRawTouches(when, policyFlags)) {
3607 mCurrentRawPointerData.clear();
3608 }
3609
3610 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3611 // with cooked pointer data that has the same ids and indices as the raw data.
3612 // The following code can use either the raw or cooked data, as needed.
3613 cookPointerData();
3614
3615 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003616 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003617 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3618 uint32_t id = idBits.clearFirstMarkedBit();
3619 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3620 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3621 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3622 mCurrentStylusIdBits.markBit(id);
3623 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3624 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3625 mCurrentFingerIdBits.markBit(id);
3626 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3627 mCurrentMouseIdBits.markBit(id);
3628 }
3629 }
3630 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3631 uint32_t id = idBits.clearFirstMarkedBit();
3632 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3633 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3634 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3635 mCurrentStylusIdBits.markBit(id);
3636 }
3637 }
3638
3639 // Stylus takes precedence over all tools, then mouse, then finger.
3640 PointerUsage pointerUsage = mPointerUsage;
3641 if (!mCurrentStylusIdBits.isEmpty()) {
3642 mCurrentMouseIdBits.clear();
3643 mCurrentFingerIdBits.clear();
3644 pointerUsage = POINTER_USAGE_STYLUS;
3645 } else if (!mCurrentMouseIdBits.isEmpty()) {
3646 mCurrentFingerIdBits.clear();
3647 pointerUsage = POINTER_USAGE_MOUSE;
3648 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3649 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003650 }
3651
3652 dispatchPointerUsage(when, policyFlags, pointerUsage);
3653 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003654 if (mDeviceMode == DEVICE_MODE_DIRECT
3655 && mConfig.showTouches && mPointerController != NULL) {
3656 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3657 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3658
3659 mPointerController->setButtonState(mCurrentButtonState);
3660 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3661 mCurrentCookedPointerData.idToIndex,
3662 mCurrentCookedPointerData.touchingIdBits);
3663 }
3664
Jeff Brown65fd2512011-08-18 11:20:58 -07003665 dispatchHoverExit(when, policyFlags);
3666 dispatchTouches(when, policyFlags);
3667 dispatchHoverEnterAndMove(when, policyFlags);
3668 }
3669
3670 // Synthesize key up from raw buttons if needed.
3671 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3672 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003673 }
3674
Jeff Brown6328cdc2010-07-29 18:18:33 -07003675 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003676 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3677 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3678 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003679 mLastFingerIdBits = mCurrentFingerIdBits;
3680 mLastStylusIdBits = mCurrentStylusIdBits;
3681 mLastMouseIdBits = mCurrentMouseIdBits;
3682
3683 // Clear some transient state.
3684 mCurrentRawVScroll = 0;
3685 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003686}
3687
Jeff Brown79ac9692011-04-19 21:20:10 -07003688void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003689 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003690 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3691 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3692 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003693 }
3694}
3695
Jeff Brownbe1aa822011-07-27 16:04:54 -07003696bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3697 // Check for release of a virtual key.
3698 if (mCurrentVirtualKey.down) {
3699 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3700 // Pointer went up while virtual key was down.
3701 mCurrentVirtualKey.down = false;
3702 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003703#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003704 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003705 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003706#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003707 dispatchVirtualKey(when, policyFlags,
3708 AKEY_EVENT_ACTION_UP,
3709 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003710 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003711 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003712 }
3713
Jeff Brownbe1aa822011-07-27 16:04:54 -07003714 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3715 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3716 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3717 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3718 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3719 // Pointer is still within the space of the virtual key.
3720 return true;
3721 }
3722 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003723
Jeff Brownbe1aa822011-07-27 16:04:54 -07003724 // Pointer left virtual key area or another pointer also went down.
3725 // Send key cancellation but do not consume the touch yet.
3726 // This is useful when the user swipes through from the virtual key area
3727 // into the main display surface.
3728 mCurrentVirtualKey.down = false;
3729 if (!mCurrentVirtualKey.ignored) {
3730#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003731 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003732 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3733#endif
3734 dispatchVirtualKey(when, policyFlags,
3735 AKEY_EVENT_ACTION_UP,
3736 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3737 | AKEY_EVENT_FLAG_CANCELED);
3738 }
3739 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003740
Jeff Brownbe1aa822011-07-27 16:04:54 -07003741 if (mLastRawPointerData.touchingIdBits.isEmpty()
3742 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3743 // Pointer just went down. Check for virtual key press or off-screen touches.
3744 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3745 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3746 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3747 // If exactly one pointer went down, check for virtual key hit.
3748 // Otherwise we will drop the entire stroke.
3749 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3750 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3751 if (virtualKey) {
3752 mCurrentVirtualKey.down = true;
3753 mCurrentVirtualKey.downTime = when;
3754 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3755 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3756 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3757 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3758
3759 if (!mCurrentVirtualKey.ignored) {
3760#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003761 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003762 mCurrentVirtualKey.keyCode,
3763 mCurrentVirtualKey.scanCode);
3764#endif
3765 dispatchVirtualKey(when, policyFlags,
3766 AKEY_EVENT_ACTION_DOWN,
3767 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3768 }
3769 }
3770 }
3771 return true;
3772 }
3773 }
3774
Jeff Brownfe508922011-01-18 15:10:10 -08003775 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003776 // most recent touch within the screen area. The idea is to filter out stray
3777 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003778 //
3779 // Problems we're trying to solve:
3780 //
3781 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3782 // virtual key area that is implemented by a separate touch panel and accidentally
3783 // triggers a virtual key.
3784 //
3785 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3786 // area and accidentally triggers a virtual key. This often happens when virtual keys
3787 // are layed out below the screen near to where the on screen keyboard's space bar
3788 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003789 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003790 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003791 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003792 return false;
3793}
3794
3795void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3796 int32_t keyEventAction, int32_t keyEventFlags) {
3797 int32_t keyCode = mCurrentVirtualKey.keyCode;
3798 int32_t scanCode = mCurrentVirtualKey.scanCode;
3799 nsecs_t downTime = mCurrentVirtualKey.downTime;
3800 int32_t metaState = mContext->getGlobalMetaState();
3801 policyFlags |= POLICY_FLAG_VIRTUAL;
3802
3803 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3804 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3805 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003806}
3807
Jeff Brown6d0fec22010-07-23 21:28:06 -07003808void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003809 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3810 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003811 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003812 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003813
3814 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003815 if (!currentIdBits.isEmpty()) {
3816 // No pointer id changes so this is a move event.
3817 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003818 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003819 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3820 AMOTION_EVENT_EDGE_FLAG_NONE,
3821 mCurrentCookedPointerData.pointerProperties,
3822 mCurrentCookedPointerData.pointerCoords,
3823 mCurrentCookedPointerData.idToIndex,
3824 currentIdBits, -1,
3825 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3826 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003827 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003828 // There may be pointers going up and pointers going down and pointers moving
3829 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003830 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3831 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003832 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003833 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003834
Jeff Brownace13b12011-03-09 17:39:48 -08003835 // Update last coordinates of pointers that have moved so that we observe the new
3836 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003837 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003838 mCurrentCookedPointerData.pointerProperties,
3839 mCurrentCookedPointerData.pointerCoords,
3840 mCurrentCookedPointerData.idToIndex,
3841 mLastCookedPointerData.pointerProperties,
3842 mLastCookedPointerData.pointerCoords,
3843 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003844 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003845 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003846 moveNeeded = true;
3847 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003848
Jeff Brownace13b12011-03-09 17:39:48 -08003849 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003850 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003851 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003852
Jeff Brown65fd2512011-08-18 11:20:58 -07003853 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003854 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003855 mLastCookedPointerData.pointerProperties,
3856 mLastCookedPointerData.pointerCoords,
3857 mLastCookedPointerData.idToIndex,
3858 dispatchedIdBits, upId,
3859 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003860 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003861 }
3862
Jeff Brownc3db8582010-10-20 15:33:38 -07003863 // Dispatch move events if any of the remaining pointers moved from their old locations.
3864 // Although applications receive new locations as part of individual pointer up
3865 // events, they do not generally handle them except when presented in a move event.
3866 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003867 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003868 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003869 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003870 mCurrentCookedPointerData.pointerProperties,
3871 mCurrentCookedPointerData.pointerCoords,
3872 mCurrentCookedPointerData.idToIndex,
3873 dispatchedIdBits, -1,
3874 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003875 }
3876
3877 // Dispatch pointer down events using the new pointer locations.
3878 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003879 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003880 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003881
Jeff Brownace13b12011-03-09 17:39:48 -08003882 if (dispatchedIdBits.count() == 1) {
3883 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003884 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003885 }
3886
Jeff Brown65fd2512011-08-18 11:20:58 -07003887 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003888 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003889 mCurrentCookedPointerData.pointerProperties,
3890 mCurrentCookedPointerData.pointerCoords,
3891 mCurrentCookedPointerData.idToIndex,
3892 dispatchedIdBits, downId,
3893 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003894 }
3895 }
Jeff Brownace13b12011-03-09 17:39:48 -08003896}
3897
Jeff Brownbe1aa822011-07-27 16:04:54 -07003898void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3899 if (mSentHoverEnter &&
3900 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3901 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3902 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003903 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003904 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3905 mLastCookedPointerData.pointerProperties,
3906 mLastCookedPointerData.pointerCoords,
3907 mLastCookedPointerData.idToIndex,
3908 mLastCookedPointerData.hoveringIdBits, -1,
3909 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3910 mSentHoverEnter = false;
3911 }
3912}
Jeff Brownace13b12011-03-09 17:39:48 -08003913
Jeff Brownbe1aa822011-07-27 16:04:54 -07003914void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3915 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3916 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3917 int32_t metaState = getContext()->getGlobalMetaState();
3918 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003919 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003920 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3921 mCurrentCookedPointerData.pointerProperties,
3922 mCurrentCookedPointerData.pointerCoords,
3923 mCurrentCookedPointerData.idToIndex,
3924 mCurrentCookedPointerData.hoveringIdBits, -1,
3925 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3926 mSentHoverEnter = true;
3927 }
Jeff Brownace13b12011-03-09 17:39:48 -08003928
Jeff Brown65fd2512011-08-18 11:20:58 -07003929 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003930 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3931 mCurrentCookedPointerData.pointerProperties,
3932 mCurrentCookedPointerData.pointerCoords,
3933 mCurrentCookedPointerData.idToIndex,
3934 mCurrentCookedPointerData.hoveringIdBits, -1,
3935 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3936 }
3937}
3938
3939void TouchInputMapper::cookPointerData() {
3940 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3941
3942 mCurrentCookedPointerData.clear();
3943 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3944 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3945 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3946
3947 // Walk through the the active pointers and map device coordinates onto
3948 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003949 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003950 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003951
Jeff Browna1f89ce2011-08-11 00:05:01 -07003952 // Size
3953 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3954 switch (mCalibration.sizeCalibration) {
3955 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3956 case Calibration::SIZE_CALIBRATION_DIAMETER:
3957 case Calibration::SIZE_CALIBRATION_AREA:
3958 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3959 touchMajor = in.touchMajor;
3960 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3961 toolMajor = in.toolMajor;
3962 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3963 size = mRawPointerAxes.touchMinor.valid
3964 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3965 } else if (mRawPointerAxes.touchMajor.valid) {
3966 toolMajor = touchMajor = in.touchMajor;
3967 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3968 ? in.touchMinor : in.touchMajor;
3969 size = mRawPointerAxes.touchMinor.valid
3970 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3971 } else if (mRawPointerAxes.toolMajor.valid) {
3972 touchMajor = toolMajor = in.toolMajor;
3973 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3974 ? in.toolMinor : in.toolMajor;
3975 size = mRawPointerAxes.toolMinor.valid
3976 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003977 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003978 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003979 "Size calibration should have been resolved to NONE.");
3980 touchMajor = 0;
3981 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003982 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003983 toolMinor = 0;
3984 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003985 }
Jeff Brownace13b12011-03-09 17:39:48 -08003986
Jeff Browna1f89ce2011-08-11 00:05:01 -07003987 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3988 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3989 if (touchingCount > 1) {
3990 touchMajor /= touchingCount;
3991 touchMinor /= touchingCount;
3992 toolMajor /= touchingCount;
3993 toolMinor /= touchingCount;
3994 size /= touchingCount;
3995 }
3996 }
Jeff Brownace13b12011-03-09 17:39:48 -08003997
Jeff Browna1f89ce2011-08-11 00:05:01 -07003998 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3999 touchMajor *= mGeometricScale;
4000 touchMinor *= mGeometricScale;
4001 toolMajor *= mGeometricScale;
4002 toolMinor *= mGeometricScale;
4003 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4004 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004005 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004006 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4007 toolMinor = toolMajor;
4008 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4009 touchMinor = touchMajor;
4010 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004011 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004012
4013 mCalibration.applySizeScaleAndBias(&touchMajor);
4014 mCalibration.applySizeScaleAndBias(&touchMinor);
4015 mCalibration.applySizeScaleAndBias(&toolMajor);
4016 mCalibration.applySizeScaleAndBias(&toolMinor);
4017 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004018 break;
4019 default:
4020 touchMajor = 0;
4021 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004022 toolMajor = 0;
4023 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004024 size = 0;
4025 break;
4026 }
4027
Jeff Browna1f89ce2011-08-11 00:05:01 -07004028 // Pressure
4029 float pressure;
4030 switch (mCalibration.pressureCalibration) {
4031 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4032 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4033 pressure = in.pressure * mPressureScale;
4034 break;
4035 default:
4036 pressure = in.isHovering ? 0 : 1;
4037 break;
4038 }
4039
Jeff Brown65fd2512011-08-18 11:20:58 -07004040 // Tilt and Orientation
4041 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004042 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004043 if (mHaveTilt) {
4044 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4045 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4046 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4047 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4048 } else {
4049 tilt = 0;
4050
4051 switch (mCalibration.orientationCalibration) {
4052 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
4053 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
4054 break;
4055 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4056 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4057 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4058 if (c1 != 0 || c2 != 0) {
4059 orientation = atan2f(c1, c2) * 0.5f;
4060 float confidence = hypotf(c1, c2);
4061 float scale = 1.0f + confidence / 16.0f;
4062 touchMajor *= scale;
4063 touchMinor /= scale;
4064 toolMajor *= scale;
4065 toolMinor /= scale;
4066 } else {
4067 orientation = 0;
4068 }
4069 break;
4070 }
4071 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004072 orientation = 0;
4073 }
Jeff Brownace13b12011-03-09 17:39:48 -08004074 }
4075
Jeff Brown80fd47c2011-05-24 01:07:44 -07004076 // Distance
4077 float distance;
4078 switch (mCalibration.distanceCalibration) {
4079 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004080 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004081 break;
4082 default:
4083 distance = 0;
4084 }
4085
Jeff Brownace13b12011-03-09 17:39:48 -08004086 // X and Y
4087 // Adjust coords for surface orientation.
4088 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004089 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004090 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004091 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
4092 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004093 orientation -= M_PI_2;
4094 if (orientation < - M_PI_2) {
4095 orientation += M_PI;
4096 }
4097 break;
4098 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004099 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
4100 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004101 break;
4102 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004103 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
4104 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004105 orientation += M_PI_2;
4106 if (orientation > M_PI_2) {
4107 orientation -= M_PI;
4108 }
4109 break;
4110 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004111 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
4112 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004113 break;
4114 }
4115
4116 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004117 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004118 out.clear();
4119 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4120 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4121 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4122 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4123 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4124 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
4125 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4126 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4127 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004128 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004129 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004130
4131 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004132 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4133 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004134 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004135 properties.id = id;
4136 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004137
Jeff Brownbe1aa822011-07-27 16:04:54 -07004138 // Write id index.
4139 mCurrentCookedPointerData.idToIndex[id] = i;
4140 }
Jeff Brownace13b12011-03-09 17:39:48 -08004141}
4142
Jeff Brown65fd2512011-08-18 11:20:58 -07004143void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4144 PointerUsage pointerUsage) {
4145 if (pointerUsage != mPointerUsage) {
4146 abortPointerUsage(when, policyFlags);
4147 mPointerUsage = pointerUsage;
4148 }
4149
4150 switch (mPointerUsage) {
4151 case POINTER_USAGE_GESTURES:
4152 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4153 break;
4154 case POINTER_USAGE_STYLUS:
4155 dispatchPointerStylus(when, policyFlags);
4156 break;
4157 case POINTER_USAGE_MOUSE:
4158 dispatchPointerMouse(when, policyFlags);
4159 break;
4160 default:
4161 break;
4162 }
4163}
4164
4165void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4166 switch (mPointerUsage) {
4167 case POINTER_USAGE_GESTURES:
4168 abortPointerGestures(when, policyFlags);
4169 break;
4170 case POINTER_USAGE_STYLUS:
4171 abortPointerStylus(when, policyFlags);
4172 break;
4173 case POINTER_USAGE_MOUSE:
4174 abortPointerMouse(when, policyFlags);
4175 break;
4176 default:
4177 break;
4178 }
4179
4180 mPointerUsage = POINTER_USAGE_NONE;
4181}
4182
Jeff Brown79ac9692011-04-19 21:20:10 -07004183void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4184 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004185 // Update current gesture coordinates.
4186 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004187 bool sendEvents = preparePointerGestures(when,
4188 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4189 if (!sendEvents) {
4190 return;
4191 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004192 if (finishPreviousGesture) {
4193 cancelPreviousGesture = false;
4194 }
Jeff Brownace13b12011-03-09 17:39:48 -08004195
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004196 // Update the pointer presentation and spots.
4197 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4198 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4199 if (finishPreviousGesture || cancelPreviousGesture) {
4200 mPointerController->clearSpots();
4201 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004202 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4203 mPointerGesture.currentGestureIdToIndex,
4204 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004205 } else {
4206 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4207 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004208
Jeff Brown538881e2011-05-25 18:23:38 -07004209 // Show or hide the pointer if needed.
4210 switch (mPointerGesture.currentGestureMode) {
4211 case PointerGesture::NEUTRAL:
4212 case PointerGesture::QUIET:
4213 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4214 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4215 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4216 // Remind the user of where the pointer is after finishing a gesture with spots.
4217 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4218 }
4219 break;
4220 case PointerGesture::TAP:
4221 case PointerGesture::TAP_DRAG:
4222 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4223 case PointerGesture::HOVER:
4224 case PointerGesture::PRESS:
4225 // Unfade the pointer when the current gesture manipulates the
4226 // area directly under the pointer.
4227 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4228 break;
4229 case PointerGesture::SWIPE:
4230 case PointerGesture::FREEFORM:
4231 // Fade the pointer when the current gesture manipulates a different
4232 // area and there are spots to guide the user experience.
4233 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4234 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4235 } else {
4236 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4237 }
4238 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004239 }
4240
Jeff Brownace13b12011-03-09 17:39:48 -08004241 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004242 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004243 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004244
4245 // Update last coordinates of pointers that have moved so that we observe the new
4246 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004247 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4248 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4249 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004250 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004251 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4252 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4253 bool moveNeeded = false;
4254 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004255 && !mPointerGesture.lastGestureIdBits.isEmpty()
4256 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004257 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4258 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004259 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004260 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004261 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004262 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4263 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004264 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004265 moveNeeded = true;
4266 }
Jeff Brownace13b12011-03-09 17:39:48 -08004267 }
4268
4269 // Send motion events for all pointers that went up or were canceled.
4270 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4271 if (!dispatchedGestureIdBits.isEmpty()) {
4272 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004273 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004274 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4275 AMOTION_EVENT_EDGE_FLAG_NONE,
4276 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004277 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4278 dispatchedGestureIdBits, -1,
4279 0, 0, mPointerGesture.downTime);
4280
4281 dispatchedGestureIdBits.clear();
4282 } else {
4283 BitSet32 upGestureIdBits;
4284 if (finishPreviousGesture) {
4285 upGestureIdBits = dispatchedGestureIdBits;
4286 } else {
4287 upGestureIdBits.value = dispatchedGestureIdBits.value
4288 & ~mPointerGesture.currentGestureIdBits.value;
4289 }
4290 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004291 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004292
Jeff Brown65fd2512011-08-18 11:20:58 -07004293 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004294 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004295 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4296 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004297 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4298 dispatchedGestureIdBits, id,
4299 0, 0, mPointerGesture.downTime);
4300
4301 dispatchedGestureIdBits.clearBit(id);
4302 }
4303 }
4304 }
4305
4306 // Send motion events for all pointers that moved.
4307 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004308 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004309 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4310 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004311 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4312 dispatchedGestureIdBits, -1,
4313 0, 0, mPointerGesture.downTime);
4314 }
4315
4316 // Send motion events for all pointers that went down.
4317 if (down) {
4318 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4319 & ~dispatchedGestureIdBits.value);
4320 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004321 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004322 dispatchedGestureIdBits.markBit(id);
4323
Jeff Brownace13b12011-03-09 17:39:48 -08004324 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004325 mPointerGesture.downTime = when;
4326 }
4327
Jeff Brown65fd2512011-08-18 11:20:58 -07004328 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004329 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004330 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004331 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4332 dispatchedGestureIdBits, id,
4333 0, 0, mPointerGesture.downTime);
4334 }
4335 }
4336
Jeff Brownace13b12011-03-09 17:39:48 -08004337 // Send motion events for hover.
4338 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004339 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004340 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4341 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4342 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004343 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4344 mPointerGesture.currentGestureIdBits, -1,
4345 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004346 } else if (dispatchedGestureIdBits.isEmpty()
4347 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4348 // Synthesize a hover move event after all pointers go up to indicate that
4349 // the pointer is hovering again even if the user is not currently touching
4350 // the touch pad. This ensures that a view will receive a fresh hover enter
4351 // event after a tap.
4352 float x, y;
4353 mPointerController->getPosition(&x, &y);
4354
4355 PointerProperties pointerProperties;
4356 pointerProperties.clear();
4357 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004358 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004359
4360 PointerCoords pointerCoords;
4361 pointerCoords.clear();
4362 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4363 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4364
Jeff Brown65fd2512011-08-18 11:20:58 -07004365 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004366 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4367 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4368 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004369 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004370 }
4371
4372 // Update state.
4373 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4374 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004375 mPointerGesture.lastGestureIdBits.clear();
4376 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004377 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4378 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004379 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004380 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004381 mPointerGesture.lastGestureProperties[index].copyFrom(
4382 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004383 mPointerGesture.lastGestureCoords[index].copyFrom(
4384 mPointerGesture.currentGestureCoords[index]);
4385 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004386 }
4387 }
4388}
4389
Jeff Brown65fd2512011-08-18 11:20:58 -07004390void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4391 // Cancel previously dispatches pointers.
4392 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4393 int32_t metaState = getContext()->getGlobalMetaState();
4394 int32_t buttonState = mCurrentButtonState;
4395 dispatchMotion(when, policyFlags, mSource,
4396 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4397 AMOTION_EVENT_EDGE_FLAG_NONE,
4398 mPointerGesture.lastGestureProperties,
4399 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4400 mPointerGesture.lastGestureIdBits, -1,
4401 0, 0, mPointerGesture.downTime);
4402 }
4403
4404 // Reset the current pointer gesture.
4405 mPointerGesture.reset();
4406 mPointerVelocityControl.reset();
4407
4408 // Remove any current spots.
4409 if (mPointerController != NULL) {
4410 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4411 mPointerController->clearSpots();
4412 }
4413}
4414
Jeff Brown79ac9692011-04-19 21:20:10 -07004415bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4416 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004417 *outCancelPreviousGesture = false;
4418 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004419
Jeff Brown79ac9692011-04-19 21:20:10 -07004420 // Handle TAP timeout.
4421 if (isTimeout) {
4422#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004423 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004424#endif
4425
4426 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004427 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004428 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004429 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004430 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004431 } else {
4432 // The tap is finished.
4433#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004434 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004435#endif
4436 *outFinishPreviousGesture = true;
4437
4438 mPointerGesture.activeGestureId = -1;
4439 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4440 mPointerGesture.currentGestureIdBits.clear();
4441
Jeff Brown65fd2512011-08-18 11:20:58 -07004442 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004443 return true;
4444 }
4445 }
4446
4447 // We did not handle this timeout.
4448 return false;
4449 }
4450
Jeff Brown65fd2512011-08-18 11:20:58 -07004451 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4452 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4453
Jeff Brownace13b12011-03-09 17:39:48 -08004454 // Update the velocity tracker.
4455 {
4456 VelocityTracker::Position positions[MAX_POINTERS];
4457 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004458 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004459 uint32_t id = idBits.clearFirstMarkedBit();
4460 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004461 positions[count].x = pointer.x * mPointerXMovementScale;
4462 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004463 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004464 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004465 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004466 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004467
Jeff Brownace13b12011-03-09 17:39:48 -08004468 // Pick a new active touch id if needed.
4469 // Choose an arbitrary pointer that just went down, if there is one.
4470 // Otherwise choose an arbitrary remaining pointer.
4471 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004472 // We keep the same active touch id for as long as possible.
4473 bool activeTouchChanged = false;
4474 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4475 int32_t activeTouchId = lastActiveTouchId;
4476 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004477 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004478 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004479 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004480 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004481 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004482 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004483 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004484 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004485 if (!mCurrentFingerIdBits.isEmpty()) {
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 } else {
4489 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004490 }
4491 }
4492
4493 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004494 bool isQuietTime = false;
4495 if (activeTouchId < 0) {
4496 mPointerGesture.resetQuietTime();
4497 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004498 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004499 if (!isQuietTime) {
4500 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4501 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4502 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004503 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004504 // Enter quiet time when exiting swipe or freeform state.
4505 // This is to prevent accidentally entering the hover state and flinging the
4506 // pointer when finishing a swipe and there is still one pointer left onscreen.
4507 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004508 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004509 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004510 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004511 // Enter quiet time when releasing the button and there are still two or more
4512 // fingers down. This may indicate that one finger was used to press the button
4513 // but it has not gone up yet.
4514 isQuietTime = true;
4515 }
4516 if (isQuietTime) {
4517 mPointerGesture.quietTime = when;
4518 }
Jeff Brownace13b12011-03-09 17:39:48 -08004519 }
4520 }
4521
4522 // Switch states based on button and pointer state.
4523 if (isQuietTime) {
4524 // Case 1: Quiet time. (QUIET)
4525#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004526 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004527 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004528#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004529 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4530 *outFinishPreviousGesture = true;
4531 }
Jeff Brownace13b12011-03-09 17:39:48 -08004532
4533 mPointerGesture.activeGestureId = -1;
4534 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004535 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004536
Jeff Brown65fd2512011-08-18 11:20:58 -07004537 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004538 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004539 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004540 // The pointer follows the active touch point.
4541 // Emit DOWN, MOVE, UP events at the pointer location.
4542 //
4543 // Only the active touch matters; other fingers are ignored. This policy helps
4544 // to handle the case where the user places a second finger on the touch pad
4545 // to apply the necessary force to depress an integrated button below the surface.
4546 // We don't want the second finger to be delivered to applications.
4547 //
4548 // For this to work well, we need to make sure to track the pointer that is really
4549 // active. If the user first puts one finger down to click then adds another
4550 // finger to drag then the active pointer should switch to the finger that is
4551 // being dragged.
4552#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004553 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004554 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004555#endif
4556 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004557 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004558 *outFinishPreviousGesture = true;
4559 mPointerGesture.activeGestureId = 0;
4560 }
4561
4562 // Switch pointers if needed.
4563 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004564 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004565 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004566 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004567 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004568 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004569 float vx, vy;
4570 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4571 float speed = hypotf(vx, vy);
4572 if (speed > bestSpeed) {
4573 bestId = id;
4574 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004575 }
Jeff Brown8d608662010-08-30 03:02:23 -07004576 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004577 }
4578 if (bestId >= 0 && bestId != activeTouchId) {
4579 mPointerGesture.activeTouchId = activeTouchId = bestId;
4580 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004581#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004582 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004583 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004584#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004585 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004586 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004587
Jeff Brown65fd2512011-08-18 11:20:58 -07004588 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004589 const RawPointerData::Pointer& currentPointer =
4590 mCurrentRawPointerData.pointerForId(activeTouchId);
4591 const RawPointerData::Pointer& lastPointer =
4592 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004593 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4594 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004595
Jeff Brownbe1aa822011-07-27 16:04:54 -07004596 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004597 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004598
4599 // Move the pointer using a relative motion.
4600 // When using spots, the click will occur at the position of the anchor
4601 // spot and all other spots will move there.
4602 mPointerController->move(deltaX, deltaY);
4603 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004604 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004605 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004606
Jeff Brownace13b12011-03-09 17:39:48 -08004607 float x, y;
4608 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004609
Jeff Brown79ac9692011-04-19 21:20:10 -07004610 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004611 mPointerGesture.currentGestureIdBits.clear();
4612 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4613 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004614 mPointerGesture.currentGestureProperties[0].clear();
4615 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004616 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004617 mPointerGesture.currentGestureCoords[0].clear();
4618 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4619 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4620 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004621 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004622 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004623 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4624 *outFinishPreviousGesture = true;
4625 }
Jeff Brownace13b12011-03-09 17:39:48 -08004626
Jeff Brown79ac9692011-04-19 21:20:10 -07004627 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004628 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004629 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004630 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4631 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004632 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004633 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004634 float x, y;
4635 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004636 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4637 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004638#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004639 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004640#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004641
4642 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004643 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004644 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004645
Jeff Brownace13b12011-03-09 17:39:48 -08004646 mPointerGesture.activeGestureId = 0;
4647 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004648 mPointerGesture.currentGestureIdBits.clear();
4649 mPointerGesture.currentGestureIdBits.markBit(
4650 mPointerGesture.activeGestureId);
4651 mPointerGesture.currentGestureIdToIndex[
4652 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004653 mPointerGesture.currentGestureProperties[0].clear();
4654 mPointerGesture.currentGestureProperties[0].id =
4655 mPointerGesture.activeGestureId;
4656 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004657 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004658 mPointerGesture.currentGestureCoords[0].clear();
4659 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004660 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004661 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004662 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004663 mPointerGesture.currentGestureCoords[0].setAxisValue(
4664 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004665
Jeff Brownace13b12011-03-09 17:39:48 -08004666 tapped = true;
4667 } else {
4668#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004669 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004670 x - mPointerGesture.tapX,
4671 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004672#endif
4673 }
4674 } else {
4675#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004676 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004677 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004678#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004679 }
Jeff Brownace13b12011-03-09 17:39:48 -08004680 }
Jeff Brown2352b972011-04-12 22:39:53 -07004681
Jeff Brown65fd2512011-08-18 11:20:58 -07004682 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004683
Jeff Brownace13b12011-03-09 17:39:48 -08004684 if (!tapped) {
4685#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004686 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004687#endif
4688 mPointerGesture.activeGestureId = -1;
4689 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004690 mPointerGesture.currentGestureIdBits.clear();
4691 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004692 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004693 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004694 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004695 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4696 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004697 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004698
Jeff Brown79ac9692011-04-19 21:20:10 -07004699 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4700 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004701 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004702 float x, y;
4703 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004704 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4705 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004706 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4707 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004708#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004709 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004710 x - mPointerGesture.tapX,
4711 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004712#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004713 }
4714 } else {
4715#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004716 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004717 (when - mPointerGesture.tapUpTime) * 0.000001f);
4718#endif
4719 }
4720 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4721 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4722 }
Jeff Brownace13b12011-03-09 17:39:48 -08004723
Jeff Brown65fd2512011-08-18 11:20:58 -07004724 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004725 const RawPointerData::Pointer& currentPointer =
4726 mCurrentRawPointerData.pointerForId(activeTouchId);
4727 const RawPointerData::Pointer& lastPointer =
4728 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004729 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004730 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004731 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004732 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004733
Jeff Brownbe1aa822011-07-27 16:04:54 -07004734 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004735 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004736
Jeff Brown2352b972011-04-12 22:39:53 -07004737 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004738 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004739 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004740 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004741 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004742 }
4743
Jeff Brown79ac9692011-04-19 21:20:10 -07004744 bool down;
4745 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4746#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004747 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004748#endif
4749 down = true;
4750 } else {
4751#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004752 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004753#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004754 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4755 *outFinishPreviousGesture = true;
4756 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004757 mPointerGesture.activeGestureId = 0;
4758 down = false;
4759 }
Jeff Brownace13b12011-03-09 17:39:48 -08004760
4761 float x, y;
4762 mPointerController->getPosition(&x, &y);
4763
Jeff Brownace13b12011-03-09 17:39:48 -08004764 mPointerGesture.currentGestureIdBits.clear();
4765 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4766 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004767 mPointerGesture.currentGestureProperties[0].clear();
4768 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4769 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004770 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004771 mPointerGesture.currentGestureCoords[0].clear();
4772 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4773 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004774 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4775 down ? 1.0f : 0.0f);
4776
Jeff Brown65fd2512011-08-18 11:20:58 -07004777 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004778 mPointerGesture.resetTap();
4779 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004780 mPointerGesture.tapX = x;
4781 mPointerGesture.tapY = y;
4782 }
Jeff Brownace13b12011-03-09 17:39:48 -08004783 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004784 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4785 // We need to provide feedback for each finger that goes down so we cannot wait
4786 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004787 //
Jeff Brown2352b972011-04-12 22:39:53 -07004788 // The ambiguous case is deciding what to do when there are two fingers down but they
4789 // have not moved enough to determine whether they are part of a drag or part of a
4790 // freeform gesture, or just a press or long-press at the pointer location.
4791 //
4792 // When there are two fingers we start with the PRESS hypothesis and we generate a
4793 // down at the pointer location.
4794 //
4795 // When the two fingers move enough or when additional fingers are added, we make
4796 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004797 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004798
Jeff Brown214eaf42011-05-26 19:17:02 -07004799 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004800 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004801 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004802 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4803 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004804 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004805 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004806 // Additional pointers have gone down but not yet settled.
4807 // Reset the gesture.
4808#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004809 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004810 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004811 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004812 * 0.000001f);
4813#endif
4814 *outCancelPreviousGesture = true;
4815 } else {
4816 // Continue previous gesture.
4817 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4818 }
4819
4820 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004821 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4822 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004823 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004824 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004825
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004826 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004827#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004828 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004829 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004830 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004831 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004832#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004833 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4834 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004835 &mPointerGesture.referenceTouchY);
4836 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4837 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004838 }
Jeff Brownace13b12011-03-09 17:39:48 -08004839
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004840 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004841 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004842 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4843 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004844 mPointerGesture.referenceDeltas[id].dx = 0;
4845 mPointerGesture.referenceDeltas[id].dy = 0;
4846 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004847 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004848
4849 // Add delta for all fingers and calculate a common movement delta.
4850 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004851 BitSet32 commonIdBits(mLastFingerIdBits.value
4852 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004853 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4854 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004855 uint32_t id = idBits.clearFirstMarkedBit();
4856 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4857 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004858 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4859 delta.dx += cpd.x - lpd.x;
4860 delta.dy += cpd.y - lpd.y;
4861
4862 if (first) {
4863 commonDeltaX = delta.dx;
4864 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004865 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004866 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4867 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4868 }
4869 }
Jeff Brownace13b12011-03-09 17:39:48 -08004870
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004871 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4872 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4873 float dist[MAX_POINTER_ID + 1];
4874 int32_t distOverThreshold = 0;
4875 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004876 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004877 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004878 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4879 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004880 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004881 distOverThreshold += 1;
4882 }
4883 }
4884
4885 // Only transition when at least two pointers have moved further than
4886 // the minimum distance threshold.
4887 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004888 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004889 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004890#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004891 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004892 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004893#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004894 *outCancelPreviousGesture = true;
4895 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4896 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004897 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004898 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004899 uint32_t id1 = idBits.clearFirstMarkedBit();
4900 uint32_t id2 = idBits.firstMarkedBit();
4901 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4902 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4903 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4904 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4905 // There are two pointers but they are too far apart for a SWIPE,
4906 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004907#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004908 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004909 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004910#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004911 *outCancelPreviousGesture = true;
4912 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4913 } else {
4914 // There are two pointers. Wait for both pointers to start moving
4915 // before deciding whether this is a SWIPE or FREEFORM gesture.
4916 float dist1 = dist[id1];
4917 float dist2 = dist[id2];
4918 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4919 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4920 // Calculate the dot product of the displacement vectors.
4921 // When the vectors are oriented in approximately the same direction,
4922 // the angle betweeen them is near zero and the cosine of the angle
4923 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4924 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4925 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004926 float dx1 = delta1.dx * mPointerXZoomScale;
4927 float dy1 = delta1.dy * mPointerYZoomScale;
4928 float dx2 = delta2.dx * mPointerXZoomScale;
4929 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004930 float dot = dx1 * dx2 + dy1 * dy2;
4931 float cosine = dot / (dist1 * dist2); // denominator always > 0
4932 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4933 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004934#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004935 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004936 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4937 "cosine %0.3f >= %0.3f",
4938 dist1, mConfig.pointerGestureMultitouchMinDistance,
4939 dist2, mConfig.pointerGestureMultitouchMinDistance,
4940 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004941#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004942 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4943 } else {
4944 // Pointers are moving in different directions. Switch to FREEFORM.
4945#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004946 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004947 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4948 "cosine %0.3f < %0.3f",
4949 dist1, mConfig.pointerGestureMultitouchMinDistance,
4950 dist2, mConfig.pointerGestureMultitouchMinDistance,
4951 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4952#endif
4953 *outCancelPreviousGesture = true;
4954 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4955 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004956 }
Jeff Brownace13b12011-03-09 17:39:48 -08004957 }
4958 }
Jeff Brownace13b12011-03-09 17:39:48 -08004959 }
4960 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004961 // Switch from SWIPE to FREEFORM if additional pointers go down.
4962 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004963 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004964#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004965 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004966 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004967#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004968 *outCancelPreviousGesture = true;
4969 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004970 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004971 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004972
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004973 // Move the reference points based on the overall group motion of the fingers
4974 // except in PRESS mode while waiting for a transition to occur.
4975 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4976 && (commonDeltaX || commonDeltaY)) {
4977 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004978 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004979 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004980 delta.dx = 0;
4981 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004982 }
4983
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004984 mPointerGesture.referenceTouchX += commonDeltaX;
4985 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004986
Jeff Brown65fd2512011-08-18 11:20:58 -07004987 commonDeltaX *= mPointerXMovementScale;
4988 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004989
Jeff Brownbe1aa822011-07-27 16:04:54 -07004990 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004991 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004992
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004993 mPointerGesture.referenceGestureX += commonDeltaX;
4994 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004995 }
4996
4997 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004998 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4999 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5000 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005001#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005002 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005003 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005004 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005005#endif
Steve Blockec193de2012-01-09 18:35:44 +00005006 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005007
5008 mPointerGesture.currentGestureIdBits.clear();
5009 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5010 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005011 mPointerGesture.currentGestureProperties[0].clear();
5012 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5013 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005014 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005015 mPointerGesture.currentGestureCoords[0].clear();
5016 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5017 mPointerGesture.referenceGestureX);
5018 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5019 mPointerGesture.referenceGestureY);
5020 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005021 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5022 // FREEFORM mode.
5023#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005024 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005025 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005026 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005027#endif
Steve Blockec193de2012-01-09 18:35:44 +00005028 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005029
Jeff Brownace13b12011-03-09 17:39:48 -08005030 mPointerGesture.currentGestureIdBits.clear();
5031
5032 BitSet32 mappedTouchIdBits;
5033 BitSet32 usedGestureIdBits;
5034 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5035 // Initially, assign the active gesture id to the active touch point
5036 // if there is one. No other touch id bits are mapped yet.
5037 if (!*outCancelPreviousGesture) {
5038 mappedTouchIdBits.markBit(activeTouchId);
5039 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5040 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5041 mPointerGesture.activeGestureId;
5042 } else {
5043 mPointerGesture.activeGestureId = -1;
5044 }
5045 } else {
5046 // Otherwise, assume we mapped all touches from the previous frame.
5047 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005048 mappedTouchIdBits.value = mLastFingerIdBits.value
5049 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005050 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5051
5052 // Check whether we need to choose a new active gesture id because the
5053 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005054 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5055 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005056 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005057 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005058 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5059 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5060 mPointerGesture.activeGestureId = -1;
5061 break;
5062 }
5063 }
5064 }
5065
5066#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005067 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005068 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5069 "activeGestureId=%d",
5070 mappedTouchIdBits.value, usedGestureIdBits.value,
5071 mPointerGesture.activeGestureId);
5072#endif
5073
Jeff Brown65fd2512011-08-18 11:20:58 -07005074 BitSet32 idBits(mCurrentFingerIdBits);
5075 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005076 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005077 uint32_t gestureId;
5078 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005079 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005080 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5081#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005082 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005083 "new mapping for touch id %d -> gesture id %d",
5084 touchId, gestureId);
5085#endif
5086 } else {
5087 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5088#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005089 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005090 "existing mapping for touch id %d -> gesture id %d",
5091 touchId, gestureId);
5092#endif
5093 }
5094 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5095 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5096
Jeff Brownbe1aa822011-07-27 16:04:54 -07005097 const RawPointerData::Pointer& pointer =
5098 mCurrentRawPointerData.pointerForId(touchId);
5099 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005100 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005101 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005102 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005103 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005104
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005105 mPointerGesture.currentGestureProperties[i].clear();
5106 mPointerGesture.currentGestureProperties[i].id = gestureId;
5107 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005108 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005109 mPointerGesture.currentGestureCoords[i].clear();
5110 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005111 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005112 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005113 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005114 mPointerGesture.currentGestureCoords[i].setAxisValue(
5115 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5116 }
5117
5118 if (mPointerGesture.activeGestureId < 0) {
5119 mPointerGesture.activeGestureId =
5120 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5121#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005122 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005123 "activeGestureId=%d", mPointerGesture.activeGestureId);
5124#endif
5125 }
Jeff Brown2352b972011-04-12 22:39:53 -07005126 }
Jeff Brownace13b12011-03-09 17:39:48 -08005127 }
5128
Jeff Brownbe1aa822011-07-27 16:04:54 -07005129 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005130
Jeff Brownace13b12011-03-09 17:39:48 -08005131#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005132 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005133 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5134 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005135 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005136 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5137 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005138 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005139 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005140 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005141 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005142 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005143 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005144 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5145 id, index, properties.toolType,
5146 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005147 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5148 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5149 }
5150 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005151 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005152 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005153 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005154 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005155 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005156 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5157 id, index, properties.toolType,
5158 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005159 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5160 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5161 }
5162#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005163 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005164}
5165
Jeff Brown65fd2512011-08-18 11:20:58 -07005166void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5167 mPointerSimple.currentCoords.clear();
5168 mPointerSimple.currentProperties.clear();
5169
5170 bool down, hovering;
5171 if (!mCurrentStylusIdBits.isEmpty()) {
5172 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5173 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5174 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5175 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5176 mPointerController->setPosition(x, y);
5177
5178 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5179 down = !hovering;
5180
5181 mPointerController->getPosition(&x, &y);
5182 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5183 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5184 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5185 mPointerSimple.currentProperties.id = 0;
5186 mPointerSimple.currentProperties.toolType =
5187 mCurrentCookedPointerData.pointerProperties[index].toolType;
5188 } else {
5189 down = false;
5190 hovering = false;
5191 }
5192
5193 dispatchPointerSimple(when, policyFlags, down, hovering);
5194}
5195
5196void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5197 abortPointerSimple(when, policyFlags);
5198}
5199
5200void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5201 mPointerSimple.currentCoords.clear();
5202 mPointerSimple.currentProperties.clear();
5203
5204 bool down, hovering;
5205 if (!mCurrentMouseIdBits.isEmpty()) {
5206 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5207 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5208 if (mLastMouseIdBits.hasBit(id)) {
5209 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5210 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5211 - mLastRawPointerData.pointers[lastIndex].x)
5212 * mPointerXMovementScale;
5213 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5214 - mLastRawPointerData.pointers[lastIndex].y)
5215 * mPointerYMovementScale;
5216
5217 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5218 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5219
5220 mPointerController->move(deltaX, deltaY);
5221 } else {
5222 mPointerVelocityControl.reset();
5223 }
5224
5225 down = isPointerDown(mCurrentButtonState);
5226 hovering = !down;
5227
5228 float x, y;
5229 mPointerController->getPosition(&x, &y);
5230 mPointerSimple.currentCoords.copyFrom(
5231 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5232 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5233 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5234 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5235 hovering ? 0.0f : 1.0f);
5236 mPointerSimple.currentProperties.id = 0;
5237 mPointerSimple.currentProperties.toolType =
5238 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5239 } else {
5240 mPointerVelocityControl.reset();
5241
5242 down = false;
5243 hovering = false;
5244 }
5245
5246 dispatchPointerSimple(when, policyFlags, down, hovering);
5247}
5248
5249void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5250 abortPointerSimple(when, policyFlags);
5251
5252 mPointerVelocityControl.reset();
5253}
5254
5255void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5256 bool down, bool hovering) {
5257 int32_t metaState = getContext()->getGlobalMetaState();
5258
5259 if (mPointerController != NULL) {
5260 if (down || hovering) {
5261 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5262 mPointerController->clearSpots();
5263 mPointerController->setButtonState(mCurrentButtonState);
5264 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5265 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5266 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5267 }
5268 }
5269
5270 if (mPointerSimple.down && !down) {
5271 mPointerSimple.down = false;
5272
5273 // Send up.
5274 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5275 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5276 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5277 mOrientedXPrecision, mOrientedYPrecision,
5278 mPointerSimple.downTime);
5279 getListener()->notifyMotion(&args);
5280 }
5281
5282 if (mPointerSimple.hovering && !hovering) {
5283 mPointerSimple.hovering = false;
5284
5285 // Send hover exit.
5286 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5287 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5288 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5289 mOrientedXPrecision, mOrientedYPrecision,
5290 mPointerSimple.downTime);
5291 getListener()->notifyMotion(&args);
5292 }
5293
5294 if (down) {
5295 if (!mPointerSimple.down) {
5296 mPointerSimple.down = true;
5297 mPointerSimple.downTime = when;
5298
5299 // Send down.
5300 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5301 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5302 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5303 mOrientedXPrecision, mOrientedYPrecision,
5304 mPointerSimple.downTime);
5305 getListener()->notifyMotion(&args);
5306 }
5307
5308 // Send move.
5309 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5310 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5311 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5312 mOrientedXPrecision, mOrientedYPrecision,
5313 mPointerSimple.downTime);
5314 getListener()->notifyMotion(&args);
5315 }
5316
5317 if (hovering) {
5318 if (!mPointerSimple.hovering) {
5319 mPointerSimple.hovering = true;
5320
5321 // Send hover enter.
5322 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5323 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5324 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5325 mOrientedXPrecision, mOrientedYPrecision,
5326 mPointerSimple.downTime);
5327 getListener()->notifyMotion(&args);
5328 }
5329
5330 // Send hover move.
5331 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5332 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5333 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5334 mOrientedXPrecision, mOrientedYPrecision,
5335 mPointerSimple.downTime);
5336 getListener()->notifyMotion(&args);
5337 }
5338
5339 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5340 float vscroll = mCurrentRawVScroll;
5341 float hscroll = mCurrentRawHScroll;
5342 mWheelYVelocityControl.move(when, NULL, &vscroll);
5343 mWheelXVelocityControl.move(when, &hscroll, NULL);
5344
5345 // Send scroll.
5346 PointerCoords pointerCoords;
5347 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5348 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5349 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5350
5351 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5352 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5353 1, &mPointerSimple.currentProperties, &pointerCoords,
5354 mOrientedXPrecision, mOrientedYPrecision,
5355 mPointerSimple.downTime);
5356 getListener()->notifyMotion(&args);
5357 }
5358
5359 // Save state.
5360 if (down || hovering) {
5361 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5362 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5363 } else {
5364 mPointerSimple.reset();
5365 }
5366}
5367
5368void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5369 mPointerSimple.currentCoords.clear();
5370 mPointerSimple.currentProperties.clear();
5371
5372 dispatchPointerSimple(when, policyFlags, false, false);
5373}
5374
Jeff Brownace13b12011-03-09 17:39:48 -08005375void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005376 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5377 const PointerProperties* properties, const PointerCoords* coords,
5378 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005379 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5380 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005381 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005382 uint32_t pointerCount = 0;
5383 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005384 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005385 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005386 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005387 pointerCoords[pointerCount].copyFrom(coords[index]);
5388
5389 if (changedId >= 0 && id == uint32_t(changedId)) {
5390 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5391 }
5392
5393 pointerCount += 1;
5394 }
5395
Steve Blockec193de2012-01-09 18:35:44 +00005396 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005397
5398 if (changedId >= 0 && pointerCount == 1) {
5399 // Replace initial down and final up action.
5400 // We can compare the action without masking off the changed pointer index
5401 // because we know the index is 0.
5402 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5403 action = AMOTION_EVENT_ACTION_DOWN;
5404 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5405 action = AMOTION_EVENT_ACTION_UP;
5406 } else {
5407 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005408 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005409 }
5410 }
5411
Jeff Brownbe1aa822011-07-27 16:04:54 -07005412 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005413 action, flags, metaState, buttonState, edgeFlags,
5414 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005415 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005416}
5417
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005418bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005419 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005420 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5421 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005422 bool changed = false;
5423 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005424 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005425 uint32_t inIndex = inIdToIndex[id];
5426 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005427
5428 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005429 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005430 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005431 PointerCoords& curOutCoords = outCoords[outIndex];
5432
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005433 if (curInProperties != curOutProperties) {
5434 curOutProperties.copyFrom(curInProperties);
5435 changed = true;
5436 }
5437
Jeff Brownace13b12011-03-09 17:39:48 -08005438 if (curInCoords != curOutCoords) {
5439 curOutCoords.copyFrom(curInCoords);
5440 changed = true;
5441 }
5442 }
5443 return changed;
5444}
5445
5446void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005447 if (mPointerController != NULL) {
5448 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5449 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005450}
5451
Jeff Brownbe1aa822011-07-27 16:04:54 -07005452bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5453 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5454 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005455}
5456
Jeff Brownbe1aa822011-07-27 16:04:54 -07005457const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005458 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005459 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005460 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005461 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005462
5463#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005464 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005465 "left=%d, top=%d, right=%d, bottom=%d",
5466 x, y,
5467 virtualKey.keyCode, virtualKey.scanCode,
5468 virtualKey.hitLeft, virtualKey.hitTop,
5469 virtualKey.hitRight, virtualKey.hitBottom);
5470#endif
5471
5472 if (virtualKey.isHit(x, y)) {
5473 return & virtualKey;
5474 }
5475 }
5476
5477 return NULL;
5478}
5479
Jeff Brownbe1aa822011-07-27 16:04:54 -07005480void TouchInputMapper::assignPointerIds() {
5481 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5482 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5483
5484 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005485
5486 if (currentPointerCount == 0) {
5487 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005488 return;
5489 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005490
Jeff Brownbe1aa822011-07-27 16:04:54 -07005491 if (lastPointerCount == 0) {
5492 // All pointers are new.
5493 for (uint32_t i = 0; i < currentPointerCount; i++) {
5494 uint32_t id = i;
5495 mCurrentRawPointerData.pointers[i].id = id;
5496 mCurrentRawPointerData.idToIndex[id] = i;
5497 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5498 }
5499 return;
5500 }
5501
5502 if (currentPointerCount == 1 && lastPointerCount == 1
5503 && mCurrentRawPointerData.pointers[0].toolType
5504 == mLastRawPointerData.pointers[0].toolType) {
5505 // Only one pointer and no change in count so it must have the same id as before.
5506 uint32_t id = mLastRawPointerData.pointers[0].id;
5507 mCurrentRawPointerData.pointers[0].id = id;
5508 mCurrentRawPointerData.idToIndex[id] = 0;
5509 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5510 return;
5511 }
5512
5513 // General case.
5514 // We build a heap of squared euclidean distances between current and last pointers
5515 // associated with the current and last pointer indices. Then, we find the best
5516 // match (by distance) for each current pointer.
5517 // The pointers must have the same tool type but it is possible for them to
5518 // transition from hovering to touching or vice-versa while retaining the same id.
5519 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5520
5521 uint32_t heapSize = 0;
5522 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5523 currentPointerIndex++) {
5524 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5525 lastPointerIndex++) {
5526 const RawPointerData::Pointer& currentPointer =
5527 mCurrentRawPointerData.pointers[currentPointerIndex];
5528 const RawPointerData::Pointer& lastPointer =
5529 mLastRawPointerData.pointers[lastPointerIndex];
5530 if (currentPointer.toolType == lastPointer.toolType) {
5531 int64_t deltaX = currentPointer.x - lastPointer.x;
5532 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005533
5534 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5535
5536 // Insert new element into the heap (sift up).
5537 heap[heapSize].currentPointerIndex = currentPointerIndex;
5538 heap[heapSize].lastPointerIndex = lastPointerIndex;
5539 heap[heapSize].distance = distance;
5540 heapSize += 1;
5541 }
5542 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005543 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005544
Jeff Brownbe1aa822011-07-27 16:04:54 -07005545 // Heapify
5546 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5547 startIndex -= 1;
5548 for (uint32_t parentIndex = startIndex; ;) {
5549 uint32_t childIndex = parentIndex * 2 + 1;
5550 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005551 break;
5552 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005553
5554 if (childIndex + 1 < heapSize
5555 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5556 childIndex += 1;
5557 }
5558
5559 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5560 break;
5561 }
5562
5563 swap(heap[parentIndex], heap[childIndex]);
5564 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005565 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005566 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005567
5568#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005569 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005570 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005571 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005572 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5573 heap[i].distance);
5574 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005575#endif
5576
Jeff Brownbe1aa822011-07-27 16:04:54 -07005577 // Pull matches out by increasing order of distance.
5578 // To avoid reassigning pointers that have already been matched, the loop keeps track
5579 // of which last and current pointers have been matched using the matchedXXXBits variables.
5580 // It also tracks the used pointer id bits.
5581 BitSet32 matchedLastBits(0);
5582 BitSet32 matchedCurrentBits(0);
5583 BitSet32 usedIdBits(0);
5584 bool first = true;
5585 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5586 while (heapSize > 0) {
5587 if (first) {
5588 // The first time through the loop, we just consume the root element of
5589 // the heap (the one with smallest distance).
5590 first = false;
5591 } else {
5592 // Previous iterations consumed the root element of the heap.
5593 // Pop root element off of the heap (sift down).
5594 heap[0] = heap[heapSize];
5595 for (uint32_t parentIndex = 0; ;) {
5596 uint32_t childIndex = parentIndex * 2 + 1;
5597 if (childIndex >= heapSize) {
5598 break;
5599 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005600
Jeff Brownbe1aa822011-07-27 16:04:54 -07005601 if (childIndex + 1 < heapSize
5602 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5603 childIndex += 1;
5604 }
5605
5606 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5607 break;
5608 }
5609
5610 swap(heap[parentIndex], heap[childIndex]);
5611 parentIndex = childIndex;
5612 }
5613
5614#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005615 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005616 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005617 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005618 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5619 heap[i].distance);
5620 }
5621#endif
5622 }
5623
5624 heapSize -= 1;
5625
5626 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5627 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5628
5629 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5630 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5631
5632 matchedCurrentBits.markBit(currentPointerIndex);
5633 matchedLastBits.markBit(lastPointerIndex);
5634
5635 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5636 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5637 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5638 mCurrentRawPointerData.markIdBit(id,
5639 mCurrentRawPointerData.isHovering(currentPointerIndex));
5640 usedIdBits.markBit(id);
5641
5642#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005643 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005644 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5645#endif
5646 break;
5647 }
5648 }
5649
5650 // Assign fresh ids to pointers that were not matched in the process.
5651 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5652 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5653 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5654
5655 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5656 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5657 mCurrentRawPointerData.markIdBit(id,
5658 mCurrentRawPointerData.isHovering(currentPointerIndex));
5659
5660#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005661 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005662 currentPointerIndex, id);
5663#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005664 }
5665}
5666
Jeff Brown6d0fec22010-07-23 21:28:06 -07005667int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005668 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5669 return AKEY_STATE_VIRTUAL;
5670 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005671
Jeff Brownbe1aa822011-07-27 16:04:54 -07005672 size_t numVirtualKeys = mVirtualKeys.size();
5673 for (size_t i = 0; i < numVirtualKeys; i++) {
5674 const VirtualKey& virtualKey = mVirtualKeys[i];
5675 if (virtualKey.keyCode == keyCode) {
5676 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005677 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005678 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005679
5680 return AKEY_STATE_UNKNOWN;
5681}
5682
5683int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005684 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5685 return AKEY_STATE_VIRTUAL;
5686 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005687
Jeff Brownbe1aa822011-07-27 16:04:54 -07005688 size_t numVirtualKeys = mVirtualKeys.size();
5689 for (size_t i = 0; i < numVirtualKeys; i++) {
5690 const VirtualKey& virtualKey = mVirtualKeys[i];
5691 if (virtualKey.scanCode == scanCode) {
5692 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005693 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005694 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005695
5696 return AKEY_STATE_UNKNOWN;
5697}
5698
5699bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5700 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005701 size_t numVirtualKeys = mVirtualKeys.size();
5702 for (size_t i = 0; i < numVirtualKeys; i++) {
5703 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005704
Jeff Brownbe1aa822011-07-27 16:04:54 -07005705 for (size_t i = 0; i < numCodes; i++) {
5706 if (virtualKey.keyCode == keyCodes[i]) {
5707 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005708 }
5709 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005710 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005711
5712 return true;
5713}
5714
5715
5716// --- SingleTouchInputMapper ---
5717
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005718SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5719 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005720}
5721
5722SingleTouchInputMapper::~SingleTouchInputMapper() {
5723}
5724
Jeff Brown65fd2512011-08-18 11:20:58 -07005725void SingleTouchInputMapper::reset(nsecs_t when) {
5726 mSingleTouchMotionAccumulator.reset(getDevice());
5727
5728 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005729}
5730
Jeff Brown6d0fec22010-07-23 21:28:06 -07005731void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005732 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005733
Jeff Brown65fd2512011-08-18 11:20:58 -07005734 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005735}
5736
Jeff Brown65fd2512011-08-18 11:20:58 -07005737void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005738 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005739 mCurrentRawPointerData.pointerCount = 1;
5740 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005741
Jeff Brown65fd2512011-08-18 11:20:58 -07005742 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5743 && (mTouchButtonAccumulator.isHovering()
5744 || (mRawPointerAxes.pressure.valid
5745 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005746 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005747
Jeff Brownbe1aa822011-07-27 16:04:54 -07005748 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005749 outPointer.id = 0;
5750 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5751 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5752 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5753 outPointer.touchMajor = 0;
5754 outPointer.touchMinor = 0;
5755 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5756 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5757 outPointer.orientation = 0;
5758 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005759 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5760 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005761 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5762 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5763 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5764 }
5765 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005766 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005767}
5768
Jeff Brownbe1aa822011-07-27 16:04:54 -07005769void SingleTouchInputMapper::configureRawPointerAxes() {
5770 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005771
Jeff Brownbe1aa822011-07-27 16:04:54 -07005772 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5773 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5774 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5775 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5776 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005777 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5778 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005779}
5780
Jeff Brown00710e92012-04-19 15:18:26 -07005781bool SingleTouchInputMapper::hasStylus() const {
5782 return mTouchButtonAccumulator.hasStylus();
5783}
5784
Jeff Brown6d0fec22010-07-23 21:28:06 -07005785
5786// --- MultiTouchInputMapper ---
5787
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005788MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005789 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005790}
5791
5792MultiTouchInputMapper::~MultiTouchInputMapper() {
5793}
5794
Jeff Brown65fd2512011-08-18 11:20:58 -07005795void MultiTouchInputMapper::reset(nsecs_t when) {
5796 mMultiTouchMotionAccumulator.reset(getDevice());
5797
Jeff Brown6894a292011-07-01 17:59:27 -07005798 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005799
Jeff Brown65fd2512011-08-18 11:20:58 -07005800 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005801}
5802
5803void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005804 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005805
Jeff Brown65fd2512011-08-18 11:20:58 -07005806 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005807}
5808
Jeff Brown65fd2512011-08-18 11:20:58 -07005809void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005810 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005811 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005812 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005813
Jeff Brown80fd47c2011-05-24 01:07:44 -07005814 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005815 const MultiTouchMotionAccumulator::Slot* inSlot =
5816 mMultiTouchMotionAccumulator.getSlot(inIndex);
5817 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005818 continue;
5819 }
5820
Jeff Brown80fd47c2011-05-24 01:07:44 -07005821 if (outCount >= MAX_POINTERS) {
5822#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005823 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005824 "ignoring the rest.",
5825 getDeviceName().string(), MAX_POINTERS);
5826#endif
5827 break; // too many fingers!
5828 }
5829
Jeff Brownbe1aa822011-07-27 16:04:54 -07005830 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005831 outPointer.x = inSlot->getX();
5832 outPointer.y = inSlot->getY();
5833 outPointer.pressure = inSlot->getPressure();
5834 outPointer.touchMajor = inSlot->getTouchMajor();
5835 outPointer.touchMinor = inSlot->getTouchMinor();
5836 outPointer.toolMajor = inSlot->getToolMajor();
5837 outPointer.toolMinor = inSlot->getToolMinor();
5838 outPointer.orientation = inSlot->getOrientation();
5839 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005840 outPointer.tiltX = 0;
5841 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005842
Jeff Brown49754db2011-07-01 17:37:58 -07005843 outPointer.toolType = inSlot->getToolType();
5844 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5845 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5846 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5847 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5848 }
Jeff Brown8d608662010-08-30 03:02:23 -07005849 }
5850
Jeff Brown65fd2512011-08-18 11:20:58 -07005851 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5852 && (mTouchButtonAccumulator.isHovering()
5853 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005854 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005855
Jeff Brown8d608662010-08-30 03:02:23 -07005856 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005857 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005858 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005859 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005860 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005861 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005862 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005863 if (mPointerTrackingIdMap[n] == trackingId) {
5864 id = n;
5865 }
5866 }
5867
5868 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005869 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005870 mPointerTrackingIdMap[id] = trackingId;
5871 }
5872 }
5873 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005874 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005875 mCurrentRawPointerData.clearIdBits();
5876 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005877 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005878 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005879 mCurrentRawPointerData.idToIndex[id] = outCount;
5880 mCurrentRawPointerData.markIdBit(id, isHovering);
5881 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005882 }
5883 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005884
Jeff Brown6d0fec22010-07-23 21:28:06 -07005885 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005886 }
5887
Jeff Brownbe1aa822011-07-27 16:04:54 -07005888 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005889 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005890
Jeff Brown65fd2512011-08-18 11:20:58 -07005891 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005892}
5893
Jeff Brownbe1aa822011-07-27 16:04:54 -07005894void MultiTouchInputMapper::configureRawPointerAxes() {
5895 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005896
Jeff Brownbe1aa822011-07-27 16:04:54 -07005897 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5898 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5899 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5900 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5901 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5902 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5903 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5904 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5905 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5906 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5907 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005908
Jeff Brownbe1aa822011-07-27 16:04:54 -07005909 if (mRawPointerAxes.trackingId.valid
5910 && mRawPointerAxes.slot.valid
5911 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5912 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005913 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005914 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005915 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005916 getDeviceName().string(), slotCount, MAX_SLOTS);
5917 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005918 }
Jeff Brown00710e92012-04-19 15:18:26 -07005919 mMultiTouchMotionAccumulator.configure(getDevice(),
5920 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005921 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07005922 mMultiTouchMotionAccumulator.configure(getDevice(),
5923 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005924 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005925}
5926
Jeff Brown00710e92012-04-19 15:18:26 -07005927bool MultiTouchInputMapper::hasStylus() const {
5928 return mMultiTouchMotionAccumulator.hasStylus()
5929 || mTouchButtonAccumulator.hasStylus();
5930}
5931
Jeff Brown46b9ac02010-04-22 18:58:52 -07005932
Jeff Browncb1404e2011-01-15 18:14:15 -08005933// --- JoystickInputMapper ---
5934
5935JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5936 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005937}
5938
5939JoystickInputMapper::~JoystickInputMapper() {
5940}
5941
5942uint32_t JoystickInputMapper::getSources() {
5943 return AINPUT_SOURCE_JOYSTICK;
5944}
5945
5946void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5947 InputMapper::populateDeviceInfo(info);
5948
Jeff Brown6f2fba42011-02-19 01:08:02 -08005949 for (size_t i = 0; i < mAxes.size(); i++) {
5950 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005951 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5952 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005953 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005954 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5955 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005956 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005957 }
5958}
5959
5960void JoystickInputMapper::dump(String8& dump) {
5961 dump.append(INDENT2 "Joystick Input Mapper:\n");
5962
Jeff Brown6f2fba42011-02-19 01:08:02 -08005963 dump.append(INDENT3 "Axes:\n");
5964 size_t numAxes = mAxes.size();
5965 for (size_t i = 0; i < numAxes; i++) {
5966 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005967 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005968 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005969 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005970 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005971 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005972 }
Jeff Brown85297452011-03-04 13:07:49 -08005973 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5974 label = getAxisLabel(axis.axisInfo.highAxis);
5975 if (label) {
5976 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5977 } else {
5978 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5979 axis.axisInfo.splitValue);
5980 }
5981 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5982 dump.append(" (invert)");
5983 }
5984
5985 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5986 axis.min, axis.max, axis.flat, axis.fuzz);
5987 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5988 "highScale=%0.5f, highOffset=%0.5f\n",
5989 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005990 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5991 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005992 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005993 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005994 }
5995}
5996
Jeff Brown65fd2512011-08-18 11:20:58 -07005997void JoystickInputMapper::configure(nsecs_t when,
5998 const InputReaderConfiguration* config, uint32_t changes) {
5999 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006000
Jeff Brown474dcb52011-06-14 20:22:50 -07006001 if (!changes) { // first time only
6002 // Collect all axes.
6003 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285a2011-08-31 12:56:34 -07006004 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6005 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6006 continue; // axis must be claimed by a different device
6007 }
6008
Jeff Brown474dcb52011-06-14 20:22:50 -07006009 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006010 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006011 if (rawAxisInfo.valid) {
6012 // Map axis.
6013 AxisInfo axisInfo;
6014 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6015 if (!explicitlyMapped) {
6016 // Axis is not explicitly mapped, will choose a generic axis later.
6017 axisInfo.mode = AxisInfo::MODE_NORMAL;
6018 axisInfo.axis = -1;
6019 }
6020
6021 // Apply flat override.
6022 int32_t rawFlat = axisInfo.flatOverride < 0
6023 ? rawAxisInfo.flat : axisInfo.flatOverride;
6024
6025 // Calculate scaling factors and limits.
6026 Axis axis;
6027 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6028 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6029 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6030 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6031 scale, 0.0f, highScale, 0.0f,
6032 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6033 } else if (isCenteredAxis(axisInfo.axis)) {
6034 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6035 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6036 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6037 scale, offset, scale, offset,
6038 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6039 } else {
6040 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6041 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6042 scale, 0.0f, scale, 0.0f,
6043 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
6044 }
6045
6046 // To eliminate noise while the joystick is at rest, filter out small variations
6047 // in axis values up front.
6048 axis.filter = axis.flat * 0.25f;
6049
6050 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006051 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006052 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006053
Jeff Brown474dcb52011-06-14 20:22:50 -07006054 // If there are too many axes, start dropping them.
6055 // Prefer to keep explicitly mapped axes.
6056 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006057 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006058 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6059 pruneAxes(true);
6060 pruneAxes(false);
6061 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006062
Jeff Brown474dcb52011-06-14 20:22:50 -07006063 // Assign generic axis ids to remaining axes.
6064 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6065 size_t numAxes = mAxes.size();
6066 for (size_t i = 0; i < numAxes; i++) {
6067 Axis& axis = mAxes.editValueAt(i);
6068 if (axis.axisInfo.axis < 0) {
6069 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6070 && haveAxis(nextGenericAxisId)) {
6071 nextGenericAxisId += 1;
6072 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006073
Jeff Brown474dcb52011-06-14 20:22:50 -07006074 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6075 axis.axisInfo.axis = nextGenericAxisId;
6076 nextGenericAxisId += 1;
6077 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006078 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006079 "have already been assigned to other axes.",
6080 getDeviceName().string(), mAxes.keyAt(i));
6081 mAxes.removeItemsAt(i--);
6082 numAxes -= 1;
6083 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006084 }
6085 }
6086 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006087}
6088
Jeff Brown85297452011-03-04 13:07:49 -08006089bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006090 size_t numAxes = mAxes.size();
6091 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006092 const Axis& axis = mAxes.valueAt(i);
6093 if (axis.axisInfo.axis == axisId
6094 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6095 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006096 return true;
6097 }
6098 }
6099 return false;
6100}
Jeff Browncb1404e2011-01-15 18:14:15 -08006101
Jeff Brown6f2fba42011-02-19 01:08:02 -08006102void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6103 size_t i = mAxes.size();
6104 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6105 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6106 continue;
6107 }
Steve Block6215d3f2012-01-04 20:05:49 +00006108 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006109 getDeviceName().string(), mAxes.keyAt(i));
6110 mAxes.removeItemsAt(i);
6111 }
6112}
6113
6114bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6115 switch (axis) {
6116 case AMOTION_EVENT_AXIS_X:
6117 case AMOTION_EVENT_AXIS_Y:
6118 case AMOTION_EVENT_AXIS_Z:
6119 case AMOTION_EVENT_AXIS_RX:
6120 case AMOTION_EVENT_AXIS_RY:
6121 case AMOTION_EVENT_AXIS_RZ:
6122 case AMOTION_EVENT_AXIS_HAT_X:
6123 case AMOTION_EVENT_AXIS_HAT_Y:
6124 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006125 case AMOTION_EVENT_AXIS_RUDDER:
6126 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006127 return true;
6128 default:
6129 return false;
6130 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006131}
6132
Jeff Brown65fd2512011-08-18 11:20:58 -07006133void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006134 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006135 size_t numAxes = mAxes.size();
6136 for (size_t i = 0; i < numAxes; i++) {
6137 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006138 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006139 }
6140
Jeff Brown65fd2512011-08-18 11:20:58 -07006141 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006142}
6143
6144void JoystickInputMapper::process(const RawEvent* rawEvent) {
6145 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006146 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006147 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006148 if (index >= 0) {
6149 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006150 float newValue, highNewValue;
6151 switch (axis.axisInfo.mode) {
6152 case AxisInfo::MODE_INVERT:
6153 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6154 * axis.scale + axis.offset;
6155 highNewValue = 0.0f;
6156 break;
6157 case AxisInfo::MODE_SPLIT:
6158 if (rawEvent->value < axis.axisInfo.splitValue) {
6159 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6160 * axis.scale + axis.offset;
6161 highNewValue = 0.0f;
6162 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6163 newValue = 0.0f;
6164 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6165 * axis.highScale + axis.highOffset;
6166 } else {
6167 newValue = 0.0f;
6168 highNewValue = 0.0f;
6169 }
6170 break;
6171 default:
6172 newValue = rawEvent->value * axis.scale + axis.offset;
6173 highNewValue = 0.0f;
6174 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006175 }
Jeff Brown85297452011-03-04 13:07:49 -08006176 axis.newValue = newValue;
6177 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006178 }
6179 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006180 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006181
6182 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006183 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006184 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006185 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006186 break;
6187 }
6188 break;
6189 }
6190}
6191
Jeff Brown6f2fba42011-02-19 01:08:02 -08006192void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006193 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006194 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006195 }
6196
6197 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006198 int32_t buttonState = 0;
6199
6200 PointerProperties pointerProperties;
6201 pointerProperties.clear();
6202 pointerProperties.id = 0;
6203 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006204
Jeff Brown6f2fba42011-02-19 01:08:02 -08006205 PointerCoords pointerCoords;
6206 pointerCoords.clear();
6207
6208 size_t numAxes = mAxes.size();
6209 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006210 const Axis& axis = mAxes.valueAt(i);
6211 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6212 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6213 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6214 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006215 }
6216
Jeff Brown56194eb2011-03-02 19:23:13 -08006217 // Moving a joystick axis should not wake the devide because joysticks can
6218 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6219 // button will likely wake the device.
6220 // TODO: Use the input device configuration to control this behavior more finely.
6221 uint32_t policyFlags = 0;
6222
Jeff Brownbe1aa822011-07-27 16:04:54 -07006223 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006224 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
6225 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006226 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006227}
6228
Jeff Brown85297452011-03-04 13:07:49 -08006229bool JoystickInputMapper::filterAxes(bool force) {
6230 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006231 size_t numAxes = mAxes.size();
6232 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006233 Axis& axis = mAxes.editValueAt(i);
6234 if (force || hasValueChangedSignificantly(axis.filter,
6235 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6236 axis.currentValue = axis.newValue;
6237 atLeastOneSignificantChange = true;
6238 }
6239 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6240 if (force || hasValueChangedSignificantly(axis.filter,
6241 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6242 axis.highCurrentValue = axis.highNewValue;
6243 atLeastOneSignificantChange = true;
6244 }
6245 }
6246 }
6247 return atLeastOneSignificantChange;
6248}
6249
6250bool JoystickInputMapper::hasValueChangedSignificantly(
6251 float filter, float newValue, float currentValue, float min, float max) {
6252 if (newValue != currentValue) {
6253 // Filter out small changes in value unless the value is converging on the axis
6254 // bounds or center point. This is intended to reduce the amount of information
6255 // sent to applications by particularly noisy joysticks (such as PS3).
6256 if (fabs(newValue - currentValue) > filter
6257 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6258 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6259 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6260 return true;
6261 }
6262 }
6263 return false;
6264}
6265
6266bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6267 float filter, float newValue, float currentValue, float thresholdValue) {
6268 float newDistance = fabs(newValue - thresholdValue);
6269 if (newDistance < filter) {
6270 float oldDistance = fabs(currentValue - thresholdValue);
6271 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006272 return true;
6273 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006274 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006275 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006276}
6277
Jeff Brown46b9ac02010-04-22 18:58:52 -07006278} // namespace android