blob: 71eba523bbf692d9cacd0ff9de0aa8feeea8cb45 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041#include <cutils/log.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080042#include <androidfw/Keyboard.h>
43#include <androidfw/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700183 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700185 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 }
187}
188
189static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
190 nsecs_t when, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
192 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
193 lastButtonState, currentButtonState,
194 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
198}
199
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700200
Jeff Brown65fd2512011-08-18 11:20:58 -0700201// --- InputReaderConfiguration ---
202
203bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,
204 int32_t* width, int32_t* height, int32_t* orientation) const {
205 if (displayId == 0) {
206 const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
207 if (info.width > 0 && info.height > 0) {
208 if (width) {
209 *width = info.width;
210 }
211 if (height) {
212 *height = info.height;
213 }
214 if (orientation) {
215 *orientation = info.orientation;
216 }
217 return true;
218 }
219 }
220 return false;
221}
222
223void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,
224 int32_t width, int32_t height, int32_t orientation) {
225 if (displayId == 0) {
226 DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
227 info.width = width;
228 info.height = height;
229 info.orientation = orientation;
230 }
231}
232
233
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234// --- InputReader ---
235
236InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700238 const sp<InputListenerInterface>& listener) :
239 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700240 mGlobalMetaState(0), mGeneration(1),
241 mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700242 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700243 mQueuedListener = new QueuedInputListener(listener);
244
245 { // acquire lock
246 AutoMutex _l(mLock);
247
248 refreshConfigurationLocked(0);
249 updateGlobalMetaStateLocked();
250 updateInputConfigurationLocked();
251 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700252}
253
254InputReader::~InputReader() {
255 for (size_t i = 0; i < mDevices.size(); i++) {
256 delete mDevices.valueAt(i);
257 }
258}
259
260void InputReader::loopOnce() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700261 int32_t oldGeneration;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700262 int32_t timeoutMillis;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700263 bool inputDevicesChanged = false;
264 Vector<InputDeviceInfo> inputDevices;
Jeff Brown474dcb52011-06-14 20:22:50 -0700265 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700266 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700267
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700268 oldGeneration = mGeneration;
269 timeoutMillis = -1;
270
Jeff Brownbe1aa822011-07-27 16:04:54 -0700271 uint32_t changes = mConfigurationChangesToRefresh;
272 if (changes) {
273 mConfigurationChangesToRefresh = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700274 timeoutMillis = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700275 refreshConfigurationLocked(changes);
276 }
277
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700278 if (timeoutMillis < 0 && mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700279 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
280 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
281 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700282 } // release lock
283
Jeff Brownb7198742011-03-18 18:14:26 -0700284 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700285
286 { // acquire lock
287 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800288 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700289
290 if (count) {
291 processEventsLocked(mEventBuffer, count);
292 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700293
294 if (mNextTimeout != LLONG_MAX) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700295 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800296 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700297#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800298 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700299#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800300 mNextTimeout = LLONG_MAX;
301 timeoutExpiredLocked(now);
302 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700303 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700304
305 if (oldGeneration != mGeneration) {
306 inputDevicesChanged = true;
307 getInputDevicesLocked(inputDevices);
308 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700309 } // release lock
310
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700311 // Send out a message that the describes the changed input devices.
312 if (inputDevicesChanged) {
313 mPolicy->notifyInputDevicesChanged(inputDevices);
314 }
315
Jeff Brownbe1aa822011-07-27 16:04:54 -0700316 // Flush queued events out to the listener.
317 // This must happen outside of the lock because the listener could potentially call
318 // back into the InputReader's methods, such as getScanCodeState, or become blocked
319 // on another thread similarly waiting to acquire the InputReader lock thereby
320 // resulting in a deadlock. This situation is actually quite plausible because the
321 // listener is actually the input dispatcher, which calls into the window manager,
322 // which occasionally calls into the input reader.
323 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700324}
325
Jeff Brownbe1aa822011-07-27 16:04:54 -0700326void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700327 for (const RawEvent* rawEvent = rawEvents; count;) {
328 int32_t type = rawEvent->type;
329 size_t batchSize = 1;
330 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
331 int32_t deviceId = rawEvent->deviceId;
332 while (batchSize < count) {
333 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
334 || rawEvent[batchSize].deviceId != deviceId) {
335 break;
336 }
337 batchSize += 1;
338 }
339#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000340 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700341#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700342 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700343 } else {
344 switch (rawEvent->type) {
345 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700346 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700347 break;
348 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700349 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700350 break;
351 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700352 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700353 break;
354 default:
Steve Blockec193de2012-01-09 18:35:44 +0000355 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700356 break;
357 }
358 }
359 count -= batchSize;
360 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700361 }
362}
363
Jeff Brown65fd2512011-08-18 11:20:58 -0700364void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700365 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
366 if (deviceIndex >= 0) {
367 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
368 return;
369 }
370
Jeff Browne38fdfa2012-04-06 14:51:01 -0700371 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
373
Jeff Browne38fdfa2012-04-06 14:51:01 -0700374 InputDevice* device = createDeviceLocked(deviceId, identifier, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700375 device->configure(when, &mConfig, 0);
376 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377
Jeff Brown8d608662010-08-30 03:02:23 -0700378 if (device->isIgnored()) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700379 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
380 identifier.name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700381 } else {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700382 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
383 identifier.name.string(), device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700384 }
385
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700386 mDevices.add(deviceId, device);
387 bumpGenerationLocked();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700388}
389
Jeff Brown65fd2512011-08-18 11:20:58 -0700390void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700391 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700392 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700393 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000394 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700395 return;
396 }
397
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700398 device = mDevices.valueAt(deviceIndex);
399 mDevices.removeItemsAt(deviceIndex, 1);
400 bumpGenerationLocked();
401
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000403 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700404 device->getId(), device->getName().string());
405 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000406 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 device->getId(), device->getName().string(), device->getSources());
408 }
409
Jeff Brown65fd2512011-08-18 11:20:58 -0700410 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700411 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700412}
413
Jeff Brownbe1aa822011-07-27 16:04:54 -0700414InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700415 const InputDeviceIdentifier& identifier, uint32_t classes) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700416 InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(),
417 identifier, classes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700418
Jeff Brown56194eb2011-03-02 19:23:13 -0800419 // External devices.
420 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
421 device->setExternal(true);
422 }
423
Jeff Brown6d0fec22010-07-23 21:28:06 -0700424 // Switch-like devices.
425 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
426 device->addMapper(new SwitchInputMapper(device));
427 }
428
429 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800430 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
432 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800433 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434 }
435 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
436 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
437 }
438 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800439 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700440 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800441 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800442 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800443 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444
Jeff Brownefd32662011-03-08 15:13:06 -0800445 if (keyboardSource != 0) {
446 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447 }
448
Jeff Brown83c09682010-12-23 17:50:18 -0800449 // Cursor-like devices.
450 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
451 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452 }
453
Jeff Brown58a2da82011-01-25 16:02:22 -0800454 // Touchscreens and touchpad devices.
455 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800456 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800457 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800458 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700459 }
460
Jeff Browncb1404e2011-01-15 18:14:15 -0800461 // Joystick-like devices.
462 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
463 device->addMapper(new JoystickInputMapper(device));
464 }
465
Jeff Brown6d0fec22010-07-23 21:28:06 -0700466 return device;
467}
468
Jeff Brownbe1aa822011-07-27 16:04:54 -0700469void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700470 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
472 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000473 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700474 return;
475 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476
Jeff Brownbe1aa822011-07-27 16:04:54 -0700477 InputDevice* device = mDevices.valueAt(deviceIndex);
478 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000479 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700480 return;
481 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700482
Jeff Brownbe1aa822011-07-27 16:04:54 -0700483 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700484}
485
Jeff Brownbe1aa822011-07-27 16:04:54 -0700486void InputReader::timeoutExpiredLocked(nsecs_t when) {
487 for (size_t i = 0; i < mDevices.size(); i++) {
488 InputDevice* device = mDevices.valueAt(i);
489 if (!device->isIgnored()) {
490 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700491 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700492 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700493}
494
Jeff Brownbe1aa822011-07-27 16:04:54 -0700495void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700496 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700497 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700498
499 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700500 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700501
502 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700503 NotifyConfigurationChangedArgs args(when);
504 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700505}
506
Jeff Brownbe1aa822011-07-27 16:04:54 -0700507void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700508 mPolicy->getReaderConfiguration(&mConfig);
509 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
510
Jeff Brown474dcb52011-06-14 20:22:50 -0700511 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000512 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700513 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700514
515 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
516 mEventHub->requestReopenDevices();
517 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700518 for (size_t i = 0; i < mDevices.size(); i++) {
519 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700520 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700521 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700522 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700523 }
524}
525
Jeff Brownbe1aa822011-07-27 16:04:54 -0700526void InputReader::updateGlobalMetaStateLocked() {
527 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700528
Jeff Brownbe1aa822011-07-27 16:04:54 -0700529 for (size_t i = 0; i < mDevices.size(); i++) {
530 InputDevice* device = mDevices.valueAt(i);
531 mGlobalMetaState |= device->getMetaState();
532 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700533}
534
Jeff Brownbe1aa822011-07-27 16:04:54 -0700535int32_t InputReader::getGlobalMetaStateLocked() {
536 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700537}
538
Jeff Brownbe1aa822011-07-27 16:04:54 -0700539void InputReader::updateInputConfigurationLocked() {
540 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
541 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
542 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
543 InputDeviceInfo deviceInfo;
544 for (size_t i = 0; i < mDevices.size(); i++) {
545 InputDevice* device = mDevices.valueAt(i);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700546 if (!(device->getClasses() & INPUT_DEVICE_CLASS_VIRTUAL)) {
547 device->getDeviceInfo(& deviceInfo);
548 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700549
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700550 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
551 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
552 }
553 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
554 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
555 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
556 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
557 }
558 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
559 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
560 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700561 }
562 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700563
Jeff Brownbe1aa822011-07-27 16:04:54 -0700564 mInputConfiguration.touchScreen = touchScreenConfig;
565 mInputConfiguration.keyboard = keyboardConfig;
566 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700567}
568
Jeff Brownbe1aa822011-07-27 16:04:54 -0700569void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800570 mDisableVirtualKeysTimeout = time;
571}
572
Jeff Brownbe1aa822011-07-27 16:04:54 -0700573bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800574 InputDevice* device, int32_t keyCode, int32_t scanCode) {
575 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000576 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800577 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
578 device->getName().string(),
579 (mDisableVirtualKeysTimeout - now) * 0.000001,
580 keyCode, scanCode);
581 return true;
582 } else {
583 return false;
584 }
585}
586
Jeff Brownbe1aa822011-07-27 16:04:54 -0700587void InputReader::fadePointerLocked() {
588 for (size_t i = 0; i < mDevices.size(); i++) {
589 InputDevice* device = mDevices.valueAt(i);
590 device->fadePointer();
591 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800592}
593
Jeff Brownbe1aa822011-07-27 16:04:54 -0700594void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700595 if (when < mNextTimeout) {
596 mNextTimeout = when;
597 }
598}
599
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700600int32_t InputReader::bumpGenerationLocked() {
601 return ++mGeneration;
602}
603
Jeff Brown6d0fec22010-07-23 21:28:06 -0700604void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700605 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700606
Jeff Brownbe1aa822011-07-27 16:04:54 -0700607 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700608}
609
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700610void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700611 AutoMutex _l(mLock);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700612 getInputDevicesLocked(outInputDevices);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700613}
614
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700615void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) {
616 outInputDevices.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700617
Jeff Brownbe1aa822011-07-27 16:04:54 -0700618 size_t numDevices = mDevices.size();
619 for (size_t i = 0; i < numDevices; i++) {
620 InputDevice* device = mDevices.valueAt(i);
621 if (!device->isIgnored()) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700622 outInputDevices.push();
623 device->getDeviceInfo(&outInputDevices.editTop());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700624 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700625 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700626}
627
628int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
629 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700630 AutoMutex _l(mLock);
631
632 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700633}
634
635int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
636 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700637 AutoMutex _l(mLock);
638
639 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700640}
641
642int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700643 AutoMutex _l(mLock);
644
645 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700646}
647
Jeff Brownbe1aa822011-07-27 16:04:54 -0700648int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700649 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700650 int32_t result = AKEY_STATE_UNKNOWN;
651 if (deviceId >= 0) {
652 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
653 if (deviceIndex >= 0) {
654 InputDevice* device = mDevices.valueAt(deviceIndex);
655 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
656 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700657 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700658 }
659 } else {
660 size_t numDevices = mDevices.size();
661 for (size_t i = 0; i < numDevices; i++) {
662 InputDevice* device = mDevices.valueAt(i);
663 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800664 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
665 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
666 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
667 if (currentResult >= AKEY_STATE_DOWN) {
668 return currentResult;
669 } else if (currentResult == AKEY_STATE_UP) {
670 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700671 }
672 }
673 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700674 }
675 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700676}
677
678bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
679 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700680 AutoMutex _l(mLock);
681
Jeff Brown6d0fec22010-07-23 21:28:06 -0700682 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700683 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700684}
685
Jeff Brownbe1aa822011-07-27 16:04:54 -0700686bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
687 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
688 bool result = false;
689 if (deviceId >= 0) {
690 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
691 if (deviceIndex >= 0) {
692 InputDevice* device = mDevices.valueAt(deviceIndex);
693 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
694 result = device->markSupportedKeyCodes(sourceMask,
695 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700696 }
697 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700698 } else {
699 size_t numDevices = mDevices.size();
700 for (size_t i = 0; i < numDevices; i++) {
701 InputDevice* device = mDevices.valueAt(i);
702 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
703 result |= device->markSupportedKeyCodes(sourceMask,
704 numCodes, keyCodes, outFlags);
705 }
706 }
707 }
708 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700709}
710
Jeff Brown474dcb52011-06-14 20:22:50 -0700711void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700712 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700713
Jeff Brownbe1aa822011-07-27 16:04:54 -0700714 if (changes) {
715 bool needWake = !mConfigurationChangesToRefresh;
716 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700717
718 if (needWake) {
719 mEventHub->wake();
720 }
721 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700722}
723
Jeff Brownb88102f2010-09-08 11:49:43 -0700724void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700725 AutoMutex _l(mLock);
726
Jeff Brownf2f487182010-10-01 17:46:21 -0700727 mEventHub->dump(dump);
728 dump.append("\n");
729
730 dump.append("Input Reader State:\n");
731
Jeff Brownbe1aa822011-07-27 16:04:54 -0700732 for (size_t i = 0; i < mDevices.size(); i++) {
733 mDevices.valueAt(i)->dump(dump);
734 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700735
736 dump.append(INDENT "Configuration:\n");
737 dump.append(INDENT2 "ExcludedDeviceNames: [");
738 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
739 if (i != 0) {
740 dump.append(", ");
741 }
742 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
743 }
744 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700745 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
746 mConfig.virtualKeyQuietTime * 0.000001f);
747
Jeff Brown19c97d462011-06-01 12:33:19 -0700748 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
749 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
750 mConfig.pointerVelocityControlParameters.scale,
751 mConfig.pointerVelocityControlParameters.lowThreshold,
752 mConfig.pointerVelocityControlParameters.highThreshold,
753 mConfig.pointerVelocityControlParameters.acceleration);
754
755 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
756 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
757 mConfig.wheelVelocityControlParameters.scale,
758 mConfig.wheelVelocityControlParameters.lowThreshold,
759 mConfig.wheelVelocityControlParameters.highThreshold,
760 mConfig.wheelVelocityControlParameters.acceleration);
761
Jeff Brown214eaf42011-05-26 19:17:02 -0700762 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700763 dump.appendFormat(INDENT3 "Enabled: %s\n",
764 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700765 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
766 mConfig.pointerGestureQuietInterval * 0.000001f);
767 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
768 mConfig.pointerGestureDragMinSwitchSpeed);
769 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
770 mConfig.pointerGestureTapInterval * 0.000001f);
771 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
772 mConfig.pointerGestureTapDragInterval * 0.000001f);
773 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
774 mConfig.pointerGestureTapSlop);
775 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
776 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700777 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
778 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700779 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
780 mConfig.pointerGestureSwipeTransitionAngleCosine);
781 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
782 mConfig.pointerGestureSwipeMaxWidthRatio);
783 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
784 mConfig.pointerGestureMovementSpeedRatio);
785 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
786 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700787}
788
Jeff Brown89ef0722011-08-10 16:25:21 -0700789void InputReader::monitor() {
790 // Acquire and release the lock to ensure that the reader has not deadlocked.
791 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800792 mEventHub->wake();
793 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700794 mLock.unlock();
795
796 // Check the EventHub
797 mEventHub->monitor();
798}
799
Jeff Brown6d0fec22010-07-23 21:28:06 -0700800
Jeff Brownbe1aa822011-07-27 16:04:54 -0700801// --- InputReader::ContextImpl ---
802
803InputReader::ContextImpl::ContextImpl(InputReader* reader) :
804 mReader(reader) {
805}
806
807void InputReader::ContextImpl::updateGlobalMetaState() {
808 // lock is already held by the input loop
809 mReader->updateGlobalMetaStateLocked();
810}
811
812int32_t InputReader::ContextImpl::getGlobalMetaState() {
813 // lock is already held by the input loop
814 return mReader->getGlobalMetaStateLocked();
815}
816
817void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
818 // lock is already held by the input loop
819 mReader->disableVirtualKeysUntilLocked(time);
820}
821
822bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
823 InputDevice* device, int32_t keyCode, int32_t scanCode) {
824 // lock is already held by the input loop
825 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
826}
827
828void InputReader::ContextImpl::fadePointer() {
829 // lock is already held by the input loop
830 mReader->fadePointerLocked();
831}
832
833void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
834 // lock is already held by the input loop
835 mReader->requestTimeoutAtTimeLocked(when);
836}
837
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700838int32_t InputReader::ContextImpl::bumpGeneration() {
839 // lock is already held by the input loop
840 return mReader->bumpGenerationLocked();
841}
842
Jeff Brownbe1aa822011-07-27 16:04:54 -0700843InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
844 return mReader->mPolicy.get();
845}
846
847InputListenerInterface* InputReader::ContextImpl::getListener() {
848 return mReader->mQueuedListener.get();
849}
850
851EventHubInterface* InputReader::ContextImpl::getEventHub() {
852 return mReader->mEventHub.get();
853}
854
855
Jeff Brown6d0fec22010-07-23 21:28:06 -0700856// --- InputReaderThread ---
857
858InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
859 Thread(/*canCallJava*/ true), mReader(reader) {
860}
861
862InputReaderThread::~InputReaderThread() {
863}
864
865bool InputReaderThread::threadLoop() {
866 mReader->loopOnce();
867 return true;
868}
869
870
871// --- InputDevice ---
872
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700873InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700874 const InputDeviceIdentifier& identifier, uint32_t classes) :
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700875 mContext(context), mId(id), mGeneration(generation),
876 mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285af2011-08-31 12:56:34 -0700877 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700878}
879
880InputDevice::~InputDevice() {
881 size_t numMappers = mMappers.size();
882 for (size_t i = 0; i < numMappers; i++) {
883 delete mMappers[i];
884 }
885 mMappers.clear();
886}
887
Jeff Brownef3d7e82010-09-30 14:33:04 -0700888void InputDevice::dump(String8& dump) {
889 InputDeviceInfo deviceInfo;
890 getDeviceInfo(& deviceInfo);
891
Jeff Brown90655042010-12-02 13:50:46 -0800892 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700893 deviceInfo.getName().string());
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700894 dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration);
Jeff Brown56194eb2011-03-02 19:23:13 -0800895 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700896 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
897 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800898
Jeff Brownefd32662011-03-08 15:13:06 -0800899 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800900 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700901 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800902 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800903 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
904 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800905 char name[32];
906 if (label) {
907 strncpy(name, label, sizeof(name));
908 name[sizeof(name) - 1] = '\0';
909 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800910 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800911 }
Jeff Brownefd32662011-03-08 15:13:06 -0800912 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
913 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
914 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800915 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700916 }
917
918 size_t numMappers = mMappers.size();
919 for (size_t i = 0; i < numMappers; i++) {
920 InputMapper* mapper = mMappers[i];
921 mapper->dump(dump);
922 }
923}
924
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925void InputDevice::addMapper(InputMapper* mapper) {
926 mMappers.add(mapper);
927}
928
Jeff Brown65fd2512011-08-18 11:20:58 -0700929void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700930 mSources = 0;
931
Jeff Brown474dcb52011-06-14 20:22:50 -0700932 if (!isIgnored()) {
933 if (!changes) { // first time only
934 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
935 }
936
937 size_t numMappers = mMappers.size();
938 for (size_t i = 0; i < numMappers; i++) {
939 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700940 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700941 mSources |= mapper->getSources();
942 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700943 }
944}
945
Jeff Brown65fd2512011-08-18 11:20:58 -0700946void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700947 size_t numMappers = mMappers.size();
948 for (size_t i = 0; i < numMappers; i++) {
949 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700950 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700951 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700952
953 mContext->updateGlobalMetaState();
954
955 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700956}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700957
Jeff Brownb7198742011-03-18 18:14:26 -0700958void InputDevice::process(const RawEvent* rawEvents, size_t count) {
959 // Process all of the events in order for each mapper.
960 // We cannot simply ask each mapper to process them in bulk because mappers may
961 // have side-effects that must be interleaved. For example, joystick movement events and
962 // gamepad button presses are handled by different mappers but they should be dispatched
963 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700964 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700965 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
966#if DEBUG_RAW_EVENTS
Jeff Brown49ccac52012-04-11 18:27:33 -0700967 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
968 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
Jeff Brownb7198742011-03-18 18:14:26 -0700969#endif
970
Jeff Brown80fd47c2011-05-24 01:07:44 -0700971 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700972 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700973 mDropUntilNextSync = false;
974#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000975 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700976#endif
977 } else {
978#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000979 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700980#endif
981 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700982 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700983 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700984 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700985 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700986 } else {
987 for (size_t i = 0; i < numMappers; i++) {
988 InputMapper* mapper = mMappers[i];
989 mapper->process(rawEvent);
990 }
Jeff Brownb7198742011-03-18 18:14:26 -0700991 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700992 }
993}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700994
Jeff Brownaa3855d2011-03-17 01:34:19 -0700995void InputDevice::timeoutExpired(nsecs_t when) {
996 size_t numMappers = mMappers.size();
997 for (size_t i = 0; i < numMappers; i++) {
998 InputMapper* mapper = mMappers[i];
999 mapper->timeoutExpired(when);
1000 }
1001}
1002
Jeff Brown6d0fec22010-07-23 21:28:06 -07001003void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001004 outDeviceInfo->initialize(mId, mGeneration, mIdentifier.name, mIdentifier.descriptor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005
1006 size_t numMappers = mMappers.size();
1007 for (size_t i = 0; i < numMappers; i++) {
1008 InputMapper* mapper = mMappers[i];
1009 mapper->populateDeviceInfo(outDeviceInfo);
1010 }
1011}
1012
1013int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1014 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
1015}
1016
1017int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1018 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1019}
1020
1021int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1022 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1023}
1024
1025int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1026 int32_t result = AKEY_STATE_UNKNOWN;
1027 size_t numMappers = mMappers.size();
1028 for (size_t i = 0; i < numMappers; i++) {
1029 InputMapper* mapper = mMappers[i];
1030 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001031 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1032 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1033 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1034 if (currentResult >= AKEY_STATE_DOWN) {
1035 return currentResult;
1036 } else if (currentResult == AKEY_STATE_UP) {
1037 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001038 }
1039 }
1040 }
1041 return result;
1042}
1043
1044bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1045 const int32_t* keyCodes, uint8_t* outFlags) {
1046 bool result = false;
1047 size_t numMappers = mMappers.size();
1048 for (size_t i = 0; i < numMappers; i++) {
1049 InputMapper* mapper = mMappers[i];
1050 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1051 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1052 }
1053 }
1054 return result;
1055}
1056
1057int32_t InputDevice::getMetaState() {
1058 int32_t result = 0;
1059 size_t numMappers = mMappers.size();
1060 for (size_t i = 0; i < numMappers; i++) {
1061 InputMapper* mapper = mMappers[i];
1062 result |= mapper->getMetaState();
1063 }
1064 return result;
1065}
1066
Jeff Brown05dc66a2011-03-02 14:41:58 -08001067void InputDevice::fadePointer() {
1068 size_t numMappers = mMappers.size();
1069 for (size_t i = 0; i < numMappers; i++) {
1070 InputMapper* mapper = mMappers[i];
1071 mapper->fadePointer();
1072 }
1073}
1074
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001075void InputDevice::bumpGeneration() {
1076 mGeneration = mContext->bumpGeneration();
1077}
1078
Jeff Brown65fd2512011-08-18 11:20:58 -07001079void InputDevice::notifyReset(nsecs_t when) {
1080 NotifyDeviceResetArgs args(when, mId);
1081 mContext->getListener()->notifyDeviceReset(&args);
1082}
1083
Jeff Brown6d0fec22010-07-23 21:28:06 -07001084
Jeff Brown49754db2011-07-01 17:37:58 -07001085// --- CursorButtonAccumulator ---
1086
1087CursorButtonAccumulator::CursorButtonAccumulator() {
1088 clearButtons();
1089}
1090
Jeff Brown65fd2512011-08-18 11:20:58 -07001091void CursorButtonAccumulator::reset(InputDevice* device) {
1092 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1093 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1094 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1095 mBtnBack = device->isKeyPressed(BTN_BACK);
1096 mBtnSide = device->isKeyPressed(BTN_SIDE);
1097 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1098 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1099 mBtnTask = device->isKeyPressed(BTN_TASK);
1100}
1101
Jeff Brown49754db2011-07-01 17:37:58 -07001102void CursorButtonAccumulator::clearButtons() {
1103 mBtnLeft = 0;
1104 mBtnRight = 0;
1105 mBtnMiddle = 0;
1106 mBtnBack = 0;
1107 mBtnSide = 0;
1108 mBtnForward = 0;
1109 mBtnExtra = 0;
1110 mBtnTask = 0;
1111}
1112
1113void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1114 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001115 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001116 case BTN_LEFT:
1117 mBtnLeft = rawEvent->value;
1118 break;
1119 case BTN_RIGHT:
1120 mBtnRight = rawEvent->value;
1121 break;
1122 case BTN_MIDDLE:
1123 mBtnMiddle = rawEvent->value;
1124 break;
1125 case BTN_BACK:
1126 mBtnBack = rawEvent->value;
1127 break;
1128 case BTN_SIDE:
1129 mBtnSide = rawEvent->value;
1130 break;
1131 case BTN_FORWARD:
1132 mBtnForward = rawEvent->value;
1133 break;
1134 case BTN_EXTRA:
1135 mBtnExtra = rawEvent->value;
1136 break;
1137 case BTN_TASK:
1138 mBtnTask = rawEvent->value;
1139 break;
1140 }
1141 }
1142}
1143
1144uint32_t CursorButtonAccumulator::getButtonState() const {
1145 uint32_t result = 0;
1146 if (mBtnLeft) {
1147 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1148 }
1149 if (mBtnRight) {
1150 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1151 }
1152 if (mBtnMiddle) {
1153 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1154 }
1155 if (mBtnBack || mBtnSide) {
1156 result |= AMOTION_EVENT_BUTTON_BACK;
1157 }
1158 if (mBtnForward || mBtnExtra) {
1159 result |= AMOTION_EVENT_BUTTON_FORWARD;
1160 }
1161 return result;
1162}
1163
1164
1165// --- CursorMotionAccumulator ---
1166
Jeff Brown65fd2512011-08-18 11:20:58 -07001167CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001168 clearRelativeAxes();
1169}
1170
Jeff Brown65fd2512011-08-18 11:20:58 -07001171void CursorMotionAccumulator::reset(InputDevice* device) {
1172 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001173}
1174
1175void CursorMotionAccumulator::clearRelativeAxes() {
1176 mRelX = 0;
1177 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001178}
1179
1180void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1181 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001182 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001183 case REL_X:
1184 mRelX = rawEvent->value;
1185 break;
1186 case REL_Y:
1187 mRelY = rawEvent->value;
1188 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001189 }
1190 }
1191}
1192
1193void CursorMotionAccumulator::finishSync() {
1194 clearRelativeAxes();
1195}
1196
1197
1198// --- CursorScrollAccumulator ---
1199
1200CursorScrollAccumulator::CursorScrollAccumulator() :
1201 mHaveRelWheel(false), mHaveRelHWheel(false) {
1202 clearRelativeAxes();
1203}
1204
1205void CursorScrollAccumulator::configure(InputDevice* device) {
1206 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1207 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1208}
1209
1210void CursorScrollAccumulator::reset(InputDevice* device) {
1211 clearRelativeAxes();
1212}
1213
1214void CursorScrollAccumulator::clearRelativeAxes() {
1215 mRelWheel = 0;
1216 mRelHWheel = 0;
1217}
1218
1219void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1220 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001221 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001222 case REL_WHEEL:
1223 mRelWheel = rawEvent->value;
1224 break;
1225 case REL_HWHEEL:
1226 mRelHWheel = rawEvent->value;
1227 break;
1228 }
1229 }
1230}
1231
Jeff Brown65fd2512011-08-18 11:20:58 -07001232void CursorScrollAccumulator::finishSync() {
1233 clearRelativeAxes();
1234}
1235
Jeff Brown49754db2011-07-01 17:37:58 -07001236
1237// --- TouchButtonAccumulator ---
1238
1239TouchButtonAccumulator::TouchButtonAccumulator() :
1240 mHaveBtnTouch(false) {
1241 clearButtons();
1242}
1243
1244void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001245 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1246}
1247
1248void TouchButtonAccumulator::reset(InputDevice* device) {
1249 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1250 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1251 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1252 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1253 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1254 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1255 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1256 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1257 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1258 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1259 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001260 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1261 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1262 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001263}
1264
1265void TouchButtonAccumulator::clearButtons() {
1266 mBtnTouch = 0;
1267 mBtnStylus = 0;
1268 mBtnStylus2 = 0;
1269 mBtnToolFinger = 0;
1270 mBtnToolPen = 0;
1271 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001272 mBtnToolBrush = 0;
1273 mBtnToolPencil = 0;
1274 mBtnToolAirbrush = 0;
1275 mBtnToolMouse = 0;
1276 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001277 mBtnToolDoubleTap = 0;
1278 mBtnToolTripleTap = 0;
1279 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001280}
1281
1282void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1283 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001284 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001285 case BTN_TOUCH:
1286 mBtnTouch = rawEvent->value;
1287 break;
1288 case BTN_STYLUS:
1289 mBtnStylus = rawEvent->value;
1290 break;
1291 case BTN_STYLUS2:
1292 mBtnStylus2 = rawEvent->value;
1293 break;
1294 case BTN_TOOL_FINGER:
1295 mBtnToolFinger = rawEvent->value;
1296 break;
1297 case BTN_TOOL_PEN:
1298 mBtnToolPen = rawEvent->value;
1299 break;
1300 case BTN_TOOL_RUBBER:
1301 mBtnToolRubber = rawEvent->value;
1302 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001303 case BTN_TOOL_BRUSH:
1304 mBtnToolBrush = rawEvent->value;
1305 break;
1306 case BTN_TOOL_PENCIL:
1307 mBtnToolPencil = rawEvent->value;
1308 break;
1309 case BTN_TOOL_AIRBRUSH:
1310 mBtnToolAirbrush = rawEvent->value;
1311 break;
1312 case BTN_TOOL_MOUSE:
1313 mBtnToolMouse = rawEvent->value;
1314 break;
1315 case BTN_TOOL_LENS:
1316 mBtnToolLens = rawEvent->value;
1317 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001318 case BTN_TOOL_DOUBLETAP:
1319 mBtnToolDoubleTap = rawEvent->value;
1320 break;
1321 case BTN_TOOL_TRIPLETAP:
1322 mBtnToolTripleTap = rawEvent->value;
1323 break;
1324 case BTN_TOOL_QUADTAP:
1325 mBtnToolQuadTap = rawEvent->value;
1326 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001327 }
1328 }
1329}
1330
1331uint32_t TouchButtonAccumulator::getButtonState() const {
1332 uint32_t result = 0;
1333 if (mBtnStylus) {
1334 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1335 }
1336 if (mBtnStylus2) {
1337 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1338 }
1339 return result;
1340}
1341
1342int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001343 if (mBtnToolMouse || mBtnToolLens) {
1344 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1345 }
Jeff Brown49754db2011-07-01 17:37:58 -07001346 if (mBtnToolRubber) {
1347 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1348 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001349 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001350 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1351 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001352 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001353 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1354 }
1355 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1356}
1357
Jeff Brownd87c6d52011-08-10 14:55:59 -07001358bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001359 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1360 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001361 || mBtnToolMouse || mBtnToolLens
1362 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001363}
1364
1365bool TouchButtonAccumulator::isHovering() const {
1366 return mHaveBtnTouch && !mBtnTouch;
1367}
1368
1369
Jeff Brownbe1aa822011-07-27 16:04:54 -07001370// --- RawPointerAxes ---
1371
1372RawPointerAxes::RawPointerAxes() {
1373 clear();
1374}
1375
1376void RawPointerAxes::clear() {
1377 x.clear();
1378 y.clear();
1379 pressure.clear();
1380 touchMajor.clear();
1381 touchMinor.clear();
1382 toolMajor.clear();
1383 toolMinor.clear();
1384 orientation.clear();
1385 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001386 tiltX.clear();
1387 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001388 trackingId.clear();
1389 slot.clear();
1390}
1391
1392
1393// --- RawPointerData ---
1394
1395RawPointerData::RawPointerData() {
1396 clear();
1397}
1398
1399void RawPointerData::clear() {
1400 pointerCount = 0;
1401 clearIdBits();
1402}
1403
1404void RawPointerData::copyFrom(const RawPointerData& other) {
1405 pointerCount = other.pointerCount;
1406 hoveringIdBits = other.hoveringIdBits;
1407 touchingIdBits = other.touchingIdBits;
1408
1409 for (uint32_t i = 0; i < pointerCount; i++) {
1410 pointers[i] = other.pointers[i];
1411
1412 int id = pointers[i].id;
1413 idToIndex[id] = other.idToIndex[id];
1414 }
1415}
1416
1417void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1418 float x = 0, y = 0;
1419 uint32_t count = touchingIdBits.count();
1420 if (count) {
1421 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1422 uint32_t id = idBits.clearFirstMarkedBit();
1423 const Pointer& pointer = pointerForId(id);
1424 x += pointer.x;
1425 y += pointer.y;
1426 }
1427 x /= count;
1428 y /= count;
1429 }
1430 *outX = x;
1431 *outY = y;
1432}
1433
1434
1435// --- CookedPointerData ---
1436
1437CookedPointerData::CookedPointerData() {
1438 clear();
1439}
1440
1441void CookedPointerData::clear() {
1442 pointerCount = 0;
1443 hoveringIdBits.clear();
1444 touchingIdBits.clear();
1445}
1446
1447void CookedPointerData::copyFrom(const CookedPointerData& other) {
1448 pointerCount = other.pointerCount;
1449 hoveringIdBits = other.hoveringIdBits;
1450 touchingIdBits = other.touchingIdBits;
1451
1452 for (uint32_t i = 0; i < pointerCount; i++) {
1453 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1454 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1455
1456 int id = pointerProperties[i].id;
1457 idToIndex[id] = other.idToIndex[id];
1458 }
1459}
1460
1461
Jeff Brown49754db2011-07-01 17:37:58 -07001462// --- SingleTouchMotionAccumulator ---
1463
1464SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1465 clearAbsoluteAxes();
1466}
1467
Jeff Brown65fd2512011-08-18 11:20:58 -07001468void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1469 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1470 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1471 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1472 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1473 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1474 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1475 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1476}
1477
Jeff Brown49754db2011-07-01 17:37:58 -07001478void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1479 mAbsX = 0;
1480 mAbsY = 0;
1481 mAbsPressure = 0;
1482 mAbsToolWidth = 0;
1483 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001484 mAbsTiltX = 0;
1485 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001486}
1487
1488void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1489 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001490 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001491 case ABS_X:
1492 mAbsX = rawEvent->value;
1493 break;
1494 case ABS_Y:
1495 mAbsY = rawEvent->value;
1496 break;
1497 case ABS_PRESSURE:
1498 mAbsPressure = rawEvent->value;
1499 break;
1500 case ABS_TOOL_WIDTH:
1501 mAbsToolWidth = rawEvent->value;
1502 break;
1503 case ABS_DISTANCE:
1504 mAbsDistance = rawEvent->value;
1505 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001506 case ABS_TILT_X:
1507 mAbsTiltX = rawEvent->value;
1508 break;
1509 case ABS_TILT_Y:
1510 mAbsTiltY = rawEvent->value;
1511 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001512 }
1513 }
1514}
1515
1516
1517// --- MultiTouchMotionAccumulator ---
1518
1519MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1520 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1521}
1522
1523MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1524 delete[] mSlots;
1525}
1526
1527void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1528 mSlotCount = slotCount;
1529 mUsingSlotsProtocol = usingSlotsProtocol;
1530
1531 delete[] mSlots;
1532 mSlots = new Slot[slotCount];
1533}
1534
Jeff Brown65fd2512011-08-18 11:20:58 -07001535void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1536 // Unfortunately there is no way to read the initial contents of the slots.
1537 // So when we reset the accumulator, we must assume they are all zeroes.
1538 if (mUsingSlotsProtocol) {
1539 // Query the driver for the current slot index and use it as the initial slot
1540 // before we start reading events from the device. It is possible that the
1541 // current slot index will not be the same as it was when the first event was
1542 // written into the evdev buffer, which means the input mapper could start
1543 // out of sync with the initial state of the events in the evdev buffer.
1544 // In the extremely unlikely case that this happens, the data from
1545 // two slots will be confused until the next ABS_MT_SLOT event is received.
1546 // This can cause the touch point to "jump", but at least there will be
1547 // no stuck touches.
1548 int32_t initialSlot;
1549 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1550 ABS_MT_SLOT, &initialSlot);
1551 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001552 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001553 initialSlot = -1;
1554 }
1555 clearSlots(initialSlot);
1556 } else {
1557 clearSlots(-1);
1558 }
1559}
1560
Jeff Brown49754db2011-07-01 17:37:58 -07001561void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001562 if (mSlots) {
1563 for (size_t i = 0; i < mSlotCount; i++) {
1564 mSlots[i].clear();
1565 }
Jeff Brown49754db2011-07-01 17:37:58 -07001566 }
1567 mCurrentSlot = initialSlot;
1568}
1569
1570void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1571 if (rawEvent->type == EV_ABS) {
1572 bool newSlot = false;
1573 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001574 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001575 mCurrentSlot = rawEvent->value;
1576 newSlot = true;
1577 }
1578 } else if (mCurrentSlot < 0) {
1579 mCurrentSlot = 0;
1580 }
1581
1582 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1583#if DEBUG_POINTERS
1584 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001585 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001586 "should be between 0 and %d; ignoring this slot.",
1587 mCurrentSlot, mSlotCount - 1);
1588 }
1589#endif
1590 } else {
1591 Slot* slot = &mSlots[mCurrentSlot];
1592
Jeff Brown49ccac52012-04-11 18:27:33 -07001593 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001594 case ABS_MT_POSITION_X:
1595 slot->mInUse = true;
1596 slot->mAbsMTPositionX = rawEvent->value;
1597 break;
1598 case ABS_MT_POSITION_Y:
1599 slot->mInUse = true;
1600 slot->mAbsMTPositionY = rawEvent->value;
1601 break;
1602 case ABS_MT_TOUCH_MAJOR:
1603 slot->mInUse = true;
1604 slot->mAbsMTTouchMajor = rawEvent->value;
1605 break;
1606 case ABS_MT_TOUCH_MINOR:
1607 slot->mInUse = true;
1608 slot->mAbsMTTouchMinor = rawEvent->value;
1609 slot->mHaveAbsMTTouchMinor = true;
1610 break;
1611 case ABS_MT_WIDTH_MAJOR:
1612 slot->mInUse = true;
1613 slot->mAbsMTWidthMajor = rawEvent->value;
1614 break;
1615 case ABS_MT_WIDTH_MINOR:
1616 slot->mInUse = true;
1617 slot->mAbsMTWidthMinor = rawEvent->value;
1618 slot->mHaveAbsMTWidthMinor = true;
1619 break;
1620 case ABS_MT_ORIENTATION:
1621 slot->mInUse = true;
1622 slot->mAbsMTOrientation = rawEvent->value;
1623 break;
1624 case ABS_MT_TRACKING_ID:
1625 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001626 // The slot is no longer in use but it retains its previous contents,
1627 // which may be reused for subsequent touches.
1628 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001629 } else {
1630 slot->mInUse = true;
1631 slot->mAbsMTTrackingId = rawEvent->value;
1632 }
1633 break;
1634 case ABS_MT_PRESSURE:
1635 slot->mInUse = true;
1636 slot->mAbsMTPressure = rawEvent->value;
1637 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001638 case ABS_MT_DISTANCE:
1639 slot->mInUse = true;
1640 slot->mAbsMTDistance = rawEvent->value;
1641 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001642 case ABS_MT_TOOL_TYPE:
1643 slot->mInUse = true;
1644 slot->mAbsMTToolType = rawEvent->value;
1645 slot->mHaveAbsMTToolType = true;
1646 break;
1647 }
1648 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001649 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001650 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1651 mCurrentSlot += 1;
1652 }
1653}
1654
Jeff Brown65fd2512011-08-18 11:20:58 -07001655void MultiTouchMotionAccumulator::finishSync() {
1656 if (!mUsingSlotsProtocol) {
1657 clearSlots(-1);
1658 }
1659}
1660
Jeff Brown49754db2011-07-01 17:37:58 -07001661
1662// --- MultiTouchMotionAccumulator::Slot ---
1663
1664MultiTouchMotionAccumulator::Slot::Slot() {
1665 clear();
1666}
1667
Jeff Brown49754db2011-07-01 17:37:58 -07001668void MultiTouchMotionAccumulator::Slot::clear() {
1669 mInUse = false;
1670 mHaveAbsMTTouchMinor = false;
1671 mHaveAbsMTWidthMinor = false;
1672 mHaveAbsMTToolType = false;
1673 mAbsMTPositionX = 0;
1674 mAbsMTPositionY = 0;
1675 mAbsMTTouchMajor = 0;
1676 mAbsMTTouchMinor = 0;
1677 mAbsMTWidthMajor = 0;
1678 mAbsMTWidthMinor = 0;
1679 mAbsMTOrientation = 0;
1680 mAbsMTTrackingId = -1;
1681 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001682 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001683 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001684}
1685
1686int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1687 if (mHaveAbsMTToolType) {
1688 switch (mAbsMTToolType) {
1689 case MT_TOOL_FINGER:
1690 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1691 case MT_TOOL_PEN:
1692 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1693 }
1694 }
1695 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1696}
1697
1698
Jeff Brown6d0fec22010-07-23 21:28:06 -07001699// --- InputMapper ---
1700
1701InputMapper::InputMapper(InputDevice* device) :
1702 mDevice(device), mContext(device->getContext()) {
1703}
1704
1705InputMapper::~InputMapper() {
1706}
1707
1708void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1709 info->addSource(getSources());
1710}
1711
Jeff Brownef3d7e82010-09-30 14:33:04 -07001712void InputMapper::dump(String8& dump) {
1713}
1714
Jeff Brown65fd2512011-08-18 11:20:58 -07001715void InputMapper::configure(nsecs_t when,
1716 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001717}
1718
Jeff Brown65fd2512011-08-18 11:20:58 -07001719void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001720}
1721
Jeff Brownaa3855d2011-03-17 01:34:19 -07001722void InputMapper::timeoutExpired(nsecs_t when) {
1723}
1724
Jeff Brown6d0fec22010-07-23 21:28:06 -07001725int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1726 return AKEY_STATE_UNKNOWN;
1727}
1728
1729int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1730 return AKEY_STATE_UNKNOWN;
1731}
1732
1733int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1734 return AKEY_STATE_UNKNOWN;
1735}
1736
1737bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1738 const int32_t* keyCodes, uint8_t* outFlags) {
1739 return false;
1740}
1741
1742int32_t InputMapper::getMetaState() {
1743 return 0;
1744}
1745
Jeff Brown05dc66a2011-03-02 14:41:58 -08001746void InputMapper::fadePointer() {
1747}
1748
Jeff Brownbe1aa822011-07-27 16:04:54 -07001749status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1750 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1751}
1752
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001753void InputMapper::bumpGeneration() {
1754 mDevice->bumpGeneration();
1755}
1756
Jeff Browncb1404e2011-01-15 18:14:15 -08001757void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1758 const RawAbsoluteAxisInfo& axis, const char* name) {
1759 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001760 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1761 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001762 } else {
1763 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1764 }
1765}
1766
Jeff Brown6d0fec22010-07-23 21:28:06 -07001767
1768// --- SwitchInputMapper ---
1769
1770SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1771 InputMapper(device) {
1772}
1773
1774SwitchInputMapper::~SwitchInputMapper() {
1775}
1776
1777uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001778 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001779}
1780
1781void SwitchInputMapper::process(const RawEvent* rawEvent) {
1782 switch (rawEvent->type) {
1783 case EV_SW:
Jeff Brown49ccac52012-04-11 18:27:33 -07001784 processSwitch(rawEvent->when, rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001785 break;
1786 }
1787}
1788
1789void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001790 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1791 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001792}
1793
1794int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1795 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1796}
1797
1798
1799// --- KeyboardInputMapper ---
1800
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001801KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001802 uint32_t source, int32_t keyboardType) :
1803 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001804 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001805}
1806
1807KeyboardInputMapper::~KeyboardInputMapper() {
1808}
1809
Jeff Brown6d0fec22010-07-23 21:28:06 -07001810uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001811 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001812}
1813
1814void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1815 InputMapper::populateDeviceInfo(info);
1816
1817 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001818 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001819}
1820
Jeff Brownef3d7e82010-09-30 14:33:04 -07001821void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001822 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1823 dumpParameters(dump);
1824 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001825 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001826 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1827 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1828 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001829}
1830
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001831
Jeff Brown65fd2512011-08-18 11:20:58 -07001832void KeyboardInputMapper::configure(nsecs_t when,
1833 const InputReaderConfiguration* config, uint32_t changes) {
1834 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001835
Jeff Brown474dcb52011-06-14 20:22:50 -07001836 if (!changes) { // first time only
1837 // Configure basic parameters.
1838 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001839 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001840
Jeff Brown65fd2512011-08-18 11:20:58 -07001841 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1842 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1843 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1844 false /*external*/, NULL, NULL, &mOrientation)) {
1845 mOrientation = DISPLAY_ORIENTATION_0;
1846 }
1847 } else {
1848 mOrientation = DISPLAY_ORIENTATION_0;
1849 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001850 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001851}
1852
1853void KeyboardInputMapper::configureParameters() {
1854 mParameters.orientationAware = false;
1855 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1856 mParameters.orientationAware);
1857
Jeff Brownbc68a592011-07-25 12:58:12 -07001858 mParameters.associatedDisplayId = -1;
1859 if (mParameters.orientationAware) {
1860 mParameters.associatedDisplayId = 0;
1861 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001862}
1863
1864void KeyboardInputMapper::dumpParameters(String8& dump) {
1865 dump.append(INDENT3 "Parameters:\n");
1866 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1867 mParameters.associatedDisplayId);
1868 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1869 toString(mParameters.orientationAware));
1870}
1871
Jeff Brown65fd2512011-08-18 11:20:58 -07001872void KeyboardInputMapper::reset(nsecs_t when) {
1873 mMetaState = AMETA_NONE;
1874 mDownTime = 0;
1875 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07001876 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001877
Jeff Brownbe1aa822011-07-27 16:04:54 -07001878 resetLedState();
1879
Jeff Brown65fd2512011-08-18 11:20:58 -07001880 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001881}
1882
1883void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1884 switch (rawEvent->type) {
1885 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07001886 int32_t scanCode = rawEvent->code;
1887 int32_t usageCode = mCurrentHidUsage;
1888 mCurrentHidUsage = 0;
1889
Jeff Brown6d0fec22010-07-23 21:28:06 -07001890 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001891 int32_t keyCode;
1892 uint32_t flags;
1893 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
1894 keyCode = AKEYCODE_UNKNOWN;
1895 flags = 0;
1896 }
1897 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001898 }
1899 break;
1900 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001901 case EV_MSC: {
1902 if (rawEvent->code == MSC_SCAN) {
1903 mCurrentHidUsage = rawEvent->value;
1904 }
1905 break;
1906 }
1907 case EV_SYN: {
1908 if (rawEvent->code == SYN_REPORT) {
1909 mCurrentHidUsage = 0;
1910 }
1911 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001912 }
1913}
1914
1915bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1916 return scanCode < BTN_MOUSE
1917 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001918 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001919 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001920}
1921
Jeff Brown6328cdc2010-07-29 18:18:33 -07001922void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1923 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001924
Jeff Brownbe1aa822011-07-27 16:04:54 -07001925 if (down) {
1926 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001927 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001928 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001929 }
Jeff Brownfe508922011-01-18 15:10:10 -08001930
Jeff Brownbe1aa822011-07-27 16:04:54 -07001931 // Add key down.
1932 ssize_t keyDownIndex = findKeyDown(scanCode);
1933 if (keyDownIndex >= 0) {
1934 // key repeat, be sure to use same keycode as before in case of rotation
1935 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001936 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001937 // key down
1938 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1939 && mContext->shouldDropVirtualKey(when,
1940 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001941 return;
1942 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001943
1944 mKeyDowns.push();
1945 KeyDown& keyDown = mKeyDowns.editTop();
1946 keyDown.keyCode = keyCode;
1947 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001948 }
1949
Jeff Brownbe1aa822011-07-27 16:04:54 -07001950 mDownTime = when;
1951 } else {
1952 // Remove key down.
1953 ssize_t keyDownIndex = findKeyDown(scanCode);
1954 if (keyDownIndex >= 0) {
1955 // key up, be sure to use same keycode as before in case of rotation
1956 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1957 mKeyDowns.removeAt(size_t(keyDownIndex));
1958 } else {
1959 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00001960 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07001961 "keyCode=%d, scanCode=%d",
1962 getDeviceName().string(), keyCode, scanCode);
1963 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001964 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001965 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001966
Jeff Brownbe1aa822011-07-27 16:04:54 -07001967 bool metaStateChanged = false;
1968 int32_t oldMetaState = mMetaState;
1969 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1970 if (oldMetaState != newMetaState) {
1971 mMetaState = newMetaState;
1972 metaStateChanged = true;
1973 updateLedState(false);
1974 }
1975
1976 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001977
Jeff Brown56194eb2011-03-02 19:23:13 -08001978 // Key down on external an keyboard should wake the device.
1979 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1980 // For internal keyboards, the key layout file should specify the policy flags for
1981 // each wake key individually.
1982 // TODO: Use the input device configuration to control this behavior more finely.
1983 if (down && getDevice()->isExternal()
1984 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1985 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1986 }
1987
Jeff Brown6328cdc2010-07-29 18:18:33 -07001988 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001989 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001990 }
1991
Jeff Brown05dc66a2011-03-02 14:41:58 -08001992 if (down && !isMetaKey(keyCode)) {
1993 getContext()->fadePointer();
1994 }
1995
Jeff Brownbe1aa822011-07-27 16:04:54 -07001996 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001997 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1998 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001999 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002000}
2001
Jeff Brownbe1aa822011-07-27 16:04:54 -07002002ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
2003 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002004 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002005 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002006 return i;
2007 }
2008 }
2009 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002010}
2011
Jeff Brown6d0fec22010-07-23 21:28:06 -07002012int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
2013 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
2014}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002015
Jeff Brown6d0fec22010-07-23 21:28:06 -07002016int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
2017 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2018}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002019
Jeff Brown6d0fec22010-07-23 21:28:06 -07002020bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
2021 const int32_t* keyCodes, uint8_t* outFlags) {
2022 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
2023}
2024
2025int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002026 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002027}
2028
Jeff Brownbe1aa822011-07-27 16:04:54 -07002029void KeyboardInputMapper::resetLedState() {
2030 initializeLedState(mCapsLockLedState, LED_CAPSL);
2031 initializeLedState(mNumLockLedState, LED_NUML);
2032 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002033
Jeff Brownbe1aa822011-07-27 16:04:54 -07002034 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002035}
2036
Jeff Brownbe1aa822011-07-27 16:04:54 -07002037void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002038 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2039 ledState.on = false;
2040}
2041
Jeff Brownbe1aa822011-07-27 16:04:54 -07002042void KeyboardInputMapper::updateLedState(bool reset) {
2043 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002044 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002045 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002046 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002047 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002048 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002049}
2050
Jeff Brownbe1aa822011-07-27 16:04:54 -07002051void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002052 int32_t led, int32_t modifier, bool reset) {
2053 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002054 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002055 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002056 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2057 ledState.on = desiredState;
2058 }
2059 }
2060}
2061
Jeff Brown6d0fec22010-07-23 21:28:06 -07002062
Jeff Brown83c09682010-12-23 17:50:18 -08002063// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002064
Jeff Brown83c09682010-12-23 17:50:18 -08002065CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002066 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002067}
2068
Jeff Brown83c09682010-12-23 17:50:18 -08002069CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002070}
2071
Jeff Brown83c09682010-12-23 17:50:18 -08002072uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002073 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002074}
2075
Jeff Brown83c09682010-12-23 17:50:18 -08002076void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002077 InputMapper::populateDeviceInfo(info);
2078
Jeff Brown83c09682010-12-23 17:50:18 -08002079 if (mParameters.mode == Parameters::MODE_POINTER) {
2080 float minX, minY, maxX, maxY;
2081 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002082 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2083 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002084 }
2085 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002086 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2087 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002088 }
Jeff Brownefd32662011-03-08 15:13:06 -08002089 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002090
Jeff Brown65fd2512011-08-18 11:20:58 -07002091 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002092 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002093 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002094 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002095 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002096 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002097}
2098
Jeff Brown83c09682010-12-23 17:50:18 -08002099void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002100 dump.append(INDENT2 "Cursor Input Mapper:\n");
2101 dumpParameters(dump);
2102 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2103 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2104 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2105 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2106 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002107 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002108 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002109 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002110 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2111 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002112 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002113 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2114 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2115 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002116}
2117
Jeff Brown65fd2512011-08-18 11:20:58 -07002118void CursorInputMapper::configure(nsecs_t when,
2119 const InputReaderConfiguration* config, uint32_t changes) {
2120 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002121
Jeff Brown474dcb52011-06-14 20:22:50 -07002122 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002123 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002124
Jeff Brown474dcb52011-06-14 20:22:50 -07002125 // Configure basic parameters.
2126 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002127
Jeff Brown474dcb52011-06-14 20:22:50 -07002128 // Configure device mode.
2129 switch (mParameters.mode) {
2130 case Parameters::MODE_POINTER:
2131 mSource = AINPUT_SOURCE_MOUSE;
2132 mXPrecision = 1.0f;
2133 mYPrecision = 1.0f;
2134 mXScale = 1.0f;
2135 mYScale = 1.0f;
2136 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2137 break;
2138 case Parameters::MODE_NAVIGATION:
2139 mSource = AINPUT_SOURCE_TRACKBALL;
2140 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2141 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2142 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2143 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2144 break;
2145 }
2146
2147 mVWheelScale = 1.0f;
2148 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002149 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002150
Jeff Brown474dcb52011-06-14 20:22:50 -07002151 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2152 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2153 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2154 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2155 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002156
2157 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2158 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2159 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2160 false /*external*/, NULL, NULL, &mOrientation)) {
2161 mOrientation = DISPLAY_ORIENTATION_0;
2162 }
2163 } else {
2164 mOrientation = DISPLAY_ORIENTATION_0;
2165 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002166 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07002167 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002168}
2169
Jeff Brown83c09682010-12-23 17:50:18 -08002170void CursorInputMapper::configureParameters() {
2171 mParameters.mode = Parameters::MODE_POINTER;
2172 String8 cursorModeString;
2173 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2174 if (cursorModeString == "navigation") {
2175 mParameters.mode = Parameters::MODE_NAVIGATION;
2176 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002177 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002178 }
2179 }
2180
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002181 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002182 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002183 mParameters.orientationAware);
2184
Jeff Brownbc68a592011-07-25 12:58:12 -07002185 mParameters.associatedDisplayId = -1;
2186 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2187 mParameters.associatedDisplayId = 0;
2188 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002189}
2190
Jeff Brown83c09682010-12-23 17:50:18 -08002191void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002192 dump.append(INDENT3 "Parameters:\n");
2193 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2194 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002195
2196 switch (mParameters.mode) {
2197 case Parameters::MODE_POINTER:
2198 dump.append(INDENT4 "Mode: pointer\n");
2199 break;
2200 case Parameters::MODE_NAVIGATION:
2201 dump.append(INDENT4 "Mode: navigation\n");
2202 break;
2203 default:
Steve Blockec193de2012-01-09 18:35:44 +00002204 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002205 }
2206
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002207 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2208 toString(mParameters.orientationAware));
2209}
2210
Jeff Brown65fd2512011-08-18 11:20:58 -07002211void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002212 mButtonState = 0;
2213 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002214
Jeff Brownbe1aa822011-07-27 16:04:54 -07002215 mPointerVelocityControl.reset();
2216 mWheelXVelocityControl.reset();
2217 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002218
Jeff Brown65fd2512011-08-18 11:20:58 -07002219 mCursorButtonAccumulator.reset(getDevice());
2220 mCursorMotionAccumulator.reset(getDevice());
2221 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002222
Jeff Brown65fd2512011-08-18 11:20:58 -07002223 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002224}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002225
Jeff Brown83c09682010-12-23 17:50:18 -08002226void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002227 mCursorButtonAccumulator.process(rawEvent);
2228 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002229 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002230
Jeff Brown49ccac52012-04-11 18:27:33 -07002231 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002232 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002233 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002234}
2235
Jeff Brown83c09682010-12-23 17:50:18 -08002236void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002237 int32_t lastButtonState = mButtonState;
2238 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2239 mButtonState = currentButtonState;
2240
2241 bool wasDown = isPointerDown(lastButtonState);
2242 bool down = isPointerDown(currentButtonState);
2243 bool downChanged;
2244 if (!wasDown && down) {
2245 mDownTime = when;
2246 downChanged = true;
2247 } else if (wasDown && !down) {
2248 downChanged = true;
2249 } else {
2250 downChanged = false;
2251 }
2252 nsecs_t downTime = mDownTime;
2253 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002254 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002255
2256 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2257 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2258 bool moved = deltaX != 0 || deltaY != 0;
2259
Jeff Brown65fd2512011-08-18 11:20:58 -07002260 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002261 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2262 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002263 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002264 }
2265
Jeff Brown65fd2512011-08-18 11:20:58 -07002266 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002267 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002268 pointerProperties.clear();
2269 pointerProperties.id = 0;
2270 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2271
Jeff Brown6328cdc2010-07-29 18:18:33 -07002272 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002273 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002274
Jeff Brown65fd2512011-08-18 11:20:58 -07002275 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2276 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002277 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002278
Jeff Brownbe1aa822011-07-27 16:04:54 -07002279 mWheelYVelocityControl.move(when, NULL, &vscroll);
2280 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002281
Jeff Brownbe1aa822011-07-27 16:04:54 -07002282 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002283
Jeff Brownbe1aa822011-07-27 16:04:54 -07002284 if (mPointerController != NULL) {
2285 if (moved || scrolled || buttonsChanged) {
2286 mPointerController->setPresentation(
2287 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002288
Jeff Brownbe1aa822011-07-27 16:04:54 -07002289 if (moved) {
2290 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002291 }
2292
Jeff Brownbe1aa822011-07-27 16:04:54 -07002293 if (buttonsChanged) {
2294 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002295 }
Jeff Brownefd32662011-03-08 15:13:06 -08002296
Jeff Brownbe1aa822011-07-27 16:04:54 -07002297 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002298 }
2299
Jeff Brownbe1aa822011-07-27 16:04:54 -07002300 float x, y;
2301 mPointerController->getPosition(&x, &y);
2302 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2303 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2304 } else {
2305 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2306 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2307 }
2308
2309 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002310
Jeff Brown56194eb2011-03-02 19:23:13 -08002311 // Moving an external trackball or mouse should wake the device.
2312 // We don't do this for internal cursor devices to prevent them from waking up
2313 // the device in your pocket.
2314 // TODO: Use the input device configuration to control this behavior more finely.
2315 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002316 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002317 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2318 }
2319
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002320 // Synthesize key down from buttons if needed.
2321 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2322 policyFlags, lastButtonState, currentButtonState);
2323
2324 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002325 if (downChanged || moved || scrolled || buttonsChanged) {
2326 int32_t metaState = mContext->getGlobalMetaState();
2327 int32_t motionEventAction;
2328 if (downChanged) {
2329 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2330 } else if (down || mPointerController == NULL) {
2331 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2332 } else {
2333 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2334 }
Jeff Brownb6997262010-10-08 22:31:17 -07002335
Jeff Brownbe1aa822011-07-27 16:04:54 -07002336 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2337 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002338 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002339 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002340
Jeff Brownbe1aa822011-07-27 16:04:54 -07002341 // Send hover move after UP to tell the application that the mouse is hovering now.
2342 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2343 && mPointerController != NULL) {
2344 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2345 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2346 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2347 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2348 getListener()->notifyMotion(&hoverArgs);
2349 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002350
Jeff Brownbe1aa822011-07-27 16:04:54 -07002351 // Send scroll events.
2352 if (scrolled) {
2353 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2354 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2355
2356 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2357 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2358 AMOTION_EVENT_EDGE_FLAG_NONE,
2359 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2360 getListener()->notifyMotion(&scrollArgs);
2361 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002362 }
Jeff Browna032cc02011-03-07 16:56:21 -08002363
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002364 // Synthesize key up from buttons if needed.
2365 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2366 policyFlags, lastButtonState, currentButtonState);
2367
Jeff Brown65fd2512011-08-18 11:20:58 -07002368 mCursorMotionAccumulator.finishSync();
2369 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002370}
2371
Jeff Brown83c09682010-12-23 17:50:18 -08002372int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002373 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2374 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2375 } else {
2376 return AKEY_STATE_UNKNOWN;
2377 }
2378}
2379
Jeff Brown05dc66a2011-03-02 14:41:58 -08002380void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002381 if (mPointerController != NULL) {
2382 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2383 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002384}
2385
Jeff Brown6d0fec22010-07-23 21:28:06 -07002386
2387// --- TouchInputMapper ---
2388
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002389TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002390 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002391 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002392 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002393}
2394
2395TouchInputMapper::~TouchInputMapper() {
2396}
2397
2398uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002399 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002400}
2401
2402void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2403 InputMapper::populateDeviceInfo(info);
2404
Jeff Brown65fd2512011-08-18 11:20:58 -07002405 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2406 info->addMotionRange(mOrientedRanges.x);
2407 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002408 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002409
Jeff Brown65fd2512011-08-18 11:20:58 -07002410 if (mOrientedRanges.haveSize) {
2411 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002412 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002413
2414 if (mOrientedRanges.haveTouchSize) {
2415 info->addMotionRange(mOrientedRanges.touchMajor);
2416 info->addMotionRange(mOrientedRanges.touchMinor);
2417 }
2418
2419 if (mOrientedRanges.haveToolSize) {
2420 info->addMotionRange(mOrientedRanges.toolMajor);
2421 info->addMotionRange(mOrientedRanges.toolMinor);
2422 }
2423
2424 if (mOrientedRanges.haveOrientation) {
2425 info->addMotionRange(mOrientedRanges.orientation);
2426 }
2427
2428 if (mOrientedRanges.haveDistance) {
2429 info->addMotionRange(mOrientedRanges.distance);
2430 }
2431
2432 if (mOrientedRanges.haveTilt) {
2433 info->addMotionRange(mOrientedRanges.tilt);
2434 }
2435
2436 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2437 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2438 }
2439 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2440 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2441 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002442 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002443}
2444
Jeff Brownef3d7e82010-09-30 14:33:04 -07002445void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002446 dump.append(INDENT2 "Touch Input Mapper:\n");
2447 dumpParameters(dump);
2448 dumpVirtualKeys(dump);
2449 dumpRawPointerAxes(dump);
2450 dumpCalibration(dump);
2451 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002452
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2454 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2455 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2456 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2457 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2458 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2460 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002461 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002462 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2463 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002464 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2465 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2466 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2467 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2468 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002469
Jeff Brownbe1aa822011-07-27 16:04:54 -07002470 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002471
Jeff Brownbe1aa822011-07-27 16:04:54 -07002472 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2473 mLastRawPointerData.pointerCount);
2474 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2475 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2476 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2477 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002478 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2479 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002480 pointer.id, pointer.x, pointer.y, pointer.pressure,
2481 pointer.touchMajor, pointer.touchMinor,
2482 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002483 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002484 pointer.toolType, toString(pointer.isHovering));
2485 }
2486
2487 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2488 mLastCookedPointerData.pointerCount);
2489 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2490 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2491 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2492 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2493 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002494 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2495 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002496 pointerProperties.id,
2497 pointerCoords.getX(),
2498 pointerCoords.getY(),
2499 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2500 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2501 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2502 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2503 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2504 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002505 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002506 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2507 pointerProperties.toolType,
2508 toString(mLastCookedPointerData.isHovering(i)));
2509 }
2510
Jeff Brown65fd2512011-08-18 11:20:58 -07002511 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002512 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2513 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002514 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002515 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002516 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002517 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002518 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002519 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002520 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002521 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2522 mPointerGestureMaxSwipeWidth);
2523 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002524}
2525
Jeff Brown65fd2512011-08-18 11:20:58 -07002526void TouchInputMapper::configure(nsecs_t when,
2527 const InputReaderConfiguration* config, uint32_t changes) {
2528 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002529
Jeff Brown474dcb52011-06-14 20:22:50 -07002530 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002531
Jeff Brown474dcb52011-06-14 20:22:50 -07002532 if (!changes) { // first time only
2533 // Configure basic parameters.
2534 configureParameters();
2535
Jeff Brown65fd2512011-08-18 11:20:58 -07002536 // Configure common accumulators.
2537 mCursorScrollAccumulator.configure(getDevice());
2538 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002539
2540 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002541 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002542
2543 // Prepare input device calibration.
2544 parseCalibration();
2545 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002546 }
2547
Jeff Brown474dcb52011-06-14 20:22:50 -07002548 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002549 // Update pointer speed.
2550 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2551 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2552 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002553 }
Jeff Brown8d608662010-08-30 03:02:23 -07002554
Jeff Brown65fd2512011-08-18 11:20:58 -07002555 bool resetNeeded = false;
2556 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002557 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2558 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002559 // Configure device sources, surface dimensions, orientation and
2560 // scaling factors.
2561 configureSurface(when, &resetNeeded);
2562 }
2563
2564 if (changes && resetNeeded) {
2565 // Send reset, unless this is the first time the device has been configured,
2566 // in which case the reader will call reset itself after all mappers are ready.
2567 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002568 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002569}
2570
Jeff Brown8d608662010-08-30 03:02:23 -07002571void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002572 // Use the pointer presentation mode for devices that do not support distinct
2573 // multitouch. The spot-based presentation relies on being able to accurately
2574 // locate two or more fingers on the touch pad.
2575 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2576 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002577
Jeff Brown538881e2011-05-25 18:23:38 -07002578 String8 gestureModeString;
2579 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2580 gestureModeString)) {
2581 if (gestureModeString == "pointer") {
2582 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2583 } else if (gestureModeString == "spots") {
2584 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2585 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002586 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002587 }
2588 }
2589
Jeff Browndeffe072011-08-26 18:38:46 -07002590 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2591 // The device is a touch screen.
2592 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2593 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2594 // The device is a pointing device like a track pad.
2595 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2596 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002597 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2598 // The device is a cursor device with a touch pad attached.
2599 // By default don't use the touch pad to move the pointer.
2600 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2601 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002602 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002603 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2604 }
2605
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002606 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002607 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2608 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002609 if (deviceTypeString == "touchScreen") {
2610 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002611 } else if (deviceTypeString == "touchPad") {
2612 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002613 } else if (deviceTypeString == "pointer") {
2614 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002615 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002616 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002617 }
2618 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002619
Jeff Brownefd32662011-03-08 15:13:06 -08002620 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002621 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2622 mParameters.orientationAware);
2623
Jeff Brownbc68a592011-07-25 12:58:12 -07002624 mParameters.associatedDisplayId = -1;
2625 mParameters.associatedDisplayIsExternal = false;
2626 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002627 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002628 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2629 mParameters.associatedDisplayIsExternal =
2630 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2631 && getDevice()->isExternal();
2632 mParameters.associatedDisplayId = 0;
2633 }
Jeff Brown8d608662010-08-30 03:02:23 -07002634}
2635
Jeff Brownef3d7e82010-09-30 14:33:04 -07002636void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002637 dump.append(INDENT3 "Parameters:\n");
2638
Jeff Brown538881e2011-05-25 18:23:38 -07002639 switch (mParameters.gestureMode) {
2640 case Parameters::GESTURE_MODE_POINTER:
2641 dump.append(INDENT4 "GestureMode: pointer\n");
2642 break;
2643 case Parameters::GESTURE_MODE_SPOTS:
2644 dump.append(INDENT4 "GestureMode: spots\n");
2645 break;
2646 default:
2647 assert(false);
2648 }
2649
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002650 switch (mParameters.deviceType) {
2651 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2652 dump.append(INDENT4 "DeviceType: touchScreen\n");
2653 break;
2654 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2655 dump.append(INDENT4 "DeviceType: touchPad\n");
2656 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002657 case Parameters::DEVICE_TYPE_POINTER:
2658 dump.append(INDENT4 "DeviceType: pointer\n");
2659 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002660 default:
Steve Blockec193de2012-01-09 18:35:44 +00002661 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002662 }
2663
Jeff Brown65fd2512011-08-18 11:20:58 -07002664 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2665 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002666 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2667 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002668}
2669
Jeff Brownbe1aa822011-07-27 16:04:54 -07002670void TouchInputMapper::configureRawPointerAxes() {
2671 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002672}
2673
Jeff Brownbe1aa822011-07-27 16:04:54 -07002674void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2675 dump.append(INDENT3 "Raw Touch Axes:\n");
2676 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2677 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2678 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2679 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2680 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2681 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2682 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2683 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2684 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002685 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2686 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002687 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2688 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002689}
2690
Jeff Brown65fd2512011-08-18 11:20:58 -07002691void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2692 int32_t oldDeviceMode = mDeviceMode;
2693
2694 // Determine device mode.
2695 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2696 && mConfig.pointerGesturesEnabled) {
2697 mSource = AINPUT_SOURCE_MOUSE;
2698 mDeviceMode = DEVICE_MODE_POINTER;
2699 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2700 && mParameters.associatedDisplayId >= 0) {
2701 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2702 mDeviceMode = DEVICE_MODE_DIRECT;
2703 } else {
2704 mSource = AINPUT_SOURCE_TOUCHPAD;
2705 mDeviceMode = DEVICE_MODE_UNSCALED;
2706 }
2707
Jeff Brown9626b142011-03-03 02:09:54 -08002708 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002709 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002710 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002711 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002712 mDeviceMode = DEVICE_MODE_DISABLED;
2713 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002714 }
2715
Jeff Brown65fd2512011-08-18 11:20:58 -07002716 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002717 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002718 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002719 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002720 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2721 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002722 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002723 "display %d. The device will be inoperable until the display size "
2724 "becomes available.",
2725 getDeviceName().string(), mParameters.associatedDisplayId);
2726 mDeviceMode = DEVICE_MODE_DISABLED;
2727 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002728 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002729 }
2730
Jeff Brown65fd2512011-08-18 11:20:58 -07002731 // Configure dimensions.
2732 int32_t width, height, orientation;
2733 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2734 width = mAssociatedDisplayWidth;
2735 height = mAssociatedDisplayHeight;
2736 orientation = mParameters.orientationAware ?
2737 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2738 } else {
2739 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2740 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2741 orientation = DISPLAY_ORIENTATION_0;
2742 }
2743
2744 // If moving between pointer modes, need to reset some state.
2745 bool deviceModeChanged;
2746 if (mDeviceMode != oldDeviceMode) {
2747 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002748 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002749 }
2750
Jeff Browndaf4a122011-08-26 17:14:14 -07002751 // Create pointer controller if needed.
2752 if (mDeviceMode == DEVICE_MODE_POINTER ||
2753 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2754 if (mPointerController == NULL) {
2755 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2756 }
2757 } else {
2758 mPointerController.clear();
2759 }
2760
Jeff Brownbe1aa822011-07-27 16:04:54 -07002761 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002762 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002763 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002764 }
2765
Jeff Brownbe1aa822011-07-27 16:04:54 -07002766 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002767 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002768 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002769 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002770
Jeff Brownbe1aa822011-07-27 16:04:54 -07002771 mSurfaceWidth = width;
2772 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002773
Jeff Brown8d608662010-08-30 03:02:23 -07002774 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002775 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2776 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2777 mXPrecision = 1.0f / mXScale;
2778 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002779
Jeff Brownbe1aa822011-07-27 16:04:54 -07002780 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002781 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002782 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002783 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002784
Jeff Brownbe1aa822011-07-27 16:04:54 -07002785 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002786
Jeff Brown8d608662010-08-30 03:02:23 -07002787 // Scale factor for terms that are not oriented in a particular axis.
2788 // If the pixels are square then xScale == yScale otherwise we fake it
2789 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002790 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002791
Jeff Brown8d608662010-08-30 03:02:23 -07002792 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002793 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002794
Jeff Browna1f89ce2011-08-11 00:05:01 -07002795 // Size factors.
2796 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2797 if (mRawPointerAxes.touchMajor.valid
2798 && mRawPointerAxes.touchMajor.maxValue != 0) {
2799 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2800 } else if (mRawPointerAxes.toolMajor.valid
2801 && mRawPointerAxes.toolMajor.maxValue != 0) {
2802 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2803 } else {
2804 mSizeScale = 0.0f;
2805 }
2806
Jeff Brownbe1aa822011-07-27 16:04:54 -07002807 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002808 mOrientedRanges.haveToolSize = true;
2809 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002810
Jeff Brownbe1aa822011-07-27 16:04:54 -07002811 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002812 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002813 mOrientedRanges.touchMajor.min = 0;
2814 mOrientedRanges.touchMajor.max = diagonalSize;
2815 mOrientedRanges.touchMajor.flat = 0;
2816 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002817
Jeff Brownbe1aa822011-07-27 16:04:54 -07002818 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2819 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002820
Jeff Brownbe1aa822011-07-27 16:04:54 -07002821 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002822 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002823 mOrientedRanges.toolMajor.min = 0;
2824 mOrientedRanges.toolMajor.max = diagonalSize;
2825 mOrientedRanges.toolMajor.flat = 0;
2826 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002827
Jeff Brownbe1aa822011-07-27 16:04:54 -07002828 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2829 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002830
2831 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002832 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002833 mOrientedRanges.size.min = 0;
2834 mOrientedRanges.size.max = 1.0;
2835 mOrientedRanges.size.flat = 0;
2836 mOrientedRanges.size.fuzz = 0;
2837 } else {
2838 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002839 }
2840
2841 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002842 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002843 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2844 || mCalibration.pressureCalibration
2845 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2846 if (mCalibration.havePressureScale) {
2847 mPressureScale = mCalibration.pressureScale;
2848 } else if (mRawPointerAxes.pressure.valid
2849 && mRawPointerAxes.pressure.maxValue != 0) {
2850 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002851 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002852 }
Jeff Brown8d608662010-08-30 03:02:23 -07002853
Jeff Brown65fd2512011-08-18 11:20:58 -07002854 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2855 mOrientedRanges.pressure.source = mSource;
2856 mOrientedRanges.pressure.min = 0;
2857 mOrientedRanges.pressure.max = 1.0;
2858 mOrientedRanges.pressure.flat = 0;
2859 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002860
Jeff Brown65fd2512011-08-18 11:20:58 -07002861 // Tilt
2862 mTiltXCenter = 0;
2863 mTiltXScale = 0;
2864 mTiltYCenter = 0;
2865 mTiltYScale = 0;
2866 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2867 if (mHaveTilt) {
2868 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2869 mRawPointerAxes.tiltX.maxValue);
2870 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2871 mRawPointerAxes.tiltY.maxValue);
2872 mTiltXScale = M_PI / 180;
2873 mTiltYScale = M_PI / 180;
2874
2875 mOrientedRanges.haveTilt = true;
2876
2877 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2878 mOrientedRanges.tilt.source = mSource;
2879 mOrientedRanges.tilt.min = 0;
2880 mOrientedRanges.tilt.max = M_PI_2;
2881 mOrientedRanges.tilt.flat = 0;
2882 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002883 }
2884
Jeff Brown8d608662010-08-30 03:02:23 -07002885 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002886 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002887 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002888 if (mHaveTilt) {
2889 mOrientedRanges.haveOrientation = true;
2890
2891 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2892 mOrientedRanges.orientation.source = mSource;
2893 mOrientedRanges.orientation.min = -M_PI;
2894 mOrientedRanges.orientation.max = M_PI;
2895 mOrientedRanges.orientation.flat = 0;
2896 mOrientedRanges.orientation.fuzz = 0;
2897 } else if (mCalibration.orientationCalibration !=
2898 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002899 if (mCalibration.orientationCalibration
2900 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002901 if (mRawPointerAxes.orientation.valid) {
2902 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2903 mRawPointerAxes.orientation.maxValue);
2904 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2905 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002906 }
2907 }
2908
Jeff Brownbe1aa822011-07-27 16:04:54 -07002909 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002910
Jeff Brownbe1aa822011-07-27 16:04:54 -07002911 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002912 mOrientedRanges.orientation.source = mSource;
2913 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002914 mOrientedRanges.orientation.max = M_PI_2;
2915 mOrientedRanges.orientation.flat = 0;
2916 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002917 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002918
2919 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002920 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002921 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2922 if (mCalibration.distanceCalibration
2923 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2924 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002925 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002926 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002927 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002928 }
2929 }
2930
Jeff Brownbe1aa822011-07-27 16:04:54 -07002931 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002932
Jeff Brownbe1aa822011-07-27 16:04:54 -07002933 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002934 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002935 mOrientedRanges.distance.min =
2936 mRawPointerAxes.distance.minValue * mDistanceScale;
2937 mOrientedRanges.distance.max =
2938 mRawPointerAxes.distance.minValue * mDistanceScale;
2939 mOrientedRanges.distance.flat = 0;
2940 mOrientedRanges.distance.fuzz =
2941 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002942 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002943 }
2944
Jeff Brown65fd2512011-08-18 11:20:58 -07002945 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002946 // Compute oriented surface dimensions, precision, scales and ranges.
2947 // Note that the maximum value reported is an inclusive maximum value so it is one
2948 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002949 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002950 case DISPLAY_ORIENTATION_90:
2951 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002952 mOrientedSurfaceWidth = mSurfaceHeight;
2953 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002954
Jeff Brownbe1aa822011-07-27 16:04:54 -07002955 mOrientedXPrecision = mYPrecision;
2956 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002957
Jeff Brownbe1aa822011-07-27 16:04:54 -07002958 mOrientedRanges.x.min = 0;
2959 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2960 * mYScale;
2961 mOrientedRanges.x.flat = 0;
2962 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002963
Jeff Brownbe1aa822011-07-27 16:04:54 -07002964 mOrientedRanges.y.min = 0;
2965 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2966 * mXScale;
2967 mOrientedRanges.y.flat = 0;
2968 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002969 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002970
Jeff Brown6d0fec22010-07-23 21:28:06 -07002971 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002972 mOrientedSurfaceWidth = mSurfaceWidth;
2973 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002974
Jeff Brownbe1aa822011-07-27 16:04:54 -07002975 mOrientedXPrecision = mXPrecision;
2976 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002977
Jeff Brownbe1aa822011-07-27 16:04:54 -07002978 mOrientedRanges.x.min = 0;
2979 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2980 * mXScale;
2981 mOrientedRanges.x.flat = 0;
2982 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002983
Jeff Brownbe1aa822011-07-27 16:04:54 -07002984 mOrientedRanges.y.min = 0;
2985 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2986 * mYScale;
2987 mOrientedRanges.y.flat = 0;
2988 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002989 break;
2990 }
Jeff Brownace13b12011-03-09 17:39:48 -08002991
2992 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002993 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002994 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2995 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002996 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002997 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2998 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002999
Jeff Brown2352b972011-04-12 22:39:53 -07003000 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07003001 // given area relative to the diagonal size of the display when no acceleration
3002 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08003003 // Assume that the touch pad has a square aspect ratio such that movements in
3004 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07003005 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003006 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003007 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003008
3009 // Scale zooms to cover a smaller range of the display than movements do.
3010 // This value determines the area around the pointer that is affected by freeform
3011 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07003012 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07003013 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07003014 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003015
Jeff Brown2352b972011-04-12 22:39:53 -07003016 // Max width between pointers to detect a swipe gesture is more than some fraction
3017 // of the diagonal axis of the touch pad. Touches that are wider than this are
3018 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003019 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07003020 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08003021 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003022
Jeff Brown65fd2512011-08-18 11:20:58 -07003023 // Abort current pointer usages because the state has changed.
3024 abortPointerUsage(when, 0 /*policyFlags*/);
3025
3026 // Inform the dispatcher about the changes.
3027 *outResetNeeded = true;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07003028 bumpGeneration();
Jeff Brown65fd2512011-08-18 11:20:58 -07003029 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003030}
3031
Jeff Brownbe1aa822011-07-27 16:04:54 -07003032void TouchInputMapper::dumpSurface(String8& dump) {
3033 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3034 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
3035 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003036}
3037
Jeff Brownbe1aa822011-07-27 16:04:54 -07003038void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003039 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003040 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003041
Jeff Brownbe1aa822011-07-27 16:04:54 -07003042 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003043
Jeff Brown6328cdc2010-07-29 18:18:33 -07003044 if (virtualKeyDefinitions.size() == 0) {
3045 return;
3046 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003047
Jeff Brownbe1aa822011-07-27 16:04:54 -07003048 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003049
Jeff Brownbe1aa822011-07-27 16:04:54 -07003050 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3051 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3052 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3053 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003054
3055 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003056 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003057 virtualKeyDefinitions[i];
3058
Jeff Brownbe1aa822011-07-27 16:04:54 -07003059 mVirtualKeys.add();
3060 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003061
3062 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3063 int32_t keyCode;
3064 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003065 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003066 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003067 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003068 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003069 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003070 }
3071
Jeff Brown6328cdc2010-07-29 18:18:33 -07003072 virtualKey.keyCode = keyCode;
3073 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003074
Jeff Brown6328cdc2010-07-29 18:18:33 -07003075 // convert the key definition's display coordinates into touch coordinates for a hit box
3076 int32_t halfWidth = virtualKeyDefinition.width / 2;
3077 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003078
Jeff Brown6328cdc2010-07-29 18:18:33 -07003079 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003080 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003081 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003082 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003083 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003084 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003085 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003086 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003087 }
3088}
3089
Jeff Brownbe1aa822011-07-27 16:04:54 -07003090void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3091 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003092 dump.append(INDENT3 "Virtual Keys:\n");
3093
Jeff Brownbe1aa822011-07-27 16:04:54 -07003094 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3095 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003096 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3097 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3098 i, virtualKey.scanCode, virtualKey.keyCode,
3099 virtualKey.hitLeft, virtualKey.hitRight,
3100 virtualKey.hitTop, virtualKey.hitBottom);
3101 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003102 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003103}
3104
Jeff Brown8d608662010-08-30 03:02:23 -07003105void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003106 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003107 Calibration& out = mCalibration;
3108
Jeff Browna1f89ce2011-08-11 00:05:01 -07003109 // Size
3110 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3111 String8 sizeCalibrationString;
3112 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3113 if (sizeCalibrationString == "none") {
3114 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3115 } else if (sizeCalibrationString == "geometric") {
3116 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3117 } else if (sizeCalibrationString == "diameter") {
3118 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3119 } else if (sizeCalibrationString == "area") {
3120 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3121 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003122 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003123 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003124 }
3125 }
3126
Jeff Browna1f89ce2011-08-11 00:05:01 -07003127 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3128 out.sizeScale);
3129 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3130 out.sizeBias);
3131 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3132 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003133
3134 // Pressure
3135 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3136 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003137 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003138 if (pressureCalibrationString == "none") {
3139 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3140 } else if (pressureCalibrationString == "physical") {
3141 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3142 } else if (pressureCalibrationString == "amplitude") {
3143 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3144 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003145 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003146 pressureCalibrationString.string());
3147 }
3148 }
3149
Jeff Brown8d608662010-08-30 03:02:23 -07003150 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3151 out.pressureScale);
3152
Jeff Brown8d608662010-08-30 03:02:23 -07003153 // Orientation
3154 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3155 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003156 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003157 if (orientationCalibrationString == "none") {
3158 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3159 } else if (orientationCalibrationString == "interpolated") {
3160 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003161 } else if (orientationCalibrationString == "vector") {
3162 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003163 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003164 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003165 orientationCalibrationString.string());
3166 }
3167 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003168
3169 // Distance
3170 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3171 String8 distanceCalibrationString;
3172 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3173 if (distanceCalibrationString == "none") {
3174 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3175 } else if (distanceCalibrationString == "scaled") {
3176 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3177 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003178 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003179 distanceCalibrationString.string());
3180 }
3181 }
3182
3183 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3184 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003185}
3186
3187void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003188 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003189 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3190 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3191 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003192 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003193 } else {
3194 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3195 }
Jeff Brown8d608662010-08-30 03:02:23 -07003196
Jeff Browna1f89ce2011-08-11 00:05:01 -07003197 // Pressure
3198 if (mRawPointerAxes.pressure.valid) {
3199 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3200 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3201 }
3202 } else {
3203 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003204 }
3205
3206 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003207 if (mRawPointerAxes.orientation.valid) {
3208 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003209 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003210 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003211 } else {
3212 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003213 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003214
3215 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003216 if (mRawPointerAxes.distance.valid) {
3217 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003218 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003219 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003220 } else {
3221 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003222 }
Jeff Brown8d608662010-08-30 03:02:23 -07003223}
3224
Jeff Brownef3d7e82010-09-30 14:33:04 -07003225void TouchInputMapper::dumpCalibration(String8& dump) {
3226 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003227
Jeff Browna1f89ce2011-08-11 00:05:01 -07003228 // Size
3229 switch (mCalibration.sizeCalibration) {
3230 case Calibration::SIZE_CALIBRATION_NONE:
3231 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003232 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003233 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3234 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003235 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003236 case Calibration::SIZE_CALIBRATION_DIAMETER:
3237 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3238 break;
3239 case Calibration::SIZE_CALIBRATION_AREA:
3240 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003241 break;
3242 default:
Steve Blockec193de2012-01-09 18:35:44 +00003243 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003244 }
3245
Jeff Browna1f89ce2011-08-11 00:05:01 -07003246 if (mCalibration.haveSizeScale) {
3247 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3248 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003249 }
3250
Jeff Browna1f89ce2011-08-11 00:05:01 -07003251 if (mCalibration.haveSizeBias) {
3252 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3253 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003254 }
3255
Jeff Browna1f89ce2011-08-11 00:05:01 -07003256 if (mCalibration.haveSizeIsSummed) {
3257 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3258 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003259 }
3260
3261 // Pressure
3262 switch (mCalibration.pressureCalibration) {
3263 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003264 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003265 break;
3266 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003267 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003268 break;
3269 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003270 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003271 break;
3272 default:
Steve Blockec193de2012-01-09 18:35:44 +00003273 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003274 }
3275
Jeff Brown8d608662010-08-30 03:02:23 -07003276 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003277 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3278 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003279 }
3280
Jeff Brown8d608662010-08-30 03:02:23 -07003281 // Orientation
3282 switch (mCalibration.orientationCalibration) {
3283 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003284 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003285 break;
3286 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003287 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003288 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003289 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3290 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3291 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003292 default:
Steve Blockec193de2012-01-09 18:35:44 +00003293 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003294 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003295
3296 // Distance
3297 switch (mCalibration.distanceCalibration) {
3298 case Calibration::DISTANCE_CALIBRATION_NONE:
3299 dump.append(INDENT4 "touch.distance.calibration: none\n");
3300 break;
3301 case Calibration::DISTANCE_CALIBRATION_SCALED:
3302 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3303 break;
3304 default:
Steve Blockec193de2012-01-09 18:35:44 +00003305 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003306 }
3307
3308 if (mCalibration.haveDistanceScale) {
3309 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3310 mCalibration.distanceScale);
3311 }
Jeff Brown8d608662010-08-30 03:02:23 -07003312}
3313
Jeff Brown65fd2512011-08-18 11:20:58 -07003314void TouchInputMapper::reset(nsecs_t when) {
3315 mCursorButtonAccumulator.reset(getDevice());
3316 mCursorScrollAccumulator.reset(getDevice());
3317 mTouchButtonAccumulator.reset(getDevice());
3318
3319 mPointerVelocityControl.reset();
3320 mWheelXVelocityControl.reset();
3321 mWheelYVelocityControl.reset();
3322
Jeff Brownbe1aa822011-07-27 16:04:54 -07003323 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003324 mLastRawPointerData.clear();
3325 mCurrentCookedPointerData.clear();
3326 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003327 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003328 mLastButtonState = 0;
3329 mCurrentRawVScroll = 0;
3330 mCurrentRawHScroll = 0;
3331 mCurrentFingerIdBits.clear();
3332 mLastFingerIdBits.clear();
3333 mCurrentStylusIdBits.clear();
3334 mLastStylusIdBits.clear();
3335 mCurrentMouseIdBits.clear();
3336 mLastMouseIdBits.clear();
3337 mPointerUsage = POINTER_USAGE_NONE;
3338 mSentHoverEnter = false;
3339 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003340
Jeff Brown65fd2512011-08-18 11:20:58 -07003341 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003342
Jeff Brown65fd2512011-08-18 11:20:58 -07003343 mPointerGesture.reset();
3344 mPointerSimple.reset();
3345
3346 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003347 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3348 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003349 }
3350
Jeff Brown65fd2512011-08-18 11:20:58 -07003351 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003352}
3353
Jeff Brown65fd2512011-08-18 11:20:58 -07003354void TouchInputMapper::process(const RawEvent* rawEvent) {
3355 mCursorButtonAccumulator.process(rawEvent);
3356 mCursorScrollAccumulator.process(rawEvent);
3357 mTouchButtonAccumulator.process(rawEvent);
3358
Jeff Brown49ccac52012-04-11 18:27:33 -07003359 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003360 sync(rawEvent->when);
3361 }
3362}
3363
3364void TouchInputMapper::sync(nsecs_t when) {
3365 // Sync button state.
3366 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3367 | mCursorButtonAccumulator.getButtonState();
3368
3369 // Sync scroll state.
3370 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3371 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3372 mCursorScrollAccumulator.finishSync();
3373
3374 // Sync touch state.
3375 bool havePointerIds = true;
3376 mCurrentRawPointerData.clear();
3377 syncTouch(when, &havePointerIds);
3378
Jeff Brownaa3855d2011-03-17 01:34:19 -07003379#if DEBUG_RAW_EVENTS
3380 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003381 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003382 mLastRawPointerData.pointerCount,
3383 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003384 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003385 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003386 "hovering ids 0x%08x -> 0x%08x",
3387 mLastRawPointerData.pointerCount,
3388 mCurrentRawPointerData.pointerCount,
3389 mLastRawPointerData.touchingIdBits.value,
3390 mCurrentRawPointerData.touchingIdBits.value,
3391 mLastRawPointerData.hoveringIdBits.value,
3392 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003393 }
3394#endif
3395
Jeff Brown65fd2512011-08-18 11:20:58 -07003396 // Reset state that we will compute below.
3397 mCurrentFingerIdBits.clear();
3398 mCurrentStylusIdBits.clear();
3399 mCurrentMouseIdBits.clear();
3400 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003401
Jeff Brown65fd2512011-08-18 11:20:58 -07003402 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3403 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003404 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003405 mCurrentButtonState = 0;
3406 } else {
3407 // Preprocess pointer data.
3408 if (!havePointerIds) {
3409 assignPointerIds();
3410 }
3411
3412 // Handle policy on initial down or hover events.
3413 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003414 bool initialDown = mLastRawPointerData.pointerCount == 0
3415 && mCurrentRawPointerData.pointerCount != 0;
3416 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3417 if (initialDown || buttonsPressed) {
3418 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003419 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003420 getContext()->fadePointer();
3421 }
3422
3423 // Initial downs on external touch devices should wake the device.
3424 // We don't do this for internal touch screens to prevent them from waking
3425 // up in your pocket.
3426 // TODO: Use the input device configuration to control this behavior more finely.
3427 if (getDevice()->isExternal()) {
3428 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3429 }
3430 }
3431
3432 // Synthesize key down from raw buttons if needed.
3433 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3434 policyFlags, mLastButtonState, mCurrentButtonState);
3435
3436 // Consume raw off-screen touches before cooking pointer data.
3437 // If touches are consumed, subsequent code will not receive any pointer data.
3438 if (consumeRawTouches(when, policyFlags)) {
3439 mCurrentRawPointerData.clear();
3440 }
3441
3442 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3443 // with cooked pointer data that has the same ids and indices as the raw data.
3444 // The following code can use either the raw or cooked data, as needed.
3445 cookPointerData();
3446
3447 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003448 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003449 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3450 uint32_t id = idBits.clearFirstMarkedBit();
3451 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3452 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3453 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3454 mCurrentStylusIdBits.markBit(id);
3455 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3456 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3457 mCurrentFingerIdBits.markBit(id);
3458 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3459 mCurrentMouseIdBits.markBit(id);
3460 }
3461 }
3462 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3463 uint32_t id = idBits.clearFirstMarkedBit();
3464 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3465 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3466 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3467 mCurrentStylusIdBits.markBit(id);
3468 }
3469 }
3470
3471 // Stylus takes precedence over all tools, then mouse, then finger.
3472 PointerUsage pointerUsage = mPointerUsage;
3473 if (!mCurrentStylusIdBits.isEmpty()) {
3474 mCurrentMouseIdBits.clear();
3475 mCurrentFingerIdBits.clear();
3476 pointerUsage = POINTER_USAGE_STYLUS;
3477 } else if (!mCurrentMouseIdBits.isEmpty()) {
3478 mCurrentFingerIdBits.clear();
3479 pointerUsage = POINTER_USAGE_MOUSE;
3480 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3481 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003482 }
3483
3484 dispatchPointerUsage(when, policyFlags, pointerUsage);
3485 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003486 if (mDeviceMode == DEVICE_MODE_DIRECT
3487 && mConfig.showTouches && mPointerController != NULL) {
3488 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3489 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3490
3491 mPointerController->setButtonState(mCurrentButtonState);
3492 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3493 mCurrentCookedPointerData.idToIndex,
3494 mCurrentCookedPointerData.touchingIdBits);
3495 }
3496
Jeff Brown65fd2512011-08-18 11:20:58 -07003497 dispatchHoverExit(when, policyFlags);
3498 dispatchTouches(when, policyFlags);
3499 dispatchHoverEnterAndMove(when, policyFlags);
3500 }
3501
3502 // Synthesize key up from raw buttons if needed.
3503 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3504 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003505 }
3506
Jeff Brown6328cdc2010-07-29 18:18:33 -07003507 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003508 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3509 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3510 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003511 mLastFingerIdBits = mCurrentFingerIdBits;
3512 mLastStylusIdBits = mCurrentStylusIdBits;
3513 mLastMouseIdBits = mCurrentMouseIdBits;
3514
3515 // Clear some transient state.
3516 mCurrentRawVScroll = 0;
3517 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003518}
3519
Jeff Brown79ac9692011-04-19 21:20:10 -07003520void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003521 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003522 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3523 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3524 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003525 }
3526}
3527
Jeff Brownbe1aa822011-07-27 16:04:54 -07003528bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3529 // Check for release of a virtual key.
3530 if (mCurrentVirtualKey.down) {
3531 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3532 // Pointer went up while virtual key was down.
3533 mCurrentVirtualKey.down = false;
3534 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003535#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003536 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003537 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003538#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003539 dispatchVirtualKey(when, policyFlags,
3540 AKEY_EVENT_ACTION_UP,
3541 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003542 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003543 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003544 }
3545
Jeff Brownbe1aa822011-07-27 16:04:54 -07003546 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3547 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3548 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3549 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3550 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3551 // Pointer is still within the space of the virtual key.
3552 return true;
3553 }
3554 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003555
Jeff Brownbe1aa822011-07-27 16:04:54 -07003556 // Pointer left virtual key area or another pointer also went down.
3557 // Send key cancellation but do not consume the touch yet.
3558 // This is useful when the user swipes through from the virtual key area
3559 // into the main display surface.
3560 mCurrentVirtualKey.down = false;
3561 if (!mCurrentVirtualKey.ignored) {
3562#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003563 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003564 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3565#endif
3566 dispatchVirtualKey(when, policyFlags,
3567 AKEY_EVENT_ACTION_UP,
3568 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3569 | AKEY_EVENT_FLAG_CANCELED);
3570 }
3571 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003572
Jeff Brownbe1aa822011-07-27 16:04:54 -07003573 if (mLastRawPointerData.touchingIdBits.isEmpty()
3574 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3575 // Pointer just went down. Check for virtual key press or off-screen touches.
3576 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3577 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3578 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3579 // If exactly one pointer went down, check for virtual key hit.
3580 // Otherwise we will drop the entire stroke.
3581 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3582 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3583 if (virtualKey) {
3584 mCurrentVirtualKey.down = true;
3585 mCurrentVirtualKey.downTime = when;
3586 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3587 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3588 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3589 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3590
3591 if (!mCurrentVirtualKey.ignored) {
3592#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003593 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003594 mCurrentVirtualKey.keyCode,
3595 mCurrentVirtualKey.scanCode);
3596#endif
3597 dispatchVirtualKey(when, policyFlags,
3598 AKEY_EVENT_ACTION_DOWN,
3599 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3600 }
3601 }
3602 }
3603 return true;
3604 }
3605 }
3606
Jeff Brownfe508922011-01-18 15:10:10 -08003607 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003608 // most recent touch within the screen area. The idea is to filter out stray
3609 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003610 //
3611 // Problems we're trying to solve:
3612 //
3613 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3614 // virtual key area that is implemented by a separate touch panel and accidentally
3615 // triggers a virtual key.
3616 //
3617 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3618 // area and accidentally triggers a virtual key. This often happens when virtual keys
3619 // are layed out below the screen near to where the on screen keyboard's space bar
3620 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003621 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003622 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003623 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003624 return false;
3625}
3626
3627void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3628 int32_t keyEventAction, int32_t keyEventFlags) {
3629 int32_t keyCode = mCurrentVirtualKey.keyCode;
3630 int32_t scanCode = mCurrentVirtualKey.scanCode;
3631 nsecs_t downTime = mCurrentVirtualKey.downTime;
3632 int32_t metaState = mContext->getGlobalMetaState();
3633 policyFlags |= POLICY_FLAG_VIRTUAL;
3634
3635 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3636 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3637 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003638}
3639
Jeff Brown6d0fec22010-07-23 21:28:06 -07003640void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003641 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3642 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003643 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003644 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003645
3646 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003647 if (!currentIdBits.isEmpty()) {
3648 // No pointer id changes so this is a move event.
3649 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003650 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003651 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3652 AMOTION_EVENT_EDGE_FLAG_NONE,
3653 mCurrentCookedPointerData.pointerProperties,
3654 mCurrentCookedPointerData.pointerCoords,
3655 mCurrentCookedPointerData.idToIndex,
3656 currentIdBits, -1,
3657 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3658 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003659 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003660 // There may be pointers going up and pointers going down and pointers moving
3661 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003662 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3663 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003664 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003665 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003666
Jeff Brownace13b12011-03-09 17:39:48 -08003667 // Update last coordinates of pointers that have moved so that we observe the new
3668 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003669 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003670 mCurrentCookedPointerData.pointerProperties,
3671 mCurrentCookedPointerData.pointerCoords,
3672 mCurrentCookedPointerData.idToIndex,
3673 mLastCookedPointerData.pointerProperties,
3674 mLastCookedPointerData.pointerCoords,
3675 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003676 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003677 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003678 moveNeeded = true;
3679 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003680
Jeff Brownace13b12011-03-09 17:39:48 -08003681 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003682 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003683 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003684
Jeff Brown65fd2512011-08-18 11:20:58 -07003685 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003686 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003687 mLastCookedPointerData.pointerProperties,
3688 mLastCookedPointerData.pointerCoords,
3689 mLastCookedPointerData.idToIndex,
3690 dispatchedIdBits, upId,
3691 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003692 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003693 }
3694
Jeff Brownc3db8582010-10-20 15:33:38 -07003695 // Dispatch move events if any of the remaining pointers moved from their old locations.
3696 // Although applications receive new locations as part of individual pointer up
3697 // events, they do not generally handle them except when presented in a move event.
3698 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003699 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003700 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003701 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003702 mCurrentCookedPointerData.pointerProperties,
3703 mCurrentCookedPointerData.pointerCoords,
3704 mCurrentCookedPointerData.idToIndex,
3705 dispatchedIdBits, -1,
3706 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003707 }
3708
3709 // Dispatch pointer down events using the new pointer locations.
3710 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003711 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003712 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003713
Jeff Brownace13b12011-03-09 17:39:48 -08003714 if (dispatchedIdBits.count() == 1) {
3715 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003716 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003717 }
3718
Jeff Brown65fd2512011-08-18 11:20:58 -07003719 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003720 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003721 mCurrentCookedPointerData.pointerProperties,
3722 mCurrentCookedPointerData.pointerCoords,
3723 mCurrentCookedPointerData.idToIndex,
3724 dispatchedIdBits, downId,
3725 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003726 }
3727 }
Jeff Brownace13b12011-03-09 17:39:48 -08003728}
3729
Jeff Brownbe1aa822011-07-27 16:04:54 -07003730void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3731 if (mSentHoverEnter &&
3732 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3733 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3734 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003735 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003736 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3737 mLastCookedPointerData.pointerProperties,
3738 mLastCookedPointerData.pointerCoords,
3739 mLastCookedPointerData.idToIndex,
3740 mLastCookedPointerData.hoveringIdBits, -1,
3741 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3742 mSentHoverEnter = false;
3743 }
3744}
Jeff Brownace13b12011-03-09 17:39:48 -08003745
Jeff Brownbe1aa822011-07-27 16:04:54 -07003746void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3747 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3748 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3749 int32_t metaState = getContext()->getGlobalMetaState();
3750 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003751 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003752 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3753 mCurrentCookedPointerData.pointerProperties,
3754 mCurrentCookedPointerData.pointerCoords,
3755 mCurrentCookedPointerData.idToIndex,
3756 mCurrentCookedPointerData.hoveringIdBits, -1,
3757 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3758 mSentHoverEnter = true;
3759 }
Jeff Brownace13b12011-03-09 17:39:48 -08003760
Jeff Brown65fd2512011-08-18 11:20:58 -07003761 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003762 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3763 mCurrentCookedPointerData.pointerProperties,
3764 mCurrentCookedPointerData.pointerCoords,
3765 mCurrentCookedPointerData.idToIndex,
3766 mCurrentCookedPointerData.hoveringIdBits, -1,
3767 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3768 }
3769}
3770
3771void TouchInputMapper::cookPointerData() {
3772 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3773
3774 mCurrentCookedPointerData.clear();
3775 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3776 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3777 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3778
3779 // Walk through the the active pointers and map device coordinates onto
3780 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003781 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003782 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003783
Jeff Browna1f89ce2011-08-11 00:05:01 -07003784 // Size
3785 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3786 switch (mCalibration.sizeCalibration) {
3787 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3788 case Calibration::SIZE_CALIBRATION_DIAMETER:
3789 case Calibration::SIZE_CALIBRATION_AREA:
3790 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3791 touchMajor = in.touchMajor;
3792 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3793 toolMajor = in.toolMajor;
3794 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3795 size = mRawPointerAxes.touchMinor.valid
3796 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3797 } else if (mRawPointerAxes.touchMajor.valid) {
3798 toolMajor = touchMajor = in.touchMajor;
3799 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3800 ? in.touchMinor : in.touchMajor;
3801 size = mRawPointerAxes.touchMinor.valid
3802 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3803 } else if (mRawPointerAxes.toolMajor.valid) {
3804 touchMajor = toolMajor = in.toolMajor;
3805 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3806 ? in.toolMinor : in.toolMajor;
3807 size = mRawPointerAxes.toolMinor.valid
3808 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003809 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003810 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003811 "Size calibration should have been resolved to NONE.");
3812 touchMajor = 0;
3813 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003814 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003815 toolMinor = 0;
3816 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003817 }
Jeff Brownace13b12011-03-09 17:39:48 -08003818
Jeff Browna1f89ce2011-08-11 00:05:01 -07003819 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3820 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3821 if (touchingCount > 1) {
3822 touchMajor /= touchingCount;
3823 touchMinor /= touchingCount;
3824 toolMajor /= touchingCount;
3825 toolMinor /= touchingCount;
3826 size /= touchingCount;
3827 }
3828 }
Jeff Brownace13b12011-03-09 17:39:48 -08003829
Jeff Browna1f89ce2011-08-11 00:05:01 -07003830 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3831 touchMajor *= mGeometricScale;
3832 touchMinor *= mGeometricScale;
3833 toolMajor *= mGeometricScale;
3834 toolMinor *= mGeometricScale;
3835 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3836 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003837 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003838 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3839 toolMinor = toolMajor;
3840 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3841 touchMinor = touchMajor;
3842 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003843 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003844
3845 mCalibration.applySizeScaleAndBias(&touchMajor);
3846 mCalibration.applySizeScaleAndBias(&touchMinor);
3847 mCalibration.applySizeScaleAndBias(&toolMajor);
3848 mCalibration.applySizeScaleAndBias(&toolMinor);
3849 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003850 break;
3851 default:
3852 touchMajor = 0;
3853 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003854 toolMajor = 0;
3855 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003856 size = 0;
3857 break;
3858 }
3859
Jeff Browna1f89ce2011-08-11 00:05:01 -07003860 // Pressure
3861 float pressure;
3862 switch (mCalibration.pressureCalibration) {
3863 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3864 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3865 pressure = in.pressure * mPressureScale;
3866 break;
3867 default:
3868 pressure = in.isHovering ? 0 : 1;
3869 break;
3870 }
3871
Jeff Brown65fd2512011-08-18 11:20:58 -07003872 // Tilt and Orientation
3873 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003874 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003875 if (mHaveTilt) {
3876 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3877 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3878 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3879 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3880 } else {
3881 tilt = 0;
3882
3883 switch (mCalibration.orientationCalibration) {
3884 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3885 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3886 break;
3887 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3888 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3889 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3890 if (c1 != 0 || c2 != 0) {
3891 orientation = atan2f(c1, c2) * 0.5f;
3892 float confidence = hypotf(c1, c2);
3893 float scale = 1.0f + confidence / 16.0f;
3894 touchMajor *= scale;
3895 touchMinor /= scale;
3896 toolMajor *= scale;
3897 toolMinor /= scale;
3898 } else {
3899 orientation = 0;
3900 }
3901 break;
3902 }
3903 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003904 orientation = 0;
3905 }
Jeff Brownace13b12011-03-09 17:39:48 -08003906 }
3907
Jeff Brown80fd47c2011-05-24 01:07:44 -07003908 // Distance
3909 float distance;
3910 switch (mCalibration.distanceCalibration) {
3911 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003912 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003913 break;
3914 default:
3915 distance = 0;
3916 }
3917
Jeff Brownace13b12011-03-09 17:39:48 -08003918 // X and Y
3919 // Adjust coords for surface orientation.
3920 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003921 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003922 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003923 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3924 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003925 orientation -= M_PI_2;
3926 if (orientation < - M_PI_2) {
3927 orientation += M_PI;
3928 }
3929 break;
3930 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003931 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3932 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003933 break;
3934 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003935 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3936 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003937 orientation += M_PI_2;
3938 if (orientation > M_PI_2) {
3939 orientation -= M_PI;
3940 }
3941 break;
3942 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003943 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3944 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003945 break;
3946 }
3947
3948 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003949 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003950 out.clear();
3951 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3952 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3953 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3954 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3955 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3956 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3957 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3958 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3959 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003960 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003961 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003962
3963 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003964 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3965 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003966 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003967 properties.id = id;
3968 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003969
Jeff Brownbe1aa822011-07-27 16:04:54 -07003970 // Write id index.
3971 mCurrentCookedPointerData.idToIndex[id] = i;
3972 }
Jeff Brownace13b12011-03-09 17:39:48 -08003973}
3974
Jeff Brown65fd2512011-08-18 11:20:58 -07003975void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3976 PointerUsage pointerUsage) {
3977 if (pointerUsage != mPointerUsage) {
3978 abortPointerUsage(when, policyFlags);
3979 mPointerUsage = pointerUsage;
3980 }
3981
3982 switch (mPointerUsage) {
3983 case POINTER_USAGE_GESTURES:
3984 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3985 break;
3986 case POINTER_USAGE_STYLUS:
3987 dispatchPointerStylus(when, policyFlags);
3988 break;
3989 case POINTER_USAGE_MOUSE:
3990 dispatchPointerMouse(when, policyFlags);
3991 break;
3992 default:
3993 break;
3994 }
3995}
3996
3997void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3998 switch (mPointerUsage) {
3999 case POINTER_USAGE_GESTURES:
4000 abortPointerGestures(when, policyFlags);
4001 break;
4002 case POINTER_USAGE_STYLUS:
4003 abortPointerStylus(when, policyFlags);
4004 break;
4005 case POINTER_USAGE_MOUSE:
4006 abortPointerMouse(when, policyFlags);
4007 break;
4008 default:
4009 break;
4010 }
4011
4012 mPointerUsage = POINTER_USAGE_NONE;
4013}
4014
Jeff Brown79ac9692011-04-19 21:20:10 -07004015void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
4016 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004017 // Update current gesture coordinates.
4018 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07004019 bool sendEvents = preparePointerGestures(when,
4020 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
4021 if (!sendEvents) {
4022 return;
4023 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004024 if (finishPreviousGesture) {
4025 cancelPreviousGesture = false;
4026 }
Jeff Brownace13b12011-03-09 17:39:48 -08004027
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004028 // Update the pointer presentation and spots.
4029 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4030 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4031 if (finishPreviousGesture || cancelPreviousGesture) {
4032 mPointerController->clearSpots();
4033 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004034 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4035 mPointerGesture.currentGestureIdToIndex,
4036 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004037 } else {
4038 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4039 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004040
Jeff Brown538881e2011-05-25 18:23:38 -07004041 // Show or hide the pointer if needed.
4042 switch (mPointerGesture.currentGestureMode) {
4043 case PointerGesture::NEUTRAL:
4044 case PointerGesture::QUIET:
4045 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4046 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4047 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4048 // Remind the user of where the pointer is after finishing a gesture with spots.
4049 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4050 }
4051 break;
4052 case PointerGesture::TAP:
4053 case PointerGesture::TAP_DRAG:
4054 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4055 case PointerGesture::HOVER:
4056 case PointerGesture::PRESS:
4057 // Unfade the pointer when the current gesture manipulates the
4058 // area directly under the pointer.
4059 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4060 break;
4061 case PointerGesture::SWIPE:
4062 case PointerGesture::FREEFORM:
4063 // Fade the pointer when the current gesture manipulates a different
4064 // area and there are spots to guide the user experience.
4065 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4066 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4067 } else {
4068 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4069 }
4070 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004071 }
4072
Jeff Brownace13b12011-03-09 17:39:48 -08004073 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004074 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004075 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004076
4077 // Update last coordinates of pointers that have moved so that we observe the new
4078 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004079 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4080 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4081 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004082 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004083 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4084 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4085 bool moveNeeded = false;
4086 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004087 && !mPointerGesture.lastGestureIdBits.isEmpty()
4088 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004089 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4090 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004091 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004092 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004093 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004094 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4095 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004096 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004097 moveNeeded = true;
4098 }
Jeff Brownace13b12011-03-09 17:39:48 -08004099 }
4100
4101 // Send motion events for all pointers that went up or were canceled.
4102 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4103 if (!dispatchedGestureIdBits.isEmpty()) {
4104 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004105 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004106 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4107 AMOTION_EVENT_EDGE_FLAG_NONE,
4108 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004109 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4110 dispatchedGestureIdBits, -1,
4111 0, 0, mPointerGesture.downTime);
4112
4113 dispatchedGestureIdBits.clear();
4114 } else {
4115 BitSet32 upGestureIdBits;
4116 if (finishPreviousGesture) {
4117 upGestureIdBits = dispatchedGestureIdBits;
4118 } else {
4119 upGestureIdBits.value = dispatchedGestureIdBits.value
4120 & ~mPointerGesture.currentGestureIdBits.value;
4121 }
4122 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004123 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004124
Jeff Brown65fd2512011-08-18 11:20:58 -07004125 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004126 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004127 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4128 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004129 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4130 dispatchedGestureIdBits, id,
4131 0, 0, mPointerGesture.downTime);
4132
4133 dispatchedGestureIdBits.clearBit(id);
4134 }
4135 }
4136 }
4137
4138 // Send motion events for all pointers that moved.
4139 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004140 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004141 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4142 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004143 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4144 dispatchedGestureIdBits, -1,
4145 0, 0, mPointerGesture.downTime);
4146 }
4147
4148 // Send motion events for all pointers that went down.
4149 if (down) {
4150 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4151 & ~dispatchedGestureIdBits.value);
4152 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004153 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004154 dispatchedGestureIdBits.markBit(id);
4155
Jeff Brownace13b12011-03-09 17:39:48 -08004156 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004157 mPointerGesture.downTime = when;
4158 }
4159
Jeff Brown65fd2512011-08-18 11:20:58 -07004160 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004161 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004162 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004163 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4164 dispatchedGestureIdBits, id,
4165 0, 0, mPointerGesture.downTime);
4166 }
4167 }
4168
Jeff Brownace13b12011-03-09 17:39:48 -08004169 // Send motion events for hover.
4170 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004171 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004172 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4173 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4174 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004175 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4176 mPointerGesture.currentGestureIdBits, -1,
4177 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004178 } else if (dispatchedGestureIdBits.isEmpty()
4179 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4180 // Synthesize a hover move event after all pointers go up to indicate that
4181 // the pointer is hovering again even if the user is not currently touching
4182 // the touch pad. This ensures that a view will receive a fresh hover enter
4183 // event after a tap.
4184 float x, y;
4185 mPointerController->getPosition(&x, &y);
4186
4187 PointerProperties pointerProperties;
4188 pointerProperties.clear();
4189 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004190 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004191
4192 PointerCoords pointerCoords;
4193 pointerCoords.clear();
4194 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4195 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4196
Jeff Brown65fd2512011-08-18 11:20:58 -07004197 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004198 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4199 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4200 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004201 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004202 }
4203
4204 // Update state.
4205 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4206 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004207 mPointerGesture.lastGestureIdBits.clear();
4208 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004209 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4210 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004211 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004212 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004213 mPointerGesture.lastGestureProperties[index].copyFrom(
4214 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004215 mPointerGesture.lastGestureCoords[index].copyFrom(
4216 mPointerGesture.currentGestureCoords[index]);
4217 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004218 }
4219 }
4220}
4221
Jeff Brown65fd2512011-08-18 11:20:58 -07004222void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4223 // Cancel previously dispatches pointers.
4224 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4225 int32_t metaState = getContext()->getGlobalMetaState();
4226 int32_t buttonState = mCurrentButtonState;
4227 dispatchMotion(when, policyFlags, mSource,
4228 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4229 AMOTION_EVENT_EDGE_FLAG_NONE,
4230 mPointerGesture.lastGestureProperties,
4231 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4232 mPointerGesture.lastGestureIdBits, -1,
4233 0, 0, mPointerGesture.downTime);
4234 }
4235
4236 // Reset the current pointer gesture.
4237 mPointerGesture.reset();
4238 mPointerVelocityControl.reset();
4239
4240 // Remove any current spots.
4241 if (mPointerController != NULL) {
4242 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4243 mPointerController->clearSpots();
4244 }
4245}
4246
Jeff Brown79ac9692011-04-19 21:20:10 -07004247bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4248 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004249 *outCancelPreviousGesture = false;
4250 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004251
Jeff Brown79ac9692011-04-19 21:20:10 -07004252 // Handle TAP timeout.
4253 if (isTimeout) {
4254#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004255 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004256#endif
4257
4258 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004259 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004260 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004261 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004262 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004263 } else {
4264 // The tap is finished.
4265#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004266 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004267#endif
4268 *outFinishPreviousGesture = true;
4269
4270 mPointerGesture.activeGestureId = -1;
4271 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4272 mPointerGesture.currentGestureIdBits.clear();
4273
Jeff Brown65fd2512011-08-18 11:20:58 -07004274 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004275 return true;
4276 }
4277 }
4278
4279 // We did not handle this timeout.
4280 return false;
4281 }
4282
Jeff Brown65fd2512011-08-18 11:20:58 -07004283 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4284 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4285
Jeff Brownace13b12011-03-09 17:39:48 -08004286 // Update the velocity tracker.
4287 {
4288 VelocityTracker::Position positions[MAX_POINTERS];
4289 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004290 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004291 uint32_t id = idBits.clearFirstMarkedBit();
4292 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004293 positions[count].x = pointer.x * mPointerXMovementScale;
4294 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004295 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004296 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004297 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004298 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004299
Jeff Brownace13b12011-03-09 17:39:48 -08004300 // Pick a new active touch id if needed.
4301 // Choose an arbitrary pointer that just went down, if there is one.
4302 // Otherwise choose an arbitrary remaining pointer.
4303 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004304 // We keep the same active touch id for as long as possible.
4305 bool activeTouchChanged = false;
4306 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4307 int32_t activeTouchId = lastActiveTouchId;
4308 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004309 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004310 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004311 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004312 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004313 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004314 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004315 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004316 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004317 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004318 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004319 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004320 } else {
4321 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004322 }
4323 }
4324
4325 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004326 bool isQuietTime = false;
4327 if (activeTouchId < 0) {
4328 mPointerGesture.resetQuietTime();
4329 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004330 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004331 if (!isQuietTime) {
4332 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4333 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4334 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004335 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004336 // Enter quiet time when exiting swipe or freeform state.
4337 // This is to prevent accidentally entering the hover state and flinging the
4338 // pointer when finishing a swipe and there is still one pointer left onscreen.
4339 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004340 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004341 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004342 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004343 // Enter quiet time when releasing the button and there are still two or more
4344 // fingers down. This may indicate that one finger was used to press the button
4345 // but it has not gone up yet.
4346 isQuietTime = true;
4347 }
4348 if (isQuietTime) {
4349 mPointerGesture.quietTime = when;
4350 }
Jeff Brownace13b12011-03-09 17:39:48 -08004351 }
4352 }
4353
4354 // Switch states based on button and pointer state.
4355 if (isQuietTime) {
4356 // Case 1: Quiet time. (QUIET)
4357#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004358 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004359 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004360#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004361 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4362 *outFinishPreviousGesture = true;
4363 }
Jeff Brownace13b12011-03-09 17:39:48 -08004364
4365 mPointerGesture.activeGestureId = -1;
4366 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004367 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004368
Jeff Brown65fd2512011-08-18 11:20:58 -07004369 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004370 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004371 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004372 // The pointer follows the active touch point.
4373 // Emit DOWN, MOVE, UP events at the pointer location.
4374 //
4375 // Only the active touch matters; other fingers are ignored. This policy helps
4376 // to handle the case where the user places a second finger on the touch pad
4377 // to apply the necessary force to depress an integrated button below the surface.
4378 // We don't want the second finger to be delivered to applications.
4379 //
4380 // For this to work well, we need to make sure to track the pointer that is really
4381 // active. If the user first puts one finger down to click then adds another
4382 // finger to drag then the active pointer should switch to the finger that is
4383 // being dragged.
4384#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004385 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004386 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004387#endif
4388 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004389 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004390 *outFinishPreviousGesture = true;
4391 mPointerGesture.activeGestureId = 0;
4392 }
4393
4394 // Switch pointers if needed.
4395 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004396 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004397 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004398 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004399 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004400 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004401 float vx, vy;
4402 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4403 float speed = hypotf(vx, vy);
4404 if (speed > bestSpeed) {
4405 bestId = id;
4406 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004407 }
Jeff Brown8d608662010-08-30 03:02:23 -07004408 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004409 }
4410 if (bestId >= 0 && bestId != activeTouchId) {
4411 mPointerGesture.activeTouchId = activeTouchId = bestId;
4412 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004413#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004414 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004415 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004416#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004417 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004418 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004419
Jeff Brown65fd2512011-08-18 11:20:58 -07004420 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004421 const RawPointerData::Pointer& currentPointer =
4422 mCurrentRawPointerData.pointerForId(activeTouchId);
4423 const RawPointerData::Pointer& lastPointer =
4424 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004425 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4426 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004427
Jeff Brownbe1aa822011-07-27 16:04:54 -07004428 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004429 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004430
4431 // Move the pointer using a relative motion.
4432 // When using spots, the click will occur at the position of the anchor
4433 // spot and all other spots will move there.
4434 mPointerController->move(deltaX, deltaY);
4435 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004436 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004437 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004438
Jeff Brownace13b12011-03-09 17:39:48 -08004439 float x, y;
4440 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004441
Jeff Brown79ac9692011-04-19 21:20:10 -07004442 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004443 mPointerGesture.currentGestureIdBits.clear();
4444 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4445 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004446 mPointerGesture.currentGestureProperties[0].clear();
4447 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004448 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004449 mPointerGesture.currentGestureCoords[0].clear();
4450 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4451 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4452 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004453 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004454 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004455 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4456 *outFinishPreviousGesture = true;
4457 }
Jeff Brownace13b12011-03-09 17:39:48 -08004458
Jeff Brown79ac9692011-04-19 21:20:10 -07004459 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004460 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004461 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004462 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4463 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004464 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004465 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004466 float x, y;
4467 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004468 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4469 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004470#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004471 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004472#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004473
4474 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004475 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004476 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004477
Jeff Brownace13b12011-03-09 17:39:48 -08004478 mPointerGesture.activeGestureId = 0;
4479 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004480 mPointerGesture.currentGestureIdBits.clear();
4481 mPointerGesture.currentGestureIdBits.markBit(
4482 mPointerGesture.activeGestureId);
4483 mPointerGesture.currentGestureIdToIndex[
4484 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004485 mPointerGesture.currentGestureProperties[0].clear();
4486 mPointerGesture.currentGestureProperties[0].id =
4487 mPointerGesture.activeGestureId;
4488 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004489 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004490 mPointerGesture.currentGestureCoords[0].clear();
4491 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004492 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004493 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004494 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004495 mPointerGesture.currentGestureCoords[0].setAxisValue(
4496 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004497
Jeff Brownace13b12011-03-09 17:39:48 -08004498 tapped = true;
4499 } else {
4500#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004501 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004502 x - mPointerGesture.tapX,
4503 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004504#endif
4505 }
4506 } else {
4507#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004508 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004509 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004510#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004511 }
Jeff Brownace13b12011-03-09 17:39:48 -08004512 }
Jeff Brown2352b972011-04-12 22:39:53 -07004513
Jeff Brown65fd2512011-08-18 11:20:58 -07004514 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004515
Jeff Brownace13b12011-03-09 17:39:48 -08004516 if (!tapped) {
4517#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004518 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004519#endif
4520 mPointerGesture.activeGestureId = -1;
4521 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004522 mPointerGesture.currentGestureIdBits.clear();
4523 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004524 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004525 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004526 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004527 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4528 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004529 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004530
Jeff Brown79ac9692011-04-19 21:20:10 -07004531 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4532 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004533 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004534 float x, y;
4535 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004536 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4537 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004538 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4539 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004540#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004541 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004542 x - mPointerGesture.tapX,
4543 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004544#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004545 }
4546 } else {
4547#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004548 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004549 (when - mPointerGesture.tapUpTime) * 0.000001f);
4550#endif
4551 }
4552 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4553 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4554 }
Jeff Brownace13b12011-03-09 17:39:48 -08004555
Jeff Brown65fd2512011-08-18 11:20:58 -07004556 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004557 const RawPointerData::Pointer& currentPointer =
4558 mCurrentRawPointerData.pointerForId(activeTouchId);
4559 const RawPointerData::Pointer& lastPointer =
4560 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004561 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004562 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004563 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004564 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004565
Jeff Brownbe1aa822011-07-27 16:04:54 -07004566 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004567 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004568
Jeff Brown2352b972011-04-12 22:39:53 -07004569 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004570 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004571 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004572 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004573 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004574 }
4575
Jeff Brown79ac9692011-04-19 21:20:10 -07004576 bool down;
4577 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4578#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004579 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004580#endif
4581 down = true;
4582 } else {
4583#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004584 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004585#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004586 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4587 *outFinishPreviousGesture = true;
4588 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004589 mPointerGesture.activeGestureId = 0;
4590 down = false;
4591 }
Jeff Brownace13b12011-03-09 17:39:48 -08004592
4593 float x, y;
4594 mPointerController->getPosition(&x, &y);
4595
Jeff Brownace13b12011-03-09 17:39:48 -08004596 mPointerGesture.currentGestureIdBits.clear();
4597 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4598 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004599 mPointerGesture.currentGestureProperties[0].clear();
4600 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4601 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004602 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004603 mPointerGesture.currentGestureCoords[0].clear();
4604 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4605 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004606 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4607 down ? 1.0f : 0.0f);
4608
Jeff Brown65fd2512011-08-18 11:20:58 -07004609 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004610 mPointerGesture.resetTap();
4611 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004612 mPointerGesture.tapX = x;
4613 mPointerGesture.tapY = y;
4614 }
Jeff Brownace13b12011-03-09 17:39:48 -08004615 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004616 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4617 // We need to provide feedback for each finger that goes down so we cannot wait
4618 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004619 //
Jeff Brown2352b972011-04-12 22:39:53 -07004620 // The ambiguous case is deciding what to do when there are two fingers down but they
4621 // have not moved enough to determine whether they are part of a drag or part of a
4622 // freeform gesture, or just a press or long-press at the pointer location.
4623 //
4624 // When there are two fingers we start with the PRESS hypothesis and we generate a
4625 // down at the pointer location.
4626 //
4627 // When the two fingers move enough or when additional fingers are added, we make
4628 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004629 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004630
Jeff Brown214eaf42011-05-26 19:17:02 -07004631 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004632 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004633 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004634 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4635 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004636 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004637 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004638 // Additional pointers have gone down but not yet settled.
4639 // Reset the gesture.
4640#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004641 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004642 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004643 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004644 * 0.000001f);
4645#endif
4646 *outCancelPreviousGesture = true;
4647 } else {
4648 // Continue previous gesture.
4649 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4650 }
4651
4652 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004653 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4654 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004655 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004656 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004657
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004658 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004659#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004660 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004661 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004662 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004663 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004664#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004665 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4666 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004667 &mPointerGesture.referenceTouchY);
4668 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4669 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004670 }
Jeff Brownace13b12011-03-09 17:39:48 -08004671
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004672 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004673 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004674 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4675 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004676 mPointerGesture.referenceDeltas[id].dx = 0;
4677 mPointerGesture.referenceDeltas[id].dy = 0;
4678 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004679 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004680
4681 // Add delta for all fingers and calculate a common movement delta.
4682 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004683 BitSet32 commonIdBits(mLastFingerIdBits.value
4684 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004685 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4686 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004687 uint32_t id = idBits.clearFirstMarkedBit();
4688 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4689 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004690 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4691 delta.dx += cpd.x - lpd.x;
4692 delta.dy += cpd.y - lpd.y;
4693
4694 if (first) {
4695 commonDeltaX = delta.dx;
4696 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004697 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004698 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4699 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4700 }
4701 }
Jeff Brownace13b12011-03-09 17:39:48 -08004702
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004703 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4704 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4705 float dist[MAX_POINTER_ID + 1];
4706 int32_t distOverThreshold = 0;
4707 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004708 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004709 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004710 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4711 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004712 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004713 distOverThreshold += 1;
4714 }
4715 }
4716
4717 // Only transition when at least two pointers have moved further than
4718 // the minimum distance threshold.
4719 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004720 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004721 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004722#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004723 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004724 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004725#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004726 *outCancelPreviousGesture = true;
4727 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4728 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004729 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004730 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004731 uint32_t id1 = idBits.clearFirstMarkedBit();
4732 uint32_t id2 = idBits.firstMarkedBit();
4733 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4734 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4735 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4736 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4737 // There are two pointers but they are too far apart for a SWIPE,
4738 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004739#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004740 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004741 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004742#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004743 *outCancelPreviousGesture = true;
4744 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4745 } else {
4746 // There are two pointers. Wait for both pointers to start moving
4747 // before deciding whether this is a SWIPE or FREEFORM gesture.
4748 float dist1 = dist[id1];
4749 float dist2 = dist[id2];
4750 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4751 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4752 // Calculate the dot product of the displacement vectors.
4753 // When the vectors are oriented in approximately the same direction,
4754 // the angle betweeen them is near zero and the cosine of the angle
4755 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4756 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4757 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004758 float dx1 = delta1.dx * mPointerXZoomScale;
4759 float dy1 = delta1.dy * mPointerYZoomScale;
4760 float dx2 = delta2.dx * mPointerXZoomScale;
4761 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004762 float dot = dx1 * dx2 + dy1 * dy2;
4763 float cosine = dot / (dist1 * dist2); // denominator always > 0
4764 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4765 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004766#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004767 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004768 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4769 "cosine %0.3f >= %0.3f",
4770 dist1, mConfig.pointerGestureMultitouchMinDistance,
4771 dist2, mConfig.pointerGestureMultitouchMinDistance,
4772 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004773#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004774 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4775 } else {
4776 // Pointers are moving in different directions. Switch to FREEFORM.
4777#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004778 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004779 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4780 "cosine %0.3f < %0.3f",
4781 dist1, mConfig.pointerGestureMultitouchMinDistance,
4782 dist2, mConfig.pointerGestureMultitouchMinDistance,
4783 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4784#endif
4785 *outCancelPreviousGesture = true;
4786 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4787 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004788 }
Jeff Brownace13b12011-03-09 17:39:48 -08004789 }
4790 }
Jeff Brownace13b12011-03-09 17:39:48 -08004791 }
4792 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004793 // Switch from SWIPE to FREEFORM if additional pointers go down.
4794 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004795 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004796#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004797 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004798 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004799#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004800 *outCancelPreviousGesture = true;
4801 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004802 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004803 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004804
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004805 // Move the reference points based on the overall group motion of the fingers
4806 // except in PRESS mode while waiting for a transition to occur.
4807 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4808 && (commonDeltaX || commonDeltaY)) {
4809 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004810 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004811 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004812 delta.dx = 0;
4813 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004814 }
4815
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004816 mPointerGesture.referenceTouchX += commonDeltaX;
4817 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004818
Jeff Brown65fd2512011-08-18 11:20:58 -07004819 commonDeltaX *= mPointerXMovementScale;
4820 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004821
Jeff Brownbe1aa822011-07-27 16:04:54 -07004822 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004823 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004824
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004825 mPointerGesture.referenceGestureX += commonDeltaX;
4826 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004827 }
4828
4829 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004830 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4831 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4832 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004833#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004834 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004835 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004836 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004837#endif
Steve Blockec193de2012-01-09 18:35:44 +00004838 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07004839
4840 mPointerGesture.currentGestureIdBits.clear();
4841 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4842 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004843 mPointerGesture.currentGestureProperties[0].clear();
4844 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4845 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004846 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004847 mPointerGesture.currentGestureCoords[0].clear();
4848 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4849 mPointerGesture.referenceGestureX);
4850 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4851 mPointerGesture.referenceGestureY);
4852 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004853 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4854 // FREEFORM mode.
4855#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004856 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004857 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004858 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004859#endif
Steve Blockec193de2012-01-09 18:35:44 +00004860 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004861
Jeff Brownace13b12011-03-09 17:39:48 -08004862 mPointerGesture.currentGestureIdBits.clear();
4863
4864 BitSet32 mappedTouchIdBits;
4865 BitSet32 usedGestureIdBits;
4866 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4867 // Initially, assign the active gesture id to the active touch point
4868 // if there is one. No other touch id bits are mapped yet.
4869 if (!*outCancelPreviousGesture) {
4870 mappedTouchIdBits.markBit(activeTouchId);
4871 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4872 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4873 mPointerGesture.activeGestureId;
4874 } else {
4875 mPointerGesture.activeGestureId = -1;
4876 }
4877 } else {
4878 // Otherwise, assume we mapped all touches from the previous frame.
4879 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004880 mappedTouchIdBits.value = mLastFingerIdBits.value
4881 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004882 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4883
4884 // Check whether we need to choose a new active gesture id because the
4885 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004886 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4887 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004888 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004889 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004890 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4891 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4892 mPointerGesture.activeGestureId = -1;
4893 break;
4894 }
4895 }
4896 }
4897
4898#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004899 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08004900 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4901 "activeGestureId=%d",
4902 mappedTouchIdBits.value, usedGestureIdBits.value,
4903 mPointerGesture.activeGestureId);
4904#endif
4905
Jeff Brown65fd2512011-08-18 11:20:58 -07004906 BitSet32 idBits(mCurrentFingerIdBits);
4907 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004908 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004909 uint32_t gestureId;
4910 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004911 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004912 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4913#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004914 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004915 "new mapping for touch id %d -> gesture id %d",
4916 touchId, gestureId);
4917#endif
4918 } else {
4919 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4920#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004921 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004922 "existing mapping for touch id %d -> gesture id %d",
4923 touchId, gestureId);
4924#endif
4925 }
4926 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4927 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4928
Jeff Brownbe1aa822011-07-27 16:04:54 -07004929 const RawPointerData::Pointer& pointer =
4930 mCurrentRawPointerData.pointerForId(touchId);
4931 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004932 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004933 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004934 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004935 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004936
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004937 mPointerGesture.currentGestureProperties[i].clear();
4938 mPointerGesture.currentGestureProperties[i].id = gestureId;
4939 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004940 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004941 mPointerGesture.currentGestureCoords[i].clear();
4942 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004943 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004944 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004945 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004946 mPointerGesture.currentGestureCoords[i].setAxisValue(
4947 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4948 }
4949
4950 if (mPointerGesture.activeGestureId < 0) {
4951 mPointerGesture.activeGestureId =
4952 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4953#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004954 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08004955 "activeGestureId=%d", mPointerGesture.activeGestureId);
4956#endif
4957 }
Jeff Brown2352b972011-04-12 22:39:53 -07004958 }
Jeff Brownace13b12011-03-09 17:39:48 -08004959 }
4960
Jeff Brownbe1aa822011-07-27 16:04:54 -07004961 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004962
Jeff Brownace13b12011-03-09 17:39:48 -08004963#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004964 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004965 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4966 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004967 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004968 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4969 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004970 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004971 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004972 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004973 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004974 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004975 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004976 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4977 id, index, properties.toolType,
4978 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004979 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4980 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4981 }
4982 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004983 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004984 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004985 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004986 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004987 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004988 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4989 id, index, properties.toolType,
4990 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004991 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4992 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4993 }
4994#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004995 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004996}
4997
Jeff Brown65fd2512011-08-18 11:20:58 -07004998void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4999 mPointerSimple.currentCoords.clear();
5000 mPointerSimple.currentProperties.clear();
5001
5002 bool down, hovering;
5003 if (!mCurrentStylusIdBits.isEmpty()) {
5004 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
5005 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
5006 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
5007 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
5008 mPointerController->setPosition(x, y);
5009
5010 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
5011 down = !hovering;
5012
5013 mPointerController->getPosition(&x, &y);
5014 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
5015 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5016 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5017 mPointerSimple.currentProperties.id = 0;
5018 mPointerSimple.currentProperties.toolType =
5019 mCurrentCookedPointerData.pointerProperties[index].toolType;
5020 } else {
5021 down = false;
5022 hovering = false;
5023 }
5024
5025 dispatchPointerSimple(when, policyFlags, down, hovering);
5026}
5027
5028void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5029 abortPointerSimple(when, policyFlags);
5030}
5031
5032void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5033 mPointerSimple.currentCoords.clear();
5034 mPointerSimple.currentProperties.clear();
5035
5036 bool down, hovering;
5037 if (!mCurrentMouseIdBits.isEmpty()) {
5038 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5039 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5040 if (mLastMouseIdBits.hasBit(id)) {
5041 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5042 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5043 - mLastRawPointerData.pointers[lastIndex].x)
5044 * mPointerXMovementScale;
5045 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5046 - mLastRawPointerData.pointers[lastIndex].y)
5047 * mPointerYMovementScale;
5048
5049 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5050 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5051
5052 mPointerController->move(deltaX, deltaY);
5053 } else {
5054 mPointerVelocityControl.reset();
5055 }
5056
5057 down = isPointerDown(mCurrentButtonState);
5058 hovering = !down;
5059
5060 float x, y;
5061 mPointerController->getPosition(&x, &y);
5062 mPointerSimple.currentCoords.copyFrom(
5063 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5064 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5065 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5066 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5067 hovering ? 0.0f : 1.0f);
5068 mPointerSimple.currentProperties.id = 0;
5069 mPointerSimple.currentProperties.toolType =
5070 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5071 } else {
5072 mPointerVelocityControl.reset();
5073
5074 down = false;
5075 hovering = false;
5076 }
5077
5078 dispatchPointerSimple(when, policyFlags, down, hovering);
5079}
5080
5081void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5082 abortPointerSimple(when, policyFlags);
5083
5084 mPointerVelocityControl.reset();
5085}
5086
5087void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5088 bool down, bool hovering) {
5089 int32_t metaState = getContext()->getGlobalMetaState();
5090
5091 if (mPointerController != NULL) {
5092 if (down || hovering) {
5093 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5094 mPointerController->clearSpots();
5095 mPointerController->setButtonState(mCurrentButtonState);
5096 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5097 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5098 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5099 }
5100 }
5101
5102 if (mPointerSimple.down && !down) {
5103 mPointerSimple.down = false;
5104
5105 // Send up.
5106 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5107 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5108 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5109 mOrientedXPrecision, mOrientedYPrecision,
5110 mPointerSimple.downTime);
5111 getListener()->notifyMotion(&args);
5112 }
5113
5114 if (mPointerSimple.hovering && !hovering) {
5115 mPointerSimple.hovering = false;
5116
5117 // Send hover exit.
5118 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5119 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5120 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5121 mOrientedXPrecision, mOrientedYPrecision,
5122 mPointerSimple.downTime);
5123 getListener()->notifyMotion(&args);
5124 }
5125
5126 if (down) {
5127 if (!mPointerSimple.down) {
5128 mPointerSimple.down = true;
5129 mPointerSimple.downTime = when;
5130
5131 // Send down.
5132 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5133 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5134 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5135 mOrientedXPrecision, mOrientedYPrecision,
5136 mPointerSimple.downTime);
5137 getListener()->notifyMotion(&args);
5138 }
5139
5140 // Send move.
5141 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5142 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5143 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5144 mOrientedXPrecision, mOrientedYPrecision,
5145 mPointerSimple.downTime);
5146 getListener()->notifyMotion(&args);
5147 }
5148
5149 if (hovering) {
5150 if (!mPointerSimple.hovering) {
5151 mPointerSimple.hovering = true;
5152
5153 // Send hover enter.
5154 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5155 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5156 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5157 mOrientedXPrecision, mOrientedYPrecision,
5158 mPointerSimple.downTime);
5159 getListener()->notifyMotion(&args);
5160 }
5161
5162 // Send hover move.
5163 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5164 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5165 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5166 mOrientedXPrecision, mOrientedYPrecision,
5167 mPointerSimple.downTime);
5168 getListener()->notifyMotion(&args);
5169 }
5170
5171 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5172 float vscroll = mCurrentRawVScroll;
5173 float hscroll = mCurrentRawHScroll;
5174 mWheelYVelocityControl.move(when, NULL, &vscroll);
5175 mWheelXVelocityControl.move(when, &hscroll, NULL);
5176
5177 // Send scroll.
5178 PointerCoords pointerCoords;
5179 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5180 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5181 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5182
5183 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5184 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5185 1, &mPointerSimple.currentProperties, &pointerCoords,
5186 mOrientedXPrecision, mOrientedYPrecision,
5187 mPointerSimple.downTime);
5188 getListener()->notifyMotion(&args);
5189 }
5190
5191 // Save state.
5192 if (down || hovering) {
5193 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5194 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5195 } else {
5196 mPointerSimple.reset();
5197 }
5198}
5199
5200void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5201 mPointerSimple.currentCoords.clear();
5202 mPointerSimple.currentProperties.clear();
5203
5204 dispatchPointerSimple(when, policyFlags, false, false);
5205}
5206
Jeff Brownace13b12011-03-09 17:39:48 -08005207void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005208 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5209 const PointerProperties* properties, const PointerCoords* coords,
5210 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005211 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5212 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005213 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005214 uint32_t pointerCount = 0;
5215 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005216 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005217 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005218 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005219 pointerCoords[pointerCount].copyFrom(coords[index]);
5220
5221 if (changedId >= 0 && id == uint32_t(changedId)) {
5222 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5223 }
5224
5225 pointerCount += 1;
5226 }
5227
Steve Blockec193de2012-01-09 18:35:44 +00005228 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005229
5230 if (changedId >= 0 && pointerCount == 1) {
5231 // Replace initial down and final up action.
5232 // We can compare the action without masking off the changed pointer index
5233 // because we know the index is 0.
5234 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5235 action = AMOTION_EVENT_ACTION_DOWN;
5236 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5237 action = AMOTION_EVENT_ACTION_UP;
5238 } else {
5239 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005240 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005241 }
5242 }
5243
Jeff Brownbe1aa822011-07-27 16:04:54 -07005244 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005245 action, flags, metaState, buttonState, edgeFlags,
5246 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005247 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005248}
5249
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005250bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005251 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005252 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5253 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005254 bool changed = false;
5255 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005256 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005257 uint32_t inIndex = inIdToIndex[id];
5258 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005259
5260 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005261 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005262 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005263 PointerCoords& curOutCoords = outCoords[outIndex];
5264
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005265 if (curInProperties != curOutProperties) {
5266 curOutProperties.copyFrom(curInProperties);
5267 changed = true;
5268 }
5269
Jeff Brownace13b12011-03-09 17:39:48 -08005270 if (curInCoords != curOutCoords) {
5271 curOutCoords.copyFrom(curInCoords);
5272 changed = true;
5273 }
5274 }
5275 return changed;
5276}
5277
5278void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005279 if (mPointerController != NULL) {
5280 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5281 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005282}
5283
Jeff Brownbe1aa822011-07-27 16:04:54 -07005284bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5285 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5286 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005287}
5288
Jeff Brownbe1aa822011-07-27 16:04:54 -07005289const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005290 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005291 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005292 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005293 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005294
5295#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005296 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005297 "left=%d, top=%d, right=%d, bottom=%d",
5298 x, y,
5299 virtualKey.keyCode, virtualKey.scanCode,
5300 virtualKey.hitLeft, virtualKey.hitTop,
5301 virtualKey.hitRight, virtualKey.hitBottom);
5302#endif
5303
5304 if (virtualKey.isHit(x, y)) {
5305 return & virtualKey;
5306 }
5307 }
5308
5309 return NULL;
5310}
5311
Jeff Brownbe1aa822011-07-27 16:04:54 -07005312void TouchInputMapper::assignPointerIds() {
5313 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5314 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5315
5316 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005317
5318 if (currentPointerCount == 0) {
5319 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005320 return;
5321 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005322
Jeff Brownbe1aa822011-07-27 16:04:54 -07005323 if (lastPointerCount == 0) {
5324 // All pointers are new.
5325 for (uint32_t i = 0; i < currentPointerCount; i++) {
5326 uint32_t id = i;
5327 mCurrentRawPointerData.pointers[i].id = id;
5328 mCurrentRawPointerData.idToIndex[id] = i;
5329 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5330 }
5331 return;
5332 }
5333
5334 if (currentPointerCount == 1 && lastPointerCount == 1
5335 && mCurrentRawPointerData.pointers[0].toolType
5336 == mLastRawPointerData.pointers[0].toolType) {
5337 // Only one pointer and no change in count so it must have the same id as before.
5338 uint32_t id = mLastRawPointerData.pointers[0].id;
5339 mCurrentRawPointerData.pointers[0].id = id;
5340 mCurrentRawPointerData.idToIndex[id] = 0;
5341 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5342 return;
5343 }
5344
5345 // General case.
5346 // We build a heap of squared euclidean distances between current and last pointers
5347 // associated with the current and last pointer indices. Then, we find the best
5348 // match (by distance) for each current pointer.
5349 // The pointers must have the same tool type but it is possible for them to
5350 // transition from hovering to touching or vice-versa while retaining the same id.
5351 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5352
5353 uint32_t heapSize = 0;
5354 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5355 currentPointerIndex++) {
5356 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5357 lastPointerIndex++) {
5358 const RawPointerData::Pointer& currentPointer =
5359 mCurrentRawPointerData.pointers[currentPointerIndex];
5360 const RawPointerData::Pointer& lastPointer =
5361 mLastRawPointerData.pointers[lastPointerIndex];
5362 if (currentPointer.toolType == lastPointer.toolType) {
5363 int64_t deltaX = currentPointer.x - lastPointer.x;
5364 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005365
5366 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5367
5368 // Insert new element into the heap (sift up).
5369 heap[heapSize].currentPointerIndex = currentPointerIndex;
5370 heap[heapSize].lastPointerIndex = lastPointerIndex;
5371 heap[heapSize].distance = distance;
5372 heapSize += 1;
5373 }
5374 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005375 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005376
Jeff Brownbe1aa822011-07-27 16:04:54 -07005377 // Heapify
5378 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5379 startIndex -= 1;
5380 for (uint32_t parentIndex = startIndex; ;) {
5381 uint32_t childIndex = parentIndex * 2 + 1;
5382 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005383 break;
5384 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005385
5386 if (childIndex + 1 < heapSize
5387 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5388 childIndex += 1;
5389 }
5390
5391 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5392 break;
5393 }
5394
5395 swap(heap[parentIndex], heap[childIndex]);
5396 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005397 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005398 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005399
5400#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005401 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005402 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005403 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005404 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5405 heap[i].distance);
5406 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005407#endif
5408
Jeff Brownbe1aa822011-07-27 16:04:54 -07005409 // Pull matches out by increasing order of distance.
5410 // To avoid reassigning pointers that have already been matched, the loop keeps track
5411 // of which last and current pointers have been matched using the matchedXXXBits variables.
5412 // It also tracks the used pointer id bits.
5413 BitSet32 matchedLastBits(0);
5414 BitSet32 matchedCurrentBits(0);
5415 BitSet32 usedIdBits(0);
5416 bool first = true;
5417 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5418 while (heapSize > 0) {
5419 if (first) {
5420 // The first time through the loop, we just consume the root element of
5421 // the heap (the one with smallest distance).
5422 first = false;
5423 } else {
5424 // Previous iterations consumed the root element of the heap.
5425 // Pop root element off of the heap (sift down).
5426 heap[0] = heap[heapSize];
5427 for (uint32_t parentIndex = 0; ;) {
5428 uint32_t childIndex = parentIndex * 2 + 1;
5429 if (childIndex >= heapSize) {
5430 break;
5431 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005432
Jeff Brownbe1aa822011-07-27 16:04:54 -07005433 if (childIndex + 1 < heapSize
5434 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5435 childIndex += 1;
5436 }
5437
5438 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5439 break;
5440 }
5441
5442 swap(heap[parentIndex], heap[childIndex]);
5443 parentIndex = childIndex;
5444 }
5445
5446#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005447 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005448 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005449 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005450 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5451 heap[i].distance);
5452 }
5453#endif
5454 }
5455
5456 heapSize -= 1;
5457
5458 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5459 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5460
5461 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5462 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5463
5464 matchedCurrentBits.markBit(currentPointerIndex);
5465 matchedLastBits.markBit(lastPointerIndex);
5466
5467 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5468 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5469 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5470 mCurrentRawPointerData.markIdBit(id,
5471 mCurrentRawPointerData.isHovering(currentPointerIndex));
5472 usedIdBits.markBit(id);
5473
5474#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005475 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005476 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5477#endif
5478 break;
5479 }
5480 }
5481
5482 // Assign fresh ids to pointers that were not matched in the process.
5483 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5484 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5485 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5486
5487 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5488 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5489 mCurrentRawPointerData.markIdBit(id,
5490 mCurrentRawPointerData.isHovering(currentPointerIndex));
5491
5492#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005493 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005494 currentPointerIndex, id);
5495#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005496 }
5497}
5498
Jeff Brown6d0fec22010-07-23 21:28:06 -07005499int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005500 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5501 return AKEY_STATE_VIRTUAL;
5502 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005503
Jeff Brownbe1aa822011-07-27 16:04:54 -07005504 size_t numVirtualKeys = mVirtualKeys.size();
5505 for (size_t i = 0; i < numVirtualKeys; i++) {
5506 const VirtualKey& virtualKey = mVirtualKeys[i];
5507 if (virtualKey.keyCode == keyCode) {
5508 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005509 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005510 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005511
5512 return AKEY_STATE_UNKNOWN;
5513}
5514
5515int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005516 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5517 return AKEY_STATE_VIRTUAL;
5518 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005519
Jeff Brownbe1aa822011-07-27 16:04:54 -07005520 size_t numVirtualKeys = mVirtualKeys.size();
5521 for (size_t i = 0; i < numVirtualKeys; i++) {
5522 const VirtualKey& virtualKey = mVirtualKeys[i];
5523 if (virtualKey.scanCode == scanCode) {
5524 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005525 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005526 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005527
5528 return AKEY_STATE_UNKNOWN;
5529}
5530
5531bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5532 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005533 size_t numVirtualKeys = mVirtualKeys.size();
5534 for (size_t i = 0; i < numVirtualKeys; i++) {
5535 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005536
Jeff Brownbe1aa822011-07-27 16:04:54 -07005537 for (size_t i = 0; i < numCodes; i++) {
5538 if (virtualKey.keyCode == keyCodes[i]) {
5539 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005540 }
5541 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005542 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005543
5544 return true;
5545}
5546
5547
5548// --- SingleTouchInputMapper ---
5549
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005550SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5551 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005552}
5553
5554SingleTouchInputMapper::~SingleTouchInputMapper() {
5555}
5556
Jeff Brown65fd2512011-08-18 11:20:58 -07005557void SingleTouchInputMapper::reset(nsecs_t when) {
5558 mSingleTouchMotionAccumulator.reset(getDevice());
5559
5560 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005561}
5562
Jeff Brown6d0fec22010-07-23 21:28:06 -07005563void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005564 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005565
Jeff Brown65fd2512011-08-18 11:20:58 -07005566 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005567}
5568
Jeff Brown65fd2512011-08-18 11:20:58 -07005569void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005570 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005571 mCurrentRawPointerData.pointerCount = 1;
5572 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005573
Jeff Brown65fd2512011-08-18 11:20:58 -07005574 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5575 && (mTouchButtonAccumulator.isHovering()
5576 || (mRawPointerAxes.pressure.valid
5577 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005578 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005579
Jeff Brownbe1aa822011-07-27 16:04:54 -07005580 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005581 outPointer.id = 0;
5582 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5583 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5584 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5585 outPointer.touchMajor = 0;
5586 outPointer.touchMinor = 0;
5587 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5588 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5589 outPointer.orientation = 0;
5590 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005591 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5592 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005593 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5594 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5595 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5596 }
5597 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005598 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005599}
5600
Jeff Brownbe1aa822011-07-27 16:04:54 -07005601void SingleTouchInputMapper::configureRawPointerAxes() {
5602 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005603
Jeff Brownbe1aa822011-07-27 16:04:54 -07005604 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5605 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5606 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5607 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5608 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005609 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5610 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005611}
5612
5613
5614// --- MultiTouchInputMapper ---
5615
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005616MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005617 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005618}
5619
5620MultiTouchInputMapper::~MultiTouchInputMapper() {
5621}
5622
Jeff Brown65fd2512011-08-18 11:20:58 -07005623void MultiTouchInputMapper::reset(nsecs_t when) {
5624 mMultiTouchMotionAccumulator.reset(getDevice());
5625
Jeff Brown6894a292011-07-01 17:59:27 -07005626 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005627
Jeff Brown65fd2512011-08-18 11:20:58 -07005628 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005629}
5630
5631void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005632 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005633
Jeff Brown65fd2512011-08-18 11:20:58 -07005634 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005635}
5636
Jeff Brown65fd2512011-08-18 11:20:58 -07005637void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005638 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005639 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005640 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005641
Jeff Brown80fd47c2011-05-24 01:07:44 -07005642 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005643 const MultiTouchMotionAccumulator::Slot* inSlot =
5644 mMultiTouchMotionAccumulator.getSlot(inIndex);
5645 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005646 continue;
5647 }
5648
Jeff Brown80fd47c2011-05-24 01:07:44 -07005649 if (outCount >= MAX_POINTERS) {
5650#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005651 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005652 "ignoring the rest.",
5653 getDeviceName().string(), MAX_POINTERS);
5654#endif
5655 break; // too many fingers!
5656 }
5657
Jeff Brownbe1aa822011-07-27 16:04:54 -07005658 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005659 outPointer.x = inSlot->getX();
5660 outPointer.y = inSlot->getY();
5661 outPointer.pressure = inSlot->getPressure();
5662 outPointer.touchMajor = inSlot->getTouchMajor();
5663 outPointer.touchMinor = inSlot->getTouchMinor();
5664 outPointer.toolMajor = inSlot->getToolMajor();
5665 outPointer.toolMinor = inSlot->getToolMinor();
5666 outPointer.orientation = inSlot->getOrientation();
5667 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005668 outPointer.tiltX = 0;
5669 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005670
Jeff Brown49754db2011-07-01 17:37:58 -07005671 outPointer.toolType = inSlot->getToolType();
5672 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5673 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5674 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5675 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5676 }
Jeff Brown8d608662010-08-30 03:02:23 -07005677 }
5678
Jeff Brown65fd2512011-08-18 11:20:58 -07005679 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5680 && (mTouchButtonAccumulator.isHovering()
5681 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005682 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005683
Jeff Brown8d608662010-08-30 03:02:23 -07005684 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005685 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005686 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005687 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005688 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005689 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005690 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005691 if (mPointerTrackingIdMap[n] == trackingId) {
5692 id = n;
5693 }
5694 }
5695
5696 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005697 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005698 mPointerTrackingIdMap[id] = trackingId;
5699 }
5700 }
5701 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005702 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005703 mCurrentRawPointerData.clearIdBits();
5704 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005705 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005706 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005707 mCurrentRawPointerData.idToIndex[id] = outCount;
5708 mCurrentRawPointerData.markIdBit(id, isHovering);
5709 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005710 }
5711 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005712
Jeff Brown6d0fec22010-07-23 21:28:06 -07005713 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005714 }
5715
Jeff Brownbe1aa822011-07-27 16:04:54 -07005716 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005717 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005718
Jeff Brown65fd2512011-08-18 11:20:58 -07005719 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005720}
5721
Jeff Brownbe1aa822011-07-27 16:04:54 -07005722void MultiTouchInputMapper::configureRawPointerAxes() {
5723 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005724
Jeff Brownbe1aa822011-07-27 16:04:54 -07005725 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5726 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5727 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5728 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5729 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5730 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5731 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5732 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5733 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5734 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5735 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005736
Jeff Brownbe1aa822011-07-27 16:04:54 -07005737 if (mRawPointerAxes.trackingId.valid
5738 && mRawPointerAxes.slot.valid
5739 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5740 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005741 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005742 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005743 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005744 getDeviceName().string(), slotCount, MAX_SLOTS);
5745 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005746 }
Jeff Brown49754db2011-07-01 17:37:58 -07005747 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005748 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005749 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005750 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005751}
5752
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005753
Jeff Browncb1404e2011-01-15 18:14:15 -08005754// --- JoystickInputMapper ---
5755
5756JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5757 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005758}
5759
5760JoystickInputMapper::~JoystickInputMapper() {
5761}
5762
5763uint32_t JoystickInputMapper::getSources() {
5764 return AINPUT_SOURCE_JOYSTICK;
5765}
5766
5767void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5768 InputMapper::populateDeviceInfo(info);
5769
Jeff Brown6f2fba42011-02-19 01:08:02 -08005770 for (size_t i = 0; i < mAxes.size(); i++) {
5771 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005772 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5773 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005774 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005775 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5776 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005777 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005778 }
5779}
5780
5781void JoystickInputMapper::dump(String8& dump) {
5782 dump.append(INDENT2 "Joystick Input Mapper:\n");
5783
Jeff Brown6f2fba42011-02-19 01:08:02 -08005784 dump.append(INDENT3 "Axes:\n");
5785 size_t numAxes = mAxes.size();
5786 for (size_t i = 0; i < numAxes; i++) {
5787 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005788 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005789 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005790 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005791 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005792 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005793 }
Jeff Brown85297452011-03-04 13:07:49 -08005794 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5795 label = getAxisLabel(axis.axisInfo.highAxis);
5796 if (label) {
5797 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5798 } else {
5799 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5800 axis.axisInfo.splitValue);
5801 }
5802 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5803 dump.append(" (invert)");
5804 }
5805
5806 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5807 axis.min, axis.max, axis.flat, axis.fuzz);
5808 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5809 "highScale=%0.5f, highOffset=%0.5f\n",
5810 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005811 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5812 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005813 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005814 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005815 }
5816}
5817
Jeff Brown65fd2512011-08-18 11:20:58 -07005818void JoystickInputMapper::configure(nsecs_t when,
5819 const InputReaderConfiguration* config, uint32_t changes) {
5820 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005821
Jeff Brown474dcb52011-06-14 20:22:50 -07005822 if (!changes) { // first time only
5823 // Collect all axes.
5824 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07005825 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5826 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5827 continue; // axis must be claimed by a different device
5828 }
5829
Jeff Brown474dcb52011-06-14 20:22:50 -07005830 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005831 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005832 if (rawAxisInfo.valid) {
5833 // Map axis.
5834 AxisInfo axisInfo;
5835 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5836 if (!explicitlyMapped) {
5837 // Axis is not explicitly mapped, will choose a generic axis later.
5838 axisInfo.mode = AxisInfo::MODE_NORMAL;
5839 axisInfo.axis = -1;
5840 }
5841
5842 // Apply flat override.
5843 int32_t rawFlat = axisInfo.flatOverride < 0
5844 ? rawAxisInfo.flat : axisInfo.flatOverride;
5845
5846 // Calculate scaling factors and limits.
5847 Axis axis;
5848 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5849 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5850 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5851 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5852 scale, 0.0f, highScale, 0.0f,
5853 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5854 } else if (isCenteredAxis(axisInfo.axis)) {
5855 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5856 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5857 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5858 scale, offset, scale, offset,
5859 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5860 } else {
5861 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5862 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5863 scale, 0.0f, scale, 0.0f,
5864 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5865 }
5866
5867 // To eliminate noise while the joystick is at rest, filter out small variations
5868 // in axis values up front.
5869 axis.filter = axis.flat * 0.25f;
5870
5871 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005872 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005873 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005874
Jeff Brown474dcb52011-06-14 20:22:50 -07005875 // If there are too many axes, start dropping them.
5876 // Prefer to keep explicitly mapped axes.
5877 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00005878 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07005879 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5880 pruneAxes(true);
5881 pruneAxes(false);
5882 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005883
Jeff Brown474dcb52011-06-14 20:22:50 -07005884 // Assign generic axis ids to remaining axes.
5885 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5886 size_t numAxes = mAxes.size();
5887 for (size_t i = 0; i < numAxes; i++) {
5888 Axis& axis = mAxes.editValueAt(i);
5889 if (axis.axisInfo.axis < 0) {
5890 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5891 && haveAxis(nextGenericAxisId)) {
5892 nextGenericAxisId += 1;
5893 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005894
Jeff Brown474dcb52011-06-14 20:22:50 -07005895 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5896 axis.axisInfo.axis = nextGenericAxisId;
5897 nextGenericAxisId += 1;
5898 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00005899 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07005900 "have already been assigned to other axes.",
5901 getDeviceName().string(), mAxes.keyAt(i));
5902 mAxes.removeItemsAt(i--);
5903 numAxes -= 1;
5904 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005905 }
5906 }
5907 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005908}
5909
Jeff Brown85297452011-03-04 13:07:49 -08005910bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005911 size_t numAxes = mAxes.size();
5912 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005913 const Axis& axis = mAxes.valueAt(i);
5914 if (axis.axisInfo.axis == axisId
5915 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5916 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005917 return true;
5918 }
5919 }
5920 return false;
5921}
Jeff Browncb1404e2011-01-15 18:14:15 -08005922
Jeff Brown6f2fba42011-02-19 01:08:02 -08005923void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5924 size_t i = mAxes.size();
5925 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5926 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5927 continue;
5928 }
Steve Block6215d3f2012-01-04 20:05:49 +00005929 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005930 getDeviceName().string(), mAxes.keyAt(i));
5931 mAxes.removeItemsAt(i);
5932 }
5933}
5934
5935bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5936 switch (axis) {
5937 case AMOTION_EVENT_AXIS_X:
5938 case AMOTION_EVENT_AXIS_Y:
5939 case AMOTION_EVENT_AXIS_Z:
5940 case AMOTION_EVENT_AXIS_RX:
5941 case AMOTION_EVENT_AXIS_RY:
5942 case AMOTION_EVENT_AXIS_RZ:
5943 case AMOTION_EVENT_AXIS_HAT_X:
5944 case AMOTION_EVENT_AXIS_HAT_Y:
5945 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005946 case AMOTION_EVENT_AXIS_RUDDER:
5947 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005948 return true;
5949 default:
5950 return false;
5951 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005952}
5953
Jeff Brown65fd2512011-08-18 11:20:58 -07005954void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005955 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005956 size_t numAxes = mAxes.size();
5957 for (size_t i = 0; i < numAxes; i++) {
5958 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005959 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005960 }
5961
Jeff Brown65fd2512011-08-18 11:20:58 -07005962 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005963}
5964
5965void JoystickInputMapper::process(const RawEvent* rawEvent) {
5966 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005967 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07005968 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005969 if (index >= 0) {
5970 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005971 float newValue, highNewValue;
5972 switch (axis.axisInfo.mode) {
5973 case AxisInfo::MODE_INVERT:
5974 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5975 * axis.scale + axis.offset;
5976 highNewValue = 0.0f;
5977 break;
5978 case AxisInfo::MODE_SPLIT:
5979 if (rawEvent->value < axis.axisInfo.splitValue) {
5980 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5981 * axis.scale + axis.offset;
5982 highNewValue = 0.0f;
5983 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5984 newValue = 0.0f;
5985 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5986 * axis.highScale + axis.highOffset;
5987 } else {
5988 newValue = 0.0f;
5989 highNewValue = 0.0f;
5990 }
5991 break;
5992 default:
5993 newValue = rawEvent->value * axis.scale + axis.offset;
5994 highNewValue = 0.0f;
5995 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005996 }
Jeff Brown85297452011-03-04 13:07:49 -08005997 axis.newValue = newValue;
5998 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005999 }
6000 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006001 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006002
6003 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07006004 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08006005 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08006006 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08006007 break;
6008 }
6009 break;
6010 }
6011}
6012
Jeff Brown6f2fba42011-02-19 01:08:02 -08006013void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08006014 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006015 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08006016 }
6017
6018 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006019 int32_t buttonState = 0;
6020
6021 PointerProperties pointerProperties;
6022 pointerProperties.clear();
6023 pointerProperties.id = 0;
6024 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08006025
Jeff Brown6f2fba42011-02-19 01:08:02 -08006026 PointerCoords pointerCoords;
6027 pointerCoords.clear();
6028
6029 size_t numAxes = mAxes.size();
6030 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006031 const Axis& axis = mAxes.valueAt(i);
6032 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6033 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6034 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6035 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006036 }
6037
Jeff Brown56194eb2011-03-02 19:23:13 -08006038 // Moving a joystick axis should not wake the devide because joysticks can
6039 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6040 // button will likely wake the device.
6041 // TODO: Use the input device configuration to control this behavior more finely.
6042 uint32_t policyFlags = 0;
6043
Jeff Brownbe1aa822011-07-27 16:04:54 -07006044 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006045 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
6046 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006047 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006048}
6049
Jeff Brown85297452011-03-04 13:07:49 -08006050bool JoystickInputMapper::filterAxes(bool force) {
6051 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006052 size_t numAxes = mAxes.size();
6053 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006054 Axis& axis = mAxes.editValueAt(i);
6055 if (force || hasValueChangedSignificantly(axis.filter,
6056 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6057 axis.currentValue = axis.newValue;
6058 atLeastOneSignificantChange = true;
6059 }
6060 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6061 if (force || hasValueChangedSignificantly(axis.filter,
6062 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6063 axis.highCurrentValue = axis.highNewValue;
6064 atLeastOneSignificantChange = true;
6065 }
6066 }
6067 }
6068 return atLeastOneSignificantChange;
6069}
6070
6071bool JoystickInputMapper::hasValueChangedSignificantly(
6072 float filter, float newValue, float currentValue, float min, float max) {
6073 if (newValue != currentValue) {
6074 // Filter out small changes in value unless the value is converging on the axis
6075 // bounds or center point. This is intended to reduce the amount of information
6076 // sent to applications by particularly noisy joysticks (such as PS3).
6077 if (fabs(newValue - currentValue) > filter
6078 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6079 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6080 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6081 return true;
6082 }
6083 }
6084 return false;
6085}
6086
6087bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6088 float filter, float newValue, float currentValue, float thresholdValue) {
6089 float newDistance = fabs(newValue - thresholdValue);
6090 if (newDistance < filter) {
6091 float oldDistance = fabs(currentValue - thresholdValue);
6092 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006093 return true;
6094 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006095 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006096 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006097}
6098
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006099} // namespace android