blob: 48c5223dd415edba6bdaf12768c7e3925aa64808 [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>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070045#include <input/Keyboard.h>
46#include <input/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
Jeff Brownd728bf52012-09-08 18:05:28 -0700206bool InputReaderConfiguration::getDisplayInfo(bool external, DisplayViewport* outViewport) const {
207 const DisplayViewport& viewport = external ? mExternalDisplay : mInternalDisplay;
208 if (viewport.displayId >= 0) {
209 *outViewport = viewport;
210 return true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700211 }
212 return false;
213}
214
Jeff Brownd728bf52012-09-08 18:05:28 -0700215void InputReaderConfiguration::setDisplayInfo(bool external, const DisplayViewport& viewport) {
216 DisplayViewport& v = external ? mExternalDisplay : mInternalDisplay;
217 v = viewport;
Jeff Brown65fd2512011-08-18 11:20:58 -0700218}
219
220
Jeff Brown46b9ac02010-04-22 18:58:52 -0700221// --- InputReader ---
222
223InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700224 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700225 const sp<InputListenerInterface>& listener) :
226 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700227 mGlobalMetaState(0), mGeneration(1),
228 mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700229 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700230 mQueuedListener = new QueuedInputListener(listener);
231
232 { // acquire lock
233 AutoMutex _l(mLock);
234
235 refreshConfigurationLocked(0);
236 updateGlobalMetaStateLocked();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700237 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700238}
239
240InputReader::~InputReader() {
241 for (size_t i = 0; i < mDevices.size(); i++) {
242 delete mDevices.valueAt(i);
243 }
244}
245
246void InputReader::loopOnce() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700247 int32_t oldGeneration;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700248 int32_t timeoutMillis;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700249 bool inputDevicesChanged = false;
250 Vector<InputDeviceInfo> inputDevices;
Jeff Brown474dcb52011-06-14 20:22:50 -0700251 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700252 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700253
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700254 oldGeneration = mGeneration;
255 timeoutMillis = -1;
256
Jeff Brownbe1aa822011-07-27 16:04:54 -0700257 uint32_t changes = mConfigurationChangesToRefresh;
258 if (changes) {
259 mConfigurationChangesToRefresh = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700260 timeoutMillis = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700261 refreshConfigurationLocked(changes);
Jeff Browna47425a2012-04-13 04:09:27 -0700262 } else if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700263 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
264 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
265 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700266 } // release lock
267
Jeff Brownb7198742011-03-18 18:14:26 -0700268 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700269
270 { // acquire lock
271 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800272 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700273
274 if (count) {
275 processEventsLocked(mEventBuffer, count);
276 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700277
278 if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700279 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800280 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700281#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800282 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700283#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800284 mNextTimeout = LLONG_MAX;
285 timeoutExpiredLocked(now);
286 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700287 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700288
289 if (oldGeneration != mGeneration) {
290 inputDevicesChanged = true;
291 getInputDevicesLocked(inputDevices);
292 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700293 } // release lock
294
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700295 // Send out a message that the describes the changed input devices.
296 if (inputDevicesChanged) {
297 mPolicy->notifyInputDevicesChanged(inputDevices);
298 }
299
Jeff Brownbe1aa822011-07-27 16:04:54 -0700300 // Flush queued events out to the listener.
301 // This must happen outside of the lock because the listener could potentially call
302 // back into the InputReader's methods, such as getScanCodeState, or become blocked
303 // on another thread similarly waiting to acquire the InputReader lock thereby
304 // resulting in a deadlock. This situation is actually quite plausible because the
305 // listener is actually the input dispatcher, which calls into the window manager,
306 // which occasionally calls into the input reader.
307 mQueuedListener->flush();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700308}
309
Jeff Brownbe1aa822011-07-27 16:04:54 -0700310void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700311 for (const RawEvent* rawEvent = rawEvents; count;) {
312 int32_t type = rawEvent->type;
313 size_t batchSize = 1;
314 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
315 int32_t deviceId = rawEvent->deviceId;
316 while (batchSize < count) {
317 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
318 || rawEvent[batchSize].deviceId != deviceId) {
319 break;
320 }
321 batchSize += 1;
322 }
323#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000324 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700325#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700326 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700327 } else {
328 switch (rawEvent->type) {
329 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700330 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700331 break;
332 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700333 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700334 break;
335 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700336 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700337 break;
338 default:
Steve Blockec193de2012-01-09 18:35:44 +0000339 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700340 break;
341 }
342 }
343 count -= batchSize;
344 rawEvent += batchSize;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345 }
346}
347
Jeff Brown65fd2512011-08-18 11:20:58 -0700348void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700349 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
350 if (deviceIndex >= 0) {
351 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
352 return;
353 }
354
Jeff Browne38fdfa2012-04-06 14:51:01 -0700355 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
Michael Wrightac6c78b2013-07-17 13:21:45 -0700357 int32_t controllerNumber = mEventHub->getDeviceControllerNumber(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700358
Michael Wrightac6c78b2013-07-17 13:21:45 -0700359 InputDevice* device = createDeviceLocked(deviceId, controllerNumber, identifier, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700360 device->configure(when, &mConfig, 0);
361 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362
Jeff Brown8d608662010-08-30 03:02:23 -0700363 if (device->isIgnored()) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700364 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
365 identifier.name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700366 } else {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700367 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
368 identifier.name.string(), device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700369 }
370
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700371 mDevices.add(deviceId, device);
372 bumpGenerationLocked();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700373}
374
Jeff Brown65fd2512011-08-18 11:20:58 -0700375void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700376 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700377 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700378 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000379 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700380 return;
381 }
382
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700383 device = mDevices.valueAt(deviceIndex);
384 mDevices.removeItemsAt(deviceIndex, 1);
385 bumpGenerationLocked();
386
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000388 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700389 device->getId(), device->getName().string());
390 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000391 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392 device->getId(), device->getName().string(), device->getSources());
393 }
394
Jeff Brown65fd2512011-08-18 11:20:58 -0700395 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700396 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700397}
398
Michael Wrightac6c78b2013-07-17 13:21:45 -0700399InputDevice* InputReader::createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700400 const InputDeviceIdentifier& identifier, uint32_t classes) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700401 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
Michael Wrightac6c78b2013-07-17 13:21:45 -0700402 controllerNumber, identifier, classes);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700403
Jeff Brown56194eb2011-03-02 19:23:13 -0800404 // External devices.
405 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
406 device->setExternal(true);
407 }
408
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 // Switch-like devices.
410 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
411 device->addMapper(new SwitchInputMapper(device));
412 }
413
Jeff Browna47425a2012-04-13 04:09:27 -0700414 // Vibrator-like devices.
415 if (classes & INPUT_DEVICE_CLASS_VIBRATOR) {
416 device->addMapper(new VibratorInputMapper(device));
417 }
418
Jeff Brown6d0fec22010-07-23 21:28:06 -0700419 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800420 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700421 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
422 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800423 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700424 }
425 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
426 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
427 }
428 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800429 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700430 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800431 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800432 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800433 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434
Jeff Brownefd32662011-03-08 15:13:06 -0800435 if (keyboardSource != 0) {
436 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700437 }
438
Jeff Brown83c09682010-12-23 17:50:18 -0800439 // Cursor-like devices.
440 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
441 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 }
443
Jeff Brown58a2da82011-01-25 16:02:22 -0800444 // Touchscreens and touchpad devices.
445 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800446 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800447 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800448 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449 }
450
Jeff Browncb1404e2011-01-15 18:14:15 -0800451 // Joystick-like devices.
452 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
453 device->addMapper(new JoystickInputMapper(device));
454 }
455
Jeff Brown6d0fec22010-07-23 21:28:06 -0700456 return device;
457}
458
Jeff Brownbe1aa822011-07-27 16:04:54 -0700459void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700460 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700461 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
462 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000463 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700464 return;
465 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700466
Jeff Brownbe1aa822011-07-27 16:04:54 -0700467 InputDevice* device = mDevices.valueAt(deviceIndex);
468 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000469 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700470 return;
471 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472
Jeff Brownbe1aa822011-07-27 16:04:54 -0700473 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700474}
475
Jeff Brownbe1aa822011-07-27 16:04:54 -0700476void InputReader::timeoutExpiredLocked(nsecs_t when) {
477 for (size_t i = 0; i < mDevices.size(); i++) {
478 InputDevice* device = mDevices.valueAt(i);
479 if (!device->isIgnored()) {
480 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700481 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700482 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700483}
484
Jeff Brownbe1aa822011-07-27 16:04:54 -0700485void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700486 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700487 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700488
Jeff Brown6d0fec22010-07-23 21:28:06 -0700489 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700490 NotifyConfigurationChangedArgs args(when);
491 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700492}
493
Jeff Brownbe1aa822011-07-27 16:04:54 -0700494void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700495 mPolicy->getReaderConfiguration(&mConfig);
496 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
497
Jeff Brown474dcb52011-06-14 20:22:50 -0700498 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000499 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700500 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700501
502 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
503 mEventHub->requestReopenDevices();
504 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700505 for (size_t i = 0; i < mDevices.size(); i++) {
506 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700507 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700508 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700509 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700510 }
511}
512
Jeff Brownbe1aa822011-07-27 16:04:54 -0700513void InputReader::updateGlobalMetaStateLocked() {
514 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700515
Jeff Brownbe1aa822011-07-27 16:04:54 -0700516 for (size_t i = 0; i < mDevices.size(); i++) {
517 InputDevice* device = mDevices.valueAt(i);
518 mGlobalMetaState |= device->getMetaState();
519 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700520}
521
Jeff Brownbe1aa822011-07-27 16:04:54 -0700522int32_t InputReader::getGlobalMetaStateLocked() {
523 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700524}
525
Jeff Brownbe1aa822011-07-27 16:04:54 -0700526void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800527 mDisableVirtualKeysTimeout = time;
528}
529
Jeff Brownbe1aa822011-07-27 16:04:54 -0700530bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800531 InputDevice* device, int32_t keyCode, int32_t scanCode) {
532 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000533 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800534 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
535 device->getName().string(),
536 (mDisableVirtualKeysTimeout - now) * 0.000001,
537 keyCode, scanCode);
538 return true;
539 } else {
540 return false;
541 }
542}
543
Jeff Brownbe1aa822011-07-27 16:04:54 -0700544void InputReader::fadePointerLocked() {
545 for (size_t i = 0; i < mDevices.size(); i++) {
546 InputDevice* device = mDevices.valueAt(i);
547 device->fadePointer();
548 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800549}
550
Jeff Brownbe1aa822011-07-27 16:04:54 -0700551void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700552 if (when < mNextTimeout) {
553 mNextTimeout = when;
Jeff Browna47425a2012-04-13 04:09:27 -0700554 mEventHub->wake();
Jeff Brownaa3855d2011-03-17 01:34:19 -0700555 }
556}
557
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700558int32_t InputReader::bumpGenerationLocked() {
559 return ++mGeneration;
560}
561
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700562void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700563 AutoMutex _l(mLock);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700564 getInputDevicesLocked(outInputDevices);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700565}
566
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700567void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) {
568 outInputDevices.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700569
Jeff Brownbe1aa822011-07-27 16:04:54 -0700570 size_t numDevices = mDevices.size();
571 for (size_t i = 0; i < numDevices; i++) {
572 InputDevice* device = mDevices.valueAt(i);
573 if (!device->isIgnored()) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700574 outInputDevices.push();
575 device->getDeviceInfo(&outInputDevices.editTop());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700576 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700577 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578}
579
580int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
581 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700582 AutoMutex _l(mLock);
583
584 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700585}
586
587int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
588 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700589 AutoMutex _l(mLock);
590
591 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700592}
593
594int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700595 AutoMutex _l(mLock);
596
597 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598}
599
Jeff Brownbe1aa822011-07-27 16:04:54 -0700600int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700601 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700602 int32_t result = AKEY_STATE_UNKNOWN;
603 if (deviceId >= 0) {
604 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
605 if (deviceIndex >= 0) {
606 InputDevice* device = mDevices.valueAt(deviceIndex);
607 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
608 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700609 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700610 }
611 } else {
612 size_t numDevices = mDevices.size();
613 for (size_t i = 0; i < numDevices; i++) {
614 InputDevice* device = mDevices.valueAt(i);
615 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800616 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
617 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
618 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
619 if (currentResult >= AKEY_STATE_DOWN) {
620 return currentResult;
621 } else if (currentResult == AKEY_STATE_UP) {
622 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700623 }
624 }
625 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700626 }
627 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700628}
629
630bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
631 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700632 AutoMutex _l(mLock);
633
Jeff Brown6d0fec22010-07-23 21:28:06 -0700634 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700635 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700636}
637
Jeff Brownbe1aa822011-07-27 16:04:54 -0700638bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
639 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
640 bool result = false;
641 if (deviceId >= 0) {
642 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
643 if (deviceIndex >= 0) {
644 InputDevice* device = mDevices.valueAt(deviceIndex);
645 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
646 result = device->markSupportedKeyCodes(sourceMask,
647 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700648 }
649 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700650 } else {
651 size_t numDevices = mDevices.size();
652 for (size_t i = 0; i < numDevices; i++) {
653 InputDevice* device = mDevices.valueAt(i);
654 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
655 result |= device->markSupportedKeyCodes(sourceMask,
656 numCodes, keyCodes, outFlags);
657 }
658 }
659 }
660 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700661}
662
Jeff Brown474dcb52011-06-14 20:22:50 -0700663void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700664 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700665
Jeff Brownbe1aa822011-07-27 16:04:54 -0700666 if (changes) {
667 bool needWake = !mConfigurationChangesToRefresh;
668 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700669
670 if (needWake) {
671 mEventHub->wake();
672 }
673 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700674}
675
Jeff Browna47425a2012-04-13 04:09:27 -0700676void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
677 ssize_t repeat, int32_t token) {
678 AutoMutex _l(mLock);
679
680 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
681 if (deviceIndex >= 0) {
682 InputDevice* device = mDevices.valueAt(deviceIndex);
683 device->vibrate(pattern, patternSize, repeat, token);
684 }
685}
686
687void InputReader::cancelVibrate(int32_t deviceId, int32_t token) {
688 AutoMutex _l(mLock);
689
690 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
691 if (deviceIndex >= 0) {
692 InputDevice* device = mDevices.valueAt(deviceIndex);
693 device->cancelVibrate(token);
694 }
695}
696
Jeff Brownb88102f2010-09-08 11:49:43 -0700697void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700698 AutoMutex _l(mLock);
699
Jeff Brownf2f48712010-10-01 17:46:21 -0700700 mEventHub->dump(dump);
701 dump.append("\n");
702
703 dump.append("Input Reader State:\n");
704
Jeff Brownbe1aa822011-07-27 16:04:54 -0700705 for (size_t i = 0; i < mDevices.size(); i++) {
706 mDevices.valueAt(i)->dump(dump);
707 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700708
709 dump.append(INDENT "Configuration:\n");
710 dump.append(INDENT2 "ExcludedDeviceNames: [");
711 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
712 if (i != 0) {
713 dump.append(", ");
714 }
715 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
716 }
717 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700718 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
719 mConfig.virtualKeyQuietTime * 0.000001f);
720
Jeff Brown19c97d462011-06-01 12:33:19 -0700721 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
722 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
723 mConfig.pointerVelocityControlParameters.scale,
724 mConfig.pointerVelocityControlParameters.lowThreshold,
725 mConfig.pointerVelocityControlParameters.highThreshold,
726 mConfig.pointerVelocityControlParameters.acceleration);
727
728 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
729 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
730 mConfig.wheelVelocityControlParameters.scale,
731 mConfig.wheelVelocityControlParameters.lowThreshold,
732 mConfig.wheelVelocityControlParameters.highThreshold,
733 mConfig.wheelVelocityControlParameters.acceleration);
734
Jeff Brown214eaf42011-05-26 19:17:02 -0700735 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700736 dump.appendFormat(INDENT3 "Enabled: %s\n",
737 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700738 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
739 mConfig.pointerGestureQuietInterval * 0.000001f);
740 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
741 mConfig.pointerGestureDragMinSwitchSpeed);
742 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
743 mConfig.pointerGestureTapInterval * 0.000001f);
744 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
745 mConfig.pointerGestureTapDragInterval * 0.000001f);
746 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
747 mConfig.pointerGestureTapSlop);
748 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
749 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700750 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
751 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700752 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
753 mConfig.pointerGestureSwipeTransitionAngleCosine);
754 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
755 mConfig.pointerGestureSwipeMaxWidthRatio);
756 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
757 mConfig.pointerGestureMovementSpeedRatio);
758 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
759 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700760}
761
Jeff Brown89ef0722011-08-10 16:25:21 -0700762void InputReader::monitor() {
763 // Acquire and release the lock to ensure that the reader has not deadlocked.
764 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800765 mEventHub->wake();
766 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700767 mLock.unlock();
768
769 // Check the EventHub
770 mEventHub->monitor();
771}
772
Jeff Brown6d0fec22010-07-23 21:28:06 -0700773
Jeff Brownbe1aa822011-07-27 16:04:54 -0700774// --- InputReader::ContextImpl ---
775
776InputReader::ContextImpl::ContextImpl(InputReader* reader) :
777 mReader(reader) {
778}
779
780void InputReader::ContextImpl::updateGlobalMetaState() {
781 // lock is already held by the input loop
782 mReader->updateGlobalMetaStateLocked();
783}
784
785int32_t InputReader::ContextImpl::getGlobalMetaState() {
786 // lock is already held by the input loop
787 return mReader->getGlobalMetaStateLocked();
788}
789
790void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
791 // lock is already held by the input loop
792 mReader->disableVirtualKeysUntilLocked(time);
793}
794
795bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
796 InputDevice* device, int32_t keyCode, int32_t scanCode) {
797 // lock is already held by the input loop
798 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
799}
800
801void InputReader::ContextImpl::fadePointer() {
802 // lock is already held by the input loop
803 mReader->fadePointerLocked();
804}
805
806void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
807 // lock is already held by the input loop
808 mReader->requestTimeoutAtTimeLocked(when);
809}
810
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700811int32_t InputReader::ContextImpl::bumpGeneration() {
812 // lock is already held by the input loop
813 return mReader->bumpGenerationLocked();
814}
815
Jeff Brownbe1aa822011-07-27 16:04:54 -0700816InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
817 return mReader->mPolicy.get();
818}
819
820InputListenerInterface* InputReader::ContextImpl::getListener() {
821 return mReader->mQueuedListener.get();
822}
823
824EventHubInterface* InputReader::ContextImpl::getEventHub() {
825 return mReader->mEventHub.get();
826}
827
828
Jeff Brown6d0fec22010-07-23 21:28:06 -0700829// --- InputReaderThread ---
830
831InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
832 Thread(/*canCallJava*/ true), mReader(reader) {
833}
834
835InputReaderThread::~InputReaderThread() {
836}
837
838bool InputReaderThread::threadLoop() {
839 mReader->loopOnce();
840 return true;
841}
842
843
844// --- InputDevice ---
845
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700846InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Michael Wrightac6c78b2013-07-17 13:21:45 -0700847 int32_t controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes) :
848 mContext(context), mId(id), mGeneration(generation), mControllerNumber(controllerNumber),
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700849 mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285a2011-08-31 12:56:34 -0700850 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700851}
852
853InputDevice::~InputDevice() {
854 size_t numMappers = mMappers.size();
855 for (size_t i = 0; i < numMappers; i++) {
856 delete mMappers[i];
857 }
858 mMappers.clear();
859}
860
Jeff Brownef3d7e82010-09-30 14:33:04 -0700861void InputDevice::dump(String8& dump) {
862 InputDeviceInfo deviceInfo;
863 getDeviceInfo(& deviceInfo);
864
Jeff Brown90655042010-12-02 13:50:46 -0800865 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700866 deviceInfo.getDisplayName().string());
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700867 dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration);
Jeff Brown56194eb2011-03-02 19:23:13 -0800868 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700869 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
870 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800871
Jeff Brownefd32662011-03-08 15:13:06 -0800872 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800873 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700874 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800875 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800876 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
877 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800878 char name[32];
879 if (label) {
880 strncpy(name, label, sizeof(name));
881 name[sizeof(name) - 1] = '\0';
882 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800883 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800884 }
Jeff Brownefd32662011-03-08 15:13:06 -0800885 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
Michael Wrightc6091c62013-04-01 20:56:04 -0700886 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
887 name, range.source, range.min, range.max, range.flat, range.fuzz,
888 range.resolution);
Jeff Browncc0c1592011-02-19 05:07:28 -0800889 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700890 }
891
892 size_t numMappers = mMappers.size();
893 for (size_t i = 0; i < numMappers; i++) {
894 InputMapper* mapper = mMappers[i];
895 mapper->dump(dump);
896 }
897}
898
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899void InputDevice::addMapper(InputMapper* mapper) {
900 mMappers.add(mapper);
901}
902
Jeff Brown65fd2512011-08-18 11:20:58 -0700903void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700904 mSources = 0;
905
Jeff Brown474dcb52011-06-14 20:22:50 -0700906 if (!isIgnored()) {
907 if (!changes) { // first time only
908 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
909 }
910
Jeff Brown6ec6f792012-04-17 16:52:41 -0700911 if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) {
Jeff Brown61c08242012-04-19 11:14:33 -0700912 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
913 sp<KeyCharacterMap> keyboardLayout =
RoboErikca9eef62013-12-16 11:27:55 -0800914 mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier);
Jeff Brown61c08242012-04-19 11:14:33 -0700915 if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) {
916 bumpGeneration();
917 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700918 }
919 }
920
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700921 if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) {
922 if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) {
923 String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier);
924 if (mAlias != alias) {
925 mAlias = alias;
926 bumpGeneration();
927 }
928 }
929 }
930
Jeff Brown474dcb52011-06-14 20:22:50 -0700931 size_t numMappers = mMappers.size();
932 for (size_t i = 0; i < numMappers; i++) {
933 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700934 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700935 mSources |= mapper->getSources();
936 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700937 }
938}
939
Jeff Brown65fd2512011-08-18 11:20:58 -0700940void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700941 size_t numMappers = mMappers.size();
942 for (size_t i = 0; i < numMappers; i++) {
943 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700944 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700945 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700946
947 mContext->updateGlobalMetaState();
948
949 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700950}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700951
Jeff Brownb7198742011-03-18 18:14:26 -0700952void InputDevice::process(const RawEvent* rawEvents, size_t count) {
953 // Process all of the events in order for each mapper.
954 // We cannot simply ask each mapper to process them in bulk because mappers may
955 // have side-effects that must be interleaved. For example, joystick movement events and
956 // gamepad button presses are handled by different mappers but they should be dispatched
957 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700958 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700959 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
960#if DEBUG_RAW_EVENTS
Jeff Brownf33b2b22012-10-05 17:59:56 -0700961 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld",
962 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
963 rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700964#endif
965
Jeff Brown80fd47c2011-05-24 01:07:44 -0700966 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700967 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700968 mDropUntilNextSync = false;
969#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000970 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700971#endif
972 } else {
973#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000974 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700975#endif
976 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700977 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700978 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700979 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700980 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700981 } else {
982 for (size_t i = 0; i < numMappers; i++) {
983 InputMapper* mapper = mMappers[i];
984 mapper->process(rawEvent);
985 }
Jeff Brownb7198742011-03-18 18:14:26 -0700986 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700987 }
988}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700989
Jeff Brownaa3855d2011-03-17 01:34:19 -0700990void InputDevice::timeoutExpired(nsecs_t when) {
991 size_t numMappers = mMappers.size();
992 for (size_t i = 0; i < numMappers; i++) {
993 InputMapper* mapper = mMappers[i];
994 mapper->timeoutExpired(when);
995 }
996}
997
Jeff Brown6d0fec22010-07-23 21:28:06 -0700998void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Michael Wrightac6c78b2013-07-17 13:21:45 -0700999 outDeviceInfo->initialize(mId, mGeneration, mControllerNumber, mIdentifier, mAlias,
1000 mIsExternal);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001001
1002 size_t numMappers = mMappers.size();
1003 for (size_t i = 0; i < numMappers; i++) {
1004 InputMapper* mapper = mMappers[i];
1005 mapper->populateDeviceInfo(outDeviceInfo);
1006 }
1007}
1008
1009int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1010 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1011}
1012
1013int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1014 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1015}
1016
1017int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1018 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1019}
1020
1021int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1022 int32_t result = AKEY_STATE_UNKNOWN;
1023 size_t numMappers = mMappers.size();
1024 for (size_t i = 0; i < numMappers; i++) {
1025 InputMapper* mapper = mMappers[i];
1026 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001027 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1028 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1029 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1030 if (currentResult >= AKEY_STATE_DOWN) {
1031 return currentResult;
1032 } else if (currentResult == AKEY_STATE_UP) {
1033 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001034 }
1035 }
1036 }
1037 return result;
1038}
1039
1040bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1041 const int32_t* keyCodes, uint8_t* outFlags) {
1042 bool result = false;
1043 size_t numMappers = mMappers.size();
1044 for (size_t i = 0; i < numMappers; i++) {
1045 InputMapper* mapper = mMappers[i];
1046 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1047 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1048 }
1049 }
1050 return result;
1051}
1052
Jeff Browna47425a2012-04-13 04:09:27 -07001053void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1054 int32_t token) {
1055 size_t numMappers = mMappers.size();
1056 for (size_t i = 0; i < numMappers; i++) {
1057 InputMapper* mapper = mMappers[i];
1058 mapper->vibrate(pattern, patternSize, repeat, token);
1059 }
1060}
1061
1062void InputDevice::cancelVibrate(int32_t token) {
1063 size_t numMappers = mMappers.size();
1064 for (size_t i = 0; i < numMappers; i++) {
1065 InputMapper* mapper = mMappers[i];
1066 mapper->cancelVibrate(token);
1067 }
1068}
1069
Jeff Brown6d0fec22010-07-23 21:28:06 -07001070int32_t InputDevice::getMetaState() {
1071 int32_t result = 0;
1072 size_t numMappers = mMappers.size();
1073 for (size_t i = 0; i < numMappers; i++) {
1074 InputMapper* mapper = mMappers[i];
1075 result |= mapper->getMetaState();
1076 }
1077 return result;
1078}
1079
Jeff Brown05dc66a2011-03-02 14:41:58 -08001080void InputDevice::fadePointer() {
1081 size_t numMappers = mMappers.size();
1082 for (size_t i = 0; i < numMappers; i++) {
1083 InputMapper* mapper = mMappers[i];
1084 mapper->fadePointer();
1085 }
1086}
1087
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001088void InputDevice::bumpGeneration() {
1089 mGeneration = mContext->bumpGeneration();
1090}
1091
Jeff Brown65fd2512011-08-18 11:20:58 -07001092void InputDevice::notifyReset(nsecs_t when) {
1093 NotifyDeviceResetArgs args(when, mId);
1094 mContext->getListener()->notifyDeviceReset(&args);
1095}
1096
Jeff Brown6d0fec22010-07-23 21:28:06 -07001097
Jeff Brown49754db2011-07-01 17:37:58 -07001098// --- CursorButtonAccumulator ---
1099
1100CursorButtonAccumulator::CursorButtonAccumulator() {
1101 clearButtons();
1102}
1103
Jeff Brown65fd2512011-08-18 11:20:58 -07001104void CursorButtonAccumulator::reset(InputDevice* device) {
1105 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1106 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1107 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1108 mBtnBack = device->isKeyPressed(BTN_BACK);
1109 mBtnSide = device->isKeyPressed(BTN_SIDE);
1110 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1111 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1112 mBtnTask = device->isKeyPressed(BTN_TASK);
1113}
1114
Jeff Brown49754db2011-07-01 17:37:58 -07001115void CursorButtonAccumulator::clearButtons() {
1116 mBtnLeft = 0;
1117 mBtnRight = 0;
1118 mBtnMiddle = 0;
1119 mBtnBack = 0;
1120 mBtnSide = 0;
1121 mBtnForward = 0;
1122 mBtnExtra = 0;
1123 mBtnTask = 0;
1124}
1125
1126void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1127 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001128 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001129 case BTN_LEFT:
1130 mBtnLeft = rawEvent->value;
1131 break;
1132 case BTN_RIGHT:
1133 mBtnRight = rawEvent->value;
1134 break;
1135 case BTN_MIDDLE:
1136 mBtnMiddle = rawEvent->value;
1137 break;
1138 case BTN_BACK:
1139 mBtnBack = rawEvent->value;
1140 break;
1141 case BTN_SIDE:
1142 mBtnSide = rawEvent->value;
1143 break;
1144 case BTN_FORWARD:
1145 mBtnForward = rawEvent->value;
1146 break;
1147 case BTN_EXTRA:
1148 mBtnExtra = rawEvent->value;
1149 break;
1150 case BTN_TASK:
1151 mBtnTask = rawEvent->value;
1152 break;
1153 }
1154 }
1155}
1156
1157uint32_t CursorButtonAccumulator::getButtonState() const {
1158 uint32_t result = 0;
1159 if (mBtnLeft) {
1160 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1161 }
1162 if (mBtnRight) {
1163 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1164 }
1165 if (mBtnMiddle) {
1166 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1167 }
1168 if (mBtnBack || mBtnSide) {
1169 result |= AMOTION_EVENT_BUTTON_BACK;
1170 }
1171 if (mBtnForward || mBtnExtra) {
1172 result |= AMOTION_EVENT_BUTTON_FORWARD;
1173 }
1174 return result;
1175}
1176
1177
1178// --- CursorMotionAccumulator ---
1179
Jeff Brown65fd2512011-08-18 11:20:58 -07001180CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001181 clearRelativeAxes();
1182}
1183
Jeff Brown65fd2512011-08-18 11:20:58 -07001184void CursorMotionAccumulator::reset(InputDevice* device) {
1185 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001186}
1187
1188void CursorMotionAccumulator::clearRelativeAxes() {
1189 mRelX = 0;
1190 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001191}
1192
1193void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1194 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001195 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001196 case REL_X:
1197 mRelX = rawEvent->value;
1198 break;
1199 case REL_Y:
1200 mRelY = rawEvent->value;
1201 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001202 }
1203 }
1204}
1205
1206void CursorMotionAccumulator::finishSync() {
1207 clearRelativeAxes();
1208}
1209
1210
1211// --- CursorScrollAccumulator ---
1212
1213CursorScrollAccumulator::CursorScrollAccumulator() :
1214 mHaveRelWheel(false), mHaveRelHWheel(false) {
1215 clearRelativeAxes();
1216}
1217
1218void CursorScrollAccumulator::configure(InputDevice* device) {
1219 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1220 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1221}
1222
1223void CursorScrollAccumulator::reset(InputDevice* device) {
1224 clearRelativeAxes();
1225}
1226
1227void CursorScrollAccumulator::clearRelativeAxes() {
1228 mRelWheel = 0;
1229 mRelHWheel = 0;
1230}
1231
1232void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1233 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001234 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001235 case REL_WHEEL:
1236 mRelWheel = rawEvent->value;
1237 break;
1238 case REL_HWHEEL:
1239 mRelHWheel = rawEvent->value;
1240 break;
1241 }
1242 }
1243}
1244
Jeff Brown65fd2512011-08-18 11:20:58 -07001245void CursorScrollAccumulator::finishSync() {
1246 clearRelativeAxes();
1247}
1248
Jeff Brown49754db2011-07-01 17:37:58 -07001249
1250// --- TouchButtonAccumulator ---
1251
1252TouchButtonAccumulator::TouchButtonAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001253 mHaveBtnTouch(false), mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001254 clearButtons();
1255}
1256
1257void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001258 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
Jeff Brown00710e92012-04-19 15:18:26 -07001259 mHaveStylus = device->hasKey(BTN_TOOL_PEN)
1260 || device->hasKey(BTN_TOOL_RUBBER)
1261 || device->hasKey(BTN_TOOL_BRUSH)
1262 || device->hasKey(BTN_TOOL_PENCIL)
1263 || device->hasKey(BTN_TOOL_AIRBRUSH);
Jeff Brown65fd2512011-08-18 11:20:58 -07001264}
1265
1266void TouchButtonAccumulator::reset(InputDevice* device) {
1267 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1268 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1269 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1270 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1271 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1272 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1273 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1274 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1275 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1276 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1277 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001278 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1279 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1280 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001281}
1282
1283void TouchButtonAccumulator::clearButtons() {
1284 mBtnTouch = 0;
1285 mBtnStylus = 0;
1286 mBtnStylus2 = 0;
1287 mBtnToolFinger = 0;
1288 mBtnToolPen = 0;
1289 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001290 mBtnToolBrush = 0;
1291 mBtnToolPencil = 0;
1292 mBtnToolAirbrush = 0;
1293 mBtnToolMouse = 0;
1294 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001295 mBtnToolDoubleTap = 0;
1296 mBtnToolTripleTap = 0;
1297 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001298}
1299
1300void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1301 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001302 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001303 case BTN_TOUCH:
1304 mBtnTouch = rawEvent->value;
1305 break;
1306 case BTN_STYLUS:
1307 mBtnStylus = rawEvent->value;
1308 break;
1309 case BTN_STYLUS2:
1310 mBtnStylus2 = rawEvent->value;
1311 break;
1312 case BTN_TOOL_FINGER:
1313 mBtnToolFinger = rawEvent->value;
1314 break;
1315 case BTN_TOOL_PEN:
1316 mBtnToolPen = rawEvent->value;
1317 break;
1318 case BTN_TOOL_RUBBER:
1319 mBtnToolRubber = rawEvent->value;
1320 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001321 case BTN_TOOL_BRUSH:
1322 mBtnToolBrush = rawEvent->value;
1323 break;
1324 case BTN_TOOL_PENCIL:
1325 mBtnToolPencil = rawEvent->value;
1326 break;
1327 case BTN_TOOL_AIRBRUSH:
1328 mBtnToolAirbrush = rawEvent->value;
1329 break;
1330 case BTN_TOOL_MOUSE:
1331 mBtnToolMouse = rawEvent->value;
1332 break;
1333 case BTN_TOOL_LENS:
1334 mBtnToolLens = rawEvent->value;
1335 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001336 case BTN_TOOL_DOUBLETAP:
1337 mBtnToolDoubleTap = rawEvent->value;
1338 break;
1339 case BTN_TOOL_TRIPLETAP:
1340 mBtnToolTripleTap = rawEvent->value;
1341 break;
1342 case BTN_TOOL_QUADTAP:
1343 mBtnToolQuadTap = rawEvent->value;
1344 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001345 }
1346 }
1347}
1348
1349uint32_t TouchButtonAccumulator::getButtonState() const {
1350 uint32_t result = 0;
1351 if (mBtnStylus) {
1352 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1353 }
1354 if (mBtnStylus2) {
1355 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1356 }
1357 return result;
1358}
1359
1360int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001361 if (mBtnToolMouse || mBtnToolLens) {
1362 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1363 }
Jeff Brown49754db2011-07-01 17:37:58 -07001364 if (mBtnToolRubber) {
1365 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1366 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001367 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001368 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1369 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001370 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001371 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1372 }
1373 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1374}
1375
Jeff Brownd87c6d52011-08-10 14:55:59 -07001376bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001377 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1378 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001379 || mBtnToolMouse || mBtnToolLens
1380 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001381}
1382
1383bool TouchButtonAccumulator::isHovering() const {
1384 return mHaveBtnTouch && !mBtnTouch;
1385}
1386
Jeff Brown00710e92012-04-19 15:18:26 -07001387bool TouchButtonAccumulator::hasStylus() const {
1388 return mHaveStylus;
1389}
1390
Jeff Brown49754db2011-07-01 17:37:58 -07001391
Jeff Brownbe1aa822011-07-27 16:04:54 -07001392// --- RawPointerAxes ---
1393
1394RawPointerAxes::RawPointerAxes() {
1395 clear();
1396}
1397
1398void RawPointerAxes::clear() {
1399 x.clear();
1400 y.clear();
1401 pressure.clear();
1402 touchMajor.clear();
1403 touchMinor.clear();
1404 toolMajor.clear();
1405 toolMinor.clear();
1406 orientation.clear();
1407 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001408 tiltX.clear();
1409 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001410 trackingId.clear();
1411 slot.clear();
1412}
1413
1414
1415// --- RawPointerData ---
1416
1417RawPointerData::RawPointerData() {
1418 clear();
1419}
1420
1421void RawPointerData::clear() {
1422 pointerCount = 0;
1423 clearIdBits();
1424}
1425
1426void RawPointerData::copyFrom(const RawPointerData& other) {
1427 pointerCount = other.pointerCount;
1428 hoveringIdBits = other.hoveringIdBits;
1429 touchingIdBits = other.touchingIdBits;
1430
1431 for (uint32_t i = 0; i < pointerCount; i++) {
1432 pointers[i] = other.pointers[i];
1433
1434 int id = pointers[i].id;
1435 idToIndex[id] = other.idToIndex[id];
1436 }
1437}
1438
1439void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1440 float x = 0, y = 0;
1441 uint32_t count = touchingIdBits.count();
1442 if (count) {
1443 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1444 uint32_t id = idBits.clearFirstMarkedBit();
1445 const Pointer& pointer = pointerForId(id);
1446 x += pointer.x;
1447 y += pointer.y;
1448 }
1449 x /= count;
1450 y /= count;
1451 }
1452 *outX = x;
1453 *outY = y;
1454}
1455
1456
1457// --- CookedPointerData ---
1458
1459CookedPointerData::CookedPointerData() {
1460 clear();
1461}
1462
1463void CookedPointerData::clear() {
1464 pointerCount = 0;
1465 hoveringIdBits.clear();
1466 touchingIdBits.clear();
1467}
1468
1469void CookedPointerData::copyFrom(const CookedPointerData& other) {
1470 pointerCount = other.pointerCount;
1471 hoveringIdBits = other.hoveringIdBits;
1472 touchingIdBits = other.touchingIdBits;
1473
1474 for (uint32_t i = 0; i < pointerCount; i++) {
1475 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1476 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1477
1478 int id = pointerProperties[i].id;
1479 idToIndex[id] = other.idToIndex[id];
1480 }
1481}
1482
1483
Jeff Brown49754db2011-07-01 17:37:58 -07001484// --- SingleTouchMotionAccumulator ---
1485
1486SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1487 clearAbsoluteAxes();
1488}
1489
Jeff Brown65fd2512011-08-18 11:20:58 -07001490void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1491 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1492 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1493 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1494 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1495 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1496 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1497 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1498}
1499
Jeff Brown49754db2011-07-01 17:37:58 -07001500void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1501 mAbsX = 0;
1502 mAbsY = 0;
1503 mAbsPressure = 0;
1504 mAbsToolWidth = 0;
1505 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001506 mAbsTiltX = 0;
1507 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001508}
1509
1510void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1511 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001512 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001513 case ABS_X:
1514 mAbsX = rawEvent->value;
1515 break;
1516 case ABS_Y:
1517 mAbsY = rawEvent->value;
1518 break;
1519 case ABS_PRESSURE:
1520 mAbsPressure = rawEvent->value;
1521 break;
1522 case ABS_TOOL_WIDTH:
1523 mAbsToolWidth = rawEvent->value;
1524 break;
1525 case ABS_DISTANCE:
1526 mAbsDistance = rawEvent->value;
1527 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001528 case ABS_TILT_X:
1529 mAbsTiltX = rawEvent->value;
1530 break;
1531 case ABS_TILT_Y:
1532 mAbsTiltY = rawEvent->value;
1533 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001534 }
1535 }
1536}
1537
1538
1539// --- MultiTouchMotionAccumulator ---
1540
1541MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
Jeff Brown00710e92012-04-19 15:18:26 -07001542 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false),
1543 mHaveStylus(false) {
Jeff Brown49754db2011-07-01 17:37:58 -07001544}
1545
1546MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1547 delete[] mSlots;
1548}
1549
Jeff Brown00710e92012-04-19 15:18:26 -07001550void MultiTouchMotionAccumulator::configure(InputDevice* device,
1551 size_t slotCount, bool usingSlotsProtocol) {
Jeff Brown49754db2011-07-01 17:37:58 -07001552 mSlotCount = slotCount;
1553 mUsingSlotsProtocol = usingSlotsProtocol;
Jeff Brown00710e92012-04-19 15:18:26 -07001554 mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE);
Jeff Brown49754db2011-07-01 17:37:58 -07001555
1556 delete[] mSlots;
1557 mSlots = new Slot[slotCount];
1558}
1559
Jeff Brown65fd2512011-08-18 11:20:58 -07001560void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1561 // Unfortunately there is no way to read the initial contents of the slots.
1562 // So when we reset the accumulator, we must assume they are all zeroes.
1563 if (mUsingSlotsProtocol) {
1564 // Query the driver for the current slot index and use it as the initial slot
1565 // before we start reading events from the device. It is possible that the
1566 // current slot index will not be the same as it was when the first event was
1567 // written into the evdev buffer, which means the input mapper could start
1568 // out of sync with the initial state of the events in the evdev buffer.
1569 // In the extremely unlikely case that this happens, the data from
1570 // two slots will be confused until the next ABS_MT_SLOT event is received.
1571 // This can cause the touch point to "jump", but at least there will be
1572 // no stuck touches.
1573 int32_t initialSlot;
1574 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1575 ABS_MT_SLOT, &initialSlot);
1576 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001577 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001578 initialSlot = -1;
1579 }
1580 clearSlots(initialSlot);
1581 } else {
1582 clearSlots(-1);
1583 }
1584}
1585
Jeff Brown49754db2011-07-01 17:37:58 -07001586void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001587 if (mSlots) {
1588 for (size_t i = 0; i < mSlotCount; i++) {
1589 mSlots[i].clear();
1590 }
Jeff Brown49754db2011-07-01 17:37:58 -07001591 }
1592 mCurrentSlot = initialSlot;
1593}
1594
1595void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1596 if (rawEvent->type == EV_ABS) {
1597 bool newSlot = false;
1598 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001599 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001600 mCurrentSlot = rawEvent->value;
1601 newSlot = true;
1602 }
1603 } else if (mCurrentSlot < 0) {
1604 mCurrentSlot = 0;
1605 }
1606
1607 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1608#if DEBUG_POINTERS
1609 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001610 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001611 "should be between 0 and %d; ignoring this slot.",
1612 mCurrentSlot, mSlotCount - 1);
1613 }
1614#endif
1615 } else {
1616 Slot* slot = &mSlots[mCurrentSlot];
1617
Jeff Brown49ccac52012-04-11 18:27:33 -07001618 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001619 case ABS_MT_POSITION_X:
1620 slot->mInUse = true;
1621 slot->mAbsMTPositionX = rawEvent->value;
1622 break;
1623 case ABS_MT_POSITION_Y:
1624 slot->mInUse = true;
1625 slot->mAbsMTPositionY = rawEvent->value;
1626 break;
1627 case ABS_MT_TOUCH_MAJOR:
1628 slot->mInUse = true;
1629 slot->mAbsMTTouchMajor = rawEvent->value;
1630 break;
1631 case ABS_MT_TOUCH_MINOR:
1632 slot->mInUse = true;
1633 slot->mAbsMTTouchMinor = rawEvent->value;
1634 slot->mHaveAbsMTTouchMinor = true;
1635 break;
1636 case ABS_MT_WIDTH_MAJOR:
1637 slot->mInUse = true;
1638 slot->mAbsMTWidthMajor = rawEvent->value;
1639 break;
1640 case ABS_MT_WIDTH_MINOR:
1641 slot->mInUse = true;
1642 slot->mAbsMTWidthMinor = rawEvent->value;
1643 slot->mHaveAbsMTWidthMinor = true;
1644 break;
1645 case ABS_MT_ORIENTATION:
1646 slot->mInUse = true;
1647 slot->mAbsMTOrientation = rawEvent->value;
1648 break;
1649 case ABS_MT_TRACKING_ID:
1650 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001651 // The slot is no longer in use but it retains its previous contents,
1652 // which may be reused for subsequent touches.
1653 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001654 } else {
1655 slot->mInUse = true;
1656 slot->mAbsMTTrackingId = rawEvent->value;
1657 }
1658 break;
1659 case ABS_MT_PRESSURE:
1660 slot->mInUse = true;
1661 slot->mAbsMTPressure = rawEvent->value;
1662 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001663 case ABS_MT_DISTANCE:
1664 slot->mInUse = true;
1665 slot->mAbsMTDistance = rawEvent->value;
1666 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001667 case ABS_MT_TOOL_TYPE:
1668 slot->mInUse = true;
1669 slot->mAbsMTToolType = rawEvent->value;
1670 slot->mHaveAbsMTToolType = true;
1671 break;
1672 }
1673 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001674 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001675 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1676 mCurrentSlot += 1;
1677 }
1678}
1679
Jeff Brown65fd2512011-08-18 11:20:58 -07001680void MultiTouchMotionAccumulator::finishSync() {
1681 if (!mUsingSlotsProtocol) {
1682 clearSlots(-1);
1683 }
1684}
1685
Jeff Brown00710e92012-04-19 15:18:26 -07001686bool MultiTouchMotionAccumulator::hasStylus() const {
1687 return mHaveStylus;
1688}
1689
Jeff Brown49754db2011-07-01 17:37:58 -07001690
1691// --- MultiTouchMotionAccumulator::Slot ---
1692
1693MultiTouchMotionAccumulator::Slot::Slot() {
1694 clear();
1695}
1696
Jeff Brown49754db2011-07-01 17:37:58 -07001697void MultiTouchMotionAccumulator::Slot::clear() {
1698 mInUse = false;
1699 mHaveAbsMTTouchMinor = false;
1700 mHaveAbsMTWidthMinor = false;
1701 mHaveAbsMTToolType = false;
1702 mAbsMTPositionX = 0;
1703 mAbsMTPositionY = 0;
1704 mAbsMTTouchMajor = 0;
1705 mAbsMTTouchMinor = 0;
1706 mAbsMTWidthMajor = 0;
1707 mAbsMTWidthMinor = 0;
1708 mAbsMTOrientation = 0;
1709 mAbsMTTrackingId = -1;
1710 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001711 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001712 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001713}
1714
1715int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1716 if (mHaveAbsMTToolType) {
1717 switch (mAbsMTToolType) {
1718 case MT_TOOL_FINGER:
1719 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1720 case MT_TOOL_PEN:
1721 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1722 }
1723 }
1724 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1725}
1726
1727
Jeff Brown6d0fec22010-07-23 21:28:06 -07001728// --- InputMapper ---
1729
1730InputMapper::InputMapper(InputDevice* device) :
1731 mDevice(device), mContext(device->getContext()) {
1732}
1733
1734InputMapper::~InputMapper() {
1735}
1736
1737void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1738 info->addSource(getSources());
1739}
1740
Jeff Brownef3d7e82010-09-30 14:33:04 -07001741void InputMapper::dump(String8& dump) {
1742}
1743
Jeff Brown65fd2512011-08-18 11:20:58 -07001744void InputMapper::configure(nsecs_t when,
1745 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001746}
1747
Jeff Brown65fd2512011-08-18 11:20:58 -07001748void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001749}
1750
Jeff Brownaa3855d2011-03-17 01:34:19 -07001751void InputMapper::timeoutExpired(nsecs_t when) {
1752}
1753
Jeff Brown6d0fec22010-07-23 21:28:06 -07001754int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1755 return AKEY_STATE_UNKNOWN;
1756}
1757
1758int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1759 return AKEY_STATE_UNKNOWN;
1760}
1761
1762int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1763 return AKEY_STATE_UNKNOWN;
1764}
1765
1766bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1767 const int32_t* keyCodes, uint8_t* outFlags) {
1768 return false;
1769}
1770
Jeff Browna47425a2012-04-13 04:09:27 -07001771void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1772 int32_t token) {
1773}
1774
1775void InputMapper::cancelVibrate(int32_t token) {
1776}
1777
Jeff Brown6d0fec22010-07-23 21:28:06 -07001778int32_t InputMapper::getMetaState() {
1779 return 0;
1780}
1781
Jeff Brown05dc66a2011-03-02 14:41:58 -08001782void InputMapper::fadePointer() {
1783}
1784
Jeff Brownbe1aa822011-07-27 16:04:54 -07001785status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1786 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1787}
1788
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001789void InputMapper::bumpGeneration() {
1790 mDevice->bumpGeneration();
1791}
1792
Jeff Browncb1404e2011-01-15 18:14:15 -08001793void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1794 const RawAbsoluteAxisInfo& axis, const char* name) {
1795 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001796 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1797 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001798 } else {
1799 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1800 }
1801}
1802
Jeff Brown6d0fec22010-07-23 21:28:06 -07001803
1804// --- SwitchInputMapper ---
1805
1806SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
Jeff Brownbcc046a2012-09-27 20:46:43 -07001807 InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001808}
1809
1810SwitchInputMapper::~SwitchInputMapper() {
1811}
1812
1813uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001814 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001815}
1816
1817void SwitchInputMapper::process(const RawEvent* rawEvent) {
1818 switch (rawEvent->type) {
1819 case EV_SW:
Jeff Brownbcc046a2012-09-27 20:46:43 -07001820 processSwitch(rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001821 break;
Jeff Brownbcc046a2012-09-27 20:46:43 -07001822
1823 case EV_SYN:
1824 if (rawEvent->code == SYN_REPORT) {
1825 sync(rawEvent->when);
1826 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001827 }
1828}
1829
Jeff Brownbcc046a2012-09-27 20:46:43 -07001830void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) {
1831 if (switchCode >= 0 && switchCode < 32) {
1832 if (switchValue) {
1833 mUpdatedSwitchValues |= 1 << switchCode;
1834 }
1835 mUpdatedSwitchMask |= 1 << switchCode;
1836 }
1837}
1838
1839void SwitchInputMapper::sync(nsecs_t when) {
1840 if (mUpdatedSwitchMask) {
1841 NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask);
1842 getListener()->notifySwitch(&args);
1843
1844 mUpdatedSwitchValues = 0;
1845 mUpdatedSwitchMask = 0;
1846 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001847}
1848
1849int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1850 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1851}
1852
1853
Jeff Browna47425a2012-04-13 04:09:27 -07001854// --- VibratorInputMapper ---
1855
1856VibratorInputMapper::VibratorInputMapper(InputDevice* device) :
1857 InputMapper(device), mVibrating(false) {
1858}
1859
1860VibratorInputMapper::~VibratorInputMapper() {
1861}
1862
1863uint32_t VibratorInputMapper::getSources() {
1864 return 0;
1865}
1866
1867void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1868 InputMapper::populateDeviceInfo(info);
1869
1870 info->setVibrator(true);
1871}
1872
1873void VibratorInputMapper::process(const RawEvent* rawEvent) {
1874 // TODO: Handle FF_STATUS, although it does not seem to be widely supported.
1875}
1876
1877void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat,
1878 int32_t token) {
1879#if DEBUG_VIBRATOR
1880 String8 patternStr;
1881 for (size_t i = 0; i < patternSize; i++) {
1882 if (i != 0) {
1883 patternStr.append(", ");
1884 }
1885 patternStr.appendFormat("%lld", pattern[i]);
1886 }
1887 ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d",
1888 getDeviceId(), patternStr.string(), repeat, token);
1889#endif
1890
1891 mVibrating = true;
1892 memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t));
1893 mPatternSize = patternSize;
1894 mRepeat = repeat;
1895 mToken = token;
1896 mIndex = -1;
1897
1898 nextStep();
1899}
1900
1901void VibratorInputMapper::cancelVibrate(int32_t token) {
1902#if DEBUG_VIBRATOR
1903 ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token);
1904#endif
1905
1906 if (mVibrating && mToken == token) {
1907 stopVibrating();
1908 }
1909}
1910
1911void VibratorInputMapper::timeoutExpired(nsecs_t when) {
1912 if (mVibrating) {
1913 if (when >= mNextStepTime) {
1914 nextStep();
1915 } else {
1916 getContext()->requestTimeoutAtTime(mNextStepTime);
1917 }
1918 }
1919}
1920
1921void VibratorInputMapper::nextStep() {
1922 mIndex += 1;
1923 if (size_t(mIndex) >= mPatternSize) {
1924 if (mRepeat < 0) {
1925 // We are done.
1926 stopVibrating();
1927 return;
1928 }
1929 mIndex = mRepeat;
1930 }
1931
1932 bool vibratorOn = mIndex & 1;
1933 nsecs_t duration = mPattern[mIndex];
1934 if (vibratorOn) {
1935#if DEBUG_VIBRATOR
1936 ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld",
1937 getDeviceId(), duration);
1938#endif
1939 getEventHub()->vibrate(getDeviceId(), duration);
1940 } else {
1941#if DEBUG_VIBRATOR
1942 ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId());
1943#endif
1944 getEventHub()->cancelVibrate(getDeviceId());
1945 }
1946 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
1947 mNextStepTime = now + duration;
1948 getContext()->requestTimeoutAtTime(mNextStepTime);
1949#if DEBUG_VIBRATOR
1950 ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f);
1951#endif
1952}
1953
1954void VibratorInputMapper::stopVibrating() {
1955 mVibrating = false;
1956#if DEBUG_VIBRATOR
1957 ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId());
1958#endif
1959 getEventHub()->cancelVibrate(getDeviceId());
1960}
1961
1962void VibratorInputMapper::dump(String8& dump) {
1963 dump.append(INDENT2 "Vibrator Input Mapper:\n");
1964 dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating));
1965}
1966
1967
Jeff Brown6d0fec22010-07-23 21:28:06 -07001968// --- KeyboardInputMapper ---
1969
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001970KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001971 uint32_t source, int32_t keyboardType) :
1972 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001973 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001974}
1975
1976KeyboardInputMapper::~KeyboardInputMapper() {
1977}
1978
Jeff Brown6d0fec22010-07-23 21:28:06 -07001979uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001980 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001981}
1982
1983void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1984 InputMapper::populateDeviceInfo(info);
1985
1986 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001987 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001988}
1989
Jeff Brownef3d7e82010-09-30 14:33:04 -07001990void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001991 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1992 dumpParameters(dump);
1993 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001994 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001995 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1996 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1997 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001998}
1999
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002000
Jeff Brown65fd2512011-08-18 11:20:58 -07002001void KeyboardInputMapper::configure(nsecs_t when,
2002 const InputReaderConfiguration* config, uint32_t changes) {
2003 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002004
Jeff Brown474dcb52011-06-14 20:22:50 -07002005 if (!changes) { // first time only
2006 // Configure basic parameters.
2007 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07002008 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002009
Jeff Brown65fd2512011-08-18 11:20:58 -07002010 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002011 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2012 DisplayViewport v;
2013 if (config->getDisplayInfo(false /*external*/, &v)) {
2014 mOrientation = v.orientation;
2015 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002016 mOrientation = DISPLAY_ORIENTATION_0;
2017 }
2018 } else {
2019 mOrientation = DISPLAY_ORIENTATION_0;
2020 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08002021 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002022}
2023
2024void KeyboardInputMapper::configureParameters() {
2025 mParameters.orientationAware = false;
2026 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
2027 mParameters.orientationAware);
2028
Jeff Brownd728bf52012-09-08 18:05:28 -07002029 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002030 if (mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002031 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002032 }
Michael Wright39ca0522014-03-17 13:03:47 -07002033
2034 mParameters.handlesKeyRepeat = false;
2035 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.handlesKeyRepeat"),
2036 mParameters.handlesKeyRepeat);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002037}
2038
2039void KeyboardInputMapper::dumpParameters(String8& dump) {
2040 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002041 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2042 toString(mParameters.hasAssociatedDisplay));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002043 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2044 toString(mParameters.orientationAware));
Michael Wright39ca0522014-03-17 13:03:47 -07002045 dump.appendFormat(INDENT4 "HandlesKeyRepeat: %s\n",
2046 toString(mParameters.handlesKeyRepeat));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002047}
2048
Jeff Brown65fd2512011-08-18 11:20:58 -07002049void KeyboardInputMapper::reset(nsecs_t when) {
2050 mMetaState = AMETA_NONE;
2051 mDownTime = 0;
2052 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07002053 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002054
Jeff Brownbe1aa822011-07-27 16:04:54 -07002055 resetLedState();
2056
Jeff Brown65fd2512011-08-18 11:20:58 -07002057 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002058}
2059
2060void KeyboardInputMapper::process(const RawEvent* rawEvent) {
2061 switch (rawEvent->type) {
2062 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07002063 int32_t scanCode = rawEvent->code;
2064 int32_t usageCode = mCurrentHidUsage;
2065 mCurrentHidUsage = 0;
2066
Jeff Brown6d0fec22010-07-23 21:28:06 -07002067 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07002068 int32_t keyCode;
2069 uint32_t flags;
2070 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
2071 keyCode = AKEYCODE_UNKNOWN;
2072 flags = 0;
2073 }
2074 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002075 }
2076 break;
2077 }
Jeff Brown49ccac52012-04-11 18:27:33 -07002078 case EV_MSC: {
2079 if (rawEvent->code == MSC_SCAN) {
2080 mCurrentHidUsage = rawEvent->value;
2081 }
2082 break;
2083 }
2084 case EV_SYN: {
2085 if (rawEvent->code == SYN_REPORT) {
2086 mCurrentHidUsage = 0;
2087 }
2088 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002089 }
2090}
2091
2092bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
2093 return scanCode < BTN_MOUSE
2094 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08002095 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08002096 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002097}
2098
Jeff Brown6328cdc2010-07-29 18:18:33 -07002099void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
2100 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002101
Jeff Brownbe1aa822011-07-27 16:04:54 -07002102 if (down) {
2103 // Rotate key codes according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002104 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002105 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002106 }
Jeff Brownfe508922011-01-18 15:10:10 -08002107
Jeff Brownbe1aa822011-07-27 16:04:54 -07002108 // Add key down.
2109 ssize_t keyDownIndex = findKeyDown(scanCode);
2110 if (keyDownIndex >= 0) {
2111 // key repeat, be sure to use same keycode as before in case of rotation
2112 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002113 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002114 // key down
2115 if ((policyFlags & POLICY_FLAG_VIRTUAL)
2116 && mContext->shouldDropVirtualKey(when,
2117 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002118 return;
2119 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002120
2121 mKeyDowns.push();
2122 KeyDown& keyDown = mKeyDowns.editTop();
2123 keyDown.keyCode = keyCode;
2124 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002125 }
2126
Jeff Brownbe1aa822011-07-27 16:04:54 -07002127 mDownTime = when;
2128 } else {
2129 // Remove key down.
2130 ssize_t keyDownIndex = findKeyDown(scanCode);
2131 if (keyDownIndex >= 0) {
2132 // key up, be sure to use same keycode as before in case of rotation
2133 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
2134 mKeyDowns.removeAt(size_t(keyDownIndex));
2135 } else {
2136 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00002137 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07002138 "keyCode=%d, scanCode=%d",
2139 getDeviceName().string(), keyCode, scanCode);
2140 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002141 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002142 }
Jeff Brownfd035822010-06-30 16:10:35 -07002143
Jeff Brownbe1aa822011-07-27 16:04:54 -07002144 int32_t oldMetaState = mMetaState;
2145 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
Michael Wrightf583d0d2013-10-29 13:24:41 -07002146 bool metaStateChanged = oldMetaState != newMetaState;
2147 if (metaStateChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002148 mMetaState = newMetaState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002149 updateLedState(false);
2150 }
2151
2152 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002153
Jeff Brown56194eb2011-03-02 19:23:13 -08002154 // Key down on external an keyboard should wake the device.
2155 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
2156 // For internal keyboards, the key layout file should specify the policy flags for
2157 // each wake key individually.
2158 // TODO: Use the input device configuration to control this behavior more finely.
2159 if (down && getDevice()->isExternal()
2160 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
2161 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2162 }
2163
Michael Wright39ca0522014-03-17 13:03:47 -07002164 if (mParameters.handlesKeyRepeat) {
2165 policyFlags |= POLICY_FLAG_DISABLE_KEY_REPEAT;
2166 }
2167
Jeff Brown6328cdc2010-07-29 18:18:33 -07002168 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002169 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002170 }
2171
Jeff Brown05dc66a2011-03-02 14:41:58 -08002172 if (down && !isMetaKey(keyCode)) {
2173 getContext()->fadePointer();
2174 }
2175
Jeff Brownbe1aa822011-07-27 16:04:54 -07002176 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07002177 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
2178 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002179 getListener()->notifyKey(&args);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002180}
2181
Jeff Brownbe1aa822011-07-27 16:04:54 -07002182ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2183 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002184 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002185 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002186 return i;
2187 }
2188 }
2189 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002190}
2191
Jeff Brown6d0fec22010-07-23 21:28:06 -07002192int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2193 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2194}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002195
Jeff Brown6d0fec22010-07-23 21:28:06 -07002196int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2197 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2198}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002199
Jeff Brown6d0fec22010-07-23 21:28:06 -07002200bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2201 const int32_t* keyCodes, uint8_t* outFlags) {
2202 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2203}
2204
2205int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002206 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002207}
2208
Jeff Brownbe1aa822011-07-27 16:04:54 -07002209void KeyboardInputMapper::resetLedState() {
Michael Wrighted28fc82013-10-18 15:26:48 -07002210 initializeLedState(mCapsLockLedState, ALED_CAPS_LOCK);
2211 initializeLedState(mNumLockLedState, ALED_NUM_LOCK);
2212 initializeLedState(mScrollLockLedState, ALED_SCROLL_LOCK);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002213
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002215}
2216
Jeff Brownbe1aa822011-07-27 16:04:54 -07002217void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002218 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2219 ledState.on = false;
2220}
2221
Jeff Brownbe1aa822011-07-27 16:04:54 -07002222void KeyboardInputMapper::updateLedState(bool reset) {
Michael Wrighted28fc82013-10-18 15:26:48 -07002223 updateLedStateForModifier(mCapsLockLedState, ALED_CAPS_LOCK,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002224 AMETA_CAPS_LOCK_ON, reset);
Michael Wrighted28fc82013-10-18 15:26:48 -07002225 updateLedStateForModifier(mNumLockLedState, ALED_NUM_LOCK,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002226 AMETA_NUM_LOCK_ON, reset);
Michael Wrighted28fc82013-10-18 15:26:48 -07002227 updateLedStateForModifier(mScrollLockLedState, ALED_SCROLL_LOCK,
Jeff Brown51e7fe72010-10-29 22:19:53 -07002228 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002229}
2230
Jeff Brownbe1aa822011-07-27 16:04:54 -07002231void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002232 int32_t led, int32_t modifier, bool reset) {
2233 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002234 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002235 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002236 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2237 ledState.on = desiredState;
2238 }
2239 }
2240}
2241
Jeff Brown6d0fec22010-07-23 21:28:06 -07002242
Jeff Brown83c09682010-12-23 17:50:18 -08002243// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002244
Jeff Brown83c09682010-12-23 17:50:18 -08002245CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002246 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002247}
2248
Jeff Brown83c09682010-12-23 17:50:18 -08002249CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002250}
2251
Jeff Brown83c09682010-12-23 17:50:18 -08002252uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002253 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002254}
2255
Jeff Brown83c09682010-12-23 17:50:18 -08002256void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002257 InputMapper::populateDeviceInfo(info);
2258
Jeff Brown83c09682010-12-23 17:50:18 -08002259 if (mParameters.mode == Parameters::MODE_POINTER) {
2260 float minX, minY, maxX, maxY;
2261 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002262 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f, 0.0f);
2263 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002264 }
2265 } else {
Michael Wrightc6091c62013-04-01 20:56:04 -07002266 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale, 0.0f);
2267 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002268 }
Michael Wrightc6091c62013-04-01 20:56:04 -07002269 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002270
Jeff Brown65fd2512011-08-18 11:20:58 -07002271 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002272 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002273 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002274 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002275 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002276 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002277}
2278
Jeff Brown83c09682010-12-23 17:50:18 -08002279void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002280 dump.append(INDENT2 "Cursor Input Mapper:\n");
2281 dumpParameters(dump);
2282 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2283 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2284 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2285 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2286 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002287 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002288 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002289 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002290 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2291 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002292 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002293 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2294 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2295 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002296}
2297
Jeff Brown65fd2512011-08-18 11:20:58 -07002298void CursorInputMapper::configure(nsecs_t when,
2299 const InputReaderConfiguration* config, uint32_t changes) {
2300 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002301
Jeff Brown474dcb52011-06-14 20:22:50 -07002302 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002303 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002304
Jeff Brown474dcb52011-06-14 20:22:50 -07002305 // Configure basic parameters.
2306 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002307
Jeff Brown474dcb52011-06-14 20:22:50 -07002308 // Configure device mode.
2309 switch (mParameters.mode) {
2310 case Parameters::MODE_POINTER:
2311 mSource = AINPUT_SOURCE_MOUSE;
2312 mXPrecision = 1.0f;
2313 mYPrecision = 1.0f;
2314 mXScale = 1.0f;
2315 mYScale = 1.0f;
2316 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2317 break;
2318 case Parameters::MODE_NAVIGATION:
2319 mSource = AINPUT_SOURCE_TRACKBALL;
2320 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2321 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2322 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2323 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2324 break;
2325 }
2326
2327 mVWheelScale = 1.0f;
2328 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002329 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002330
Jeff Brown474dcb52011-06-14 20:22:50 -07002331 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2332 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2333 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2334 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2335 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002336
2337 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002338 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) {
2339 DisplayViewport v;
2340 if (config->getDisplayInfo(false /*external*/, &v)) {
2341 mOrientation = v.orientation;
2342 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07002343 mOrientation = DISPLAY_ORIENTATION_0;
2344 }
2345 } else {
2346 mOrientation = DISPLAY_ORIENTATION_0;
2347 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002348 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002349 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002350}
2351
Jeff Brown83c09682010-12-23 17:50:18 -08002352void CursorInputMapper::configureParameters() {
2353 mParameters.mode = Parameters::MODE_POINTER;
2354 String8 cursorModeString;
2355 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2356 if (cursorModeString == "navigation") {
2357 mParameters.mode = Parameters::MODE_NAVIGATION;
2358 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002359 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002360 }
2361 }
2362
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002363 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002364 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002365 mParameters.orientationAware);
2366
Jeff Brownd728bf52012-09-08 18:05:28 -07002367 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002368 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002369 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002370 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002371}
2372
Jeff Brown83c09682010-12-23 17:50:18 -08002373void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002374 dump.append(INDENT3 "Parameters:\n");
Jeff Brownd728bf52012-09-08 18:05:28 -07002375 dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n",
2376 toString(mParameters.hasAssociatedDisplay));
Jeff Brown83c09682010-12-23 17:50:18 -08002377
2378 switch (mParameters.mode) {
2379 case Parameters::MODE_POINTER:
2380 dump.append(INDENT4 "Mode: pointer\n");
2381 break;
2382 case Parameters::MODE_NAVIGATION:
2383 dump.append(INDENT4 "Mode: navigation\n");
2384 break;
2385 default:
Steve Blockec193de2012-01-09 18:35:44 +00002386 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002387 }
2388
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002389 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2390 toString(mParameters.orientationAware));
2391}
2392
Jeff Brown65fd2512011-08-18 11:20:58 -07002393void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002394 mButtonState = 0;
2395 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002396
Jeff Brownbe1aa822011-07-27 16:04:54 -07002397 mPointerVelocityControl.reset();
2398 mWheelXVelocityControl.reset();
2399 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002400
Jeff Brown65fd2512011-08-18 11:20:58 -07002401 mCursorButtonAccumulator.reset(getDevice());
2402 mCursorMotionAccumulator.reset(getDevice());
2403 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002404
Jeff Brown65fd2512011-08-18 11:20:58 -07002405 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002406}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002407
Jeff Brown83c09682010-12-23 17:50:18 -08002408void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002409 mCursorButtonAccumulator.process(rawEvent);
2410 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002411 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002412
Jeff Brown49ccac52012-04-11 18:27:33 -07002413 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002414 sync(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002415 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002416}
2417
Jeff Brown83c09682010-12-23 17:50:18 -08002418void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002419 int32_t lastButtonState = mButtonState;
2420 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2421 mButtonState = currentButtonState;
2422
2423 bool wasDown = isPointerDown(lastButtonState);
2424 bool down = isPointerDown(currentButtonState);
2425 bool downChanged;
2426 if (!wasDown && down) {
2427 mDownTime = when;
2428 downChanged = true;
2429 } else if (wasDown && !down) {
2430 downChanged = true;
2431 } else {
2432 downChanged = false;
2433 }
2434 nsecs_t downTime = mDownTime;
2435 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002436 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002437
2438 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2439 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2440 bool moved = deltaX != 0 || deltaY != 0;
2441
Jeff Brown65fd2512011-08-18 11:20:58 -07002442 // Rotate delta according to orientation if needed.
Jeff Brownd728bf52012-09-08 18:05:28 -07002443 if (mParameters.orientationAware && mParameters.hasAssociatedDisplay
Jeff Brownbe1aa822011-07-27 16:04:54 -07002444 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002445 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002446 }
2447
Jeff Brown65fd2512011-08-18 11:20:58 -07002448 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002449 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002450 pointerProperties.clear();
2451 pointerProperties.id = 0;
2452 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2453
Jeff Brown6328cdc2010-07-29 18:18:33 -07002454 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002455 pointerCoords.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002456
Jeff Brown65fd2512011-08-18 11:20:58 -07002457 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2458 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002460
Jeff Brownbe1aa822011-07-27 16:04:54 -07002461 mWheelYVelocityControl.move(when, NULL, &vscroll);
2462 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002463
Jeff Brownbe1aa822011-07-27 16:04:54 -07002464 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002465
Jeff Brown83d616a2012-09-09 20:33:43 -07002466 int32_t displayId;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002467 if (mPointerController != NULL) {
2468 if (moved || scrolled || buttonsChanged) {
2469 mPointerController->setPresentation(
2470 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002471
Jeff Brownbe1aa822011-07-27 16:04:54 -07002472 if (moved) {
2473 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002474 }
2475
Jeff Brownbe1aa822011-07-27 16:04:54 -07002476 if (buttonsChanged) {
2477 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002478 }
Jeff Brownefd32662011-03-08 15:13:06 -08002479
Jeff Brownbe1aa822011-07-27 16:04:54 -07002480 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002481 }
2482
Jeff Brownbe1aa822011-07-27 16:04:54 -07002483 float x, y;
2484 mPointerController->getPosition(&x, &y);
2485 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2486 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83d616a2012-09-09 20:33:43 -07002487 displayId = ADISPLAY_ID_DEFAULT;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002488 } else {
2489 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2490 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83d616a2012-09-09 20:33:43 -07002491 displayId = ADISPLAY_ID_NONE;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002492 }
2493
2494 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002495
Jeff Brown56194eb2011-03-02 19:23:13 -08002496 // Moving an external trackball or mouse should wake the device.
2497 // We don't do this for internal cursor devices to prevent them from waking up
2498 // the device in your pocket.
2499 // TODO: Use the input device configuration to control this behavior more finely.
2500 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002501 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002502 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2503 }
2504
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002505 // Synthesize key down from buttons if needed.
2506 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2507 policyFlags, lastButtonState, currentButtonState);
2508
2509 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002510 if (downChanged || moved || scrolled || buttonsChanged) {
2511 int32_t metaState = mContext->getGlobalMetaState();
2512 int32_t motionEventAction;
2513 if (downChanged) {
2514 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2515 } else if (down || mPointerController == NULL) {
2516 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2517 } else {
2518 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2519 }
Jeff Brownb6997262010-10-08 22:31:17 -07002520
Jeff Brownbe1aa822011-07-27 16:04:54 -07002521 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2522 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07002523 displayId, 1, &pointerProperties, &pointerCoords,
2524 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002525 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002526
Jeff Brownbe1aa822011-07-27 16:04:54 -07002527 // Send hover move after UP to tell the application that the mouse is hovering now.
2528 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2529 && mPointerController != NULL) {
2530 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2531 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2532 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002533 displayId, 1, &pointerProperties, &pointerCoords,
2534 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002535 getListener()->notifyMotion(&hoverArgs);
2536 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002537
Jeff Brownbe1aa822011-07-27 16:04:54 -07002538 // Send scroll events.
2539 if (scrolled) {
2540 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2541 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2542
2543 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2544 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2545 AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07002546 displayId, 1, &pointerProperties, &pointerCoords,
2547 mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002548 getListener()->notifyMotion(&scrollArgs);
2549 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002550 }
Jeff Browna032cc02011-03-07 16:56:21 -08002551
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002552 // Synthesize key up from buttons if needed.
2553 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2554 policyFlags, lastButtonState, currentButtonState);
2555
Jeff Brown65fd2512011-08-18 11:20:58 -07002556 mCursorMotionAccumulator.finishSync();
2557 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002558}
2559
Jeff Brown83c09682010-12-23 17:50:18 -08002560int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002561 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2562 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2563 } else {
2564 return AKEY_STATE_UNKNOWN;
2565 }
2566}
2567
Jeff Brown05dc66a2011-03-02 14:41:58 -08002568void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002569 if (mPointerController != NULL) {
2570 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2571 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002572}
2573
Jeff Brown6d0fec22010-07-23 21:28:06 -07002574
2575// --- TouchInputMapper ---
2576
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002577TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002578 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002579 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brown83d616a2012-09-09 20:33:43 -07002580 mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0),
2581 mSurfaceOrientation(DISPLAY_ORIENTATION_0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002582}
2583
2584TouchInputMapper::~TouchInputMapper() {
2585}
2586
2587uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002588 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002589}
2590
2591void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2592 InputMapper::populateDeviceInfo(info);
2593
Jeff Brown65fd2512011-08-18 11:20:58 -07002594 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2595 info->addMotionRange(mOrientedRanges.x);
2596 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002597 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002598
Jeff Brown65fd2512011-08-18 11:20:58 -07002599 if (mOrientedRanges.haveSize) {
2600 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002601 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002602
2603 if (mOrientedRanges.haveTouchSize) {
2604 info->addMotionRange(mOrientedRanges.touchMajor);
2605 info->addMotionRange(mOrientedRanges.touchMinor);
2606 }
2607
2608 if (mOrientedRanges.haveToolSize) {
2609 info->addMotionRange(mOrientedRanges.toolMajor);
2610 info->addMotionRange(mOrientedRanges.toolMinor);
2611 }
2612
2613 if (mOrientedRanges.haveOrientation) {
2614 info->addMotionRange(mOrientedRanges.orientation);
2615 }
2616
2617 if (mOrientedRanges.haveDistance) {
2618 info->addMotionRange(mOrientedRanges.distance);
2619 }
2620
2621 if (mOrientedRanges.haveTilt) {
2622 info->addMotionRange(mOrientedRanges.tilt);
2623 }
2624
2625 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002626 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
2627 0.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07002628 }
2629 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Michael Wrightc6091c62013-04-01 20:56:04 -07002630 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f,
2631 0.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07002632 }
Michael Wright5e025eb2013-05-15 23:16:54 -07002633 if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) {
2634 const InputDeviceInfo::MotionRange& x = mOrientedRanges.x;
2635 const InputDeviceInfo::MotionRange& y = mOrientedRanges.y;
2636 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat,
2637 x.fuzz, x.resolution);
2638 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat,
2639 y.fuzz, y.resolution);
2640 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat,
2641 x.fuzz, x.resolution);
2642 info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat,
2643 y.fuzz, y.resolution);
2644 }
Michael Wright7ddd1102013-05-20 15:04:55 -07002645 info->setButtonUnderPad(mParameters.hasButtonUnderPad);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002646 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002647}
2648
Jeff Brownef3d7e82010-09-30 14:33:04 -07002649void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002650 dump.append(INDENT2 "Touch Input Mapper:\n");
2651 dumpParameters(dump);
2652 dumpVirtualKeys(dump);
2653 dumpRawPointerAxes(dump);
2654 dumpCalibration(dump);
2655 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002656
Jeff Brownbe1aa822011-07-27 16:04:54 -07002657 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown83d616a2012-09-09 20:33:43 -07002658 dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate);
2659 dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002660 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2661 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2662 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2663 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2664 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002665 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2666 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2667 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2668 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002669 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2670 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2671 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2672 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2673 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002674
Jeff Brownbe1aa822011-07-27 16:04:54 -07002675 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002676
Jeff Brownbe1aa822011-07-27 16:04:54 -07002677 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2678 mLastRawPointerData.pointerCount);
2679 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2680 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2681 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2682 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002683 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2684 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002685 pointer.id, pointer.x, pointer.y, pointer.pressure,
2686 pointer.touchMajor, pointer.touchMinor,
2687 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002688 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002689 pointer.toolType, toString(pointer.isHovering));
2690 }
2691
2692 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2693 mLastCookedPointerData.pointerCount);
2694 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2695 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2696 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2697 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2698 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002699 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2700 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002701 pointerProperties.id,
2702 pointerCoords.getX(),
2703 pointerCoords.getY(),
2704 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2705 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2706 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2707 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2708 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2709 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002710 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002711 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2712 pointerProperties.toolType,
2713 toString(mLastCookedPointerData.isHovering(i)));
2714 }
2715
Jeff Brown65fd2512011-08-18 11:20:58 -07002716 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002717 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2718 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002719 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002720 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002721 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002722 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002723 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002724 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002725 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002726 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2727 mPointerGestureMaxSwipeWidth);
2728 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002729}
2730
Jeff Brown65fd2512011-08-18 11:20:58 -07002731void TouchInputMapper::configure(nsecs_t when,
2732 const InputReaderConfiguration* config, uint32_t changes) {
2733 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002734
Jeff Brown474dcb52011-06-14 20:22:50 -07002735 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002736
Jeff Brown474dcb52011-06-14 20:22:50 -07002737 if (!changes) { // first time only
2738 // Configure basic parameters.
2739 configureParameters();
2740
Jeff Brown65fd2512011-08-18 11:20:58 -07002741 // Configure common accumulators.
2742 mCursorScrollAccumulator.configure(getDevice());
2743 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002744
2745 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002746 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002747
2748 // Prepare input device calibration.
2749 parseCalibration();
2750 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002751 }
2752
Jeff Brown474dcb52011-06-14 20:22:50 -07002753 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002754 // Update pointer speed.
2755 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2756 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2757 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002758 }
Jeff Brown8d608662010-08-30 03:02:23 -07002759
Jeff Brown65fd2512011-08-18 11:20:58 -07002760 bool resetNeeded = false;
2761 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002762 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2763 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002764 // Configure device sources, surface dimensions, orientation and
2765 // scaling factors.
2766 configureSurface(when, &resetNeeded);
2767 }
2768
2769 if (changes && resetNeeded) {
2770 // Send reset, unless this is the first time the device has been configured,
2771 // in which case the reader will call reset itself after all mappers are ready.
2772 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002773 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002774}
2775
Jeff Brown8d608662010-08-30 03:02:23 -07002776void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002777 // Use the pointer presentation mode for devices that do not support distinct
2778 // multitouch. The spot-based presentation relies on being able to accurately
2779 // locate two or more fingers on the touch pad.
2780 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2781 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002782
Jeff Brown538881e2011-05-25 18:23:38 -07002783 String8 gestureModeString;
2784 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2785 gestureModeString)) {
2786 if (gestureModeString == "pointer") {
2787 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2788 } else if (gestureModeString == "spots") {
2789 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2790 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002791 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002792 }
2793 }
2794
Jeff Browndeffe072011-08-26 18:38:46 -07002795 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2796 // The device is a touch screen.
2797 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2798 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2799 // The device is a pointing device like a track pad.
2800 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2801 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002802 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2803 // The device is a cursor device with a touch pad attached.
2804 // By default don't use the touch pad to move the pointer.
2805 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2806 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002807 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002808 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2809 }
2810
Michael Wright7ddd1102013-05-20 15:04:55 -07002811 mParameters.hasButtonUnderPad=
2812 getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_BUTTONPAD);
2813
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002814 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002815 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2816 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002817 if (deviceTypeString == "touchScreen") {
2818 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002819 } else if (deviceTypeString == "touchPad") {
2820 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002821 } else if (deviceTypeString == "touchNavigation") {
2822 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION;
Jeff Brownace13b12011-03-09 17:39:48 -08002823 } else if (deviceTypeString == "pointer") {
2824 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002825 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002826 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002827 }
2828 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002829
Jeff Brownefd32662011-03-08 15:13:06 -08002830 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002831 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2832 mParameters.orientationAware);
2833
Jeff Brownd728bf52012-09-08 18:05:28 -07002834 mParameters.hasAssociatedDisplay = false;
Jeff Brownbc68a592011-07-25 12:58:12 -07002835 mParameters.associatedDisplayIsExternal = false;
2836 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002837 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002838 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownd728bf52012-09-08 18:05:28 -07002839 mParameters.hasAssociatedDisplay = true;
Jeff Brownbc68a592011-07-25 12:58:12 -07002840 mParameters.associatedDisplayIsExternal =
2841 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2842 && getDevice()->isExternal();
Jeff Brownbc68a592011-07-25 12:58:12 -07002843 }
Jeff Brown6ca90042014-02-26 15:12:45 -08002844
2845 // Initial downs on external touch devices should wake the device.
2846 // Normally we don't do this for internal touch screens to prevent them from waking
2847 // up in your pocket but you can enable it using the input device configuration.
2848 mParameters.wake = getDevice()->isExternal();
2849 getDevice()->getConfiguration().tryGetProperty(String8("touch.wake"),
2850 mParameters.wake);
Jeff Brown8d608662010-08-30 03:02:23 -07002851}
2852
Jeff Brownef3d7e82010-09-30 14:33:04 -07002853void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002854 dump.append(INDENT3 "Parameters:\n");
2855
Jeff Brown538881e2011-05-25 18:23:38 -07002856 switch (mParameters.gestureMode) {
2857 case Parameters::GESTURE_MODE_POINTER:
2858 dump.append(INDENT4 "GestureMode: pointer\n");
2859 break;
2860 case Parameters::GESTURE_MODE_SPOTS:
2861 dump.append(INDENT4 "GestureMode: spots\n");
2862 break;
2863 default:
2864 assert(false);
2865 }
2866
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002867 switch (mParameters.deviceType) {
2868 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2869 dump.append(INDENT4 "DeviceType: touchScreen\n");
2870 break;
2871 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2872 dump.append(INDENT4 "DeviceType: touchPad\n");
2873 break;
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002874 case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION:
2875 dump.append(INDENT4 "DeviceType: touchNavigation\n");
2876 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002877 case Parameters::DEVICE_TYPE_POINTER:
2878 dump.append(INDENT4 "DeviceType: pointer\n");
2879 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002880 default:
Steve Blockec193de2012-01-09 18:35:44 +00002881 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002882 }
2883
Jeff Brown83d616a2012-09-09 20:33:43 -07002884 dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n",
Jeff Brownd728bf52012-09-08 18:05:28 -07002885 toString(mParameters.hasAssociatedDisplay),
2886 toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002887 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2888 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002889}
2890
Jeff Brownbe1aa822011-07-27 16:04:54 -07002891void TouchInputMapper::configureRawPointerAxes() {
2892 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002893}
2894
Jeff Brownbe1aa822011-07-27 16:04:54 -07002895void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2896 dump.append(INDENT3 "Raw Touch Axes:\n");
2897 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2898 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2899 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2900 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2901 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2902 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2903 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2904 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2905 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002906 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2907 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002908 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2909 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002910}
2911
Jeff Brown65fd2512011-08-18 11:20:58 -07002912void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2913 int32_t oldDeviceMode = mDeviceMode;
2914
2915 // Determine device mode.
2916 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2917 && mConfig.pointerGesturesEnabled) {
2918 mSource = AINPUT_SOURCE_MOUSE;
2919 mDeviceMode = DEVICE_MODE_POINTER;
Jeff Brown00710e92012-04-19 15:18:26 -07002920 if (hasStylus()) {
2921 mSource |= AINPUT_SOURCE_STYLUS;
2922 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002923 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownd728bf52012-09-08 18:05:28 -07002924 && mParameters.hasAssociatedDisplay) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002925 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2926 mDeviceMode = DEVICE_MODE_DIRECT;
Jeff Brown00710e92012-04-19 15:18:26 -07002927 if (hasStylus()) {
2928 mSource |= AINPUT_SOURCE_STYLUS;
2929 }
Michael Wrighte7a9ae82013-03-08 15:19:19 -08002930 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) {
2931 mSource = AINPUT_SOURCE_TOUCH_NAVIGATION;
Jeff Brown4dac9012013-04-10 01:03:19 -07002932 mDeviceMode = DEVICE_MODE_NAVIGATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002933 } else {
2934 mSource = AINPUT_SOURCE_TOUCHPAD;
2935 mDeviceMode = DEVICE_MODE_UNSCALED;
2936 }
2937
Jeff Brown9626b142011-03-03 02:09:54 -08002938 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002939 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002940 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002941 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002942 mDeviceMode = DEVICE_MODE_DISABLED;
2943 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002944 }
2945
Jeff Brown83d616a2012-09-09 20:33:43 -07002946 // Raw width and height in the natural orientation.
2947 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2948 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2949
Jeff Brown65fd2512011-08-18 11:20:58 -07002950 // Get associated display dimensions.
Jeff Brown83d616a2012-09-09 20:33:43 -07002951 DisplayViewport newViewport;
Jeff Brownd728bf52012-09-08 18:05:28 -07002952 if (mParameters.hasAssociatedDisplay) {
Jeff Brown83d616a2012-09-09 20:33:43 -07002953 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002954 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brownd728bf52012-09-08 18:05:28 -07002955 "display. The device will be inoperable until the display size "
Jeff Brown65fd2512011-08-18 11:20:58 -07002956 "becomes available.",
Jeff Brownd728bf52012-09-08 18:05:28 -07002957 getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002958 mDeviceMode = DEVICE_MODE_DISABLED;
2959 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002960 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002961 } else {
Jeff Brown83d616a2012-09-09 20:33:43 -07002962 newViewport.setNonDisplayViewport(rawWidth, rawHeight);
2963 }
Michael Wrightf583d0d2013-10-29 13:24:41 -07002964 bool viewportChanged = mViewport != newViewport;
2965 if (viewportChanged) {
Jeff Brown83d616a2012-09-09 20:33:43 -07002966 mViewport = newViewport;
Jeff Brown83d616a2012-09-09 20:33:43 -07002967
2968 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2969 // Convert rotated viewport to natural surface coordinates.
2970 int32_t naturalLogicalWidth, naturalLogicalHeight;
2971 int32_t naturalPhysicalWidth, naturalPhysicalHeight;
2972 int32_t naturalPhysicalLeft, naturalPhysicalTop;
2973 int32_t naturalDeviceWidth, naturalDeviceHeight;
2974 switch (mViewport.orientation) {
2975 case DISPLAY_ORIENTATION_90:
2976 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2977 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2978 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2979 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
2980 naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
2981 naturalPhysicalTop = mViewport.physicalLeft;
2982 naturalDeviceWidth = mViewport.deviceHeight;
2983 naturalDeviceHeight = mViewport.deviceWidth;
2984 break;
2985 case DISPLAY_ORIENTATION_180:
2986 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
2987 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
2988 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
2989 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
2990 naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
2991 naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
2992 naturalDeviceWidth = mViewport.deviceWidth;
2993 naturalDeviceHeight = mViewport.deviceHeight;
2994 break;
2995 case DISPLAY_ORIENTATION_270:
2996 naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop;
2997 naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft;
2998 naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
2999 naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
3000 naturalPhysicalLeft = mViewport.physicalTop;
3001 naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
3002 naturalDeviceWidth = mViewport.deviceHeight;
3003 naturalDeviceHeight = mViewport.deviceWidth;
3004 break;
3005 case DISPLAY_ORIENTATION_0:
3006 default:
3007 naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft;
3008 naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop;
3009 naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
3010 naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
3011 naturalPhysicalLeft = mViewport.physicalLeft;
3012 naturalPhysicalTop = mViewport.physicalTop;
3013 naturalDeviceWidth = mViewport.deviceWidth;
3014 naturalDeviceHeight = mViewport.deviceHeight;
3015 break;
3016 }
3017
3018 mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth;
3019 mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight;
3020 mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth;
3021 mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight;
3022
3023 mSurfaceOrientation = mParameters.orientationAware ?
3024 mViewport.orientation : DISPLAY_ORIENTATION_0;
3025 } else {
3026 mSurfaceWidth = rawWidth;
3027 mSurfaceHeight = rawHeight;
3028 mSurfaceLeft = 0;
3029 mSurfaceTop = 0;
3030 mSurfaceOrientation = DISPLAY_ORIENTATION_0;
3031 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003032 }
3033
3034 // If moving between pointer modes, need to reset some state.
Michael Wrightf583d0d2013-10-29 13:24:41 -07003035 bool deviceModeChanged = mDeviceMode != oldDeviceMode;
3036 if (deviceModeChanged) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003037 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08003038 }
3039
Jeff Browndaf4a122011-08-26 17:14:14 -07003040 // Create pointer controller if needed.
3041 if (mDeviceMode == DEVICE_MODE_POINTER ||
3042 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
3043 if (mPointerController == NULL) {
3044 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
3045 }
3046 } else {
3047 mPointerController.clear();
3048 }
3049
Jeff Brown83d616a2012-09-09 20:33:43 -07003050 if (viewportChanged || deviceModeChanged) {
3051 ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
3052 "display id %d",
3053 getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight,
3054 mSurfaceOrientation, mDeviceMode, mViewport.displayId);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003055
Jeff Brown8d608662010-08-30 03:02:23 -07003056 // Configure X and Y factors.
Jeff Brown83d616a2012-09-09 20:33:43 -07003057 mXScale = float(mSurfaceWidth) / rawWidth;
3058 mYScale = float(mSurfaceHeight) / rawHeight;
3059 mXTranslate = -mSurfaceLeft;
3060 mYTranslate = -mSurfaceTop;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003061 mXPrecision = 1.0f / mXScale;
3062 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003063
Jeff Brownbe1aa822011-07-27 16:04:54 -07003064 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07003065 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003066 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07003067 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08003068
Jeff Brownbe1aa822011-07-27 16:04:54 -07003069 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003070
Jeff Brown8d608662010-08-30 03:02:23 -07003071 // Scale factor for terms that are not oriented in a particular axis.
3072 // If the pixels are square then xScale == yScale otherwise we fake it
3073 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003074 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003075
Jeff Brown8d608662010-08-30 03:02:23 -07003076 // Size of diagonal axis.
Jeff Brown83d616a2012-09-09 20:33:43 -07003077 float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003078
Jeff Browna1f89ce2011-08-11 00:05:01 -07003079 // Size factors.
3080 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
3081 if (mRawPointerAxes.touchMajor.valid
3082 && mRawPointerAxes.touchMajor.maxValue != 0) {
3083 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
3084 } else if (mRawPointerAxes.toolMajor.valid
3085 && mRawPointerAxes.toolMajor.maxValue != 0) {
3086 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
3087 } else {
3088 mSizeScale = 0.0f;
3089 }
3090
Jeff Brownbe1aa822011-07-27 16:04:54 -07003091 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003092 mOrientedRanges.haveToolSize = true;
3093 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08003094
Jeff Brownbe1aa822011-07-27 16:04:54 -07003095 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003096 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003097 mOrientedRanges.touchMajor.min = 0;
3098 mOrientedRanges.touchMajor.max = diagonalSize;
3099 mOrientedRanges.touchMajor.flat = 0;
3100 mOrientedRanges.touchMajor.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003101 mOrientedRanges.touchMajor.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003102
Jeff Brownbe1aa822011-07-27 16:04:54 -07003103 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
3104 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08003105
Jeff Brownbe1aa822011-07-27 16:04:54 -07003106 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07003107 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003108 mOrientedRanges.toolMajor.min = 0;
3109 mOrientedRanges.toolMajor.max = diagonalSize;
3110 mOrientedRanges.toolMajor.flat = 0;
3111 mOrientedRanges.toolMajor.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003112 mOrientedRanges.toolMajor.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003113
Jeff Brownbe1aa822011-07-27 16:04:54 -07003114 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
3115 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003116
3117 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003118 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003119 mOrientedRanges.size.min = 0;
3120 mOrientedRanges.size.max = 1.0;
3121 mOrientedRanges.size.flat = 0;
3122 mOrientedRanges.size.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003123 mOrientedRanges.size.resolution = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003124 } else {
3125 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07003126 }
3127
3128 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003129 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003130 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
3131 || mCalibration.pressureCalibration
3132 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
3133 if (mCalibration.havePressureScale) {
3134 mPressureScale = mCalibration.pressureScale;
3135 } else if (mRawPointerAxes.pressure.valid
3136 && mRawPointerAxes.pressure.maxValue != 0) {
3137 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07003138 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003139 }
Jeff Brown8d608662010-08-30 03:02:23 -07003140
Jeff Brown65fd2512011-08-18 11:20:58 -07003141 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
3142 mOrientedRanges.pressure.source = mSource;
3143 mOrientedRanges.pressure.min = 0;
3144 mOrientedRanges.pressure.max = 1.0;
3145 mOrientedRanges.pressure.flat = 0;
3146 mOrientedRanges.pressure.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003147 mOrientedRanges.pressure.resolution = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08003148
Jeff Brown65fd2512011-08-18 11:20:58 -07003149 // Tilt
3150 mTiltXCenter = 0;
3151 mTiltXScale = 0;
3152 mTiltYCenter = 0;
3153 mTiltYScale = 0;
3154 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
3155 if (mHaveTilt) {
3156 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
3157 mRawPointerAxes.tiltX.maxValue);
3158 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
3159 mRawPointerAxes.tiltY.maxValue);
3160 mTiltXScale = M_PI / 180;
3161 mTiltYScale = M_PI / 180;
3162
3163 mOrientedRanges.haveTilt = true;
3164
3165 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
3166 mOrientedRanges.tilt.source = mSource;
3167 mOrientedRanges.tilt.min = 0;
3168 mOrientedRanges.tilt.max = M_PI_2;
3169 mOrientedRanges.tilt.flat = 0;
3170 mOrientedRanges.tilt.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003171 mOrientedRanges.tilt.resolution = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003172 }
3173
Jeff Brown8d608662010-08-30 03:02:23 -07003174 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07003175 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003176 if (mHaveTilt) {
3177 mOrientedRanges.haveOrientation = true;
3178
3179 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
3180 mOrientedRanges.orientation.source = mSource;
3181 mOrientedRanges.orientation.min = -M_PI;
3182 mOrientedRanges.orientation.max = M_PI;
3183 mOrientedRanges.orientation.flat = 0;
3184 mOrientedRanges.orientation.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003185 mOrientedRanges.orientation.resolution = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003186 } else if (mCalibration.orientationCalibration !=
3187 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07003188 if (mCalibration.orientationCalibration
3189 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003190 if (mRawPointerAxes.orientation.valid) {
Jeff Brown037f7272012-06-25 17:31:23 -07003191 if (mRawPointerAxes.orientation.maxValue > 0) {
3192 mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue;
3193 } else if (mRawPointerAxes.orientation.minValue < 0) {
3194 mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue;
3195 } else {
3196 mOrientationScale = 0;
3197 }
Jeff Brown8d608662010-08-30 03:02:23 -07003198 }
3199 }
3200
Jeff Brownbe1aa822011-07-27 16:04:54 -07003201 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003202
Jeff Brownbe1aa822011-07-27 16:04:54 -07003203 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07003204 mOrientedRanges.orientation.source = mSource;
3205 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003206 mOrientedRanges.orientation.max = M_PI_2;
3207 mOrientedRanges.orientation.flat = 0;
3208 mOrientedRanges.orientation.fuzz = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003209 mOrientedRanges.orientation.resolution = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003210 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003211
3212 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07003213 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003214 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
3215 if (mCalibration.distanceCalibration
3216 == Calibration::DISTANCE_CALIBRATION_SCALED) {
3217 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003218 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003219 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003220 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003221 }
3222 }
3223
Jeff Brownbe1aa822011-07-27 16:04:54 -07003224 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003225
Jeff Brownbe1aa822011-07-27 16:04:54 -07003226 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07003227 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003228 mOrientedRanges.distance.min =
3229 mRawPointerAxes.distance.minValue * mDistanceScale;
3230 mOrientedRanges.distance.max =
Andreas Sandblad82399402012-03-21 14:39:57 +01003231 mRawPointerAxes.distance.maxValue * mDistanceScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003232 mOrientedRanges.distance.flat = 0;
3233 mOrientedRanges.distance.fuzz =
3234 mRawPointerAxes.distance.fuzz * mDistanceScale;
Michael Wrightc6091c62013-04-01 20:56:04 -07003235 mOrientedRanges.distance.resolution = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003236 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003237
Jeff Brown83d616a2012-09-09 20:33:43 -07003238 // Compute oriented precision, scales and ranges.
Jeff Brown9626b142011-03-03 02:09:54 -08003239 // Note that the maximum value reported is an inclusive maximum value so it is one
3240 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003241 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08003242 case DISPLAY_ORIENTATION_90:
3243 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003244 mOrientedXPrecision = mYPrecision;
3245 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003246
Jeff Brown83d616a2012-09-09 20:33:43 -07003247 mOrientedRanges.x.min = mYTranslate;
3248 mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003249 mOrientedRanges.x.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003250 mOrientedRanges.x.fuzz = 0;
3251 mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003252
Jeff Brown83d616a2012-09-09 20:33:43 -07003253 mOrientedRanges.y.min = mXTranslate;
3254 mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003255 mOrientedRanges.y.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003256 mOrientedRanges.y.fuzz = 0;
3257 mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003258 break;
Jeff Brown9626b142011-03-03 02:09:54 -08003259
Jeff Brown6d0fec22010-07-23 21:28:06 -07003260 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003261 mOrientedXPrecision = mXPrecision;
3262 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08003263
Jeff Brown83d616a2012-09-09 20:33:43 -07003264 mOrientedRanges.x.min = mXTranslate;
3265 mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003266 mOrientedRanges.x.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003267 mOrientedRanges.x.fuzz = 0;
3268 mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08003269
Jeff Brown83d616a2012-09-09 20:33:43 -07003270 mOrientedRanges.y.min = mYTranslate;
3271 mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003272 mOrientedRanges.y.flat = 0;
Michael Wrightc6091c62013-04-01 20:56:04 -07003273 mOrientedRanges.y.fuzz = 0;
3274 mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003275 break;
3276 }
Jeff Brownace13b12011-03-09 17:39:48 -08003277
Jeff Brown65fd2512011-08-18 11:20:58 -07003278 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown4dac9012013-04-10 01:03:19 -07003279 // Compute pointer gesture detection parameters.
Jeff Brown2352b972011-04-12 22:39:53 -07003280 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003281 float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08003282
Jeff Brown2352b972011-04-12 22:39:53 -07003283 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003284 // given area relative to the diagonal size of the display when no acceleration
3285 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003286 // Assume that the touch pad has a square aspect ratio such that movements in
3287 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003288 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003289 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003290 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003291
3292 // Scale zooms to cover a smaller range of the display than movements do.
3293 // This value determines the area around the pointer that is affected by freeform
3294 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003295 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003296 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003297 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003298
Jeff Brown2352b972011-04-12 22:39:53 -07003299 // Max width between pointers to detect a swipe gesture is more than some fraction
3300 // of the diagonal axis of the touch pad. Touches that are wider than this are
3301 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003302 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003303 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003304
Jeff Brown4dac9012013-04-10 01:03:19 -07003305 // Abort current pointer usages because the state has changed.
3306 abortPointerUsage(when, 0 /*policyFlags*/);
Jeff Brown4dac9012013-04-10 01:03:19 -07003307 }
Jeff Brown65fd2512011-08-18 11:20:58 -07003308
3309 // Inform the dispatcher about the changes.
3310 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003311 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003312 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003313}
3314
Jeff Brownbe1aa822011-07-27 16:04:54 -07003315void TouchInputMapper::dumpSurface(String8& dump) {
Jeff Brown83d616a2012-09-09 20:33:43 -07003316 dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, "
3317 "logicalFrame=[%d, %d, %d, %d], "
3318 "physicalFrame=[%d, %d, %d, %d], "
3319 "deviceSize=[%d, %d]\n",
3320 mViewport.displayId, mViewport.orientation,
3321 mViewport.logicalLeft, mViewport.logicalTop,
3322 mViewport.logicalRight, mViewport.logicalBottom,
3323 mViewport.physicalLeft, mViewport.physicalTop,
3324 mViewport.physicalRight, mViewport.physicalBottom,
3325 mViewport.deviceWidth, mViewport.deviceHeight);
3326
Jeff Brownbe1aa822011-07-27 16:04:54 -07003327 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3328 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
Jeff Brown83d616a2012-09-09 20:33:43 -07003329 dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft);
3330 dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003331 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003332}
3333
Jeff Brownbe1aa822011-07-27 16:04:54 -07003334void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003335 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003336 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003337
Jeff Brownbe1aa822011-07-27 16:04:54 -07003338 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003339
Jeff Brown6328cdc2010-07-29 18:18:33 -07003340 if (virtualKeyDefinitions.size() == 0) {
3341 return;
3342 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003343
Jeff Brownbe1aa822011-07-27 16:04:54 -07003344 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003345
Jeff Brownbe1aa822011-07-27 16:04:54 -07003346 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3347 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3348 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3349 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003350
3351 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003352 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003353 virtualKeyDefinitions[i];
3354
Jeff Brownbe1aa822011-07-27 16:04:54 -07003355 mVirtualKeys.add();
3356 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003357
3358 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3359 int32_t keyCode;
3360 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003361 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003362 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003363 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003364 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003365 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003366 }
3367
Jeff Brown6328cdc2010-07-29 18:18:33 -07003368 virtualKey.keyCode = keyCode;
3369 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003370
Jeff Brown6328cdc2010-07-29 18:18:33 -07003371 // convert the key definition's display coordinates into touch coordinates for a hit box
3372 int32_t halfWidth = virtualKeyDefinition.width / 2;
3373 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003374
Jeff Brown6328cdc2010-07-29 18:18:33 -07003375 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003376 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003377 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003378 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003379 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003380 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003381 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003382 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003383 }
3384}
3385
Jeff Brownbe1aa822011-07-27 16:04:54 -07003386void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3387 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003388 dump.append(INDENT3 "Virtual Keys:\n");
3389
Jeff Brownbe1aa822011-07-27 16:04:54 -07003390 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3391 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003392 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3393 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3394 i, virtualKey.scanCode, virtualKey.keyCode,
3395 virtualKey.hitLeft, virtualKey.hitRight,
3396 virtualKey.hitTop, virtualKey.hitBottom);
3397 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003398 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003399}
3400
Jeff Brown8d608662010-08-30 03:02:23 -07003401void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003402 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003403 Calibration& out = mCalibration;
3404
Jeff Browna1f89ce2011-08-11 00:05:01 -07003405 // Size
3406 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3407 String8 sizeCalibrationString;
3408 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3409 if (sizeCalibrationString == "none") {
3410 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3411 } else if (sizeCalibrationString == "geometric") {
3412 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3413 } else if (sizeCalibrationString == "diameter") {
3414 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
Jeff Brown037f7272012-06-25 17:31:23 -07003415 } else if (sizeCalibrationString == "box") {
3416 out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003417 } else if (sizeCalibrationString == "area") {
3418 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3419 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003420 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003421 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003422 }
3423 }
3424
Jeff Browna1f89ce2011-08-11 00:05:01 -07003425 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3426 out.sizeScale);
3427 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3428 out.sizeBias);
3429 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3430 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003431
3432 // Pressure
3433 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3434 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003435 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003436 if (pressureCalibrationString == "none") {
3437 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3438 } else if (pressureCalibrationString == "physical") {
3439 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3440 } else if (pressureCalibrationString == "amplitude") {
3441 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3442 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003443 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003444 pressureCalibrationString.string());
3445 }
3446 }
3447
Jeff Brown8d608662010-08-30 03:02:23 -07003448 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3449 out.pressureScale);
3450
Jeff Brown8d608662010-08-30 03:02:23 -07003451 // Orientation
3452 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3453 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003454 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003455 if (orientationCalibrationString == "none") {
3456 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3457 } else if (orientationCalibrationString == "interpolated") {
3458 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003459 } else if (orientationCalibrationString == "vector") {
3460 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003461 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003462 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003463 orientationCalibrationString.string());
3464 }
3465 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003466
3467 // Distance
3468 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3469 String8 distanceCalibrationString;
3470 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3471 if (distanceCalibrationString == "none") {
3472 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3473 } else if (distanceCalibrationString == "scaled") {
3474 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3475 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003476 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003477 distanceCalibrationString.string());
3478 }
3479 }
3480
3481 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3482 out.distanceScale);
Michael Wright5e025eb2013-05-15 23:16:54 -07003483
3484 out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_DEFAULT;
3485 String8 coverageCalibrationString;
3486 if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) {
3487 if (coverageCalibrationString == "none") {
3488 out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE;
3489 } else if (coverageCalibrationString == "box") {
3490 out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_BOX;
3491 } else if (coverageCalibrationString != "default") {
3492 ALOGW("Invalid value for touch.coverage.calibration: '%s'",
3493 coverageCalibrationString.string());
3494 }
3495 }
Jeff Brown8d608662010-08-30 03:02:23 -07003496}
3497
3498void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003499 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003500 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3501 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3502 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003503 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003504 } else {
3505 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3506 }
Jeff Brown8d608662010-08-30 03:02:23 -07003507
Jeff Browna1f89ce2011-08-11 00:05:01 -07003508 // Pressure
3509 if (mRawPointerAxes.pressure.valid) {
3510 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3511 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3512 }
3513 } else {
3514 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003515 }
3516
3517 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003518 if (mRawPointerAxes.orientation.valid) {
3519 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003520 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003521 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003522 } else {
3523 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003524 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003525
3526 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003527 if (mRawPointerAxes.distance.valid) {
3528 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003529 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003530 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003531 } else {
3532 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003533 }
Michael Wright5e025eb2013-05-15 23:16:54 -07003534
3535 // Coverage
3536 if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_DEFAULT) {
3537 mCalibration.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE;
3538 }
Jeff Brown8d608662010-08-30 03:02:23 -07003539}
3540
Jeff Brownef3d7e82010-09-30 14:33:04 -07003541void TouchInputMapper::dumpCalibration(String8& dump) {
3542 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003543
Jeff Browna1f89ce2011-08-11 00:05:01 -07003544 // Size
3545 switch (mCalibration.sizeCalibration) {
3546 case Calibration::SIZE_CALIBRATION_NONE:
3547 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003548 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003549 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3550 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003551 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003552 case Calibration::SIZE_CALIBRATION_DIAMETER:
3553 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3554 break;
Jeff Brown037f7272012-06-25 17:31:23 -07003555 case Calibration::SIZE_CALIBRATION_BOX:
3556 dump.append(INDENT4 "touch.size.calibration: box\n");
3557 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003558 case Calibration::SIZE_CALIBRATION_AREA:
3559 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003560 break;
3561 default:
Steve Blockec193de2012-01-09 18:35:44 +00003562 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003563 }
3564
Jeff Browna1f89ce2011-08-11 00:05:01 -07003565 if (mCalibration.haveSizeScale) {
3566 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3567 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003568 }
3569
Jeff Browna1f89ce2011-08-11 00:05:01 -07003570 if (mCalibration.haveSizeBias) {
3571 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3572 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003573 }
3574
Jeff Browna1f89ce2011-08-11 00:05:01 -07003575 if (mCalibration.haveSizeIsSummed) {
3576 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3577 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003578 }
3579
3580 // Pressure
3581 switch (mCalibration.pressureCalibration) {
3582 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003583 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003584 break;
3585 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003586 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003587 break;
3588 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003589 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003590 break;
3591 default:
Steve Blockec193de2012-01-09 18:35:44 +00003592 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003593 }
3594
Jeff Brown8d608662010-08-30 03:02:23 -07003595 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003596 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3597 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003598 }
3599
Jeff Brown8d608662010-08-30 03:02:23 -07003600 // Orientation
3601 switch (mCalibration.orientationCalibration) {
3602 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003603 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003604 break;
3605 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003606 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003607 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003608 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3609 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3610 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003611 default:
Steve Blockec193de2012-01-09 18:35:44 +00003612 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003613 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003614
3615 // Distance
3616 switch (mCalibration.distanceCalibration) {
3617 case Calibration::DISTANCE_CALIBRATION_NONE:
3618 dump.append(INDENT4 "touch.distance.calibration: none\n");
3619 break;
3620 case Calibration::DISTANCE_CALIBRATION_SCALED:
3621 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3622 break;
3623 default:
Steve Blockec193de2012-01-09 18:35:44 +00003624 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003625 }
3626
3627 if (mCalibration.haveDistanceScale) {
3628 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3629 mCalibration.distanceScale);
3630 }
Michael Wright5e025eb2013-05-15 23:16:54 -07003631
3632 switch (mCalibration.coverageCalibration) {
3633 case Calibration::COVERAGE_CALIBRATION_NONE:
3634 dump.append(INDENT4 "touch.coverage.calibration: none\n");
3635 break;
3636 case Calibration::COVERAGE_CALIBRATION_BOX:
3637 dump.append(INDENT4 "touch.coverage.calibration: box\n");
3638 break;
3639 default:
3640 ALOG_ASSERT(false);
3641 }
Jeff Brown8d608662010-08-30 03:02:23 -07003642}
3643
Jeff Brown65fd2512011-08-18 11:20:58 -07003644void TouchInputMapper::reset(nsecs_t when) {
3645 mCursorButtonAccumulator.reset(getDevice());
3646 mCursorScrollAccumulator.reset(getDevice());
3647 mTouchButtonAccumulator.reset(getDevice());
3648
3649 mPointerVelocityControl.reset();
3650 mWheelXVelocityControl.reset();
3651 mWheelYVelocityControl.reset();
3652
Jeff Brownbe1aa822011-07-27 16:04:54 -07003653 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003654 mLastRawPointerData.clear();
3655 mCurrentCookedPointerData.clear();
3656 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003657 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003658 mLastButtonState = 0;
3659 mCurrentRawVScroll = 0;
3660 mCurrentRawHScroll = 0;
3661 mCurrentFingerIdBits.clear();
3662 mLastFingerIdBits.clear();
3663 mCurrentStylusIdBits.clear();
3664 mLastStylusIdBits.clear();
3665 mCurrentMouseIdBits.clear();
3666 mLastMouseIdBits.clear();
3667 mPointerUsage = POINTER_USAGE_NONE;
3668 mSentHoverEnter = false;
3669 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003670
Jeff Brown65fd2512011-08-18 11:20:58 -07003671 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003672
Jeff Brown65fd2512011-08-18 11:20:58 -07003673 mPointerGesture.reset();
3674 mPointerSimple.reset();
3675
3676 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003677 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3678 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003679 }
3680
Jeff Brown65fd2512011-08-18 11:20:58 -07003681 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003682}
3683
Jeff Brown65fd2512011-08-18 11:20:58 -07003684void TouchInputMapper::process(const RawEvent* rawEvent) {
3685 mCursorButtonAccumulator.process(rawEvent);
3686 mCursorScrollAccumulator.process(rawEvent);
3687 mTouchButtonAccumulator.process(rawEvent);
3688
Jeff Brown49ccac52012-04-11 18:27:33 -07003689 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003690 sync(rawEvent->when);
3691 }
3692}
3693
3694void TouchInputMapper::sync(nsecs_t when) {
3695 // Sync button state.
3696 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3697 | mCursorButtonAccumulator.getButtonState();
3698
3699 // Sync scroll state.
3700 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3701 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3702 mCursorScrollAccumulator.finishSync();
3703
3704 // Sync touch state.
3705 bool havePointerIds = true;
3706 mCurrentRawPointerData.clear();
3707 syncTouch(when, &havePointerIds);
3708
Jeff Brownaa3855d2011-03-17 01:34:19 -07003709#if DEBUG_RAW_EVENTS
3710 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003711 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003712 mLastRawPointerData.pointerCount,
3713 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003714 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003715 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003716 "hovering ids 0x%08x -> 0x%08x",
3717 mLastRawPointerData.pointerCount,
3718 mCurrentRawPointerData.pointerCount,
3719 mLastRawPointerData.touchingIdBits.value,
3720 mCurrentRawPointerData.touchingIdBits.value,
3721 mLastRawPointerData.hoveringIdBits.value,
3722 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003723 }
3724#endif
3725
Jeff Brown65fd2512011-08-18 11:20:58 -07003726 // Reset state that we will compute below.
3727 mCurrentFingerIdBits.clear();
3728 mCurrentStylusIdBits.clear();
3729 mCurrentMouseIdBits.clear();
3730 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003731
Jeff Brown65fd2512011-08-18 11:20:58 -07003732 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3733 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003734 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003735 mCurrentButtonState = 0;
3736 } else {
3737 // Preprocess pointer data.
3738 if (!havePointerIds) {
3739 assignPointerIds();
3740 }
3741
3742 // Handle policy on initial down or hover events.
3743 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003744 bool initialDown = mLastRawPointerData.pointerCount == 0
3745 && mCurrentRawPointerData.pointerCount != 0;
3746 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3747 if (initialDown || buttonsPressed) {
3748 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003749 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003750 getContext()->fadePointer();
3751 }
3752
Jeff Brown6ca90042014-02-26 15:12:45 -08003753 if (mParameters.wake) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003754 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3755 }
3756 }
3757
3758 // Synthesize key down from raw buttons if needed.
3759 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3760 policyFlags, mLastButtonState, mCurrentButtonState);
3761
3762 // Consume raw off-screen touches before cooking pointer data.
3763 // If touches are consumed, subsequent code will not receive any pointer data.
3764 if (consumeRawTouches(when, policyFlags)) {
3765 mCurrentRawPointerData.clear();
3766 }
3767
3768 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3769 // with cooked pointer data that has the same ids and indices as the raw data.
3770 // The following code can use either the raw or cooked data, as needed.
3771 cookPointerData();
3772
3773 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003774 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003775 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3776 uint32_t id = idBits.clearFirstMarkedBit();
3777 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3778 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3779 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3780 mCurrentStylusIdBits.markBit(id);
3781 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3782 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3783 mCurrentFingerIdBits.markBit(id);
3784 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3785 mCurrentMouseIdBits.markBit(id);
3786 }
3787 }
3788 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3789 uint32_t id = idBits.clearFirstMarkedBit();
3790 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3791 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3792 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3793 mCurrentStylusIdBits.markBit(id);
3794 }
3795 }
3796
3797 // Stylus takes precedence over all tools, then mouse, then finger.
3798 PointerUsage pointerUsage = mPointerUsage;
3799 if (!mCurrentStylusIdBits.isEmpty()) {
3800 mCurrentMouseIdBits.clear();
3801 mCurrentFingerIdBits.clear();
3802 pointerUsage = POINTER_USAGE_STYLUS;
3803 } else if (!mCurrentMouseIdBits.isEmpty()) {
3804 mCurrentFingerIdBits.clear();
3805 pointerUsage = POINTER_USAGE_MOUSE;
3806 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3807 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003808 }
3809
3810 dispatchPointerUsage(when, policyFlags, pointerUsage);
3811 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003812 if (mDeviceMode == DEVICE_MODE_DIRECT
3813 && mConfig.showTouches && mPointerController != NULL) {
3814 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3815 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3816
3817 mPointerController->setButtonState(mCurrentButtonState);
3818 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3819 mCurrentCookedPointerData.idToIndex,
3820 mCurrentCookedPointerData.touchingIdBits);
3821 }
3822
Jeff Brown65fd2512011-08-18 11:20:58 -07003823 dispatchHoverExit(when, policyFlags);
3824 dispatchTouches(when, policyFlags);
3825 dispatchHoverEnterAndMove(when, policyFlags);
3826 }
3827
3828 // Synthesize key up from raw buttons if needed.
3829 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3830 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003831 }
3832
Jeff Brown6328cdc2010-07-29 18:18:33 -07003833 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003834 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3835 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3836 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003837 mLastFingerIdBits = mCurrentFingerIdBits;
3838 mLastStylusIdBits = mCurrentStylusIdBits;
3839 mLastMouseIdBits = mCurrentMouseIdBits;
3840
3841 // Clear some transient state.
3842 mCurrentRawVScroll = 0;
3843 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003844}
3845
Jeff Brown79ac9692011-04-19 21:20:10 -07003846void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003847 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003848 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3849 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3850 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003851 }
3852}
3853
Jeff Brownbe1aa822011-07-27 16:04:54 -07003854bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3855 // Check for release of a virtual key.
3856 if (mCurrentVirtualKey.down) {
3857 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3858 // Pointer went up while virtual key was down.
3859 mCurrentVirtualKey.down = false;
3860 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003861#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003862 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003863 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003864#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003865 dispatchVirtualKey(when, policyFlags,
3866 AKEY_EVENT_ACTION_UP,
3867 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003868 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003869 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003870 }
3871
Jeff Brownbe1aa822011-07-27 16:04:54 -07003872 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3873 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3874 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3875 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3876 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3877 // Pointer is still within the space of the virtual key.
3878 return true;
3879 }
3880 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003881
Jeff Brownbe1aa822011-07-27 16:04:54 -07003882 // Pointer left virtual key area or another pointer also went down.
3883 // Send key cancellation but do not consume the touch yet.
3884 // This is useful when the user swipes through from the virtual key area
3885 // into the main display surface.
3886 mCurrentVirtualKey.down = false;
3887 if (!mCurrentVirtualKey.ignored) {
3888#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003889 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003890 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3891#endif
3892 dispatchVirtualKey(when, policyFlags,
3893 AKEY_EVENT_ACTION_UP,
3894 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3895 | AKEY_EVENT_FLAG_CANCELED);
3896 }
3897 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003898
Jeff Brownbe1aa822011-07-27 16:04:54 -07003899 if (mLastRawPointerData.touchingIdBits.isEmpty()
3900 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3901 // Pointer just went down. Check for virtual key press or off-screen touches.
3902 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3903 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3904 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3905 // If exactly one pointer went down, check for virtual key hit.
3906 // Otherwise we will drop the entire stroke.
3907 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3908 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3909 if (virtualKey) {
3910 mCurrentVirtualKey.down = true;
3911 mCurrentVirtualKey.downTime = when;
3912 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3913 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3914 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3915 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3916
3917 if (!mCurrentVirtualKey.ignored) {
3918#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003919 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003920 mCurrentVirtualKey.keyCode,
3921 mCurrentVirtualKey.scanCode);
3922#endif
3923 dispatchVirtualKey(when, policyFlags,
3924 AKEY_EVENT_ACTION_DOWN,
3925 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3926 }
3927 }
3928 }
3929 return true;
3930 }
3931 }
3932
Jeff Brownfe508922011-01-18 15:10:10 -08003933 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003934 // most recent touch within the screen area. The idea is to filter out stray
3935 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003936 //
3937 // Problems we're trying to solve:
3938 //
3939 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3940 // virtual key area that is implemented by a separate touch panel and accidentally
3941 // triggers a virtual key.
3942 //
3943 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3944 // area and accidentally triggers a virtual key. This often happens when virtual keys
3945 // are layed out below the screen near to where the on screen keyboard's space bar
3946 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003947 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003948 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003949 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003950 return false;
3951}
3952
3953void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3954 int32_t keyEventAction, int32_t keyEventFlags) {
3955 int32_t keyCode = mCurrentVirtualKey.keyCode;
3956 int32_t scanCode = mCurrentVirtualKey.scanCode;
3957 nsecs_t downTime = mCurrentVirtualKey.downTime;
3958 int32_t metaState = mContext->getGlobalMetaState();
3959 policyFlags |= POLICY_FLAG_VIRTUAL;
3960
3961 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3962 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3963 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003964}
3965
Jeff Brown6d0fec22010-07-23 21:28:06 -07003966void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003967 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3968 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003969 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003970 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003971
3972 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003973 if (!currentIdBits.isEmpty()) {
3974 // No pointer id changes so this is a move event.
3975 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003976 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003977 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3978 AMOTION_EVENT_EDGE_FLAG_NONE,
3979 mCurrentCookedPointerData.pointerProperties,
3980 mCurrentCookedPointerData.pointerCoords,
3981 mCurrentCookedPointerData.idToIndex,
3982 currentIdBits, -1,
3983 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3984 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003985 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003986 // There may be pointers going up and pointers going down and pointers moving
3987 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003988 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3989 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003990 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003991 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003992
Jeff Brownace13b12011-03-09 17:39:48 -08003993 // Update last coordinates of pointers that have moved so that we observe the new
3994 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003995 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003996 mCurrentCookedPointerData.pointerProperties,
3997 mCurrentCookedPointerData.pointerCoords,
3998 mCurrentCookedPointerData.idToIndex,
3999 mLastCookedPointerData.pointerProperties,
4000 mLastCookedPointerData.pointerCoords,
4001 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08004002 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004003 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004004 moveNeeded = true;
4005 }
Jeff Brownc3db8582010-10-20 15:33:38 -07004006
Jeff Brownace13b12011-03-09 17:39:48 -08004007 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07004008 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004009 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004010
Jeff Brown65fd2512011-08-18 11:20:58 -07004011 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004012 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004013 mLastCookedPointerData.pointerProperties,
4014 mLastCookedPointerData.pointerCoords,
4015 mLastCookedPointerData.idToIndex,
4016 dispatchedIdBits, upId,
4017 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004018 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07004019 }
4020
Jeff Brownc3db8582010-10-20 15:33:38 -07004021 // Dispatch move events if any of the remaining pointers moved from their old locations.
4022 // Although applications receive new locations as part of individual pointer up
4023 // events, they do not generally handle them except when presented in a move event.
4024 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00004025 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07004026 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004027 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004028 mCurrentCookedPointerData.pointerProperties,
4029 mCurrentCookedPointerData.pointerCoords,
4030 mCurrentCookedPointerData.idToIndex,
4031 dispatchedIdBits, -1,
4032 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07004033 }
4034
4035 // Dispatch pointer down events using the new pointer locations.
4036 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004037 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004038 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07004039
Jeff Brownace13b12011-03-09 17:39:48 -08004040 if (dispatchedIdBits.count() == 1) {
4041 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07004042 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004043 }
4044
Jeff Brown65fd2512011-08-18 11:20:58 -07004045 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004046 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004047 mCurrentCookedPointerData.pointerProperties,
4048 mCurrentCookedPointerData.pointerCoords,
4049 mCurrentCookedPointerData.idToIndex,
4050 dispatchedIdBits, downId,
4051 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004052 }
4053 }
Jeff Brownace13b12011-03-09 17:39:48 -08004054}
4055
Jeff Brownbe1aa822011-07-27 16:04:54 -07004056void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
4057 if (mSentHoverEnter &&
4058 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
4059 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
4060 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07004061 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004062 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
4063 mLastCookedPointerData.pointerProperties,
4064 mLastCookedPointerData.pointerCoords,
4065 mLastCookedPointerData.idToIndex,
4066 mLastCookedPointerData.hoveringIdBits, -1,
4067 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4068 mSentHoverEnter = false;
4069 }
4070}
Jeff Brownace13b12011-03-09 17:39:48 -08004071
Jeff Brownbe1aa822011-07-27 16:04:54 -07004072void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
4073 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
4074 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
4075 int32_t metaState = getContext()->getGlobalMetaState();
4076 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004077 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004078 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
4079 mCurrentCookedPointerData.pointerProperties,
4080 mCurrentCookedPointerData.pointerCoords,
4081 mCurrentCookedPointerData.idToIndex,
4082 mCurrentCookedPointerData.hoveringIdBits, -1,
4083 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4084 mSentHoverEnter = true;
4085 }
Jeff Brownace13b12011-03-09 17:39:48 -08004086
Jeff Brown65fd2512011-08-18 11:20:58 -07004087 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07004088 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
4089 mCurrentCookedPointerData.pointerProperties,
4090 mCurrentCookedPointerData.pointerCoords,
4091 mCurrentCookedPointerData.idToIndex,
4092 mCurrentCookedPointerData.hoveringIdBits, -1,
4093 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
4094 }
4095}
4096
4097void TouchInputMapper::cookPointerData() {
4098 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4099
4100 mCurrentCookedPointerData.clear();
4101 mCurrentCookedPointerData.pointerCount = currentPointerCount;
4102 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
4103 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
4104
4105 // Walk through the the active pointers and map device coordinates onto
4106 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08004107 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004108 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004109
Jeff Browna1f89ce2011-08-11 00:05:01 -07004110 // Size
4111 float touchMajor, touchMinor, toolMajor, toolMinor, size;
4112 switch (mCalibration.sizeCalibration) {
4113 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
4114 case Calibration::SIZE_CALIBRATION_DIAMETER:
Jeff Brown037f7272012-06-25 17:31:23 -07004115 case Calibration::SIZE_CALIBRATION_BOX:
Jeff Browna1f89ce2011-08-11 00:05:01 -07004116 case Calibration::SIZE_CALIBRATION_AREA:
4117 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
4118 touchMajor = in.touchMajor;
4119 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
4120 toolMajor = in.toolMajor;
4121 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
4122 size = mRawPointerAxes.touchMinor.valid
4123 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4124 } else if (mRawPointerAxes.touchMajor.valid) {
4125 toolMajor = touchMajor = in.touchMajor;
4126 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
4127 ? in.touchMinor : in.touchMajor;
4128 size = mRawPointerAxes.touchMinor.valid
4129 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
4130 } else if (mRawPointerAxes.toolMajor.valid) {
4131 touchMajor = toolMajor = in.toolMajor;
4132 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
4133 ? in.toolMinor : in.toolMajor;
4134 size = mRawPointerAxes.toolMinor.valid
4135 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004136 } else {
Steve Blockec193de2012-01-09 18:35:44 +00004137 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07004138 "Size calibration should have been resolved to NONE.");
4139 touchMajor = 0;
4140 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004141 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004142 toolMinor = 0;
4143 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004144 }
Jeff Brownace13b12011-03-09 17:39:48 -08004145
Jeff Browna1f89ce2011-08-11 00:05:01 -07004146 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
4147 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
4148 if (touchingCount > 1) {
4149 touchMajor /= touchingCount;
4150 touchMinor /= touchingCount;
4151 toolMajor /= touchingCount;
4152 toolMinor /= touchingCount;
4153 size /= touchingCount;
4154 }
4155 }
Jeff Brownace13b12011-03-09 17:39:48 -08004156
Jeff Browna1f89ce2011-08-11 00:05:01 -07004157 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
4158 touchMajor *= mGeometricScale;
4159 touchMinor *= mGeometricScale;
4160 toolMajor *= mGeometricScale;
4161 toolMinor *= mGeometricScale;
4162 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
4163 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004164 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004165 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
4166 toolMinor = toolMajor;
4167 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
4168 touchMinor = touchMajor;
4169 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08004170 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07004171
4172 mCalibration.applySizeScaleAndBias(&touchMajor);
4173 mCalibration.applySizeScaleAndBias(&touchMinor);
4174 mCalibration.applySizeScaleAndBias(&toolMajor);
4175 mCalibration.applySizeScaleAndBias(&toolMinor);
4176 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004177 break;
4178 default:
4179 touchMajor = 0;
4180 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07004181 toolMajor = 0;
4182 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004183 size = 0;
4184 break;
4185 }
4186
Jeff Browna1f89ce2011-08-11 00:05:01 -07004187 // Pressure
4188 float pressure;
4189 switch (mCalibration.pressureCalibration) {
4190 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
4191 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
4192 pressure = in.pressure * mPressureScale;
4193 break;
4194 default:
4195 pressure = in.isHovering ? 0 : 1;
4196 break;
4197 }
4198
Jeff Brown65fd2512011-08-18 11:20:58 -07004199 // Tilt and Orientation
4200 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08004201 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07004202 if (mHaveTilt) {
4203 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
4204 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
4205 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
4206 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
4207 } else {
4208 tilt = 0;
4209
4210 switch (mCalibration.orientationCalibration) {
4211 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown037f7272012-06-25 17:31:23 -07004212 orientation = in.orientation * mOrientationScale;
Jeff Brown65fd2512011-08-18 11:20:58 -07004213 break;
4214 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
4215 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
4216 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
4217 if (c1 != 0 || c2 != 0) {
4218 orientation = atan2f(c1, c2) * 0.5f;
4219 float confidence = hypotf(c1, c2);
4220 float scale = 1.0f + confidence / 16.0f;
4221 touchMajor *= scale;
4222 touchMinor /= scale;
4223 toolMajor *= scale;
4224 toolMinor /= scale;
4225 } else {
4226 orientation = 0;
4227 }
4228 break;
4229 }
4230 default:
Jeff Brownace13b12011-03-09 17:39:48 -08004231 orientation = 0;
4232 }
Jeff Brownace13b12011-03-09 17:39:48 -08004233 }
4234
Jeff Brown80fd47c2011-05-24 01:07:44 -07004235 // Distance
4236 float distance;
4237 switch (mCalibration.distanceCalibration) {
4238 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07004239 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004240 break;
4241 default:
4242 distance = 0;
4243 }
4244
Michael Wright5e025eb2013-05-15 23:16:54 -07004245 // Coverage
4246 int32_t rawLeft, rawTop, rawRight, rawBottom;
4247 switch (mCalibration.coverageCalibration) {
4248 case Calibration::COVERAGE_CALIBRATION_BOX:
4249 rawLeft = (in.toolMinor & 0xffff0000) >> 16;
4250 rawRight = in.toolMinor & 0x0000ffff;
4251 rawBottom = in.toolMajor & 0x0000ffff;
4252 rawTop = (in.toolMajor & 0xffff0000) >> 16;
4253 break;
4254 default:
4255 rawLeft = rawTop = rawRight = rawBottom = 0;
4256 break;
4257 }
4258
4259 // X, Y, and the bounding box for coverage information
Jeff Brownace13b12011-03-09 17:39:48 -08004260 // Adjust coords for surface orientation.
Michael Wright5e025eb2013-05-15 23:16:54 -07004261 float x, y, left, top, right, bottom;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004262 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08004263 case DISPLAY_ORIENTATION_90:
Jeff Brown83d616a2012-09-09 20:33:43 -07004264 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4265 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004266 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4267 right = float(rawBottom- mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4268 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate;
4269 top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004270 orientation -= M_PI_2;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004271 if (orientation < mOrientedRanges.orientation.min) {
4272 orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
Jeff Brownace13b12011-03-09 17:39:48 -08004273 }
4274 break;
4275 case DISPLAY_ORIENTATION_180:
Jeff Brown83d616a2012-09-09 20:33:43 -07004276 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate;
4277 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004278 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate;
4279 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate;
4280 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate;
4281 top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004282 orientation -= M_PI;
4283 if (orientation < mOrientedRanges.orientation.min) {
4284 orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
4285 }
Jeff Brownace13b12011-03-09 17:39:48 -08004286 break;
4287 case DISPLAY_ORIENTATION_270:
Jeff Brown83d616a2012-09-09 20:33:43 -07004288 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate;
4289 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004290 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate;
4291 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate;
4292 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4293 top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004294 orientation += M_PI_2;
Jason Gerecke70bca4c2013-03-01 11:49:07 -08004295 if (orientation > mOrientedRanges.orientation.max) {
4296 orientation -= (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min);
Jeff Brownace13b12011-03-09 17:39:48 -08004297 }
4298 break;
4299 default:
Jeff Brown83d616a2012-09-09 20:33:43 -07004300 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4301 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Michael Wright5e025eb2013-05-15 23:16:54 -07004302 left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4303 right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate;
4304 bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
4305 top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate;
Jeff Brownace13b12011-03-09 17:39:48 -08004306 break;
4307 }
4308
4309 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004310 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08004311 out.clear();
4312 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4313 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4314 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
4315 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
4316 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
4317 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
Jeff Brownace13b12011-03-09 17:39:48 -08004318 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07004319 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004320 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Michael Wright5e025eb2013-05-15 23:16:54 -07004321 if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) {
4322 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left);
4323 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top);
4324 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right);
4325 out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom);
4326 } else {
4327 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
4328 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
4329 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004330
4331 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004332 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
4333 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004334 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004335 properties.id = id;
4336 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08004337
Jeff Brownbe1aa822011-07-27 16:04:54 -07004338 // Write id index.
4339 mCurrentCookedPointerData.idToIndex[id] = i;
4340 }
Jeff Brownace13b12011-03-09 17:39:48 -08004341}
4342
Jeff Brown65fd2512011-08-18 11:20:58 -07004343void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
4344 PointerUsage pointerUsage) {
4345 if (pointerUsage != mPointerUsage) {
4346 abortPointerUsage(when, policyFlags);
4347 mPointerUsage = pointerUsage;
4348 }
4349
4350 switch (mPointerUsage) {
4351 case POINTER_USAGE_GESTURES:
4352 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
4353 break;
4354 case POINTER_USAGE_STYLUS:
4355 dispatchPointerStylus(when, policyFlags);
4356 break;
4357 case POINTER_USAGE_MOUSE:
4358 dispatchPointerMouse(when, policyFlags);
4359 break;
4360 default:
4361 break;
4362 }
4363}
4364
4365void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
4366 switch (mPointerUsage) {
4367 case POINTER_USAGE_GESTURES:
4368 abortPointerGestures(when, policyFlags);
4369 break;
4370 case POINTER_USAGE_STYLUS:
4371 abortPointerStylus(when, policyFlags);
4372 break;
4373 case POINTER_USAGE_MOUSE:
4374 abortPointerMouse(when, policyFlags);
4375 break;
4376 default:
4377 break;
4378 }
4379
4380 mPointerUsage = POINTER_USAGE_NONE;
4381}
4382
Jeff Brown79ac9692011-04-19 21:20:10 -07004383void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4384 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004385 // Update current gesture coordinates.
4386 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004387 bool sendEvents = preparePointerGestures(when,
4388 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4389 if (!sendEvents) {
4390 return;
4391 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004392 if (finishPreviousGesture) {
4393 cancelPreviousGesture = false;
4394 }
Jeff Brownace13b12011-03-09 17:39:48 -08004395
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004396 // Update the pointer presentation and spots.
4397 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4398 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4399 if (finishPreviousGesture || cancelPreviousGesture) {
4400 mPointerController->clearSpots();
4401 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004402 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4403 mPointerGesture.currentGestureIdToIndex,
4404 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004405 } else {
4406 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4407 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004408
Jeff Brown538881e2011-05-25 18:23:38 -07004409 // Show or hide the pointer if needed.
4410 switch (mPointerGesture.currentGestureMode) {
4411 case PointerGesture::NEUTRAL:
4412 case PointerGesture::QUIET:
4413 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4414 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4415 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4416 // Remind the user of where the pointer is after finishing a gesture with spots.
4417 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4418 }
4419 break;
4420 case PointerGesture::TAP:
4421 case PointerGesture::TAP_DRAG:
4422 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4423 case PointerGesture::HOVER:
4424 case PointerGesture::PRESS:
4425 // Unfade the pointer when the current gesture manipulates the
4426 // area directly under the pointer.
4427 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4428 break;
4429 case PointerGesture::SWIPE:
4430 case PointerGesture::FREEFORM:
4431 // Fade the pointer when the current gesture manipulates a different
4432 // area and there are spots to guide the user experience.
4433 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4434 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4435 } else {
4436 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4437 }
4438 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004439 }
4440
Jeff Brownace13b12011-03-09 17:39:48 -08004441 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004442 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004443 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004444
4445 // Update last coordinates of pointers that have moved so that we observe the new
4446 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004447 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4448 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4449 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004450 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004451 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4452 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4453 bool moveNeeded = false;
4454 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004455 && !mPointerGesture.lastGestureIdBits.isEmpty()
4456 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004457 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4458 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004459 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004460 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004461 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004462 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4463 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004464 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004465 moveNeeded = true;
4466 }
Jeff Brownace13b12011-03-09 17:39:48 -08004467 }
4468
4469 // Send motion events for all pointers that went up or were canceled.
4470 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4471 if (!dispatchedGestureIdBits.isEmpty()) {
4472 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004473 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004474 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4475 AMOTION_EVENT_EDGE_FLAG_NONE,
4476 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004477 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4478 dispatchedGestureIdBits, -1,
4479 0, 0, mPointerGesture.downTime);
4480
4481 dispatchedGestureIdBits.clear();
4482 } else {
4483 BitSet32 upGestureIdBits;
4484 if (finishPreviousGesture) {
4485 upGestureIdBits = dispatchedGestureIdBits;
4486 } else {
4487 upGestureIdBits.value = dispatchedGestureIdBits.value
4488 & ~mPointerGesture.currentGestureIdBits.value;
4489 }
4490 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004491 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004492
Jeff Brown65fd2512011-08-18 11:20:58 -07004493 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004494 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004495 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4496 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004497 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4498 dispatchedGestureIdBits, id,
4499 0, 0, mPointerGesture.downTime);
4500
4501 dispatchedGestureIdBits.clearBit(id);
4502 }
4503 }
4504 }
4505
4506 // Send motion events for all pointers that moved.
4507 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004508 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004509 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4510 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004511 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4512 dispatchedGestureIdBits, -1,
4513 0, 0, mPointerGesture.downTime);
4514 }
4515
4516 // Send motion events for all pointers that went down.
4517 if (down) {
4518 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4519 & ~dispatchedGestureIdBits.value);
4520 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004521 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004522 dispatchedGestureIdBits.markBit(id);
4523
Jeff Brownace13b12011-03-09 17:39:48 -08004524 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004525 mPointerGesture.downTime = when;
4526 }
4527
Jeff Brown65fd2512011-08-18 11:20:58 -07004528 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004529 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004530 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004531 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4532 dispatchedGestureIdBits, id,
4533 0, 0, mPointerGesture.downTime);
4534 }
4535 }
4536
Jeff Brownace13b12011-03-09 17:39:48 -08004537 // Send motion events for hover.
4538 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004539 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004540 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4541 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4542 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004543 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4544 mPointerGesture.currentGestureIdBits, -1,
4545 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004546 } else if (dispatchedGestureIdBits.isEmpty()
4547 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4548 // Synthesize a hover move event after all pointers go up to indicate that
4549 // the pointer is hovering again even if the user is not currently touching
4550 // the touch pad. This ensures that a view will receive a fresh hover enter
4551 // event after a tap.
4552 float x, y;
4553 mPointerController->getPosition(&x, &y);
4554
4555 PointerProperties pointerProperties;
4556 pointerProperties.clear();
4557 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004558 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004559
4560 PointerCoords pointerCoords;
4561 pointerCoords.clear();
4562 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4563 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4564
Jeff Brown65fd2512011-08-18 11:20:58 -07004565 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004566 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4567 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07004568 mViewport.displayId, 1, &pointerProperties, &pointerCoords,
4569 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004570 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004571 }
4572
4573 // Update state.
4574 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4575 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004576 mPointerGesture.lastGestureIdBits.clear();
4577 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004578 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4579 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004580 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004581 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004582 mPointerGesture.lastGestureProperties[index].copyFrom(
4583 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004584 mPointerGesture.lastGestureCoords[index].copyFrom(
4585 mPointerGesture.currentGestureCoords[index]);
4586 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004587 }
4588 }
4589}
4590
Jeff Brown65fd2512011-08-18 11:20:58 -07004591void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4592 // Cancel previously dispatches pointers.
4593 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4594 int32_t metaState = getContext()->getGlobalMetaState();
4595 int32_t buttonState = mCurrentButtonState;
4596 dispatchMotion(when, policyFlags, mSource,
4597 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4598 AMOTION_EVENT_EDGE_FLAG_NONE,
4599 mPointerGesture.lastGestureProperties,
4600 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4601 mPointerGesture.lastGestureIdBits, -1,
4602 0, 0, mPointerGesture.downTime);
4603 }
4604
4605 // Reset the current pointer gesture.
4606 mPointerGesture.reset();
4607 mPointerVelocityControl.reset();
4608
4609 // Remove any current spots.
4610 if (mPointerController != NULL) {
4611 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4612 mPointerController->clearSpots();
4613 }
4614}
4615
Jeff Brown79ac9692011-04-19 21:20:10 -07004616bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4617 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004618 *outCancelPreviousGesture = false;
4619 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004620
Jeff Brown79ac9692011-04-19 21:20:10 -07004621 // Handle TAP timeout.
4622 if (isTimeout) {
4623#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004624 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004625#endif
4626
4627 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004628 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004629 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004630 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004631 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004632 } else {
4633 // The tap is finished.
4634#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004635 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004636#endif
4637 *outFinishPreviousGesture = true;
4638
4639 mPointerGesture.activeGestureId = -1;
4640 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4641 mPointerGesture.currentGestureIdBits.clear();
4642
Jeff Brown65fd2512011-08-18 11:20:58 -07004643 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004644 return true;
4645 }
4646 }
4647
4648 // We did not handle this timeout.
4649 return false;
4650 }
4651
Jeff Brown65fd2512011-08-18 11:20:58 -07004652 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4653 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4654
Jeff Brownace13b12011-03-09 17:39:48 -08004655 // Update the velocity tracker.
4656 {
4657 VelocityTracker::Position positions[MAX_POINTERS];
4658 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004659 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004660 uint32_t id = idBits.clearFirstMarkedBit();
4661 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004662 positions[count].x = pointer.x * mPointerXMovementScale;
4663 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004664 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004665 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004666 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004667 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004668
Michael Wright398d3092013-07-02 17:35:00 -07004669 // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
4670 // to NEUTRAL, then we should not generate tap event.
4671 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER
4672 && mPointerGesture.lastGestureMode != PointerGesture::TAP
4673 && mPointerGesture.lastGestureMode != PointerGesture::TAP_DRAG) {
4674 mPointerGesture.resetTap();
4675 }
4676
Jeff Brownace13b12011-03-09 17:39:48 -08004677 // Pick a new active touch id if needed.
4678 // Choose an arbitrary pointer that just went down, if there is one.
4679 // Otherwise choose an arbitrary remaining pointer.
4680 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004681 // We keep the same active touch id for as long as possible.
4682 bool activeTouchChanged = false;
4683 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4684 int32_t activeTouchId = lastActiveTouchId;
4685 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004686 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004687 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004688 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004689 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004690 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004691 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004692 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004693 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004694 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004695 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004696 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004697 } else {
4698 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004699 }
4700 }
4701
4702 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004703 bool isQuietTime = false;
4704 if (activeTouchId < 0) {
4705 mPointerGesture.resetQuietTime();
4706 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004707 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004708 if (!isQuietTime) {
4709 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4710 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4711 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004712 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004713 // Enter quiet time when exiting swipe or freeform state.
4714 // This is to prevent accidentally entering the hover state and flinging the
4715 // pointer when finishing a swipe and there is still one pointer left onscreen.
4716 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004717 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004718 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004719 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004720 // Enter quiet time when releasing the button and there are still two or more
4721 // fingers down. This may indicate that one finger was used to press the button
4722 // but it has not gone up yet.
4723 isQuietTime = true;
4724 }
4725 if (isQuietTime) {
4726 mPointerGesture.quietTime = when;
4727 }
Jeff Brownace13b12011-03-09 17:39:48 -08004728 }
4729 }
4730
4731 // Switch states based on button and pointer state.
4732 if (isQuietTime) {
4733 // Case 1: Quiet time. (QUIET)
4734#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004735 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004736 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004737#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004738 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4739 *outFinishPreviousGesture = true;
4740 }
Jeff Brownace13b12011-03-09 17:39:48 -08004741
4742 mPointerGesture.activeGestureId = -1;
4743 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004744 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004745
Jeff Brown65fd2512011-08-18 11:20:58 -07004746 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004747 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004748 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004749 // The pointer follows the active touch point.
4750 // Emit DOWN, MOVE, UP events at the pointer location.
4751 //
4752 // Only the active touch matters; other fingers are ignored. This policy helps
4753 // to handle the case where the user places a second finger on the touch pad
4754 // to apply the necessary force to depress an integrated button below the surface.
4755 // We don't want the second finger to be delivered to applications.
4756 //
4757 // For this to work well, we need to make sure to track the pointer that is really
4758 // active. If the user first puts one finger down to click then adds another
4759 // finger to drag then the active pointer should switch to the finger that is
4760 // being dragged.
4761#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004762 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004763 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004764#endif
4765 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004766 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004767 *outFinishPreviousGesture = true;
4768 mPointerGesture.activeGestureId = 0;
4769 }
4770
4771 // Switch pointers if needed.
4772 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004773 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004774 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004775 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004776 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004777 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004778 float vx, vy;
4779 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4780 float speed = hypotf(vx, vy);
4781 if (speed > bestSpeed) {
4782 bestId = id;
4783 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004784 }
Jeff Brown8d608662010-08-30 03:02:23 -07004785 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004786 }
4787 if (bestId >= 0 && bestId != activeTouchId) {
4788 mPointerGesture.activeTouchId = activeTouchId = bestId;
4789 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004790#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004791 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004792 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004793#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004794 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004795 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004796
Jeff Brown65fd2512011-08-18 11:20:58 -07004797 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004798 const RawPointerData::Pointer& currentPointer =
4799 mCurrentRawPointerData.pointerForId(activeTouchId);
4800 const RawPointerData::Pointer& lastPointer =
4801 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004802 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4803 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004804
Jeff Brownbe1aa822011-07-27 16:04:54 -07004805 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004806 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004807
4808 // Move the pointer using a relative motion.
4809 // When using spots, the click will occur at the position of the anchor
4810 // spot and all other spots will move there.
4811 mPointerController->move(deltaX, deltaY);
4812 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004813 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004814 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004815
Jeff Brownace13b12011-03-09 17:39:48 -08004816 float x, y;
4817 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004818
Jeff Brown79ac9692011-04-19 21:20:10 -07004819 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004820 mPointerGesture.currentGestureIdBits.clear();
4821 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4822 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004823 mPointerGesture.currentGestureProperties[0].clear();
4824 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004825 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004826 mPointerGesture.currentGestureCoords[0].clear();
4827 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4828 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4829 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004830 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004831 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004832 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4833 *outFinishPreviousGesture = true;
4834 }
Jeff Brownace13b12011-03-09 17:39:48 -08004835
Jeff Brown79ac9692011-04-19 21:20:10 -07004836 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004837 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004838 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004839 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4840 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004841 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004842 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004843 float x, y;
4844 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004845 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4846 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004847#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004848 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004849#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004850
4851 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004852 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004853 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004854
Jeff Brownace13b12011-03-09 17:39:48 -08004855 mPointerGesture.activeGestureId = 0;
4856 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004857 mPointerGesture.currentGestureIdBits.clear();
4858 mPointerGesture.currentGestureIdBits.markBit(
4859 mPointerGesture.activeGestureId);
4860 mPointerGesture.currentGestureIdToIndex[
4861 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004862 mPointerGesture.currentGestureProperties[0].clear();
4863 mPointerGesture.currentGestureProperties[0].id =
4864 mPointerGesture.activeGestureId;
4865 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004866 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004867 mPointerGesture.currentGestureCoords[0].clear();
4868 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004869 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004870 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004871 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004872 mPointerGesture.currentGestureCoords[0].setAxisValue(
4873 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004874
Jeff Brownace13b12011-03-09 17:39:48 -08004875 tapped = true;
4876 } else {
4877#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004878 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004879 x - mPointerGesture.tapX,
4880 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004881#endif
4882 }
4883 } else {
4884#if DEBUG_GESTURES
Michael Wright398d3092013-07-02 17:35:00 -07004885 if (mPointerGesture.tapDownTime != LLONG_MIN) {
4886 ALOGD("Gestures: Not a TAP, %0.3fms since down",
4887 (when - mPointerGesture.tapDownTime) * 0.000001f);
4888 } else {
4889 ALOGD("Gestures: Not a TAP, incompatible mode transitions");
4890 }
Jeff Brownace13b12011-03-09 17:39:48 -08004891#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004892 }
Jeff Brownace13b12011-03-09 17:39:48 -08004893 }
Jeff Brown2352b972011-04-12 22:39:53 -07004894
Jeff Brown65fd2512011-08-18 11:20:58 -07004895 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004896
Jeff Brownace13b12011-03-09 17:39:48 -08004897 if (!tapped) {
4898#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004899 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004900#endif
4901 mPointerGesture.activeGestureId = -1;
4902 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004903 mPointerGesture.currentGestureIdBits.clear();
4904 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004905 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004906 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004907 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004908 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4909 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004910 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004911
Jeff Brown79ac9692011-04-19 21:20:10 -07004912 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4913 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004914 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004915 float x, y;
4916 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004917 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4918 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004919 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4920 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004921#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004922 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004923 x - mPointerGesture.tapX,
4924 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004925#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004926 }
4927 } else {
4928#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004929 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004930 (when - mPointerGesture.tapUpTime) * 0.000001f);
4931#endif
4932 }
4933 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4934 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4935 }
Jeff Brownace13b12011-03-09 17:39:48 -08004936
Jeff Brown65fd2512011-08-18 11:20:58 -07004937 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004938 const RawPointerData::Pointer& currentPointer =
4939 mCurrentRawPointerData.pointerForId(activeTouchId);
4940 const RawPointerData::Pointer& lastPointer =
4941 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004942 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004943 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004944 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004945 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004946
Jeff Brownbe1aa822011-07-27 16:04:54 -07004947 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004948 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004949
Jeff Brown2352b972011-04-12 22:39:53 -07004950 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004951 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004952 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004953 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004954 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004955 }
4956
Jeff Brown79ac9692011-04-19 21:20:10 -07004957 bool down;
4958 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4959#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004960 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004961#endif
4962 down = true;
4963 } else {
4964#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004965 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004966#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004967 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4968 *outFinishPreviousGesture = true;
4969 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004970 mPointerGesture.activeGestureId = 0;
4971 down = false;
4972 }
Jeff Brownace13b12011-03-09 17:39:48 -08004973
4974 float x, y;
4975 mPointerController->getPosition(&x, &y);
4976
Jeff Brownace13b12011-03-09 17:39:48 -08004977 mPointerGesture.currentGestureIdBits.clear();
4978 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4979 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004980 mPointerGesture.currentGestureProperties[0].clear();
4981 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4982 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004983 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004984 mPointerGesture.currentGestureCoords[0].clear();
4985 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4986 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004987 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4988 down ? 1.0f : 0.0f);
4989
Jeff Brown65fd2512011-08-18 11:20:58 -07004990 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004991 mPointerGesture.resetTap();
4992 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004993 mPointerGesture.tapX = x;
4994 mPointerGesture.tapY = y;
4995 }
Jeff Brownace13b12011-03-09 17:39:48 -08004996 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004997 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4998 // We need to provide feedback for each finger that goes down so we cannot wait
4999 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08005000 //
Jeff Brown2352b972011-04-12 22:39:53 -07005001 // The ambiguous case is deciding what to do when there are two fingers down but they
5002 // have not moved enough to determine whether they are part of a drag or part of a
5003 // freeform gesture, or just a press or long-press at the pointer location.
5004 //
5005 // When there are two fingers we start with the PRESS hypothesis and we generate a
5006 // down at the pointer location.
5007 //
5008 // When the two fingers move enough or when additional fingers are added, we make
5009 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00005010 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005011
Jeff Brown214eaf42011-05-26 19:17:02 -07005012 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07005013 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07005014 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08005015 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
5016 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08005017 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07005018 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07005019 // Additional pointers have gone down but not yet settled.
5020 // Reset the gesture.
5021#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005022 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005023 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07005024 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07005025 * 0.000001f);
5026#endif
5027 *outCancelPreviousGesture = true;
5028 } else {
5029 // Continue previous gesture.
5030 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
5031 }
5032
5033 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07005034 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
5035 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07005036 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07005037 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08005038
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005039 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07005040#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005041 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005042 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07005043 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005044 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07005045#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005046 mCurrentRawPointerData.getCentroidOfTouchingPointers(
5047 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07005048 &mPointerGesture.referenceTouchY);
5049 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
5050 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07005051 }
Jeff Brownace13b12011-03-09 17:39:48 -08005052
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005053 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07005054 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07005055 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
5056 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005057 mPointerGesture.referenceDeltas[id].dx = 0;
5058 mPointerGesture.referenceDeltas[id].dy = 0;
5059 }
Jeff Brown65fd2512011-08-18 11:20:58 -07005060 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005061
5062 // Add delta for all fingers and calculate a common movement delta.
5063 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07005064 BitSet32 commonIdBits(mLastFingerIdBits.value
5065 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005066 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
5067 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005068 uint32_t id = idBits.clearFirstMarkedBit();
5069 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
5070 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005071 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
5072 delta.dx += cpd.x - lpd.x;
5073 delta.dy += cpd.y - lpd.y;
5074
5075 if (first) {
5076 commonDeltaX = delta.dx;
5077 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07005078 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005079 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
5080 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
5081 }
5082 }
Jeff Brownace13b12011-03-09 17:39:48 -08005083
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005084 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
5085 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
5086 float dist[MAX_POINTER_ID + 1];
5087 int32_t distOverThreshold = 0;
5088 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005089 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005090 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07005091 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
5092 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07005093 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005094 distOverThreshold += 1;
5095 }
5096 }
5097
5098 // Only transition when at least two pointers have moved further than
5099 // the minimum distance threshold.
5100 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005101 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005102 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07005103#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005104 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005105 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005106#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005107 *outCancelPreviousGesture = true;
5108 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5109 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005110 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07005111 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005112 uint32_t id1 = idBits.clearFirstMarkedBit();
5113 uint32_t id2 = idBits.firstMarkedBit();
5114 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
5115 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
5116 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
5117 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
5118 // There are two pointers but they are too far apart for a SWIPE,
5119 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005120#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005121 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005122 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005123#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005124 *outCancelPreviousGesture = true;
5125 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5126 } else {
5127 // There are two pointers. Wait for both pointers to start moving
5128 // before deciding whether this is a SWIPE or FREEFORM gesture.
5129 float dist1 = dist[id1];
5130 float dist2 = dist[id2];
5131 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
5132 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
5133 // Calculate the dot product of the displacement vectors.
5134 // When the vectors are oriented in approximately the same direction,
5135 // the angle betweeen them is near zero and the cosine of the angle
5136 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
5137 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
5138 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07005139 float dx1 = delta1.dx * mPointerXZoomScale;
5140 float dy1 = delta1.dy * mPointerYZoomScale;
5141 float dx2 = delta2.dx * mPointerXZoomScale;
5142 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005143 float dot = dx1 * dx2 + dy1 * dy2;
5144 float cosine = dot / (dist1 * dist2); // denominator always > 0
5145 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
5146 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005147#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005148 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005149 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5150 "cosine %0.3f >= %0.3f",
5151 dist1, mConfig.pointerGestureMultitouchMinDistance,
5152 dist2, mConfig.pointerGestureMultitouchMinDistance,
5153 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005154#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07005155 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
5156 } else {
5157 // Pointers are moving in different directions. Switch to FREEFORM.
5158#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005159 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07005160 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
5161 "cosine %0.3f < %0.3f",
5162 dist1, mConfig.pointerGestureMultitouchMinDistance,
5163 dist2, mConfig.pointerGestureMultitouchMinDistance,
5164 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
5165#endif
5166 *outCancelPreviousGesture = true;
5167 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
5168 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005169 }
Jeff Brownace13b12011-03-09 17:39:48 -08005170 }
5171 }
Jeff Brownace13b12011-03-09 17:39:48 -08005172 }
5173 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07005174 // Switch from SWIPE to FREEFORM if additional pointers go down.
5175 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07005176 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07005177#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005178 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07005179 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005180#endif
Jeff Brownace13b12011-03-09 17:39:48 -08005181 *outCancelPreviousGesture = true;
5182 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07005183 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005184 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005185
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005186 // Move the reference points based on the overall group motion of the fingers
5187 // except in PRESS mode while waiting for a transition to occur.
5188 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
5189 && (commonDeltaX || commonDeltaY)) {
5190 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005191 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07005192 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005193 delta.dx = 0;
5194 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07005195 }
5196
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005197 mPointerGesture.referenceTouchX += commonDeltaX;
5198 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07005199
Jeff Brown65fd2512011-08-18 11:20:58 -07005200 commonDeltaX *= mPointerXMovementScale;
5201 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07005202
Jeff Brownbe1aa822011-07-27 16:04:54 -07005203 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07005204 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07005205
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07005206 mPointerGesture.referenceGestureX += commonDeltaX;
5207 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07005208 }
5209
5210 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07005211 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
5212 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
5213 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08005214#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005215 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07005216 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005217 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07005218#endif
Steve Blockec193de2012-01-09 18:35:44 +00005219 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07005220
5221 mPointerGesture.currentGestureIdBits.clear();
5222 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
5223 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005224 mPointerGesture.currentGestureProperties[0].clear();
5225 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
5226 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005227 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07005228 mPointerGesture.currentGestureCoords[0].clear();
5229 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
5230 mPointerGesture.referenceGestureX);
5231 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
5232 mPointerGesture.referenceGestureY);
5233 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08005234 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
5235 // FREEFORM mode.
5236#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005237 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08005238 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07005239 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005240#endif
Steve Blockec193de2012-01-09 18:35:44 +00005241 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005242
Jeff Brownace13b12011-03-09 17:39:48 -08005243 mPointerGesture.currentGestureIdBits.clear();
5244
5245 BitSet32 mappedTouchIdBits;
5246 BitSet32 usedGestureIdBits;
5247 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
5248 // Initially, assign the active gesture id to the active touch point
5249 // if there is one. No other touch id bits are mapped yet.
5250 if (!*outCancelPreviousGesture) {
5251 mappedTouchIdBits.markBit(activeTouchId);
5252 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
5253 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
5254 mPointerGesture.activeGestureId;
5255 } else {
5256 mPointerGesture.activeGestureId = -1;
5257 }
5258 } else {
5259 // Otherwise, assume we mapped all touches from the previous frame.
5260 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07005261 mappedTouchIdBits.value = mLastFingerIdBits.value
5262 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08005263 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
5264
5265 // Check whether we need to choose a new active gesture id because the
5266 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07005267 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
5268 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005269 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005270 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005271 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
5272 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
5273 mPointerGesture.activeGestureId = -1;
5274 break;
5275 }
5276 }
5277 }
5278
5279#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005280 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08005281 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
5282 "activeGestureId=%d",
5283 mappedTouchIdBits.value, usedGestureIdBits.value,
5284 mPointerGesture.activeGestureId);
5285#endif
5286
Jeff Brown65fd2512011-08-18 11:20:58 -07005287 BitSet32 idBits(mCurrentFingerIdBits);
5288 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005289 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005290 uint32_t gestureId;
5291 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005292 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005293 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
5294#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005295 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005296 "new mapping for touch id %d -> gesture id %d",
5297 touchId, gestureId);
5298#endif
5299 } else {
5300 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
5301#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005302 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08005303 "existing mapping for touch id %d -> gesture id %d",
5304 touchId, gestureId);
5305#endif
5306 }
5307 mPointerGesture.currentGestureIdBits.markBit(gestureId);
5308 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
5309
Jeff Brownbe1aa822011-07-27 16:04:54 -07005310 const RawPointerData::Pointer& pointer =
5311 mCurrentRawPointerData.pointerForId(touchId);
5312 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07005313 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005314 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07005315 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005316 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005317
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005318 mPointerGesture.currentGestureProperties[i].clear();
5319 mPointerGesture.currentGestureProperties[i].id = gestureId;
5320 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07005321 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08005322 mPointerGesture.currentGestureCoords[i].clear();
5323 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005324 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08005325 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07005326 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08005327 mPointerGesture.currentGestureCoords[i].setAxisValue(
5328 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
5329 }
5330
5331 if (mPointerGesture.activeGestureId < 0) {
5332 mPointerGesture.activeGestureId =
5333 mPointerGesture.currentGestureIdBits.firstMarkedBit();
5334#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005335 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08005336 "activeGestureId=%d", mPointerGesture.activeGestureId);
5337#endif
5338 }
Jeff Brown2352b972011-04-12 22:39:53 -07005339 }
Jeff Brownace13b12011-03-09 17:39:48 -08005340 }
5341
Jeff Brownbe1aa822011-07-27 16:04:54 -07005342 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005343
Jeff Brownace13b12011-03-09 17:39:48 -08005344#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00005345 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07005346 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
5347 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08005348 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07005349 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
5350 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08005351 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005352 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005353 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005354 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005355 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005356 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005357 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5358 id, index, properties.toolType,
5359 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005360 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5361 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5362 }
5363 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005364 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005365 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005366 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08005367 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00005368 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005369 "x=%0.3f, y=%0.3f, pressure=%0.3f",
5370 id, index, properties.toolType,
5371 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08005372 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
5373 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
5374 }
5375#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07005376 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08005377}
5378
Jeff Brown65fd2512011-08-18 11:20:58 -07005379void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
5380 mPointerSimple.currentCoords.clear();
5381 mPointerSimple.currentProperties.clear();
5382
5383 bool down, hovering;
5384 if (!mCurrentStylusIdBits.isEmpty()) {
5385 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5386 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5387 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5388 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5389 mPointerController->setPosition(x, y);
5390
5391 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5392 down = !hovering;
5393
5394 mPointerController->getPosition(&x, &y);
5395 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5396 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5397 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5398 mPointerSimple.currentProperties.id = 0;
5399 mPointerSimple.currentProperties.toolType =
5400 mCurrentCookedPointerData.pointerProperties[index].toolType;
5401 } else {
5402 down = false;
5403 hovering = false;
5404 }
5405
5406 dispatchPointerSimple(when, policyFlags, down, hovering);
5407}
5408
5409void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5410 abortPointerSimple(when, policyFlags);
5411}
5412
5413void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5414 mPointerSimple.currentCoords.clear();
5415 mPointerSimple.currentProperties.clear();
5416
5417 bool down, hovering;
5418 if (!mCurrentMouseIdBits.isEmpty()) {
5419 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5420 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5421 if (mLastMouseIdBits.hasBit(id)) {
5422 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5423 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5424 - mLastRawPointerData.pointers[lastIndex].x)
5425 * mPointerXMovementScale;
5426 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5427 - mLastRawPointerData.pointers[lastIndex].y)
5428 * mPointerYMovementScale;
5429
5430 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5431 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5432
5433 mPointerController->move(deltaX, deltaY);
5434 } else {
5435 mPointerVelocityControl.reset();
5436 }
5437
5438 down = isPointerDown(mCurrentButtonState);
5439 hovering = !down;
5440
5441 float x, y;
5442 mPointerController->getPosition(&x, &y);
5443 mPointerSimple.currentCoords.copyFrom(
5444 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5445 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5446 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5447 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5448 hovering ? 0.0f : 1.0f);
5449 mPointerSimple.currentProperties.id = 0;
5450 mPointerSimple.currentProperties.toolType =
5451 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5452 } else {
5453 mPointerVelocityControl.reset();
5454
5455 down = false;
5456 hovering = false;
5457 }
5458
5459 dispatchPointerSimple(when, policyFlags, down, hovering);
5460}
5461
5462void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5463 abortPointerSimple(when, policyFlags);
5464
5465 mPointerVelocityControl.reset();
5466}
5467
5468void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5469 bool down, bool hovering) {
5470 int32_t metaState = getContext()->getGlobalMetaState();
5471
5472 if (mPointerController != NULL) {
5473 if (down || hovering) {
5474 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5475 mPointerController->clearSpots();
5476 mPointerController->setButtonState(mCurrentButtonState);
5477 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5478 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5479 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5480 }
5481 }
5482
5483 if (mPointerSimple.down && !down) {
5484 mPointerSimple.down = false;
5485
5486 // Send up.
5487 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5488 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005489 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005490 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5491 mOrientedXPrecision, mOrientedYPrecision,
5492 mPointerSimple.downTime);
5493 getListener()->notifyMotion(&args);
5494 }
5495
5496 if (mPointerSimple.hovering && !hovering) {
5497 mPointerSimple.hovering = false;
5498
5499 // Send hover exit.
5500 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5501 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005502 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005503 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5504 mOrientedXPrecision, mOrientedYPrecision,
5505 mPointerSimple.downTime);
5506 getListener()->notifyMotion(&args);
5507 }
5508
5509 if (down) {
5510 if (!mPointerSimple.down) {
5511 mPointerSimple.down = true;
5512 mPointerSimple.downTime = when;
5513
5514 // Send down.
5515 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5516 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005517 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005518 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5519 mOrientedXPrecision, mOrientedYPrecision,
5520 mPointerSimple.downTime);
5521 getListener()->notifyMotion(&args);
5522 }
5523
5524 // Send move.
5525 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5526 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005527 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005528 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5529 mOrientedXPrecision, mOrientedYPrecision,
5530 mPointerSimple.downTime);
5531 getListener()->notifyMotion(&args);
5532 }
5533
5534 if (hovering) {
5535 if (!mPointerSimple.hovering) {
5536 mPointerSimple.hovering = true;
5537
5538 // Send hover enter.
5539 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5540 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005541 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005542 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5543 mOrientedXPrecision, mOrientedYPrecision,
5544 mPointerSimple.downTime);
5545 getListener()->notifyMotion(&args);
5546 }
5547
5548 // Send hover move.
5549 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5550 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005551 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005552 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5553 mOrientedXPrecision, mOrientedYPrecision,
5554 mPointerSimple.downTime);
5555 getListener()->notifyMotion(&args);
5556 }
5557
5558 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5559 float vscroll = mCurrentRawVScroll;
5560 float hscroll = mCurrentRawHScroll;
5561 mWheelYVelocityControl.move(when, NULL, &vscroll);
5562 mWheelXVelocityControl.move(when, &hscroll, NULL);
5563
5564 // Send scroll.
5565 PointerCoords pointerCoords;
5566 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5567 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5568 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5569
5570 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5571 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
Jeff Brown83d616a2012-09-09 20:33:43 -07005572 mViewport.displayId,
Jeff Brown65fd2512011-08-18 11:20:58 -07005573 1, &mPointerSimple.currentProperties, &pointerCoords,
5574 mOrientedXPrecision, mOrientedYPrecision,
5575 mPointerSimple.downTime);
5576 getListener()->notifyMotion(&args);
5577 }
5578
5579 // Save state.
5580 if (down || hovering) {
5581 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5582 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5583 } else {
5584 mPointerSimple.reset();
5585 }
5586}
5587
5588void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5589 mPointerSimple.currentCoords.clear();
5590 mPointerSimple.currentProperties.clear();
5591
5592 dispatchPointerSimple(when, policyFlags, false, false);
5593}
5594
Jeff Brownace13b12011-03-09 17:39:48 -08005595void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005596 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5597 const PointerProperties* properties, const PointerCoords* coords,
5598 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005599 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5600 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005601 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005602 uint32_t pointerCount = 0;
5603 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005604 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005605 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005606 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005607 pointerCoords[pointerCount].copyFrom(coords[index]);
5608
5609 if (changedId >= 0 && id == uint32_t(changedId)) {
5610 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5611 }
5612
5613 pointerCount += 1;
5614 }
5615
Steve Blockec193de2012-01-09 18:35:44 +00005616 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005617
5618 if (changedId >= 0 && pointerCount == 1) {
5619 // Replace initial down and final up action.
5620 // We can compare the action without masking off the changed pointer index
5621 // because we know the index is 0.
5622 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5623 action = AMOTION_EVENT_ACTION_DOWN;
5624 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5625 action = AMOTION_EVENT_ACTION_UP;
5626 } else {
5627 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005628 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005629 }
5630 }
5631
Jeff Brownbe1aa822011-07-27 16:04:54 -07005632 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005633 action, flags, metaState, buttonState, edgeFlags,
Jeff Brown83d616a2012-09-09 20:33:43 -07005634 mViewport.displayId, pointerCount, pointerProperties, pointerCoords,
5635 xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005636 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005637}
5638
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005639bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005640 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005641 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5642 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005643 bool changed = false;
5644 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005645 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005646 uint32_t inIndex = inIdToIndex[id];
5647 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005648
5649 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005650 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005651 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005652 PointerCoords& curOutCoords = outCoords[outIndex];
5653
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005654 if (curInProperties != curOutProperties) {
5655 curOutProperties.copyFrom(curInProperties);
5656 changed = true;
5657 }
5658
Jeff Brownace13b12011-03-09 17:39:48 -08005659 if (curInCoords != curOutCoords) {
5660 curOutCoords.copyFrom(curInCoords);
5661 changed = true;
5662 }
5663 }
5664 return changed;
5665}
5666
5667void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005668 if (mPointerController != NULL) {
5669 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5670 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005671}
5672
Jeff Brownbe1aa822011-07-27 16:04:54 -07005673bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5674 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5675 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005676}
5677
Jeff Brownbe1aa822011-07-27 16:04:54 -07005678const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005679 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005680 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005681 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005682 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005683
5684#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005685 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005686 "left=%d, top=%d, right=%d, bottom=%d",
5687 x, y,
5688 virtualKey.keyCode, virtualKey.scanCode,
5689 virtualKey.hitLeft, virtualKey.hitTop,
5690 virtualKey.hitRight, virtualKey.hitBottom);
5691#endif
5692
5693 if (virtualKey.isHit(x, y)) {
5694 return & virtualKey;
5695 }
5696 }
5697
5698 return NULL;
5699}
5700
Jeff Brownbe1aa822011-07-27 16:04:54 -07005701void TouchInputMapper::assignPointerIds() {
5702 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5703 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5704
5705 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005706
5707 if (currentPointerCount == 0) {
5708 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005709 return;
5710 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005711
Jeff Brownbe1aa822011-07-27 16:04:54 -07005712 if (lastPointerCount == 0) {
5713 // All pointers are new.
5714 for (uint32_t i = 0; i < currentPointerCount; i++) {
5715 uint32_t id = i;
5716 mCurrentRawPointerData.pointers[i].id = id;
5717 mCurrentRawPointerData.idToIndex[id] = i;
5718 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5719 }
5720 return;
5721 }
5722
5723 if (currentPointerCount == 1 && lastPointerCount == 1
5724 && mCurrentRawPointerData.pointers[0].toolType
5725 == mLastRawPointerData.pointers[0].toolType) {
5726 // Only one pointer and no change in count so it must have the same id as before.
5727 uint32_t id = mLastRawPointerData.pointers[0].id;
5728 mCurrentRawPointerData.pointers[0].id = id;
5729 mCurrentRawPointerData.idToIndex[id] = 0;
5730 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5731 return;
5732 }
5733
5734 // General case.
5735 // We build a heap of squared euclidean distances between current and last pointers
5736 // associated with the current and last pointer indices. Then, we find the best
5737 // match (by distance) for each current pointer.
5738 // The pointers must have the same tool type but it is possible for them to
5739 // transition from hovering to touching or vice-versa while retaining the same id.
5740 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5741
5742 uint32_t heapSize = 0;
5743 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5744 currentPointerIndex++) {
5745 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5746 lastPointerIndex++) {
5747 const RawPointerData::Pointer& currentPointer =
5748 mCurrentRawPointerData.pointers[currentPointerIndex];
5749 const RawPointerData::Pointer& lastPointer =
5750 mLastRawPointerData.pointers[lastPointerIndex];
5751 if (currentPointer.toolType == lastPointer.toolType) {
5752 int64_t deltaX = currentPointer.x - lastPointer.x;
5753 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005754
5755 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5756
5757 // Insert new element into the heap (sift up).
5758 heap[heapSize].currentPointerIndex = currentPointerIndex;
5759 heap[heapSize].lastPointerIndex = lastPointerIndex;
5760 heap[heapSize].distance = distance;
5761 heapSize += 1;
5762 }
5763 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005764 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005765
Jeff Brownbe1aa822011-07-27 16:04:54 -07005766 // Heapify
5767 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5768 startIndex -= 1;
5769 for (uint32_t parentIndex = startIndex; ;) {
5770 uint32_t childIndex = parentIndex * 2 + 1;
5771 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005772 break;
5773 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005774
5775 if (childIndex + 1 < heapSize
5776 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5777 childIndex += 1;
5778 }
5779
5780 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5781 break;
5782 }
5783
5784 swap(heap[parentIndex], heap[childIndex]);
5785 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005786 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005787 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005788
5789#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005790 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005791 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005792 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005793 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5794 heap[i].distance);
5795 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005796#endif
5797
Jeff Brownbe1aa822011-07-27 16:04:54 -07005798 // Pull matches out by increasing order of distance.
5799 // To avoid reassigning pointers that have already been matched, the loop keeps track
5800 // of which last and current pointers have been matched using the matchedXXXBits variables.
5801 // It also tracks the used pointer id bits.
5802 BitSet32 matchedLastBits(0);
5803 BitSet32 matchedCurrentBits(0);
5804 BitSet32 usedIdBits(0);
5805 bool first = true;
5806 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5807 while (heapSize > 0) {
5808 if (first) {
5809 // The first time through the loop, we just consume the root element of
5810 // the heap (the one with smallest distance).
5811 first = false;
5812 } else {
5813 // Previous iterations consumed the root element of the heap.
5814 // Pop root element off of the heap (sift down).
5815 heap[0] = heap[heapSize];
5816 for (uint32_t parentIndex = 0; ;) {
5817 uint32_t childIndex = parentIndex * 2 + 1;
5818 if (childIndex >= heapSize) {
5819 break;
5820 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005821
Jeff Brownbe1aa822011-07-27 16:04:54 -07005822 if (childIndex + 1 < heapSize
5823 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5824 childIndex += 1;
5825 }
5826
5827 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5828 break;
5829 }
5830
5831 swap(heap[parentIndex], heap[childIndex]);
5832 parentIndex = childIndex;
5833 }
5834
5835#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005836 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005837 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005838 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005839 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5840 heap[i].distance);
5841 }
5842#endif
5843 }
5844
5845 heapSize -= 1;
5846
5847 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5848 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5849
5850 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5851 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5852
5853 matchedCurrentBits.markBit(currentPointerIndex);
5854 matchedLastBits.markBit(lastPointerIndex);
5855
5856 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5857 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5858 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5859 mCurrentRawPointerData.markIdBit(id,
5860 mCurrentRawPointerData.isHovering(currentPointerIndex));
5861 usedIdBits.markBit(id);
5862
5863#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005864 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005865 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5866#endif
5867 break;
5868 }
5869 }
5870
5871 // Assign fresh ids to pointers that were not matched in the process.
5872 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5873 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5874 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5875
5876 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5877 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5878 mCurrentRawPointerData.markIdBit(id,
5879 mCurrentRawPointerData.isHovering(currentPointerIndex));
5880
5881#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005882 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005883 currentPointerIndex, id);
5884#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005885 }
5886}
5887
Jeff Brown6d0fec22010-07-23 21:28:06 -07005888int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005889 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5890 return AKEY_STATE_VIRTUAL;
5891 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005892
Jeff Brownbe1aa822011-07-27 16:04:54 -07005893 size_t numVirtualKeys = mVirtualKeys.size();
5894 for (size_t i = 0; i < numVirtualKeys; i++) {
5895 const VirtualKey& virtualKey = mVirtualKeys[i];
5896 if (virtualKey.keyCode == keyCode) {
5897 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005898 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005899 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005900
5901 return AKEY_STATE_UNKNOWN;
5902}
5903
5904int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005905 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5906 return AKEY_STATE_VIRTUAL;
5907 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005908
Jeff Brownbe1aa822011-07-27 16:04:54 -07005909 size_t numVirtualKeys = mVirtualKeys.size();
5910 for (size_t i = 0; i < numVirtualKeys; i++) {
5911 const VirtualKey& virtualKey = mVirtualKeys[i];
5912 if (virtualKey.scanCode == scanCode) {
5913 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005914 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005915 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005916
5917 return AKEY_STATE_UNKNOWN;
5918}
5919
5920bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5921 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005922 size_t numVirtualKeys = mVirtualKeys.size();
5923 for (size_t i = 0; i < numVirtualKeys; i++) {
5924 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005925
Jeff Brownbe1aa822011-07-27 16:04:54 -07005926 for (size_t i = 0; i < numCodes; i++) {
5927 if (virtualKey.keyCode == keyCodes[i]) {
5928 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005929 }
5930 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005931 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005932
5933 return true;
5934}
5935
5936
5937// --- SingleTouchInputMapper ---
5938
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005939SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5940 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005941}
5942
5943SingleTouchInputMapper::~SingleTouchInputMapper() {
5944}
5945
Jeff Brown65fd2512011-08-18 11:20:58 -07005946void SingleTouchInputMapper::reset(nsecs_t when) {
5947 mSingleTouchMotionAccumulator.reset(getDevice());
5948
5949 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005950}
5951
Jeff Brown6d0fec22010-07-23 21:28:06 -07005952void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005953 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005954
Jeff Brown65fd2512011-08-18 11:20:58 -07005955 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005956}
5957
Jeff Brown65fd2512011-08-18 11:20:58 -07005958void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005959 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005960 mCurrentRawPointerData.pointerCount = 1;
5961 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005962
Jeff Brown65fd2512011-08-18 11:20:58 -07005963 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5964 && (mTouchButtonAccumulator.isHovering()
5965 || (mRawPointerAxes.pressure.valid
5966 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005967 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005968
Jeff Brownbe1aa822011-07-27 16:04:54 -07005969 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005970 outPointer.id = 0;
5971 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5972 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5973 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5974 outPointer.touchMajor = 0;
5975 outPointer.touchMinor = 0;
5976 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5977 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5978 outPointer.orientation = 0;
5979 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005980 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5981 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005982 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5983 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5984 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5985 }
5986 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005987 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005988}
5989
Jeff Brownbe1aa822011-07-27 16:04:54 -07005990void SingleTouchInputMapper::configureRawPointerAxes() {
5991 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005992
Jeff Brownbe1aa822011-07-27 16:04:54 -07005993 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5994 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5995 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5996 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5997 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005998 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5999 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07006000}
6001
Jeff Brown00710e92012-04-19 15:18:26 -07006002bool SingleTouchInputMapper::hasStylus() const {
6003 return mTouchButtonAccumulator.hasStylus();
6004}
6005
Jeff Brown6d0fec22010-07-23 21:28:06 -07006006
6007// --- MultiTouchInputMapper ---
6008
Jeff Brown47e6b1b2010-11-29 17:37:49 -08006009MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07006010 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07006011}
6012
6013MultiTouchInputMapper::~MultiTouchInputMapper() {
6014}
6015
Jeff Brown65fd2512011-08-18 11:20:58 -07006016void MultiTouchInputMapper::reset(nsecs_t when) {
6017 mMultiTouchMotionAccumulator.reset(getDevice());
6018
Jeff Brown6894a292011-07-01 17:59:27 -07006019 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07006020
Jeff Brown65fd2512011-08-18 11:20:58 -07006021 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07006022}
6023
6024void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07006025 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08006026
Jeff Brown65fd2512011-08-18 11:20:58 -07006027 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07006028}
6029
Jeff Brown65fd2512011-08-18 11:20:58 -07006030void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07006031 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07006032 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006033 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07006034
Jeff Brown80fd47c2011-05-24 01:07:44 -07006035 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07006036 const MultiTouchMotionAccumulator::Slot* inSlot =
6037 mMultiTouchMotionAccumulator.getSlot(inIndex);
6038 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07006039 continue;
6040 }
6041
Jeff Brown80fd47c2011-05-24 01:07:44 -07006042 if (outCount >= MAX_POINTERS) {
6043#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00006044 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006045 "ignoring the rest.",
6046 getDeviceName().string(), MAX_POINTERS);
6047#endif
6048 break; // too many fingers!
6049 }
6050
Jeff Brownbe1aa822011-07-27 16:04:54 -07006051 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07006052 outPointer.x = inSlot->getX();
6053 outPointer.y = inSlot->getY();
6054 outPointer.pressure = inSlot->getPressure();
6055 outPointer.touchMajor = inSlot->getTouchMajor();
6056 outPointer.touchMinor = inSlot->getTouchMinor();
6057 outPointer.toolMajor = inSlot->getToolMajor();
6058 outPointer.toolMinor = inSlot->getToolMinor();
6059 outPointer.orientation = inSlot->getOrientation();
6060 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07006061 outPointer.tiltX = 0;
6062 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07006063
Jeff Brown49754db2011-07-01 17:37:58 -07006064 outPointer.toolType = inSlot->getToolType();
6065 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
6066 outPointer.toolType = mTouchButtonAccumulator.getToolType();
6067 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
6068 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
6069 }
Jeff Brown8d608662010-08-30 03:02:23 -07006070 }
6071
Jeff Brown65fd2512011-08-18 11:20:58 -07006072 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
6073 && (mTouchButtonAccumulator.isHovering()
6074 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07006075 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006076
Jeff Brown8d608662010-08-30 03:02:23 -07006077 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07006078 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07006079 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07006080 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07006081 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07006082 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07006083 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07006084 if (mPointerTrackingIdMap[n] == trackingId) {
6085 id = n;
6086 }
6087 }
6088
6089 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07006090 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07006091 mPointerTrackingIdMap[id] = trackingId;
6092 }
6093 }
6094 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07006095 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006096 mCurrentRawPointerData.clearIdBits();
6097 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07006098 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07006099 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006100 mCurrentRawPointerData.idToIndex[id] = outCount;
6101 mCurrentRawPointerData.markIdBit(id, isHovering);
6102 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07006103 }
6104 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07006105
Jeff Brown6d0fec22010-07-23 21:28:06 -07006106 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07006107 }
6108
Jeff Brownbe1aa822011-07-27 16:04:54 -07006109 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006110 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07006111
Jeff Brown65fd2512011-08-18 11:20:58 -07006112 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07006113}
6114
Jeff Brownbe1aa822011-07-27 16:04:54 -07006115void MultiTouchInputMapper::configureRawPointerAxes() {
6116 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07006117
Jeff Brownbe1aa822011-07-27 16:04:54 -07006118 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
6119 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
6120 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
6121 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
6122 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
6123 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
6124 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
6125 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
6126 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
6127 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
6128 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006129
Jeff Brownbe1aa822011-07-27 16:04:54 -07006130 if (mRawPointerAxes.trackingId.valid
6131 && mRawPointerAxes.slot.valid
6132 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
6133 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07006134 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00006135 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07006136 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07006137 getDeviceName().string(), slotCount, MAX_SLOTS);
6138 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07006139 }
Jeff Brown00710e92012-04-19 15:18:26 -07006140 mMultiTouchMotionAccumulator.configure(getDevice(),
6141 slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006142 } else {
Jeff Brown00710e92012-04-19 15:18:26 -07006143 mMultiTouchMotionAccumulator.configure(getDevice(),
6144 MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07006145 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07006146}
6147
Jeff Brown00710e92012-04-19 15:18:26 -07006148bool MultiTouchInputMapper::hasStylus() const {
6149 return mMultiTouchMotionAccumulator.hasStylus()
6150 || mTouchButtonAccumulator.hasStylus();
6151}
6152
Jeff Brown46b9ac02010-04-22 18:58:52 -07006153
Jeff Browncb1404e2011-01-15 18:14:15 -08006154// --- JoystickInputMapper ---
6155
6156JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
6157 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006158}
6159
6160JoystickInputMapper::~JoystickInputMapper() {
6161}
6162
6163uint32_t JoystickInputMapper::getSources() {
6164 return AINPUT_SOURCE_JOYSTICK;
6165}
6166
6167void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
6168 InputMapper::populateDeviceInfo(info);
6169
Jeff Brown6f2fba42011-02-19 01:08:02 -08006170 for (size_t i = 0; i < mAxes.size(); i++) {
6171 const Axis& axis = mAxes.valueAt(i);
Michael Wright2b08c612013-04-24 20:05:10 -07006172 addMotionRange(axis.axisInfo.axis, axis, info);
6173
Jeff Brown85297452011-03-04 13:07:49 -08006174 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Michael Wright2b08c612013-04-24 20:05:10 -07006175 addMotionRange(axis.axisInfo.highAxis, axis, info);
6176
Jeff Brown85297452011-03-04 13:07:49 -08006177 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006178 }
6179}
6180
Michael Wright2b08c612013-04-24 20:05:10 -07006181void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis,
6182 InputDeviceInfo* info) {
6183 info->addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK,
6184 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
6185 /* In order to ease the transition for developers from using the old axes
6186 * to the newer, more semantically correct axes, we'll continue to register
6187 * the old axes as duplicates of their corresponding new ones. */
6188 int32_t compatAxis = getCompatAxis(axisId);
6189 if (compatAxis >= 0) {
6190 info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK,
6191 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
6192 }
6193}
6194
6195/* A mapping from axes the joystick actually has to the axes that should be
6196 * artificially created for compatibility purposes.
6197 * Returns -1 if no compatibility axis is needed. */
6198int32_t JoystickInputMapper::getCompatAxis(int32_t axis) {
6199 switch(axis) {
6200 case AMOTION_EVENT_AXIS_LTRIGGER:
6201 return AMOTION_EVENT_AXIS_BRAKE;
6202 case AMOTION_EVENT_AXIS_RTRIGGER:
6203 return AMOTION_EVENT_AXIS_GAS;
6204 }
6205 return -1;
6206}
6207
Jeff Browncb1404e2011-01-15 18:14:15 -08006208void JoystickInputMapper::dump(String8& dump) {
6209 dump.append(INDENT2 "Joystick Input Mapper:\n");
6210
Jeff Brown6f2fba42011-02-19 01:08:02 -08006211 dump.append(INDENT3 "Axes:\n");
6212 size_t numAxes = mAxes.size();
6213 for (size_t i = 0; i < numAxes; i++) {
6214 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006215 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006216 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08006217 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006218 } else {
Jeff Brown85297452011-03-04 13:07:49 -08006219 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006220 }
Jeff Brown85297452011-03-04 13:07:49 -08006221 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6222 label = getAxisLabel(axis.axisInfo.highAxis);
6223 if (label) {
6224 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
6225 } else {
6226 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
6227 axis.axisInfo.splitValue);
6228 }
6229 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
6230 dump.append(" (invert)");
6231 }
6232
Michael Wrightc6091c62013-04-01 20:56:04 -07006233 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n",
6234 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
Jeff Brown85297452011-03-04 13:07:49 -08006235 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
6236 "highScale=%0.5f, highOffset=%0.5f\n",
6237 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07006238 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
6239 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006240 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07006241 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08006242 }
6243}
6244
Jeff Brown65fd2512011-08-18 11:20:58 -07006245void JoystickInputMapper::configure(nsecs_t when,
6246 const InputReaderConfiguration* config, uint32_t changes) {
6247 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08006248
Jeff Brown474dcb52011-06-14 20:22:50 -07006249 if (!changes) { // first time only
6250 // Collect all axes.
6251 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285a2011-08-31 12:56:34 -07006252 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
6253 & INPUT_DEVICE_CLASS_JOYSTICK)) {
6254 continue; // axis must be claimed by a different device
6255 }
6256
Jeff Brown474dcb52011-06-14 20:22:50 -07006257 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07006258 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07006259 if (rawAxisInfo.valid) {
6260 // Map axis.
6261 AxisInfo axisInfo;
6262 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
6263 if (!explicitlyMapped) {
6264 // Axis is not explicitly mapped, will choose a generic axis later.
6265 axisInfo.mode = AxisInfo::MODE_NORMAL;
6266 axisInfo.axis = -1;
6267 }
6268
6269 // Apply flat override.
6270 int32_t rawFlat = axisInfo.flatOverride < 0
6271 ? rawAxisInfo.flat : axisInfo.flatOverride;
6272
6273 // Calculate scaling factors and limits.
6274 Axis axis;
6275 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
6276 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
6277 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
6278 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6279 scale, 0.0f, highScale, 0.0f,
Michael Wrightc6091c62013-04-01 20:56:04 -07006280 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6281 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006282 } else if (isCenteredAxis(axisInfo.axis)) {
6283 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6284 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
6285 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6286 scale, offset, scale, offset,
Michael Wrightc6091c62013-04-01 20:56:04 -07006287 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6288 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006289 } else {
6290 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
6291 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
6292 scale, 0.0f, scale, 0.0f,
Michael Wrightc6091c62013-04-01 20:56:04 -07006293 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
6294 rawAxisInfo.resolution * scale);
Jeff Brown474dcb52011-06-14 20:22:50 -07006295 }
6296
6297 // To eliminate noise while the joystick is at rest, filter out small variations
6298 // in axis values up front.
6299 axis.filter = axis.flat * 0.25f;
6300
6301 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006302 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006303 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006304
Jeff Brown474dcb52011-06-14 20:22:50 -07006305 // If there are too many axes, start dropping them.
6306 // Prefer to keep explicitly mapped axes.
6307 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00006308 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07006309 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
6310 pruneAxes(true);
6311 pruneAxes(false);
6312 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006313
Jeff Brown474dcb52011-06-14 20:22:50 -07006314 // Assign generic axis ids to remaining axes.
6315 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
6316 size_t numAxes = mAxes.size();
6317 for (size_t i = 0; i < numAxes; i++) {
6318 Axis& axis = mAxes.editValueAt(i);
6319 if (axis.axisInfo.axis < 0) {
6320 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
6321 && haveAxis(nextGenericAxisId)) {
6322 nextGenericAxisId += 1;
6323 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006324
Jeff Brown474dcb52011-06-14 20:22:50 -07006325 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
6326 axis.axisInfo.axis = nextGenericAxisId;
6327 nextGenericAxisId += 1;
6328 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00006329 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07006330 "have already been assigned to other axes.",
6331 getDeviceName().string(), mAxes.keyAt(i));
6332 mAxes.removeItemsAt(i--);
6333 numAxes -= 1;
6334 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006335 }
6336 }
6337 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006338}
6339
Jeff Brown85297452011-03-04 13:07:49 -08006340bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006341 size_t numAxes = mAxes.size();
6342 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006343 const Axis& axis = mAxes.valueAt(i);
6344 if (axis.axisInfo.axis == axisId
6345 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
6346 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006347 return true;
6348 }
6349 }
6350 return false;
6351}
Jeff Browncb1404e2011-01-15 18:14:15 -08006352
Jeff Brown6f2fba42011-02-19 01:08:02 -08006353void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
6354 size_t i = mAxes.size();
6355 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
6356 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
6357 continue;
6358 }
Steve Block6215d3f2012-01-04 20:05:49 +00006359 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08006360 getDeviceName().string(), mAxes.keyAt(i));
6361 mAxes.removeItemsAt(i);
6362 }
6363}
6364
6365bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
6366 switch (axis) {
6367 case AMOTION_EVENT_AXIS_X:
6368 case AMOTION_EVENT_AXIS_Y:
6369 case AMOTION_EVENT_AXIS_Z:
6370 case AMOTION_EVENT_AXIS_RX:
6371 case AMOTION_EVENT_AXIS_RY:
6372 case AMOTION_EVENT_AXIS_RZ:
6373 case AMOTION_EVENT_AXIS_HAT_X:
6374 case AMOTION_EVENT_AXIS_HAT_Y:
6375 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08006376 case AMOTION_EVENT_AXIS_RUDDER:
6377 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006378 return true;
6379 default:
6380 return false;
6381 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006382}
6383
Jeff Brown65fd2512011-08-18 11:20:58 -07006384void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006385 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08006386 size_t numAxes = mAxes.size();
6387 for (size_t i = 0; i < numAxes; i++) {
6388 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08006389 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08006390 }
6391
Jeff Brown65fd2512011-08-18 11:20:58 -07006392 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08006393}
6394
6395void JoystickInputMapper::process(const RawEvent* rawEvent) {
6396 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006397 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07006398 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08006399 if (index >= 0) {
6400 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08006401 float newValue, highNewValue;
6402 switch (axis.axisInfo.mode) {
6403 case AxisInfo::MODE_INVERT:
6404 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
6405 * axis.scale + axis.offset;
6406 highNewValue = 0.0f;
6407 break;
6408 case AxisInfo::MODE_SPLIT:
6409 if (rawEvent->value < axis.axisInfo.splitValue) {
6410 newValue = (axis.axisInfo.splitValue - rawEvent->value)
6411 * axis.scale + axis.offset;
6412 highNewValue = 0.0f;
6413 } else if (rawEvent->value > axis.axisInfo.splitValue) {
6414 newValue = 0.0f;
6415 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
6416 * axis.highScale + axis.highOffset;
6417 } else {
6418 newValue = 0.0f;
6419 highNewValue = 0.0f;
6420 }
6421 break;
6422 default:
6423 newValue = rawEvent->value * axis.scale + axis.offset;
6424 highNewValue = 0.0f;
6425 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006426 }
Jeff Brown85297452011-03-04 13:07:49 -08006427 axis.newValue = newValue;
6428 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08006429 }
6430 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006431 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006432
6433 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006434 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006435 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006436 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006437 break;
6438 }
6439 break;
6440 }
6441}
6442
Jeff Brown6f2fba42011-02-19 01:08:02 -08006443void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006444 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006445 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006446 }
6447
6448 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006449 int32_t buttonState = 0;
6450
6451 PointerProperties pointerProperties;
6452 pointerProperties.clear();
6453 pointerProperties.id = 0;
6454 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006455
Jeff Brown6f2fba42011-02-19 01:08:02 -08006456 PointerCoords pointerCoords;
6457 pointerCoords.clear();
6458
6459 size_t numAxes = mAxes.size();
6460 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006461 const Axis& axis = mAxes.valueAt(i);
Michael Wright2b08c612013-04-24 20:05:10 -07006462 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue);
Jeff Brown85297452011-03-04 13:07:49 -08006463 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Michael Wright2b08c612013-04-24 20:05:10 -07006464 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis,
6465 axis.highCurrentValue);
Jeff Brown85297452011-03-04 13:07:49 -08006466 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006467 }
6468
Jeff Brown83d616a2012-09-09 20:33:43 -07006469 // Moving a joystick axis should not wake the device because joysticks can
Jeff Brown56194eb2011-03-02 19:23:13 -08006470 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6471 // button will likely wake the device.
6472 // TODO: Use the input device configuration to control this behavior more finely.
6473 uint32_t policyFlags = 0;
6474
Jeff Brownbe1aa822011-07-27 16:04:54 -07006475 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006476 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown83d616a2012-09-09 20:33:43 -07006477 ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006478 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006479}
6480
Michael Wright2b08c612013-04-24 20:05:10 -07006481void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords,
6482 int32_t axis, float value) {
6483 pointerCoords->setAxisValue(axis, value);
6484 /* In order to ease the transition for developers from using the old axes
6485 * to the newer, more semantically correct axes, we'll continue to produce
6486 * values for the old axes as mirrors of the value of their corresponding
6487 * new axes. */
6488 int32_t compatAxis = getCompatAxis(axis);
6489 if (compatAxis >= 0) {
6490 pointerCoords->setAxisValue(compatAxis, value);
6491 }
6492}
6493
Jeff Brown85297452011-03-04 13:07:49 -08006494bool JoystickInputMapper::filterAxes(bool force) {
6495 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006496 size_t numAxes = mAxes.size();
6497 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006498 Axis& axis = mAxes.editValueAt(i);
6499 if (force || hasValueChangedSignificantly(axis.filter,
6500 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6501 axis.currentValue = axis.newValue;
6502 atLeastOneSignificantChange = true;
6503 }
6504 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6505 if (force || hasValueChangedSignificantly(axis.filter,
6506 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6507 axis.highCurrentValue = axis.highNewValue;
6508 atLeastOneSignificantChange = true;
6509 }
6510 }
6511 }
6512 return atLeastOneSignificantChange;
6513}
6514
6515bool JoystickInputMapper::hasValueChangedSignificantly(
6516 float filter, float newValue, float currentValue, float min, float max) {
6517 if (newValue != currentValue) {
6518 // Filter out small changes in value unless the value is converging on the axis
6519 // bounds or center point. This is intended to reduce the amount of information
6520 // sent to applications by particularly noisy joysticks (such as PS3).
6521 if (fabs(newValue - currentValue) > filter
6522 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6523 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6524 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6525 return true;
6526 }
6527 }
6528 return false;
6529}
6530
6531bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6532 float filter, float newValue, float currentValue, float thresholdValue) {
6533 float newDistance = fabs(newValue - thresholdValue);
6534 if (newDistance < filter) {
6535 float oldDistance = fabs(currentValue - thresholdValue);
6536 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006537 return true;
6538 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006539 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006540 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006541}
6542
Jeff Brown46b9ac02010-04-22 18:58:52 -07006543} // namespace android