blob: eccce292056d9e50a339727c7bcb757023f5a63a [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 Brown1a84fd12011-06-02 01:26:32 -0700240 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700241 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700242 mQueuedListener = new QueuedInputListener(listener);
243
244 { // acquire lock
245 AutoMutex _l(mLock);
246
247 refreshConfigurationLocked(0);
248 updateGlobalMetaStateLocked();
249 updateInputConfigurationLocked();
250 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700251}
252
253InputReader::~InputReader() {
254 for (size_t i = 0; i < mDevices.size(); i++) {
255 delete mDevices.valueAt(i);
256 }
257}
258
259void InputReader::loopOnce() {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700260 int32_t timeoutMillis;
Jeff Brown474dcb52011-06-14 20:22:50 -0700261 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700262 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700263
Jeff Brownbe1aa822011-07-27 16:04:54 -0700264 uint32_t changes = mConfigurationChangesToRefresh;
265 if (changes) {
266 mConfigurationChangesToRefresh = 0;
267 refreshConfigurationLocked(changes);
268 }
269
270 timeoutMillis = -1;
271 if (mNextTimeout != LLONG_MAX) {
272 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
273 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
274 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700275 } // release lock
276
Jeff Brownb7198742011-03-18 18:14:26 -0700277 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700278
279 { // acquire lock
280 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800281 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700282
283 if (count) {
284 processEventsLocked(mEventBuffer, count);
285 }
286 if (!count || timeoutMillis == 0) {
287 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800288 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700289#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800290 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700291#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800292 mNextTimeout = LLONG_MAX;
293 timeoutExpiredLocked(now);
294 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700295 }
296 } // release lock
297
298 // Flush queued events out to the listener.
299 // This must happen outside of the lock because the listener could potentially call
300 // back into the InputReader's methods, such as getScanCodeState, or become blocked
301 // on another thread similarly waiting to acquire the InputReader lock thereby
302 // resulting in a deadlock. This situation is actually quite plausible because the
303 // listener is actually the input dispatcher, which calls into the window manager,
304 // which occasionally calls into the input reader.
305 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700306}
307
Jeff Brownbe1aa822011-07-27 16:04:54 -0700308void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700309 for (const RawEvent* rawEvent = rawEvents; count;) {
310 int32_t type = rawEvent->type;
311 size_t batchSize = 1;
312 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
313 int32_t deviceId = rawEvent->deviceId;
314 while (batchSize < count) {
315 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
316 || rawEvent[batchSize].deviceId != deviceId) {
317 break;
318 }
319 batchSize += 1;
320 }
321#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000322 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700323#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700324 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700325 } else {
326 switch (rawEvent->type) {
327 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700329 break;
330 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700331 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700332 break;
333 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700334 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700335 break;
336 default:
Steve Blockec193de2012-01-09 18:35:44 +0000337 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700338 break;
339 }
340 }
341 count -= batchSize;
342 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700343 }
344}
345
Jeff Brown65fd2512011-08-18 11:20:58 -0700346void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700347 String8 name = mEventHub->getDeviceName(deviceId);
348 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
349
Jeff Brownbe1aa822011-07-27 16:04:54 -0700350 InputDevice* device = createDeviceLocked(deviceId, name, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700351 device->configure(when, &mConfig, 0);
352 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700353
Jeff Brown8d608662010-08-30 03:02:23 -0700354 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000355 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700356 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000357 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700358 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700359 }
360
Jeff Brownbe1aa822011-07-27 16:04:54 -0700361 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
362 if (deviceIndex < 0) {
363 mDevices.add(deviceId, device);
364 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000365 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700366 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700367 return;
368 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700369}
370
Jeff Brown65fd2512011-08-18 11:20:58 -0700371void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700373 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
374 if (deviceIndex >= 0) {
375 device = mDevices.valueAt(deviceIndex);
376 mDevices.removeItemsAt(deviceIndex, 1);
377 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000378 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700379 return;
380 }
381
Jeff Brown6d0fec22010-07-23 21:28:06 -0700382 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000383 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384 device->getId(), device->getName().string());
385 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000386 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387 device->getId(), device->getName().string(), device->getSources());
388 }
389
Jeff Brown65fd2512011-08-18 11:20:58 -0700390 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700391 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700392}
393
Jeff Brownbe1aa822011-07-27 16:04:54 -0700394InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
395 const String8& name, uint32_t classes) {
Jeff Brown9ee285af2011-08-31 12:56:34 -0700396 InputDevice* device = new InputDevice(&mContext, deviceId, name, classes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700397
Jeff Brown56194eb2011-03-02 19:23:13 -0800398 // External devices.
399 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
400 device->setExternal(true);
401 }
402
Jeff Brown6d0fec22010-07-23 21:28:06 -0700403 // Switch-like devices.
404 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
405 device->addMapper(new SwitchInputMapper(device));
406 }
407
408 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800409 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
411 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800412 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700413 }
414 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
415 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
416 }
417 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800418 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700419 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800420 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800421 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800422 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423
Jeff Brownefd32662011-03-08 15:13:06 -0800424 if (keyboardSource != 0) {
425 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700426 }
427
Jeff Brown83c09682010-12-23 17:50:18 -0800428 // Cursor-like devices.
429 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
430 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431 }
432
Jeff Brown58a2da82011-01-25 16:02:22 -0800433 // Touchscreens and touchpad devices.
434 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800435 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800436 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800437 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700438 }
439
Jeff Browncb1404e2011-01-15 18:14:15 -0800440 // Joystick-like devices.
441 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
442 device->addMapper(new JoystickInputMapper(device));
443 }
444
Jeff Brown6d0fec22010-07-23 21:28:06 -0700445 return device;
446}
447
Jeff Brownbe1aa822011-07-27 16:04:54 -0700448void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700449 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700450 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
451 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000452 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700453 return;
454 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455
Jeff Brownbe1aa822011-07-27 16:04:54 -0700456 InputDevice* device = mDevices.valueAt(deviceIndex);
457 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000458 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700459 return;
460 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461
Jeff Brownbe1aa822011-07-27 16:04:54 -0700462 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700463}
464
Jeff Brownbe1aa822011-07-27 16:04:54 -0700465void InputReader::timeoutExpiredLocked(nsecs_t when) {
466 for (size_t i = 0; i < mDevices.size(); i++) {
467 InputDevice* device = mDevices.valueAt(i);
468 if (!device->isIgnored()) {
469 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700470 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700472}
473
Jeff Brownbe1aa822011-07-27 16:04:54 -0700474void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700475 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700476 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477
478 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700479 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700480
481 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700482 NotifyConfigurationChangedArgs args(when);
483 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700484}
485
Jeff Brownbe1aa822011-07-27 16:04:54 -0700486void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700487 mPolicy->getReaderConfiguration(&mConfig);
488 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
489
Jeff Brown474dcb52011-06-14 20:22:50 -0700490 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000491 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700492 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700493
494 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
495 mEventHub->requestReopenDevices();
496 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700497 for (size_t i = 0; i < mDevices.size(); i++) {
498 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700499 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700500 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700501 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700502 }
503}
504
Jeff Brownbe1aa822011-07-27 16:04:54 -0700505void InputReader::updateGlobalMetaStateLocked() {
506 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507
Jeff Brownbe1aa822011-07-27 16:04:54 -0700508 for (size_t i = 0; i < mDevices.size(); i++) {
509 InputDevice* device = mDevices.valueAt(i);
510 mGlobalMetaState |= device->getMetaState();
511 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700512}
513
Jeff Brownbe1aa822011-07-27 16:04:54 -0700514int32_t InputReader::getGlobalMetaStateLocked() {
515 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700516}
517
Jeff Brownbe1aa822011-07-27 16:04:54 -0700518void InputReader::updateInputConfigurationLocked() {
519 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
520 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
521 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
522 InputDeviceInfo deviceInfo;
523 for (size_t i = 0; i < mDevices.size(); i++) {
524 InputDevice* device = mDevices.valueAt(i);
525 device->getDeviceInfo(& deviceInfo);
526 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700527
Jeff Brownbe1aa822011-07-27 16:04:54 -0700528 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
529 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
530 }
531 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
532 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
533 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
534 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
535 }
536 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
537 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
538 }
539 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700540
Jeff Brownbe1aa822011-07-27 16:04:54 -0700541 mInputConfiguration.touchScreen = touchScreenConfig;
542 mInputConfiguration.keyboard = keyboardConfig;
543 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700544}
545
Jeff Brownbe1aa822011-07-27 16:04:54 -0700546void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800547 mDisableVirtualKeysTimeout = time;
548}
549
Jeff Brownbe1aa822011-07-27 16:04:54 -0700550bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800551 InputDevice* device, int32_t keyCode, int32_t scanCode) {
552 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000553 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800554 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
555 device->getName().string(),
556 (mDisableVirtualKeysTimeout - now) * 0.000001,
557 keyCode, scanCode);
558 return true;
559 } else {
560 return false;
561 }
562}
563
Jeff Brownbe1aa822011-07-27 16:04:54 -0700564void InputReader::fadePointerLocked() {
565 for (size_t i = 0; i < mDevices.size(); i++) {
566 InputDevice* device = mDevices.valueAt(i);
567 device->fadePointer();
568 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800569}
570
Jeff Brownbe1aa822011-07-27 16:04:54 -0700571void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700572 if (when < mNextTimeout) {
573 mNextTimeout = when;
574 }
575}
576
Jeff Brown6d0fec22010-07-23 21:28:06 -0700577void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700578 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700579
Jeff Brownbe1aa822011-07-27 16:04:54 -0700580 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700581}
582
583status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700584 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700585
Jeff Brownbe1aa822011-07-27 16:04:54 -0700586 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
587 if (deviceIndex < 0) {
588 return NAME_NOT_FOUND;
589 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700590
Jeff Brownbe1aa822011-07-27 16:04:54 -0700591 InputDevice* device = mDevices.valueAt(deviceIndex);
592 if (device->isIgnored()) {
593 return NAME_NOT_FOUND;
594 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595
Jeff Brownbe1aa822011-07-27 16:04:54 -0700596 device->getDeviceInfo(outDeviceInfo);
597 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598}
599
600void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700601 AutoMutex _l(mLock);
602
Jeff Brown6d0fec22010-07-23 21:28:06 -0700603 outDeviceIds.clear();
604
Jeff Brownbe1aa822011-07-27 16:04:54 -0700605 size_t numDevices = mDevices.size();
606 for (size_t i = 0; i < numDevices; i++) {
607 InputDevice* device = mDevices.valueAt(i);
608 if (!device->isIgnored()) {
609 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700610 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700611 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700612}
613
614int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
615 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700616 AutoMutex _l(mLock);
617
618 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700619}
620
621int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
622 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700623 AutoMutex _l(mLock);
624
625 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700626}
627
628int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700629 AutoMutex _l(mLock);
630
631 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700632}
633
Jeff Brownbe1aa822011-07-27 16:04:54 -0700634int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700635 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700636 int32_t result = AKEY_STATE_UNKNOWN;
637 if (deviceId >= 0) {
638 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
639 if (deviceIndex >= 0) {
640 InputDevice* device = mDevices.valueAt(deviceIndex);
641 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
642 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700643 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700644 }
645 } else {
646 size_t numDevices = mDevices.size();
647 for (size_t i = 0; i < numDevices; i++) {
648 InputDevice* device = mDevices.valueAt(i);
649 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800650 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
651 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
652 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
653 if (currentResult >= AKEY_STATE_DOWN) {
654 return currentResult;
655 } else if (currentResult == AKEY_STATE_UP) {
656 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700657 }
658 }
659 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700660 }
661 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700662}
663
664bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
665 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700666 AutoMutex _l(mLock);
667
Jeff Brown6d0fec22010-07-23 21:28:06 -0700668 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700669 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700670}
671
Jeff Brownbe1aa822011-07-27 16:04:54 -0700672bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
673 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
674 bool result = false;
675 if (deviceId >= 0) {
676 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
677 if (deviceIndex >= 0) {
678 InputDevice* device = mDevices.valueAt(deviceIndex);
679 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
680 result = device->markSupportedKeyCodes(sourceMask,
681 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700682 }
683 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700684 } else {
685 size_t numDevices = mDevices.size();
686 for (size_t i = 0; i < numDevices; i++) {
687 InputDevice* device = mDevices.valueAt(i);
688 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
689 result |= device->markSupportedKeyCodes(sourceMask,
690 numCodes, keyCodes, outFlags);
691 }
692 }
693 }
694 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700695}
696
Jeff Brown474dcb52011-06-14 20:22:50 -0700697void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700698 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700699
Jeff Brownbe1aa822011-07-27 16:04:54 -0700700 if (changes) {
701 bool needWake = !mConfigurationChangesToRefresh;
702 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700703
704 if (needWake) {
705 mEventHub->wake();
706 }
707 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700708}
709
Jeff Brownb88102f2010-09-08 11:49:43 -0700710void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700711 AutoMutex _l(mLock);
712
Jeff Brownf2f487182010-10-01 17:46:21 -0700713 mEventHub->dump(dump);
714 dump.append("\n");
715
716 dump.append("Input Reader State:\n");
717
Jeff Brownbe1aa822011-07-27 16:04:54 -0700718 for (size_t i = 0; i < mDevices.size(); i++) {
719 mDevices.valueAt(i)->dump(dump);
720 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700721
722 dump.append(INDENT "Configuration:\n");
723 dump.append(INDENT2 "ExcludedDeviceNames: [");
724 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
725 if (i != 0) {
726 dump.append(", ");
727 }
728 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
729 }
730 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700731 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
732 mConfig.virtualKeyQuietTime * 0.000001f);
733
Jeff Brown19c97d462011-06-01 12:33:19 -0700734 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
735 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
736 mConfig.pointerVelocityControlParameters.scale,
737 mConfig.pointerVelocityControlParameters.lowThreshold,
738 mConfig.pointerVelocityControlParameters.highThreshold,
739 mConfig.pointerVelocityControlParameters.acceleration);
740
741 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
742 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
743 mConfig.wheelVelocityControlParameters.scale,
744 mConfig.wheelVelocityControlParameters.lowThreshold,
745 mConfig.wheelVelocityControlParameters.highThreshold,
746 mConfig.wheelVelocityControlParameters.acceleration);
747
Jeff Brown214eaf42011-05-26 19:17:02 -0700748 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700749 dump.appendFormat(INDENT3 "Enabled: %s\n",
750 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700751 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
752 mConfig.pointerGestureQuietInterval * 0.000001f);
753 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
754 mConfig.pointerGestureDragMinSwitchSpeed);
755 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
756 mConfig.pointerGestureTapInterval * 0.000001f);
757 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
758 mConfig.pointerGestureTapDragInterval * 0.000001f);
759 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
760 mConfig.pointerGestureTapSlop);
761 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
762 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700763 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
764 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700765 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
766 mConfig.pointerGestureSwipeTransitionAngleCosine);
767 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
768 mConfig.pointerGestureSwipeMaxWidthRatio);
769 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
770 mConfig.pointerGestureMovementSpeedRatio);
771 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
772 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700773}
774
Jeff Brown89ef0722011-08-10 16:25:21 -0700775void InputReader::monitor() {
776 // Acquire and release the lock to ensure that the reader has not deadlocked.
777 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800778 mEventHub->wake();
779 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700780 mLock.unlock();
781
782 // Check the EventHub
783 mEventHub->monitor();
784}
785
Jeff Brown6d0fec22010-07-23 21:28:06 -0700786
Jeff Brownbe1aa822011-07-27 16:04:54 -0700787// --- InputReader::ContextImpl ---
788
789InputReader::ContextImpl::ContextImpl(InputReader* reader) :
790 mReader(reader) {
791}
792
793void InputReader::ContextImpl::updateGlobalMetaState() {
794 // lock is already held by the input loop
795 mReader->updateGlobalMetaStateLocked();
796}
797
798int32_t InputReader::ContextImpl::getGlobalMetaState() {
799 // lock is already held by the input loop
800 return mReader->getGlobalMetaStateLocked();
801}
802
803void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
804 // lock is already held by the input loop
805 mReader->disableVirtualKeysUntilLocked(time);
806}
807
808bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
809 InputDevice* device, int32_t keyCode, int32_t scanCode) {
810 // lock is already held by the input loop
811 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
812}
813
814void InputReader::ContextImpl::fadePointer() {
815 // lock is already held by the input loop
816 mReader->fadePointerLocked();
817}
818
819void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
820 // lock is already held by the input loop
821 mReader->requestTimeoutAtTimeLocked(when);
822}
823
824InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
825 return mReader->mPolicy.get();
826}
827
828InputListenerInterface* InputReader::ContextImpl::getListener() {
829 return mReader->mQueuedListener.get();
830}
831
832EventHubInterface* InputReader::ContextImpl::getEventHub() {
833 return mReader->mEventHub.get();
834}
835
836
Jeff Brown6d0fec22010-07-23 21:28:06 -0700837// --- InputReaderThread ---
838
839InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
840 Thread(/*canCallJava*/ true), mReader(reader) {
841}
842
843InputReaderThread::~InputReaderThread() {
844}
845
846bool InputReaderThread::threadLoop() {
847 mReader->loopOnce();
848 return true;
849}
850
851
852// --- InputDevice ---
853
Jeff Brown9ee285af2011-08-31 12:56:34 -0700854InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name,
855 uint32_t classes) :
856 mContext(context), mId(id), mName(name), mClasses(classes),
857 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700858}
859
860InputDevice::~InputDevice() {
861 size_t numMappers = mMappers.size();
862 for (size_t i = 0; i < numMappers; i++) {
863 delete mMappers[i];
864 }
865 mMappers.clear();
866}
867
Jeff Brownef3d7e82010-09-30 14:33:04 -0700868void InputDevice::dump(String8& dump) {
869 InputDeviceInfo deviceInfo;
870 getDeviceInfo(& deviceInfo);
871
Jeff Brown90655042010-12-02 13:50:46 -0800872 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700873 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800874 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700875 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
876 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800877
Jeff Brownefd32662011-03-08 15:13:06 -0800878 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800879 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700880 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800881 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800882 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
883 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800884 char name[32];
885 if (label) {
886 strncpy(name, label, sizeof(name));
887 name[sizeof(name) - 1] = '\0';
888 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800889 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800890 }
Jeff Brownefd32662011-03-08 15:13:06 -0800891 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
892 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
893 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800894 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700895 }
896
897 size_t numMappers = mMappers.size();
898 for (size_t i = 0; i < numMappers; i++) {
899 InputMapper* mapper = mMappers[i];
900 mapper->dump(dump);
901 }
902}
903
Jeff Brown6d0fec22010-07-23 21:28:06 -0700904void InputDevice::addMapper(InputMapper* mapper) {
905 mMappers.add(mapper);
906}
907
Jeff Brown65fd2512011-08-18 11:20:58 -0700908void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700909 mSources = 0;
910
Jeff Brown474dcb52011-06-14 20:22:50 -0700911 if (!isIgnored()) {
912 if (!changes) { // first time only
913 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
914 }
915
916 size_t numMappers = mMappers.size();
917 for (size_t i = 0; i < numMappers; i++) {
918 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700919 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700920 mSources |= mapper->getSources();
921 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700922 }
923}
924
Jeff Brown65fd2512011-08-18 11:20:58 -0700925void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700926 size_t numMappers = mMappers.size();
927 for (size_t i = 0; i < numMappers; i++) {
928 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700929 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700930 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700931
932 mContext->updateGlobalMetaState();
933
934 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700935}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700936
Jeff Brownb7198742011-03-18 18:14:26 -0700937void InputDevice::process(const RawEvent* rawEvents, size_t count) {
938 // Process all of the events in order for each mapper.
939 // We cannot simply ask each mapper to process them in bulk because mappers may
940 // have side-effects that must be interleaved. For example, joystick movement events and
941 // gamepad button presses are handled by different mappers but they should be dispatched
942 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700943 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700944 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
945#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000946 ALOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700947 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700948 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
949 rawEvent->value, rawEvent->flags);
950#endif
951
Jeff Brown80fd47c2011-05-24 01:07:44 -0700952 if (mDropUntilNextSync) {
953 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
954 mDropUntilNextSync = false;
955#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000956 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700957#endif
958 } else {
959#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000960 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700961#endif
962 }
963 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
Steve Block6215d3f2012-01-04 20:05:49 +0000964 ALOGI("Detected input event buffer overrun for device %s.", mName.string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700965 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700966 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700967 } else {
968 for (size_t i = 0; i < numMappers; i++) {
969 InputMapper* mapper = mMappers[i];
970 mapper->process(rawEvent);
971 }
Jeff Brownb7198742011-03-18 18:14:26 -0700972 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700973 }
974}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700975
Jeff Brownaa3855d2011-03-17 01:34:19 -0700976void InputDevice::timeoutExpired(nsecs_t when) {
977 size_t numMappers = mMappers.size();
978 for (size_t i = 0; i < numMappers; i++) {
979 InputMapper* mapper = mMappers[i];
980 mapper->timeoutExpired(when);
981 }
982}
983
Jeff Brown6d0fec22010-07-23 21:28:06 -0700984void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
985 outDeviceInfo->initialize(mId, mName);
986
987 size_t numMappers = mMappers.size();
988 for (size_t i = 0; i < numMappers; i++) {
989 InputMapper* mapper = mMappers[i];
990 mapper->populateDeviceInfo(outDeviceInfo);
991 }
992}
993
994int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
995 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
996}
997
998int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
999 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1000}
1001
1002int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1003 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1004}
1005
1006int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1007 int32_t result = AKEY_STATE_UNKNOWN;
1008 size_t numMappers = mMappers.size();
1009 for (size_t i = 0; i < numMappers; i++) {
1010 InputMapper* mapper = mMappers[i];
1011 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001012 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1013 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1014 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1015 if (currentResult >= AKEY_STATE_DOWN) {
1016 return currentResult;
1017 } else if (currentResult == AKEY_STATE_UP) {
1018 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019 }
1020 }
1021 }
1022 return result;
1023}
1024
1025bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1026 const int32_t* keyCodes, uint8_t* outFlags) {
1027 bool result = false;
1028 size_t numMappers = mMappers.size();
1029 for (size_t i = 0; i < numMappers; i++) {
1030 InputMapper* mapper = mMappers[i];
1031 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1032 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1033 }
1034 }
1035 return result;
1036}
1037
1038int32_t InputDevice::getMetaState() {
1039 int32_t result = 0;
1040 size_t numMappers = mMappers.size();
1041 for (size_t i = 0; i < numMappers; i++) {
1042 InputMapper* mapper = mMappers[i];
1043 result |= mapper->getMetaState();
1044 }
1045 return result;
1046}
1047
Jeff Brown05dc66a2011-03-02 14:41:58 -08001048void InputDevice::fadePointer() {
1049 size_t numMappers = mMappers.size();
1050 for (size_t i = 0; i < numMappers; i++) {
1051 InputMapper* mapper = mMappers[i];
1052 mapper->fadePointer();
1053 }
1054}
1055
Jeff Brown65fd2512011-08-18 11:20:58 -07001056void InputDevice::notifyReset(nsecs_t when) {
1057 NotifyDeviceResetArgs args(when, mId);
1058 mContext->getListener()->notifyDeviceReset(&args);
1059}
1060
Jeff Brown6d0fec22010-07-23 21:28:06 -07001061
Jeff Brown49754db2011-07-01 17:37:58 -07001062// --- CursorButtonAccumulator ---
1063
1064CursorButtonAccumulator::CursorButtonAccumulator() {
1065 clearButtons();
1066}
1067
Jeff Brown65fd2512011-08-18 11:20:58 -07001068void CursorButtonAccumulator::reset(InputDevice* device) {
1069 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1070 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1071 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1072 mBtnBack = device->isKeyPressed(BTN_BACK);
1073 mBtnSide = device->isKeyPressed(BTN_SIDE);
1074 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1075 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1076 mBtnTask = device->isKeyPressed(BTN_TASK);
1077}
1078
Jeff Brown49754db2011-07-01 17:37:58 -07001079void CursorButtonAccumulator::clearButtons() {
1080 mBtnLeft = 0;
1081 mBtnRight = 0;
1082 mBtnMiddle = 0;
1083 mBtnBack = 0;
1084 mBtnSide = 0;
1085 mBtnForward = 0;
1086 mBtnExtra = 0;
1087 mBtnTask = 0;
1088}
1089
1090void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1091 if (rawEvent->type == EV_KEY) {
1092 switch (rawEvent->scanCode) {
1093 case BTN_LEFT:
1094 mBtnLeft = rawEvent->value;
1095 break;
1096 case BTN_RIGHT:
1097 mBtnRight = rawEvent->value;
1098 break;
1099 case BTN_MIDDLE:
1100 mBtnMiddle = rawEvent->value;
1101 break;
1102 case BTN_BACK:
1103 mBtnBack = rawEvent->value;
1104 break;
1105 case BTN_SIDE:
1106 mBtnSide = rawEvent->value;
1107 break;
1108 case BTN_FORWARD:
1109 mBtnForward = rawEvent->value;
1110 break;
1111 case BTN_EXTRA:
1112 mBtnExtra = rawEvent->value;
1113 break;
1114 case BTN_TASK:
1115 mBtnTask = rawEvent->value;
1116 break;
1117 }
1118 }
1119}
1120
1121uint32_t CursorButtonAccumulator::getButtonState() const {
1122 uint32_t result = 0;
1123 if (mBtnLeft) {
1124 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1125 }
1126 if (mBtnRight) {
1127 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1128 }
1129 if (mBtnMiddle) {
1130 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1131 }
1132 if (mBtnBack || mBtnSide) {
1133 result |= AMOTION_EVENT_BUTTON_BACK;
1134 }
1135 if (mBtnForward || mBtnExtra) {
1136 result |= AMOTION_EVENT_BUTTON_FORWARD;
1137 }
1138 return result;
1139}
1140
1141
1142// --- CursorMotionAccumulator ---
1143
Jeff Brown65fd2512011-08-18 11:20:58 -07001144CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001145 clearRelativeAxes();
1146}
1147
Jeff Brown65fd2512011-08-18 11:20:58 -07001148void CursorMotionAccumulator::reset(InputDevice* device) {
1149 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001150}
1151
1152void CursorMotionAccumulator::clearRelativeAxes() {
1153 mRelX = 0;
1154 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001155}
1156
1157void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1158 if (rawEvent->type == EV_REL) {
1159 switch (rawEvent->scanCode) {
1160 case REL_X:
1161 mRelX = rawEvent->value;
1162 break;
1163 case REL_Y:
1164 mRelY = rawEvent->value;
1165 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001166 }
1167 }
1168}
1169
1170void CursorMotionAccumulator::finishSync() {
1171 clearRelativeAxes();
1172}
1173
1174
1175// --- CursorScrollAccumulator ---
1176
1177CursorScrollAccumulator::CursorScrollAccumulator() :
1178 mHaveRelWheel(false), mHaveRelHWheel(false) {
1179 clearRelativeAxes();
1180}
1181
1182void CursorScrollAccumulator::configure(InputDevice* device) {
1183 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1184 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1185}
1186
1187void CursorScrollAccumulator::reset(InputDevice* device) {
1188 clearRelativeAxes();
1189}
1190
1191void CursorScrollAccumulator::clearRelativeAxes() {
1192 mRelWheel = 0;
1193 mRelHWheel = 0;
1194}
1195
1196void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1197 if (rawEvent->type == EV_REL) {
1198 switch (rawEvent->scanCode) {
Jeff Brown49754db2011-07-01 17:37:58 -07001199 case REL_WHEEL:
1200 mRelWheel = rawEvent->value;
1201 break;
1202 case REL_HWHEEL:
1203 mRelHWheel = rawEvent->value;
1204 break;
1205 }
1206 }
1207}
1208
Jeff Brown65fd2512011-08-18 11:20:58 -07001209void CursorScrollAccumulator::finishSync() {
1210 clearRelativeAxes();
1211}
1212
Jeff Brown49754db2011-07-01 17:37:58 -07001213
1214// --- TouchButtonAccumulator ---
1215
1216TouchButtonAccumulator::TouchButtonAccumulator() :
1217 mHaveBtnTouch(false) {
1218 clearButtons();
1219}
1220
1221void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001222 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1223}
1224
1225void TouchButtonAccumulator::reset(InputDevice* device) {
1226 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1227 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1228 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1229 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1230 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1231 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1232 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1233 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1234 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1235 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1236 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001237 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1238 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1239 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001240}
1241
1242void TouchButtonAccumulator::clearButtons() {
1243 mBtnTouch = 0;
1244 mBtnStylus = 0;
1245 mBtnStylus2 = 0;
1246 mBtnToolFinger = 0;
1247 mBtnToolPen = 0;
1248 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001249 mBtnToolBrush = 0;
1250 mBtnToolPencil = 0;
1251 mBtnToolAirbrush = 0;
1252 mBtnToolMouse = 0;
1253 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001254 mBtnToolDoubleTap = 0;
1255 mBtnToolTripleTap = 0;
1256 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001257}
1258
1259void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1260 if (rawEvent->type == EV_KEY) {
1261 switch (rawEvent->scanCode) {
1262 case BTN_TOUCH:
1263 mBtnTouch = rawEvent->value;
1264 break;
1265 case BTN_STYLUS:
1266 mBtnStylus = rawEvent->value;
1267 break;
1268 case BTN_STYLUS2:
1269 mBtnStylus2 = rawEvent->value;
1270 break;
1271 case BTN_TOOL_FINGER:
1272 mBtnToolFinger = rawEvent->value;
1273 break;
1274 case BTN_TOOL_PEN:
1275 mBtnToolPen = rawEvent->value;
1276 break;
1277 case BTN_TOOL_RUBBER:
1278 mBtnToolRubber = rawEvent->value;
1279 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001280 case BTN_TOOL_BRUSH:
1281 mBtnToolBrush = rawEvent->value;
1282 break;
1283 case BTN_TOOL_PENCIL:
1284 mBtnToolPencil = rawEvent->value;
1285 break;
1286 case BTN_TOOL_AIRBRUSH:
1287 mBtnToolAirbrush = rawEvent->value;
1288 break;
1289 case BTN_TOOL_MOUSE:
1290 mBtnToolMouse = rawEvent->value;
1291 break;
1292 case BTN_TOOL_LENS:
1293 mBtnToolLens = rawEvent->value;
1294 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001295 case BTN_TOOL_DOUBLETAP:
1296 mBtnToolDoubleTap = rawEvent->value;
1297 break;
1298 case BTN_TOOL_TRIPLETAP:
1299 mBtnToolTripleTap = rawEvent->value;
1300 break;
1301 case BTN_TOOL_QUADTAP:
1302 mBtnToolQuadTap = rawEvent->value;
1303 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001304 }
1305 }
1306}
1307
1308uint32_t TouchButtonAccumulator::getButtonState() const {
1309 uint32_t result = 0;
1310 if (mBtnStylus) {
1311 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1312 }
1313 if (mBtnStylus2) {
1314 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1315 }
1316 return result;
1317}
1318
1319int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001320 if (mBtnToolMouse || mBtnToolLens) {
1321 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1322 }
Jeff Brown49754db2011-07-01 17:37:58 -07001323 if (mBtnToolRubber) {
1324 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1325 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001326 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001327 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1328 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001329 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001330 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1331 }
1332 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1333}
1334
Jeff Brownd87c6d52011-08-10 14:55:59 -07001335bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001336 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1337 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001338 || mBtnToolMouse || mBtnToolLens
1339 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001340}
1341
1342bool TouchButtonAccumulator::isHovering() const {
1343 return mHaveBtnTouch && !mBtnTouch;
1344}
1345
1346
Jeff Brownbe1aa822011-07-27 16:04:54 -07001347// --- RawPointerAxes ---
1348
1349RawPointerAxes::RawPointerAxes() {
1350 clear();
1351}
1352
1353void RawPointerAxes::clear() {
1354 x.clear();
1355 y.clear();
1356 pressure.clear();
1357 touchMajor.clear();
1358 touchMinor.clear();
1359 toolMajor.clear();
1360 toolMinor.clear();
1361 orientation.clear();
1362 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001363 tiltX.clear();
1364 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001365 trackingId.clear();
1366 slot.clear();
1367}
1368
1369
1370// --- RawPointerData ---
1371
1372RawPointerData::RawPointerData() {
1373 clear();
1374}
1375
1376void RawPointerData::clear() {
1377 pointerCount = 0;
1378 clearIdBits();
1379}
1380
1381void RawPointerData::copyFrom(const RawPointerData& other) {
1382 pointerCount = other.pointerCount;
1383 hoveringIdBits = other.hoveringIdBits;
1384 touchingIdBits = other.touchingIdBits;
1385
1386 for (uint32_t i = 0; i < pointerCount; i++) {
1387 pointers[i] = other.pointers[i];
1388
1389 int id = pointers[i].id;
1390 idToIndex[id] = other.idToIndex[id];
1391 }
1392}
1393
1394void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1395 float x = 0, y = 0;
1396 uint32_t count = touchingIdBits.count();
1397 if (count) {
1398 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1399 uint32_t id = idBits.clearFirstMarkedBit();
1400 const Pointer& pointer = pointerForId(id);
1401 x += pointer.x;
1402 y += pointer.y;
1403 }
1404 x /= count;
1405 y /= count;
1406 }
1407 *outX = x;
1408 *outY = y;
1409}
1410
1411
1412// --- CookedPointerData ---
1413
1414CookedPointerData::CookedPointerData() {
1415 clear();
1416}
1417
1418void CookedPointerData::clear() {
1419 pointerCount = 0;
1420 hoveringIdBits.clear();
1421 touchingIdBits.clear();
1422}
1423
1424void CookedPointerData::copyFrom(const CookedPointerData& other) {
1425 pointerCount = other.pointerCount;
1426 hoveringIdBits = other.hoveringIdBits;
1427 touchingIdBits = other.touchingIdBits;
1428
1429 for (uint32_t i = 0; i < pointerCount; i++) {
1430 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1431 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1432
1433 int id = pointerProperties[i].id;
1434 idToIndex[id] = other.idToIndex[id];
1435 }
1436}
1437
1438
Jeff Brown49754db2011-07-01 17:37:58 -07001439// --- SingleTouchMotionAccumulator ---
1440
1441SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1442 clearAbsoluteAxes();
1443}
1444
Jeff Brown65fd2512011-08-18 11:20:58 -07001445void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1446 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1447 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1448 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1449 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1450 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1451 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1452 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1453}
1454
Jeff Brown49754db2011-07-01 17:37:58 -07001455void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1456 mAbsX = 0;
1457 mAbsY = 0;
1458 mAbsPressure = 0;
1459 mAbsToolWidth = 0;
1460 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001461 mAbsTiltX = 0;
1462 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001463}
1464
1465void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1466 if (rawEvent->type == EV_ABS) {
1467 switch (rawEvent->scanCode) {
1468 case ABS_X:
1469 mAbsX = rawEvent->value;
1470 break;
1471 case ABS_Y:
1472 mAbsY = rawEvent->value;
1473 break;
1474 case ABS_PRESSURE:
1475 mAbsPressure = rawEvent->value;
1476 break;
1477 case ABS_TOOL_WIDTH:
1478 mAbsToolWidth = rawEvent->value;
1479 break;
1480 case ABS_DISTANCE:
1481 mAbsDistance = rawEvent->value;
1482 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001483 case ABS_TILT_X:
1484 mAbsTiltX = rawEvent->value;
1485 break;
1486 case ABS_TILT_Y:
1487 mAbsTiltY = rawEvent->value;
1488 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001489 }
1490 }
1491}
1492
1493
1494// --- MultiTouchMotionAccumulator ---
1495
1496MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1497 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1498}
1499
1500MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1501 delete[] mSlots;
1502}
1503
1504void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1505 mSlotCount = slotCount;
1506 mUsingSlotsProtocol = usingSlotsProtocol;
1507
1508 delete[] mSlots;
1509 mSlots = new Slot[slotCount];
1510}
1511
Jeff Brown65fd2512011-08-18 11:20:58 -07001512void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1513 // Unfortunately there is no way to read the initial contents of the slots.
1514 // So when we reset the accumulator, we must assume they are all zeroes.
1515 if (mUsingSlotsProtocol) {
1516 // Query the driver for the current slot index and use it as the initial slot
1517 // before we start reading events from the device. It is possible that the
1518 // current slot index will not be the same as it was when the first event was
1519 // written into the evdev buffer, which means the input mapper could start
1520 // out of sync with the initial state of the events in the evdev buffer.
1521 // In the extremely unlikely case that this happens, the data from
1522 // two slots will be confused until the next ABS_MT_SLOT event is received.
1523 // This can cause the touch point to "jump", but at least there will be
1524 // no stuck touches.
1525 int32_t initialSlot;
1526 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1527 ABS_MT_SLOT, &initialSlot);
1528 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001529 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001530 initialSlot = -1;
1531 }
1532 clearSlots(initialSlot);
1533 } else {
1534 clearSlots(-1);
1535 }
1536}
1537
Jeff Brown49754db2011-07-01 17:37:58 -07001538void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001539 if (mSlots) {
1540 for (size_t i = 0; i < mSlotCount; i++) {
1541 mSlots[i].clear();
1542 }
Jeff Brown49754db2011-07-01 17:37:58 -07001543 }
1544 mCurrentSlot = initialSlot;
1545}
1546
1547void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1548 if (rawEvent->type == EV_ABS) {
1549 bool newSlot = false;
1550 if (mUsingSlotsProtocol) {
1551 if (rawEvent->scanCode == ABS_MT_SLOT) {
1552 mCurrentSlot = rawEvent->value;
1553 newSlot = true;
1554 }
1555 } else if (mCurrentSlot < 0) {
1556 mCurrentSlot = 0;
1557 }
1558
1559 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1560#if DEBUG_POINTERS
1561 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001562 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001563 "should be between 0 and %d; ignoring this slot.",
1564 mCurrentSlot, mSlotCount - 1);
1565 }
1566#endif
1567 } else {
1568 Slot* slot = &mSlots[mCurrentSlot];
1569
1570 switch (rawEvent->scanCode) {
1571 case ABS_MT_POSITION_X:
1572 slot->mInUse = true;
1573 slot->mAbsMTPositionX = rawEvent->value;
1574 break;
1575 case ABS_MT_POSITION_Y:
1576 slot->mInUse = true;
1577 slot->mAbsMTPositionY = rawEvent->value;
1578 break;
1579 case ABS_MT_TOUCH_MAJOR:
1580 slot->mInUse = true;
1581 slot->mAbsMTTouchMajor = rawEvent->value;
1582 break;
1583 case ABS_MT_TOUCH_MINOR:
1584 slot->mInUse = true;
1585 slot->mAbsMTTouchMinor = rawEvent->value;
1586 slot->mHaveAbsMTTouchMinor = true;
1587 break;
1588 case ABS_MT_WIDTH_MAJOR:
1589 slot->mInUse = true;
1590 slot->mAbsMTWidthMajor = rawEvent->value;
1591 break;
1592 case ABS_MT_WIDTH_MINOR:
1593 slot->mInUse = true;
1594 slot->mAbsMTWidthMinor = rawEvent->value;
1595 slot->mHaveAbsMTWidthMinor = true;
1596 break;
1597 case ABS_MT_ORIENTATION:
1598 slot->mInUse = true;
1599 slot->mAbsMTOrientation = rawEvent->value;
1600 break;
1601 case ABS_MT_TRACKING_ID:
1602 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001603 // The slot is no longer in use but it retains its previous contents,
1604 // which may be reused for subsequent touches.
1605 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001606 } else {
1607 slot->mInUse = true;
1608 slot->mAbsMTTrackingId = rawEvent->value;
1609 }
1610 break;
1611 case ABS_MT_PRESSURE:
1612 slot->mInUse = true;
1613 slot->mAbsMTPressure = rawEvent->value;
1614 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001615 case ABS_MT_DISTANCE:
1616 slot->mInUse = true;
1617 slot->mAbsMTDistance = rawEvent->value;
1618 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001619 case ABS_MT_TOOL_TYPE:
1620 slot->mInUse = true;
1621 slot->mAbsMTToolType = rawEvent->value;
1622 slot->mHaveAbsMTToolType = true;
1623 break;
1624 }
1625 }
1626 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1627 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1628 mCurrentSlot += 1;
1629 }
1630}
1631
Jeff Brown65fd2512011-08-18 11:20:58 -07001632void MultiTouchMotionAccumulator::finishSync() {
1633 if (!mUsingSlotsProtocol) {
1634 clearSlots(-1);
1635 }
1636}
1637
Jeff Brown49754db2011-07-01 17:37:58 -07001638
1639// --- MultiTouchMotionAccumulator::Slot ---
1640
1641MultiTouchMotionAccumulator::Slot::Slot() {
1642 clear();
1643}
1644
Jeff Brown49754db2011-07-01 17:37:58 -07001645void MultiTouchMotionAccumulator::Slot::clear() {
1646 mInUse = false;
1647 mHaveAbsMTTouchMinor = false;
1648 mHaveAbsMTWidthMinor = false;
1649 mHaveAbsMTToolType = false;
1650 mAbsMTPositionX = 0;
1651 mAbsMTPositionY = 0;
1652 mAbsMTTouchMajor = 0;
1653 mAbsMTTouchMinor = 0;
1654 mAbsMTWidthMajor = 0;
1655 mAbsMTWidthMinor = 0;
1656 mAbsMTOrientation = 0;
1657 mAbsMTTrackingId = -1;
1658 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001659 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001660 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001661}
1662
1663int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1664 if (mHaveAbsMTToolType) {
1665 switch (mAbsMTToolType) {
1666 case MT_TOOL_FINGER:
1667 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1668 case MT_TOOL_PEN:
1669 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1670 }
1671 }
1672 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1673}
1674
1675
Jeff Brown6d0fec22010-07-23 21:28:06 -07001676// --- InputMapper ---
1677
1678InputMapper::InputMapper(InputDevice* device) :
1679 mDevice(device), mContext(device->getContext()) {
1680}
1681
1682InputMapper::~InputMapper() {
1683}
1684
1685void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1686 info->addSource(getSources());
1687}
1688
Jeff Brownef3d7e82010-09-30 14:33:04 -07001689void InputMapper::dump(String8& dump) {
1690}
1691
Jeff Brown65fd2512011-08-18 11:20:58 -07001692void InputMapper::configure(nsecs_t when,
1693 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001694}
1695
Jeff Brown65fd2512011-08-18 11:20:58 -07001696void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001697}
1698
Jeff Brownaa3855d2011-03-17 01:34:19 -07001699void InputMapper::timeoutExpired(nsecs_t when) {
1700}
1701
Jeff Brown6d0fec22010-07-23 21:28:06 -07001702int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1703 return AKEY_STATE_UNKNOWN;
1704}
1705
1706int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1707 return AKEY_STATE_UNKNOWN;
1708}
1709
1710int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1711 return AKEY_STATE_UNKNOWN;
1712}
1713
1714bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1715 const int32_t* keyCodes, uint8_t* outFlags) {
1716 return false;
1717}
1718
1719int32_t InputMapper::getMetaState() {
1720 return 0;
1721}
1722
Jeff Brown05dc66a2011-03-02 14:41:58 -08001723void InputMapper::fadePointer() {
1724}
1725
Jeff Brownbe1aa822011-07-27 16:04:54 -07001726status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1727 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1728}
1729
Jeff Browncb1404e2011-01-15 18:14:15 -08001730void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1731 const RawAbsoluteAxisInfo& axis, const char* name) {
1732 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001733 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1734 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001735 } else {
1736 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1737 }
1738}
1739
Jeff Brown6d0fec22010-07-23 21:28:06 -07001740
1741// --- SwitchInputMapper ---
1742
1743SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1744 InputMapper(device) {
1745}
1746
1747SwitchInputMapper::~SwitchInputMapper() {
1748}
1749
1750uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001751 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001752}
1753
1754void SwitchInputMapper::process(const RawEvent* rawEvent) {
1755 switch (rawEvent->type) {
1756 case EV_SW:
1757 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1758 break;
1759 }
1760}
1761
1762void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001763 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1764 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001765}
1766
1767int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1768 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1769}
1770
1771
1772// --- KeyboardInputMapper ---
1773
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001774KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001775 uint32_t source, int32_t keyboardType) :
1776 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001777 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001778}
1779
1780KeyboardInputMapper::~KeyboardInputMapper() {
1781}
1782
Jeff Brown6d0fec22010-07-23 21:28:06 -07001783uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001784 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001785}
1786
1787void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1788 InputMapper::populateDeviceInfo(info);
1789
1790 info->setKeyboardType(mKeyboardType);
Jeff Brown1e08fe92011-11-15 17:48:10 -08001791 info->setKeyCharacterMapFile(getEventHub()->getKeyCharacterMapFile(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001792}
1793
Jeff Brownef3d7e82010-09-30 14:33:04 -07001794void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001795 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1796 dumpParameters(dump);
1797 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001798 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001799 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1800 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1801 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001802}
1803
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001804
Jeff Brown65fd2512011-08-18 11:20:58 -07001805void KeyboardInputMapper::configure(nsecs_t when,
1806 const InputReaderConfiguration* config, uint32_t changes) {
1807 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001808
Jeff Brown474dcb52011-06-14 20:22:50 -07001809 if (!changes) { // first time only
1810 // Configure basic parameters.
1811 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001812 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001813
Jeff Brown65fd2512011-08-18 11:20:58 -07001814 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1815 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1816 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1817 false /*external*/, NULL, NULL, &mOrientation)) {
1818 mOrientation = DISPLAY_ORIENTATION_0;
1819 }
1820 } else {
1821 mOrientation = DISPLAY_ORIENTATION_0;
1822 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001823 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001824}
1825
1826void KeyboardInputMapper::configureParameters() {
1827 mParameters.orientationAware = false;
1828 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1829 mParameters.orientationAware);
1830
Jeff Brownbc68a592011-07-25 12:58:12 -07001831 mParameters.associatedDisplayId = -1;
1832 if (mParameters.orientationAware) {
1833 mParameters.associatedDisplayId = 0;
1834 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001835}
1836
1837void KeyboardInputMapper::dumpParameters(String8& dump) {
1838 dump.append(INDENT3 "Parameters:\n");
1839 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1840 mParameters.associatedDisplayId);
1841 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1842 toString(mParameters.orientationAware));
1843}
1844
Jeff Brown65fd2512011-08-18 11:20:58 -07001845void KeyboardInputMapper::reset(nsecs_t when) {
1846 mMetaState = AMETA_NONE;
1847 mDownTime = 0;
1848 mKeyDowns.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001849
Jeff Brownbe1aa822011-07-27 16:04:54 -07001850 resetLedState();
1851
Jeff Brown65fd2512011-08-18 11:20:58 -07001852 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001853}
1854
1855void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1856 switch (rawEvent->type) {
1857 case EV_KEY: {
1858 int32_t scanCode = rawEvent->scanCode;
1859 if (isKeyboardOrGamepadKey(scanCode)) {
1860 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1861 rawEvent->flags);
1862 }
1863 break;
1864 }
1865 }
1866}
1867
1868bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1869 return scanCode < BTN_MOUSE
1870 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001871 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001872 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001873}
1874
Jeff Brown6328cdc2010-07-29 18:18:33 -07001875void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1876 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001877
Jeff Brownbe1aa822011-07-27 16:04:54 -07001878 if (down) {
1879 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001880 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001881 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001882 }
Jeff Brownfe508922011-01-18 15:10:10 -08001883
Jeff Brownbe1aa822011-07-27 16:04:54 -07001884 // Add key down.
1885 ssize_t keyDownIndex = findKeyDown(scanCode);
1886 if (keyDownIndex >= 0) {
1887 // key repeat, be sure to use same keycode as before in case of rotation
1888 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001889 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001890 // key down
1891 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1892 && mContext->shouldDropVirtualKey(when,
1893 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001894 return;
1895 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001896
1897 mKeyDowns.push();
1898 KeyDown& keyDown = mKeyDowns.editTop();
1899 keyDown.keyCode = keyCode;
1900 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001901 }
1902
Jeff Brownbe1aa822011-07-27 16:04:54 -07001903 mDownTime = when;
1904 } else {
1905 // Remove key down.
1906 ssize_t keyDownIndex = findKeyDown(scanCode);
1907 if (keyDownIndex >= 0) {
1908 // key up, be sure to use same keycode as before in case of rotation
1909 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1910 mKeyDowns.removeAt(size_t(keyDownIndex));
1911 } else {
1912 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00001913 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07001914 "keyCode=%d, scanCode=%d",
1915 getDeviceName().string(), keyCode, scanCode);
1916 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001917 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001918 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001919
Jeff Brownbe1aa822011-07-27 16:04:54 -07001920 bool metaStateChanged = false;
1921 int32_t oldMetaState = mMetaState;
1922 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1923 if (oldMetaState != newMetaState) {
1924 mMetaState = newMetaState;
1925 metaStateChanged = true;
1926 updateLedState(false);
1927 }
1928
1929 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001930
Jeff Brown56194eb2011-03-02 19:23:13 -08001931 // Key down on external an keyboard should wake the device.
1932 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1933 // For internal keyboards, the key layout file should specify the policy flags for
1934 // each wake key individually.
1935 // TODO: Use the input device configuration to control this behavior more finely.
1936 if (down && getDevice()->isExternal()
1937 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1938 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1939 }
1940
Jeff Brown6328cdc2010-07-29 18:18:33 -07001941 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001942 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001943 }
1944
Jeff Brown05dc66a2011-03-02 14:41:58 -08001945 if (down && !isMetaKey(keyCode)) {
1946 getContext()->fadePointer();
1947 }
1948
Jeff Brownbe1aa822011-07-27 16:04:54 -07001949 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001950 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1951 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001952 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001953}
1954
Jeff Brownbe1aa822011-07-27 16:04:54 -07001955ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1956 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001957 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001958 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001959 return i;
1960 }
1961 }
1962 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001963}
1964
Jeff Brown6d0fec22010-07-23 21:28:06 -07001965int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1966 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1967}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001968
Jeff Brown6d0fec22010-07-23 21:28:06 -07001969int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1970 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1971}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001972
Jeff Brown6d0fec22010-07-23 21:28:06 -07001973bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1974 const int32_t* keyCodes, uint8_t* outFlags) {
1975 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1976}
1977
1978int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001979 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001980}
1981
Jeff Brownbe1aa822011-07-27 16:04:54 -07001982void KeyboardInputMapper::resetLedState() {
1983 initializeLedState(mCapsLockLedState, LED_CAPSL);
1984 initializeLedState(mNumLockLedState, LED_NUML);
1985 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001986
Jeff Brownbe1aa822011-07-27 16:04:54 -07001987 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001988}
1989
Jeff Brownbe1aa822011-07-27 16:04:54 -07001990void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001991 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1992 ledState.on = false;
1993}
1994
Jeff Brownbe1aa822011-07-27 16:04:54 -07001995void KeyboardInputMapper::updateLedState(bool reset) {
1996 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001997 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001998 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001999 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002000 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002001 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002002}
2003
Jeff Brownbe1aa822011-07-27 16:04:54 -07002004void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002005 int32_t led, int32_t modifier, bool reset) {
2006 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002007 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002008 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002009 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2010 ledState.on = desiredState;
2011 }
2012 }
2013}
2014
Jeff Brown6d0fec22010-07-23 21:28:06 -07002015
Jeff Brown83c09682010-12-23 17:50:18 -08002016// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002017
Jeff Brown83c09682010-12-23 17:50:18 -08002018CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002019 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002020}
2021
Jeff Brown83c09682010-12-23 17:50:18 -08002022CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002023}
2024
Jeff Brown83c09682010-12-23 17:50:18 -08002025uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002026 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002027}
2028
Jeff Brown83c09682010-12-23 17:50:18 -08002029void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002030 InputMapper::populateDeviceInfo(info);
2031
Jeff Brown83c09682010-12-23 17:50:18 -08002032 if (mParameters.mode == Parameters::MODE_POINTER) {
2033 float minX, minY, maxX, maxY;
2034 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002035 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2036 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002037 }
2038 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002039 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2040 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002041 }
Jeff Brownefd32662011-03-08 15:13:06 -08002042 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002043
Jeff Brown65fd2512011-08-18 11:20:58 -07002044 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002045 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002046 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002047 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002048 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002049 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002050}
2051
Jeff Brown83c09682010-12-23 17:50:18 -08002052void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002053 dump.append(INDENT2 "Cursor Input Mapper:\n");
2054 dumpParameters(dump);
2055 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2056 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2057 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2058 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2059 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002060 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002061 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002062 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002063 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2064 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002065 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002066 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2067 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2068 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002069}
2070
Jeff Brown65fd2512011-08-18 11:20:58 -07002071void CursorInputMapper::configure(nsecs_t when,
2072 const InputReaderConfiguration* config, uint32_t changes) {
2073 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002074
Jeff Brown474dcb52011-06-14 20:22:50 -07002075 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002076 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002077
Jeff Brown474dcb52011-06-14 20:22:50 -07002078 // Configure basic parameters.
2079 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002080
Jeff Brown474dcb52011-06-14 20:22:50 -07002081 // Configure device mode.
2082 switch (mParameters.mode) {
2083 case Parameters::MODE_POINTER:
2084 mSource = AINPUT_SOURCE_MOUSE;
2085 mXPrecision = 1.0f;
2086 mYPrecision = 1.0f;
2087 mXScale = 1.0f;
2088 mYScale = 1.0f;
2089 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2090 break;
2091 case Parameters::MODE_NAVIGATION:
2092 mSource = AINPUT_SOURCE_TRACKBALL;
2093 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2094 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2095 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2096 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2097 break;
2098 }
2099
2100 mVWheelScale = 1.0f;
2101 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002102 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002103
Jeff Brown474dcb52011-06-14 20:22:50 -07002104 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2105 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2106 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2107 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2108 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002109
2110 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2111 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2112 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2113 false /*external*/, NULL, NULL, &mOrientation)) {
2114 mOrientation = DISPLAY_ORIENTATION_0;
2115 }
2116 } else {
2117 mOrientation = DISPLAY_ORIENTATION_0;
2118 }
2119 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002120}
2121
Jeff Brown83c09682010-12-23 17:50:18 -08002122void CursorInputMapper::configureParameters() {
2123 mParameters.mode = Parameters::MODE_POINTER;
2124 String8 cursorModeString;
2125 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2126 if (cursorModeString == "navigation") {
2127 mParameters.mode = Parameters::MODE_NAVIGATION;
2128 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002129 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002130 }
2131 }
2132
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002133 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002134 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002135 mParameters.orientationAware);
2136
Jeff Brownbc68a592011-07-25 12:58:12 -07002137 mParameters.associatedDisplayId = -1;
2138 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2139 mParameters.associatedDisplayId = 0;
2140 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002141}
2142
Jeff Brown83c09682010-12-23 17:50:18 -08002143void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002144 dump.append(INDENT3 "Parameters:\n");
2145 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2146 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002147
2148 switch (mParameters.mode) {
2149 case Parameters::MODE_POINTER:
2150 dump.append(INDENT4 "Mode: pointer\n");
2151 break;
2152 case Parameters::MODE_NAVIGATION:
2153 dump.append(INDENT4 "Mode: navigation\n");
2154 break;
2155 default:
Steve Blockec193de2012-01-09 18:35:44 +00002156 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002157 }
2158
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002159 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2160 toString(mParameters.orientationAware));
2161}
2162
Jeff Brown65fd2512011-08-18 11:20:58 -07002163void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002164 mButtonState = 0;
2165 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002166
Jeff Brownbe1aa822011-07-27 16:04:54 -07002167 mPointerVelocityControl.reset();
2168 mWheelXVelocityControl.reset();
2169 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002170
Jeff Brown65fd2512011-08-18 11:20:58 -07002171 mCursorButtonAccumulator.reset(getDevice());
2172 mCursorMotionAccumulator.reset(getDevice());
2173 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002174
Jeff Brown65fd2512011-08-18 11:20:58 -07002175 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002176}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002177
Jeff Brown83c09682010-12-23 17:50:18 -08002178void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002179 mCursorButtonAccumulator.process(rawEvent);
2180 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002181 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002182
Jeff Brown49754db2011-07-01 17:37:58 -07002183 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2184 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002185 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002186}
2187
Jeff Brown83c09682010-12-23 17:50:18 -08002188void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002189 int32_t lastButtonState = mButtonState;
2190 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2191 mButtonState = currentButtonState;
2192
2193 bool wasDown = isPointerDown(lastButtonState);
2194 bool down = isPointerDown(currentButtonState);
2195 bool downChanged;
2196 if (!wasDown && down) {
2197 mDownTime = when;
2198 downChanged = true;
2199 } else if (wasDown && !down) {
2200 downChanged = true;
2201 } else {
2202 downChanged = false;
2203 }
2204 nsecs_t downTime = mDownTime;
2205 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002206 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002207
2208 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2209 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2210 bool moved = deltaX != 0 || deltaY != 0;
2211
Jeff Brown65fd2512011-08-18 11:20:58 -07002212 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002213 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2214 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002215 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216 }
2217
Jeff Brown65fd2512011-08-18 11:20:58 -07002218 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002219 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 pointerProperties.clear();
2221 pointerProperties.id = 0;
2222 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2223
Jeff Brown6328cdc2010-07-29 18:18:33 -07002224 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002225 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002226
Jeff Brown65fd2512011-08-18 11:20:58 -07002227 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2228 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002229 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002230
Jeff Brownbe1aa822011-07-27 16:04:54 -07002231 mWheelYVelocityControl.move(when, NULL, &vscroll);
2232 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002233
Jeff Brownbe1aa822011-07-27 16:04:54 -07002234 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002235
Jeff Brownbe1aa822011-07-27 16:04:54 -07002236 if (mPointerController != NULL) {
2237 if (moved || scrolled || buttonsChanged) {
2238 mPointerController->setPresentation(
2239 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002240
Jeff Brownbe1aa822011-07-27 16:04:54 -07002241 if (moved) {
2242 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002243 }
2244
Jeff Brownbe1aa822011-07-27 16:04:54 -07002245 if (buttonsChanged) {
2246 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002247 }
Jeff Brownefd32662011-03-08 15:13:06 -08002248
Jeff Brownbe1aa822011-07-27 16:04:54 -07002249 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002250 }
2251
Jeff Brownbe1aa822011-07-27 16:04:54 -07002252 float x, y;
2253 mPointerController->getPosition(&x, &y);
2254 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2255 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2256 } else {
2257 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2258 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2259 }
2260
2261 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002262
Jeff Brown56194eb2011-03-02 19:23:13 -08002263 // Moving an external trackball or mouse should wake the device.
2264 // We don't do this for internal cursor devices to prevent them from waking up
2265 // the device in your pocket.
2266 // TODO: Use the input device configuration to control this behavior more finely.
2267 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002268 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002269 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2270 }
2271
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002272 // Synthesize key down from buttons if needed.
2273 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2274 policyFlags, lastButtonState, currentButtonState);
2275
2276 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002277 if (downChanged || moved || scrolled || buttonsChanged) {
2278 int32_t metaState = mContext->getGlobalMetaState();
2279 int32_t motionEventAction;
2280 if (downChanged) {
2281 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2282 } else if (down || mPointerController == NULL) {
2283 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2284 } else {
2285 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2286 }
Jeff Brownb6997262010-10-08 22:31:17 -07002287
Jeff Brownbe1aa822011-07-27 16:04:54 -07002288 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2289 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002290 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002291 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002292
Jeff Brownbe1aa822011-07-27 16:04:54 -07002293 // Send hover move after UP to tell the application that the mouse is hovering now.
2294 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2295 && mPointerController != NULL) {
2296 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2297 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2298 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2299 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2300 getListener()->notifyMotion(&hoverArgs);
2301 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002302
Jeff Brownbe1aa822011-07-27 16:04:54 -07002303 // Send scroll events.
2304 if (scrolled) {
2305 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2306 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2307
2308 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2309 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2310 AMOTION_EVENT_EDGE_FLAG_NONE,
2311 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2312 getListener()->notifyMotion(&scrollArgs);
2313 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002314 }
Jeff Browna032cc02011-03-07 16:56:21 -08002315
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002316 // Synthesize key up from buttons if needed.
2317 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2318 policyFlags, lastButtonState, currentButtonState);
2319
Jeff Brown65fd2512011-08-18 11:20:58 -07002320 mCursorMotionAccumulator.finishSync();
2321 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002322}
2323
Jeff Brown83c09682010-12-23 17:50:18 -08002324int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002325 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2326 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2327 } else {
2328 return AKEY_STATE_UNKNOWN;
2329 }
2330}
2331
Jeff Brown05dc66a2011-03-02 14:41:58 -08002332void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002333 if (mPointerController != NULL) {
2334 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2335 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002336}
2337
Jeff Brown6d0fec22010-07-23 21:28:06 -07002338
2339// --- TouchInputMapper ---
2340
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002341TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002342 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002343 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002344 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002345}
2346
2347TouchInputMapper::~TouchInputMapper() {
2348}
2349
2350uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002351 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002352}
2353
2354void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2355 InputMapper::populateDeviceInfo(info);
2356
Jeff Brown65fd2512011-08-18 11:20:58 -07002357 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2358 info->addMotionRange(mOrientedRanges.x);
2359 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002360 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002361
Jeff Brown65fd2512011-08-18 11:20:58 -07002362 if (mOrientedRanges.haveSize) {
2363 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002364 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002365
2366 if (mOrientedRanges.haveTouchSize) {
2367 info->addMotionRange(mOrientedRanges.touchMajor);
2368 info->addMotionRange(mOrientedRanges.touchMinor);
2369 }
2370
2371 if (mOrientedRanges.haveToolSize) {
2372 info->addMotionRange(mOrientedRanges.toolMajor);
2373 info->addMotionRange(mOrientedRanges.toolMinor);
2374 }
2375
2376 if (mOrientedRanges.haveOrientation) {
2377 info->addMotionRange(mOrientedRanges.orientation);
2378 }
2379
2380 if (mOrientedRanges.haveDistance) {
2381 info->addMotionRange(mOrientedRanges.distance);
2382 }
2383
2384 if (mOrientedRanges.haveTilt) {
2385 info->addMotionRange(mOrientedRanges.tilt);
2386 }
2387
2388 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2389 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2390 }
2391 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2392 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2393 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002394 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002395}
2396
Jeff Brownef3d7e82010-09-30 14:33:04 -07002397void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002398 dump.append(INDENT2 "Touch Input Mapper:\n");
2399 dumpParameters(dump);
2400 dumpVirtualKeys(dump);
2401 dumpRawPointerAxes(dump);
2402 dumpCalibration(dump);
2403 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002404
Jeff Brownbe1aa822011-07-27 16:04:54 -07002405 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2406 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2407 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2408 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2409 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2410 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002411 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2412 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002413 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002414 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2415 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002416 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2417 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2418 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2419 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2420 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002421
Jeff Brownbe1aa822011-07-27 16:04:54 -07002422 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002423
Jeff Brownbe1aa822011-07-27 16:04:54 -07002424 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2425 mLastRawPointerData.pointerCount);
2426 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2427 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2428 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2429 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002430 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2431 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002432 pointer.id, pointer.x, pointer.y, pointer.pressure,
2433 pointer.touchMajor, pointer.touchMinor,
2434 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002435 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002436 pointer.toolType, toString(pointer.isHovering));
2437 }
2438
2439 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2440 mLastCookedPointerData.pointerCount);
2441 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2442 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2443 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2444 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2445 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002446 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2447 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448 pointerProperties.id,
2449 pointerCoords.getX(),
2450 pointerCoords.getY(),
2451 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2452 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2453 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2454 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2455 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2456 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002457 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002458 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2459 pointerProperties.toolType,
2460 toString(mLastCookedPointerData.isHovering(i)));
2461 }
2462
Jeff Brown65fd2512011-08-18 11:20:58 -07002463 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002464 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2465 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002466 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002467 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002468 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002469 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002470 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002471 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002472 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002473 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2474 mPointerGestureMaxSwipeWidth);
2475 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002476}
2477
Jeff Brown65fd2512011-08-18 11:20:58 -07002478void TouchInputMapper::configure(nsecs_t when,
2479 const InputReaderConfiguration* config, uint32_t changes) {
2480 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002481
Jeff Brown474dcb52011-06-14 20:22:50 -07002482 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002483
Jeff Brown474dcb52011-06-14 20:22:50 -07002484 if (!changes) { // first time only
2485 // Configure basic parameters.
2486 configureParameters();
2487
Jeff Brown65fd2512011-08-18 11:20:58 -07002488 // Configure common accumulators.
2489 mCursorScrollAccumulator.configure(getDevice());
2490 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002491
2492 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002493 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002494
2495 // Prepare input device calibration.
2496 parseCalibration();
2497 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002498 }
2499
Jeff Brown474dcb52011-06-14 20:22:50 -07002500 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002501 // Update pointer speed.
2502 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2503 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2504 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002505 }
Jeff Brown8d608662010-08-30 03:02:23 -07002506
Jeff Brown65fd2512011-08-18 11:20:58 -07002507 bool resetNeeded = false;
2508 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002509 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2510 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002511 // Configure device sources, surface dimensions, orientation and
2512 // scaling factors.
2513 configureSurface(when, &resetNeeded);
2514 }
2515
2516 if (changes && resetNeeded) {
2517 // Send reset, unless this is the first time the device has been configured,
2518 // in which case the reader will call reset itself after all mappers are ready.
2519 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002520 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002521}
2522
Jeff Brown8d608662010-08-30 03:02:23 -07002523void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002524 // Use the pointer presentation mode for devices that do not support distinct
2525 // multitouch. The spot-based presentation relies on being able to accurately
2526 // locate two or more fingers on the touch pad.
2527 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2528 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002529
Jeff Brown538881e2011-05-25 18:23:38 -07002530 String8 gestureModeString;
2531 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2532 gestureModeString)) {
2533 if (gestureModeString == "pointer") {
2534 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2535 } else if (gestureModeString == "spots") {
2536 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2537 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002538 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002539 }
2540 }
2541
Jeff Browndeffe072011-08-26 18:38:46 -07002542 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2543 // The device is a touch screen.
2544 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2545 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2546 // The device is a pointing device like a track pad.
2547 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2548 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002549 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2550 // The device is a cursor device with a touch pad attached.
2551 // By default don't use the touch pad to move the pointer.
2552 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2553 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002554 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002555 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2556 }
2557
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002558 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002559 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2560 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002561 if (deviceTypeString == "touchScreen") {
2562 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002563 } else if (deviceTypeString == "touchPad") {
2564 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002565 } else if (deviceTypeString == "pointer") {
2566 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002567 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002568 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002569 }
2570 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002571
Jeff Brownefd32662011-03-08 15:13:06 -08002572 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002573 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2574 mParameters.orientationAware);
2575
Jeff Brownbc68a592011-07-25 12:58:12 -07002576 mParameters.associatedDisplayId = -1;
2577 mParameters.associatedDisplayIsExternal = false;
2578 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002579 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002580 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2581 mParameters.associatedDisplayIsExternal =
2582 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2583 && getDevice()->isExternal();
2584 mParameters.associatedDisplayId = 0;
2585 }
Jeff Brown8d608662010-08-30 03:02:23 -07002586}
2587
Jeff Brownef3d7e82010-09-30 14:33:04 -07002588void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002589 dump.append(INDENT3 "Parameters:\n");
2590
Jeff Brown538881e2011-05-25 18:23:38 -07002591 switch (mParameters.gestureMode) {
2592 case Parameters::GESTURE_MODE_POINTER:
2593 dump.append(INDENT4 "GestureMode: pointer\n");
2594 break;
2595 case Parameters::GESTURE_MODE_SPOTS:
2596 dump.append(INDENT4 "GestureMode: spots\n");
2597 break;
2598 default:
2599 assert(false);
2600 }
2601
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002602 switch (mParameters.deviceType) {
2603 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2604 dump.append(INDENT4 "DeviceType: touchScreen\n");
2605 break;
2606 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2607 dump.append(INDENT4 "DeviceType: touchPad\n");
2608 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002609 case Parameters::DEVICE_TYPE_POINTER:
2610 dump.append(INDENT4 "DeviceType: pointer\n");
2611 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002612 default:
Steve Blockec193de2012-01-09 18:35:44 +00002613 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002614 }
2615
Jeff Brown65fd2512011-08-18 11:20:58 -07002616 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2617 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002618 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2619 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002620}
2621
Jeff Brownbe1aa822011-07-27 16:04:54 -07002622void TouchInputMapper::configureRawPointerAxes() {
2623 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002624}
2625
Jeff Brownbe1aa822011-07-27 16:04:54 -07002626void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2627 dump.append(INDENT3 "Raw Touch Axes:\n");
2628 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2629 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2630 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2631 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2632 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2633 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2634 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2635 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2636 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002637 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2638 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002639 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2640 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002641}
2642
Jeff Brown65fd2512011-08-18 11:20:58 -07002643void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2644 int32_t oldDeviceMode = mDeviceMode;
2645
2646 // Determine device mode.
2647 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2648 && mConfig.pointerGesturesEnabled) {
2649 mSource = AINPUT_SOURCE_MOUSE;
2650 mDeviceMode = DEVICE_MODE_POINTER;
2651 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2652 && mParameters.associatedDisplayId >= 0) {
2653 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2654 mDeviceMode = DEVICE_MODE_DIRECT;
2655 } else {
2656 mSource = AINPUT_SOURCE_TOUCHPAD;
2657 mDeviceMode = DEVICE_MODE_UNSCALED;
2658 }
2659
Jeff Brown9626b142011-03-03 02:09:54 -08002660 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002661 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002662 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002663 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002664 mDeviceMode = DEVICE_MODE_DISABLED;
2665 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002666 }
2667
Jeff Brown65fd2512011-08-18 11:20:58 -07002668 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002669 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002670 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002671 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002672 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2673 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002674 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002675 "display %d. The device will be inoperable until the display size "
2676 "becomes available.",
2677 getDeviceName().string(), mParameters.associatedDisplayId);
2678 mDeviceMode = DEVICE_MODE_DISABLED;
2679 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002680 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002681 }
2682
Jeff Brown65fd2512011-08-18 11:20:58 -07002683 // Configure dimensions.
2684 int32_t width, height, orientation;
2685 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2686 width = mAssociatedDisplayWidth;
2687 height = mAssociatedDisplayHeight;
2688 orientation = mParameters.orientationAware ?
2689 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2690 } else {
2691 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2692 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2693 orientation = DISPLAY_ORIENTATION_0;
2694 }
2695
2696 // If moving between pointer modes, need to reset some state.
2697 bool deviceModeChanged;
2698 if (mDeviceMode != oldDeviceMode) {
2699 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002700 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002701 }
2702
Jeff Browndaf4a122011-08-26 17:14:14 -07002703 // Create pointer controller if needed.
2704 if (mDeviceMode == DEVICE_MODE_POINTER ||
2705 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2706 if (mPointerController == NULL) {
2707 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2708 }
2709 } else {
2710 mPointerController.clear();
2711 }
2712
Jeff Brownbe1aa822011-07-27 16:04:54 -07002713 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002714 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002715 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002716 }
2717
Jeff Brownbe1aa822011-07-27 16:04:54 -07002718 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002719 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002720 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002721 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002722
Jeff Brownbe1aa822011-07-27 16:04:54 -07002723 mSurfaceWidth = width;
2724 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002725
Jeff Brown8d608662010-08-30 03:02:23 -07002726 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002727 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2728 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2729 mXPrecision = 1.0f / mXScale;
2730 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002731
Jeff Brownbe1aa822011-07-27 16:04:54 -07002732 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002733 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002734 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002735 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002736
Jeff Brownbe1aa822011-07-27 16:04:54 -07002737 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002738
Jeff Brown8d608662010-08-30 03:02:23 -07002739 // Scale factor for terms that are not oriented in a particular axis.
2740 // If the pixels are square then xScale == yScale otherwise we fake it
2741 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002742 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002743
Jeff Brown8d608662010-08-30 03:02:23 -07002744 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002745 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002746
Jeff Browna1f89ce2011-08-11 00:05:01 -07002747 // Size factors.
2748 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2749 if (mRawPointerAxes.touchMajor.valid
2750 && mRawPointerAxes.touchMajor.maxValue != 0) {
2751 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2752 } else if (mRawPointerAxes.toolMajor.valid
2753 && mRawPointerAxes.toolMajor.maxValue != 0) {
2754 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2755 } else {
2756 mSizeScale = 0.0f;
2757 }
2758
Jeff Brownbe1aa822011-07-27 16:04:54 -07002759 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002760 mOrientedRanges.haveToolSize = true;
2761 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002762
Jeff Brownbe1aa822011-07-27 16:04:54 -07002763 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002764 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002765 mOrientedRanges.touchMajor.min = 0;
2766 mOrientedRanges.touchMajor.max = diagonalSize;
2767 mOrientedRanges.touchMajor.flat = 0;
2768 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002769
Jeff Brownbe1aa822011-07-27 16:04:54 -07002770 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2771 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002772
Jeff Brownbe1aa822011-07-27 16:04:54 -07002773 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002774 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002775 mOrientedRanges.toolMajor.min = 0;
2776 mOrientedRanges.toolMajor.max = diagonalSize;
2777 mOrientedRanges.toolMajor.flat = 0;
2778 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002779
Jeff Brownbe1aa822011-07-27 16:04:54 -07002780 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2781 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002782
2783 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002784 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002785 mOrientedRanges.size.min = 0;
2786 mOrientedRanges.size.max = 1.0;
2787 mOrientedRanges.size.flat = 0;
2788 mOrientedRanges.size.fuzz = 0;
2789 } else {
2790 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002791 }
2792
2793 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002794 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002795 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2796 || mCalibration.pressureCalibration
2797 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2798 if (mCalibration.havePressureScale) {
2799 mPressureScale = mCalibration.pressureScale;
2800 } else if (mRawPointerAxes.pressure.valid
2801 && mRawPointerAxes.pressure.maxValue != 0) {
2802 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002803 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002804 }
Jeff Brown8d608662010-08-30 03:02:23 -07002805
Jeff Brown65fd2512011-08-18 11:20:58 -07002806 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2807 mOrientedRanges.pressure.source = mSource;
2808 mOrientedRanges.pressure.min = 0;
2809 mOrientedRanges.pressure.max = 1.0;
2810 mOrientedRanges.pressure.flat = 0;
2811 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002812
Jeff Brown65fd2512011-08-18 11:20:58 -07002813 // Tilt
2814 mTiltXCenter = 0;
2815 mTiltXScale = 0;
2816 mTiltYCenter = 0;
2817 mTiltYScale = 0;
2818 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2819 if (mHaveTilt) {
2820 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2821 mRawPointerAxes.tiltX.maxValue);
2822 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2823 mRawPointerAxes.tiltY.maxValue);
2824 mTiltXScale = M_PI / 180;
2825 mTiltYScale = M_PI / 180;
2826
2827 mOrientedRanges.haveTilt = true;
2828
2829 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2830 mOrientedRanges.tilt.source = mSource;
2831 mOrientedRanges.tilt.min = 0;
2832 mOrientedRanges.tilt.max = M_PI_2;
2833 mOrientedRanges.tilt.flat = 0;
2834 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002835 }
2836
Jeff Brown8d608662010-08-30 03:02:23 -07002837 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002838 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002839 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002840 if (mHaveTilt) {
2841 mOrientedRanges.haveOrientation = true;
2842
2843 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2844 mOrientedRanges.orientation.source = mSource;
2845 mOrientedRanges.orientation.min = -M_PI;
2846 mOrientedRanges.orientation.max = M_PI;
2847 mOrientedRanges.orientation.flat = 0;
2848 mOrientedRanges.orientation.fuzz = 0;
2849 } else if (mCalibration.orientationCalibration !=
2850 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002851 if (mCalibration.orientationCalibration
2852 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002853 if (mRawPointerAxes.orientation.valid) {
2854 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2855 mRawPointerAxes.orientation.maxValue);
2856 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2857 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002858 }
2859 }
2860
Jeff Brownbe1aa822011-07-27 16:04:54 -07002861 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002862
Jeff Brownbe1aa822011-07-27 16:04:54 -07002863 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002864 mOrientedRanges.orientation.source = mSource;
2865 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002866 mOrientedRanges.orientation.max = M_PI_2;
2867 mOrientedRanges.orientation.flat = 0;
2868 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002869 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002870
2871 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002872 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002873 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2874 if (mCalibration.distanceCalibration
2875 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2876 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002877 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002878 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002879 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002880 }
2881 }
2882
Jeff Brownbe1aa822011-07-27 16:04:54 -07002883 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002884
Jeff Brownbe1aa822011-07-27 16:04:54 -07002885 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002886 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002887 mOrientedRanges.distance.min =
2888 mRawPointerAxes.distance.minValue * mDistanceScale;
2889 mOrientedRanges.distance.max =
2890 mRawPointerAxes.distance.minValue * mDistanceScale;
2891 mOrientedRanges.distance.flat = 0;
2892 mOrientedRanges.distance.fuzz =
2893 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002894 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002895 }
2896
Jeff Brown65fd2512011-08-18 11:20:58 -07002897 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002898 // Compute oriented surface dimensions, precision, scales and ranges.
2899 // Note that the maximum value reported is an inclusive maximum value so it is one
2900 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002901 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002902 case DISPLAY_ORIENTATION_90:
2903 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002904 mOrientedSurfaceWidth = mSurfaceHeight;
2905 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002906
Jeff Brownbe1aa822011-07-27 16:04:54 -07002907 mOrientedXPrecision = mYPrecision;
2908 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002909
Jeff Brownbe1aa822011-07-27 16:04:54 -07002910 mOrientedRanges.x.min = 0;
2911 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2912 * mYScale;
2913 mOrientedRanges.x.flat = 0;
2914 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002915
Jeff Brownbe1aa822011-07-27 16:04:54 -07002916 mOrientedRanges.y.min = 0;
2917 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2918 * mXScale;
2919 mOrientedRanges.y.flat = 0;
2920 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002921 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002922
Jeff Brown6d0fec22010-07-23 21:28:06 -07002923 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002924 mOrientedSurfaceWidth = mSurfaceWidth;
2925 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002926
Jeff Brownbe1aa822011-07-27 16:04:54 -07002927 mOrientedXPrecision = mXPrecision;
2928 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002929
Jeff Brownbe1aa822011-07-27 16:04:54 -07002930 mOrientedRanges.x.min = 0;
2931 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2932 * mXScale;
2933 mOrientedRanges.x.flat = 0;
2934 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002935
Jeff Brownbe1aa822011-07-27 16:04:54 -07002936 mOrientedRanges.y.min = 0;
2937 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2938 * mYScale;
2939 mOrientedRanges.y.flat = 0;
2940 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002941 break;
2942 }
Jeff Brownace13b12011-03-09 17:39:48 -08002943
2944 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002945 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002946 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2947 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002948 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002949 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2950 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002951
Jeff Brown2352b972011-04-12 22:39:53 -07002952 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002953 // given area relative to the diagonal size of the display when no acceleration
2954 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002955 // Assume that the touch pad has a square aspect ratio such that movements in
2956 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002957 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002958 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002959 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002960
2961 // Scale zooms to cover a smaller range of the display than movements do.
2962 // This value determines the area around the pointer that is affected by freeform
2963 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002964 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002965 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002966 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002967
Jeff Brown2352b972011-04-12 22:39:53 -07002968 // Max width between pointers to detect a swipe gesture is more than some fraction
2969 // of the diagonal axis of the touch pad. Touches that are wider than this are
2970 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002971 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002972 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002973 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002974
Jeff Brown65fd2512011-08-18 11:20:58 -07002975 // Abort current pointer usages because the state has changed.
2976 abortPointerUsage(when, 0 /*policyFlags*/);
2977
2978 // Inform the dispatcher about the changes.
2979 *outResetNeeded = true;
2980 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002981}
2982
Jeff Brownbe1aa822011-07-27 16:04:54 -07002983void TouchInputMapper::dumpSurface(String8& dump) {
2984 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2985 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2986 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002987}
2988
Jeff Brownbe1aa822011-07-27 16:04:54 -07002989void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002990 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002991 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002992
Jeff Brownbe1aa822011-07-27 16:04:54 -07002993 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002994
Jeff Brown6328cdc2010-07-29 18:18:33 -07002995 if (virtualKeyDefinitions.size() == 0) {
2996 return;
2997 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002998
Jeff Brownbe1aa822011-07-27 16:04:54 -07002999 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003000
Jeff Brownbe1aa822011-07-27 16:04:54 -07003001 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3002 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3003 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3004 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003005
3006 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003007 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003008 virtualKeyDefinitions[i];
3009
Jeff Brownbe1aa822011-07-27 16:04:54 -07003010 mVirtualKeys.add();
3011 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003012
3013 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3014 int32_t keyCode;
3015 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08003016 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07003017 & keyCode, & flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003018 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003019 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003020 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003021 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003022 }
3023
Jeff Brown6328cdc2010-07-29 18:18:33 -07003024 virtualKey.keyCode = keyCode;
3025 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003026
Jeff Brown6328cdc2010-07-29 18:18:33 -07003027 // convert the key definition's display coordinates into touch coordinates for a hit box
3028 int32_t halfWidth = virtualKeyDefinition.width / 2;
3029 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003030
Jeff Brown6328cdc2010-07-29 18:18:33 -07003031 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003032 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003033 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003034 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003035 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003036 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003037 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003038 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003039 }
3040}
3041
Jeff Brownbe1aa822011-07-27 16:04:54 -07003042void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3043 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003044 dump.append(INDENT3 "Virtual Keys:\n");
3045
Jeff Brownbe1aa822011-07-27 16:04:54 -07003046 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3047 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003048 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3049 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3050 i, virtualKey.scanCode, virtualKey.keyCode,
3051 virtualKey.hitLeft, virtualKey.hitRight,
3052 virtualKey.hitTop, virtualKey.hitBottom);
3053 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003054 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003055}
3056
Jeff Brown8d608662010-08-30 03:02:23 -07003057void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003058 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003059 Calibration& out = mCalibration;
3060
Jeff Browna1f89ce2011-08-11 00:05:01 -07003061 // Size
3062 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3063 String8 sizeCalibrationString;
3064 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3065 if (sizeCalibrationString == "none") {
3066 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3067 } else if (sizeCalibrationString == "geometric") {
3068 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3069 } else if (sizeCalibrationString == "diameter") {
3070 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3071 } else if (sizeCalibrationString == "area") {
3072 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3073 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003074 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003075 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003076 }
3077 }
3078
Jeff Browna1f89ce2011-08-11 00:05:01 -07003079 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3080 out.sizeScale);
3081 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3082 out.sizeBias);
3083 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3084 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003085
3086 // Pressure
3087 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3088 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003089 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003090 if (pressureCalibrationString == "none") {
3091 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3092 } else if (pressureCalibrationString == "physical") {
3093 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3094 } else if (pressureCalibrationString == "amplitude") {
3095 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3096 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003097 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003098 pressureCalibrationString.string());
3099 }
3100 }
3101
Jeff Brown8d608662010-08-30 03:02:23 -07003102 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3103 out.pressureScale);
3104
Jeff Brown8d608662010-08-30 03:02:23 -07003105 // Orientation
3106 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3107 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003108 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003109 if (orientationCalibrationString == "none") {
3110 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3111 } else if (orientationCalibrationString == "interpolated") {
3112 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003113 } else if (orientationCalibrationString == "vector") {
3114 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003115 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003116 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003117 orientationCalibrationString.string());
3118 }
3119 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003120
3121 // Distance
3122 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3123 String8 distanceCalibrationString;
3124 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3125 if (distanceCalibrationString == "none") {
3126 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3127 } else if (distanceCalibrationString == "scaled") {
3128 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3129 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003130 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003131 distanceCalibrationString.string());
3132 }
3133 }
3134
3135 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3136 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003137}
3138
3139void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003140 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003141 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3142 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3143 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003144 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003145 } else {
3146 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3147 }
Jeff Brown8d608662010-08-30 03:02:23 -07003148
Jeff Browna1f89ce2011-08-11 00:05:01 -07003149 // Pressure
3150 if (mRawPointerAxes.pressure.valid) {
3151 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3152 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3153 }
3154 } else {
3155 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003156 }
3157
3158 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003159 if (mRawPointerAxes.orientation.valid) {
3160 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003161 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003162 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003163 } else {
3164 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003165 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003166
3167 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003168 if (mRawPointerAxes.distance.valid) {
3169 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003170 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003171 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003172 } else {
3173 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003174 }
Jeff Brown8d608662010-08-30 03:02:23 -07003175}
3176
Jeff Brownef3d7e82010-09-30 14:33:04 -07003177void TouchInputMapper::dumpCalibration(String8& dump) {
3178 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003179
Jeff Browna1f89ce2011-08-11 00:05:01 -07003180 // Size
3181 switch (mCalibration.sizeCalibration) {
3182 case Calibration::SIZE_CALIBRATION_NONE:
3183 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003184 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003185 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3186 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003187 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003188 case Calibration::SIZE_CALIBRATION_DIAMETER:
3189 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3190 break;
3191 case Calibration::SIZE_CALIBRATION_AREA:
3192 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003193 break;
3194 default:
Steve Blockec193de2012-01-09 18:35:44 +00003195 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003196 }
3197
Jeff Browna1f89ce2011-08-11 00:05:01 -07003198 if (mCalibration.haveSizeScale) {
3199 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3200 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003201 }
3202
Jeff Browna1f89ce2011-08-11 00:05:01 -07003203 if (mCalibration.haveSizeBias) {
3204 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3205 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003206 }
3207
Jeff Browna1f89ce2011-08-11 00:05:01 -07003208 if (mCalibration.haveSizeIsSummed) {
3209 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3210 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003211 }
3212
3213 // Pressure
3214 switch (mCalibration.pressureCalibration) {
3215 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003216 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003217 break;
3218 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003219 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003220 break;
3221 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003222 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003223 break;
3224 default:
Steve Blockec193de2012-01-09 18:35:44 +00003225 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003226 }
3227
Jeff Brown8d608662010-08-30 03:02:23 -07003228 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003229 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3230 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003231 }
3232
Jeff Brown8d608662010-08-30 03:02:23 -07003233 // Orientation
3234 switch (mCalibration.orientationCalibration) {
3235 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003236 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003237 break;
3238 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003239 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003240 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003241 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3242 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3243 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003244 default:
Steve Blockec193de2012-01-09 18:35:44 +00003245 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003246 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003247
3248 // Distance
3249 switch (mCalibration.distanceCalibration) {
3250 case Calibration::DISTANCE_CALIBRATION_NONE:
3251 dump.append(INDENT4 "touch.distance.calibration: none\n");
3252 break;
3253 case Calibration::DISTANCE_CALIBRATION_SCALED:
3254 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3255 break;
3256 default:
Steve Blockec193de2012-01-09 18:35:44 +00003257 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003258 }
3259
3260 if (mCalibration.haveDistanceScale) {
3261 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3262 mCalibration.distanceScale);
3263 }
Jeff Brown8d608662010-08-30 03:02:23 -07003264}
3265
Jeff Brown65fd2512011-08-18 11:20:58 -07003266void TouchInputMapper::reset(nsecs_t when) {
3267 mCursorButtonAccumulator.reset(getDevice());
3268 mCursorScrollAccumulator.reset(getDevice());
3269 mTouchButtonAccumulator.reset(getDevice());
3270
3271 mPointerVelocityControl.reset();
3272 mWheelXVelocityControl.reset();
3273 mWheelYVelocityControl.reset();
3274
Jeff Brownbe1aa822011-07-27 16:04:54 -07003275 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003276 mLastRawPointerData.clear();
3277 mCurrentCookedPointerData.clear();
3278 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003279 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003280 mLastButtonState = 0;
3281 mCurrentRawVScroll = 0;
3282 mCurrentRawHScroll = 0;
3283 mCurrentFingerIdBits.clear();
3284 mLastFingerIdBits.clear();
3285 mCurrentStylusIdBits.clear();
3286 mLastStylusIdBits.clear();
3287 mCurrentMouseIdBits.clear();
3288 mLastMouseIdBits.clear();
3289 mPointerUsage = POINTER_USAGE_NONE;
3290 mSentHoverEnter = false;
3291 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003292
Jeff Brown65fd2512011-08-18 11:20:58 -07003293 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003294
Jeff Brown65fd2512011-08-18 11:20:58 -07003295 mPointerGesture.reset();
3296 mPointerSimple.reset();
3297
3298 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003299 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3300 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003301 }
3302
Jeff Brown65fd2512011-08-18 11:20:58 -07003303 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003304}
3305
Jeff Brown65fd2512011-08-18 11:20:58 -07003306void TouchInputMapper::process(const RawEvent* rawEvent) {
3307 mCursorButtonAccumulator.process(rawEvent);
3308 mCursorScrollAccumulator.process(rawEvent);
3309 mTouchButtonAccumulator.process(rawEvent);
3310
3311 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3312 sync(rawEvent->when);
3313 }
3314}
3315
3316void TouchInputMapper::sync(nsecs_t when) {
3317 // Sync button state.
3318 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3319 | mCursorButtonAccumulator.getButtonState();
3320
3321 // Sync scroll state.
3322 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3323 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3324 mCursorScrollAccumulator.finishSync();
3325
3326 // Sync touch state.
3327 bool havePointerIds = true;
3328 mCurrentRawPointerData.clear();
3329 syncTouch(when, &havePointerIds);
3330
Jeff Brownaa3855d2011-03-17 01:34:19 -07003331#if DEBUG_RAW_EVENTS
3332 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003333 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003334 mLastRawPointerData.pointerCount,
3335 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003336 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003337 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003338 "hovering ids 0x%08x -> 0x%08x",
3339 mLastRawPointerData.pointerCount,
3340 mCurrentRawPointerData.pointerCount,
3341 mLastRawPointerData.touchingIdBits.value,
3342 mCurrentRawPointerData.touchingIdBits.value,
3343 mLastRawPointerData.hoveringIdBits.value,
3344 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003345 }
3346#endif
3347
Jeff Brown65fd2512011-08-18 11:20:58 -07003348 // Reset state that we will compute below.
3349 mCurrentFingerIdBits.clear();
3350 mCurrentStylusIdBits.clear();
3351 mCurrentMouseIdBits.clear();
3352 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003353
Jeff Brown65fd2512011-08-18 11:20:58 -07003354 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3355 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003356 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003357 mCurrentButtonState = 0;
3358 } else {
3359 // Preprocess pointer data.
3360 if (!havePointerIds) {
3361 assignPointerIds();
3362 }
3363
3364 // Handle policy on initial down or hover events.
3365 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003366 bool initialDown = mLastRawPointerData.pointerCount == 0
3367 && mCurrentRawPointerData.pointerCount != 0;
3368 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3369 if (initialDown || buttonsPressed) {
3370 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003371 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003372 getContext()->fadePointer();
3373 }
3374
3375 // Initial downs on external touch devices should wake the device.
3376 // We don't do this for internal touch screens to prevent them from waking
3377 // up in your pocket.
3378 // TODO: Use the input device configuration to control this behavior more finely.
3379 if (getDevice()->isExternal()) {
3380 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3381 }
3382 }
3383
3384 // Synthesize key down from raw buttons if needed.
3385 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3386 policyFlags, mLastButtonState, mCurrentButtonState);
3387
3388 // Consume raw off-screen touches before cooking pointer data.
3389 // If touches are consumed, subsequent code will not receive any pointer data.
3390 if (consumeRawTouches(when, policyFlags)) {
3391 mCurrentRawPointerData.clear();
3392 }
3393
3394 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3395 // with cooked pointer data that has the same ids and indices as the raw data.
3396 // The following code can use either the raw or cooked data, as needed.
3397 cookPointerData();
3398
3399 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003400 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003401 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3402 uint32_t id = idBits.clearFirstMarkedBit();
3403 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3404 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3405 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3406 mCurrentStylusIdBits.markBit(id);
3407 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3408 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3409 mCurrentFingerIdBits.markBit(id);
3410 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3411 mCurrentMouseIdBits.markBit(id);
3412 }
3413 }
3414 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3415 uint32_t id = idBits.clearFirstMarkedBit();
3416 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3417 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3418 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3419 mCurrentStylusIdBits.markBit(id);
3420 }
3421 }
3422
3423 // Stylus takes precedence over all tools, then mouse, then finger.
3424 PointerUsage pointerUsage = mPointerUsage;
3425 if (!mCurrentStylusIdBits.isEmpty()) {
3426 mCurrentMouseIdBits.clear();
3427 mCurrentFingerIdBits.clear();
3428 pointerUsage = POINTER_USAGE_STYLUS;
3429 } else if (!mCurrentMouseIdBits.isEmpty()) {
3430 mCurrentFingerIdBits.clear();
3431 pointerUsage = POINTER_USAGE_MOUSE;
3432 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3433 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003434 }
3435
3436 dispatchPointerUsage(when, policyFlags, pointerUsage);
3437 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003438 if (mDeviceMode == DEVICE_MODE_DIRECT
3439 && mConfig.showTouches && mPointerController != NULL) {
3440 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3441 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3442
3443 mPointerController->setButtonState(mCurrentButtonState);
3444 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3445 mCurrentCookedPointerData.idToIndex,
3446 mCurrentCookedPointerData.touchingIdBits);
3447 }
3448
Jeff Brown65fd2512011-08-18 11:20:58 -07003449 dispatchHoverExit(when, policyFlags);
3450 dispatchTouches(when, policyFlags);
3451 dispatchHoverEnterAndMove(when, policyFlags);
3452 }
3453
3454 // Synthesize key up from raw buttons if needed.
3455 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3456 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003457 }
3458
Jeff Brown6328cdc2010-07-29 18:18:33 -07003459 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003460 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3461 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3462 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003463 mLastFingerIdBits = mCurrentFingerIdBits;
3464 mLastStylusIdBits = mCurrentStylusIdBits;
3465 mLastMouseIdBits = mCurrentMouseIdBits;
3466
3467 // Clear some transient state.
3468 mCurrentRawVScroll = 0;
3469 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003470}
3471
Jeff Brown79ac9692011-04-19 21:20:10 -07003472void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003473 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003474 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3475 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3476 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003477 }
3478}
3479
Jeff Brownbe1aa822011-07-27 16:04:54 -07003480bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3481 // Check for release of a virtual key.
3482 if (mCurrentVirtualKey.down) {
3483 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3484 // Pointer went up while virtual key was down.
3485 mCurrentVirtualKey.down = false;
3486 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003487#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003488 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003489 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003490#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003491 dispatchVirtualKey(when, policyFlags,
3492 AKEY_EVENT_ACTION_UP,
3493 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003494 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003495 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003496 }
3497
Jeff Brownbe1aa822011-07-27 16:04:54 -07003498 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3499 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3500 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3501 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3502 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3503 // Pointer is still within the space of the virtual key.
3504 return true;
3505 }
3506 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003507
Jeff Brownbe1aa822011-07-27 16:04:54 -07003508 // Pointer left virtual key area or another pointer also went down.
3509 // Send key cancellation but do not consume the touch yet.
3510 // This is useful when the user swipes through from the virtual key area
3511 // into the main display surface.
3512 mCurrentVirtualKey.down = false;
3513 if (!mCurrentVirtualKey.ignored) {
3514#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003515 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003516 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3517#endif
3518 dispatchVirtualKey(when, policyFlags,
3519 AKEY_EVENT_ACTION_UP,
3520 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3521 | AKEY_EVENT_FLAG_CANCELED);
3522 }
3523 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003524
Jeff Brownbe1aa822011-07-27 16:04:54 -07003525 if (mLastRawPointerData.touchingIdBits.isEmpty()
3526 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3527 // Pointer just went down. Check for virtual key press or off-screen touches.
3528 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3529 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3530 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3531 // If exactly one pointer went down, check for virtual key hit.
3532 // Otherwise we will drop the entire stroke.
3533 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3534 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3535 if (virtualKey) {
3536 mCurrentVirtualKey.down = true;
3537 mCurrentVirtualKey.downTime = when;
3538 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3539 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3540 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3541 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3542
3543 if (!mCurrentVirtualKey.ignored) {
3544#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003545 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003546 mCurrentVirtualKey.keyCode,
3547 mCurrentVirtualKey.scanCode);
3548#endif
3549 dispatchVirtualKey(when, policyFlags,
3550 AKEY_EVENT_ACTION_DOWN,
3551 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3552 }
3553 }
3554 }
3555 return true;
3556 }
3557 }
3558
Jeff Brownfe508922011-01-18 15:10:10 -08003559 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003560 // most recent touch within the screen area. The idea is to filter out stray
3561 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003562 //
3563 // Problems we're trying to solve:
3564 //
3565 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3566 // virtual key area that is implemented by a separate touch panel and accidentally
3567 // triggers a virtual key.
3568 //
3569 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3570 // area and accidentally triggers a virtual key. This often happens when virtual keys
3571 // are layed out below the screen near to where the on screen keyboard's space bar
3572 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003573 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003574 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003575 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003576 return false;
3577}
3578
3579void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3580 int32_t keyEventAction, int32_t keyEventFlags) {
3581 int32_t keyCode = mCurrentVirtualKey.keyCode;
3582 int32_t scanCode = mCurrentVirtualKey.scanCode;
3583 nsecs_t downTime = mCurrentVirtualKey.downTime;
3584 int32_t metaState = mContext->getGlobalMetaState();
3585 policyFlags |= POLICY_FLAG_VIRTUAL;
3586
3587 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3588 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3589 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003590}
3591
Jeff Brown6d0fec22010-07-23 21:28:06 -07003592void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003593 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3594 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003595 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003596 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003597
3598 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003599 if (!currentIdBits.isEmpty()) {
3600 // No pointer id changes so this is a move event.
3601 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003602 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003603 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3604 AMOTION_EVENT_EDGE_FLAG_NONE,
3605 mCurrentCookedPointerData.pointerProperties,
3606 mCurrentCookedPointerData.pointerCoords,
3607 mCurrentCookedPointerData.idToIndex,
3608 currentIdBits, -1,
3609 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3610 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003611 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003612 // There may be pointers going up and pointers going down and pointers moving
3613 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003614 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3615 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003616 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003617 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003618
Jeff Brownace13b12011-03-09 17:39:48 -08003619 // Update last coordinates of pointers that have moved so that we observe the new
3620 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003621 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003622 mCurrentCookedPointerData.pointerProperties,
3623 mCurrentCookedPointerData.pointerCoords,
3624 mCurrentCookedPointerData.idToIndex,
3625 mLastCookedPointerData.pointerProperties,
3626 mLastCookedPointerData.pointerCoords,
3627 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003628 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003629 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003630 moveNeeded = true;
3631 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003632
Jeff Brownace13b12011-03-09 17:39:48 -08003633 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003634 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003635 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003636
Jeff Brown65fd2512011-08-18 11:20:58 -07003637 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003638 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003639 mLastCookedPointerData.pointerProperties,
3640 mLastCookedPointerData.pointerCoords,
3641 mLastCookedPointerData.idToIndex,
3642 dispatchedIdBits, upId,
3643 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003644 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003645 }
3646
Jeff Brownc3db8582010-10-20 15:33:38 -07003647 // Dispatch move events if any of the remaining pointers moved from their old locations.
3648 // Although applications receive new locations as part of individual pointer up
3649 // events, they do not generally handle them except when presented in a move event.
3650 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003651 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003652 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003653 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003654 mCurrentCookedPointerData.pointerProperties,
3655 mCurrentCookedPointerData.pointerCoords,
3656 mCurrentCookedPointerData.idToIndex,
3657 dispatchedIdBits, -1,
3658 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003659 }
3660
3661 // Dispatch pointer down events using the new pointer locations.
3662 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003663 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003664 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003665
Jeff Brownace13b12011-03-09 17:39:48 -08003666 if (dispatchedIdBits.count() == 1) {
3667 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003668 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003669 }
3670
Jeff Brown65fd2512011-08-18 11:20:58 -07003671 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003672 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003673 mCurrentCookedPointerData.pointerProperties,
3674 mCurrentCookedPointerData.pointerCoords,
3675 mCurrentCookedPointerData.idToIndex,
3676 dispatchedIdBits, downId,
3677 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003678 }
3679 }
Jeff Brownace13b12011-03-09 17:39:48 -08003680}
3681
Jeff Brownbe1aa822011-07-27 16:04:54 -07003682void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3683 if (mSentHoverEnter &&
3684 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3685 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3686 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003687 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003688 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3689 mLastCookedPointerData.pointerProperties,
3690 mLastCookedPointerData.pointerCoords,
3691 mLastCookedPointerData.idToIndex,
3692 mLastCookedPointerData.hoveringIdBits, -1,
3693 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3694 mSentHoverEnter = false;
3695 }
3696}
Jeff Brownace13b12011-03-09 17:39:48 -08003697
Jeff Brownbe1aa822011-07-27 16:04:54 -07003698void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3699 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3700 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3701 int32_t metaState = getContext()->getGlobalMetaState();
3702 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003703 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003704 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3705 mCurrentCookedPointerData.pointerProperties,
3706 mCurrentCookedPointerData.pointerCoords,
3707 mCurrentCookedPointerData.idToIndex,
3708 mCurrentCookedPointerData.hoveringIdBits, -1,
3709 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3710 mSentHoverEnter = true;
3711 }
Jeff Brownace13b12011-03-09 17:39:48 -08003712
Jeff Brown65fd2512011-08-18 11:20:58 -07003713 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003714 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3715 mCurrentCookedPointerData.pointerProperties,
3716 mCurrentCookedPointerData.pointerCoords,
3717 mCurrentCookedPointerData.idToIndex,
3718 mCurrentCookedPointerData.hoveringIdBits, -1,
3719 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3720 }
3721}
3722
3723void TouchInputMapper::cookPointerData() {
3724 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3725
3726 mCurrentCookedPointerData.clear();
3727 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3728 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3729 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3730
3731 // Walk through the the active pointers and map device coordinates onto
3732 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003733 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003734 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003735
Jeff Browna1f89ce2011-08-11 00:05:01 -07003736 // Size
3737 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3738 switch (mCalibration.sizeCalibration) {
3739 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3740 case Calibration::SIZE_CALIBRATION_DIAMETER:
3741 case Calibration::SIZE_CALIBRATION_AREA:
3742 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3743 touchMajor = in.touchMajor;
3744 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3745 toolMajor = in.toolMajor;
3746 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3747 size = mRawPointerAxes.touchMinor.valid
3748 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3749 } else if (mRawPointerAxes.touchMajor.valid) {
3750 toolMajor = touchMajor = in.touchMajor;
3751 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3752 ? in.touchMinor : in.touchMajor;
3753 size = mRawPointerAxes.touchMinor.valid
3754 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3755 } else if (mRawPointerAxes.toolMajor.valid) {
3756 touchMajor = toolMajor = in.toolMajor;
3757 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3758 ? in.toolMinor : in.toolMajor;
3759 size = mRawPointerAxes.toolMinor.valid
3760 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003761 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003762 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003763 "Size calibration should have been resolved to NONE.");
3764 touchMajor = 0;
3765 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003766 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003767 toolMinor = 0;
3768 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003769 }
Jeff Brownace13b12011-03-09 17:39:48 -08003770
Jeff Browna1f89ce2011-08-11 00:05:01 -07003771 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3772 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3773 if (touchingCount > 1) {
3774 touchMajor /= touchingCount;
3775 touchMinor /= touchingCount;
3776 toolMajor /= touchingCount;
3777 toolMinor /= touchingCount;
3778 size /= touchingCount;
3779 }
3780 }
Jeff Brownace13b12011-03-09 17:39:48 -08003781
Jeff Browna1f89ce2011-08-11 00:05:01 -07003782 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3783 touchMajor *= mGeometricScale;
3784 touchMinor *= mGeometricScale;
3785 toolMajor *= mGeometricScale;
3786 toolMinor *= mGeometricScale;
3787 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3788 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003789 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003790 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3791 toolMinor = toolMajor;
3792 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3793 touchMinor = touchMajor;
3794 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003795 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003796
3797 mCalibration.applySizeScaleAndBias(&touchMajor);
3798 mCalibration.applySizeScaleAndBias(&touchMinor);
3799 mCalibration.applySizeScaleAndBias(&toolMajor);
3800 mCalibration.applySizeScaleAndBias(&toolMinor);
3801 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003802 break;
3803 default:
3804 touchMajor = 0;
3805 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003806 toolMajor = 0;
3807 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003808 size = 0;
3809 break;
3810 }
3811
Jeff Browna1f89ce2011-08-11 00:05:01 -07003812 // Pressure
3813 float pressure;
3814 switch (mCalibration.pressureCalibration) {
3815 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3816 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3817 pressure = in.pressure * mPressureScale;
3818 break;
3819 default:
3820 pressure = in.isHovering ? 0 : 1;
3821 break;
3822 }
3823
Jeff Brown65fd2512011-08-18 11:20:58 -07003824 // Tilt and Orientation
3825 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003826 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003827 if (mHaveTilt) {
3828 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3829 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3830 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3831 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3832 } else {
3833 tilt = 0;
3834
3835 switch (mCalibration.orientationCalibration) {
3836 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3837 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3838 break;
3839 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3840 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3841 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3842 if (c1 != 0 || c2 != 0) {
3843 orientation = atan2f(c1, c2) * 0.5f;
3844 float confidence = hypotf(c1, c2);
3845 float scale = 1.0f + confidence / 16.0f;
3846 touchMajor *= scale;
3847 touchMinor /= scale;
3848 toolMajor *= scale;
3849 toolMinor /= scale;
3850 } else {
3851 orientation = 0;
3852 }
3853 break;
3854 }
3855 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003856 orientation = 0;
3857 }
Jeff Brownace13b12011-03-09 17:39:48 -08003858 }
3859
Jeff Brown80fd47c2011-05-24 01:07:44 -07003860 // Distance
3861 float distance;
3862 switch (mCalibration.distanceCalibration) {
3863 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003864 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003865 break;
3866 default:
3867 distance = 0;
3868 }
3869
Jeff Brownace13b12011-03-09 17:39:48 -08003870 // X and Y
3871 // Adjust coords for surface orientation.
3872 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003873 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003874 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003875 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3876 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003877 orientation -= M_PI_2;
3878 if (orientation < - M_PI_2) {
3879 orientation += M_PI;
3880 }
3881 break;
3882 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003883 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3884 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003885 break;
3886 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003887 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3888 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003889 orientation += M_PI_2;
3890 if (orientation > M_PI_2) {
3891 orientation -= M_PI;
3892 }
3893 break;
3894 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003895 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3896 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003897 break;
3898 }
3899
3900 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003901 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003902 out.clear();
3903 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3904 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3905 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3906 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3907 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3908 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3909 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3910 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3911 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003912 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003913 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003914
3915 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003916 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3917 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003918 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003919 properties.id = id;
3920 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003921
Jeff Brownbe1aa822011-07-27 16:04:54 -07003922 // Write id index.
3923 mCurrentCookedPointerData.idToIndex[id] = i;
3924 }
Jeff Brownace13b12011-03-09 17:39:48 -08003925}
3926
Jeff Brown65fd2512011-08-18 11:20:58 -07003927void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3928 PointerUsage pointerUsage) {
3929 if (pointerUsage != mPointerUsage) {
3930 abortPointerUsage(when, policyFlags);
3931 mPointerUsage = pointerUsage;
3932 }
3933
3934 switch (mPointerUsage) {
3935 case POINTER_USAGE_GESTURES:
3936 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3937 break;
3938 case POINTER_USAGE_STYLUS:
3939 dispatchPointerStylus(when, policyFlags);
3940 break;
3941 case POINTER_USAGE_MOUSE:
3942 dispatchPointerMouse(when, policyFlags);
3943 break;
3944 default:
3945 break;
3946 }
3947}
3948
3949void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3950 switch (mPointerUsage) {
3951 case POINTER_USAGE_GESTURES:
3952 abortPointerGestures(when, policyFlags);
3953 break;
3954 case POINTER_USAGE_STYLUS:
3955 abortPointerStylus(when, policyFlags);
3956 break;
3957 case POINTER_USAGE_MOUSE:
3958 abortPointerMouse(when, policyFlags);
3959 break;
3960 default:
3961 break;
3962 }
3963
3964 mPointerUsage = POINTER_USAGE_NONE;
3965}
3966
Jeff Brown79ac9692011-04-19 21:20:10 -07003967void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3968 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003969 // Update current gesture coordinates.
3970 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003971 bool sendEvents = preparePointerGestures(when,
3972 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3973 if (!sendEvents) {
3974 return;
3975 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003976 if (finishPreviousGesture) {
3977 cancelPreviousGesture = false;
3978 }
Jeff Brownace13b12011-03-09 17:39:48 -08003979
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003980 // Update the pointer presentation and spots.
3981 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3982 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3983 if (finishPreviousGesture || cancelPreviousGesture) {
3984 mPointerController->clearSpots();
3985 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003986 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3987 mPointerGesture.currentGestureIdToIndex,
3988 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003989 } else {
3990 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3991 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003992
Jeff Brown538881e2011-05-25 18:23:38 -07003993 // Show or hide the pointer if needed.
3994 switch (mPointerGesture.currentGestureMode) {
3995 case PointerGesture::NEUTRAL:
3996 case PointerGesture::QUIET:
3997 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3998 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3999 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4000 // Remind the user of where the pointer is after finishing a gesture with spots.
4001 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4002 }
4003 break;
4004 case PointerGesture::TAP:
4005 case PointerGesture::TAP_DRAG:
4006 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4007 case PointerGesture::HOVER:
4008 case PointerGesture::PRESS:
4009 // Unfade the pointer when the current gesture manipulates the
4010 // area directly under the pointer.
4011 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4012 break;
4013 case PointerGesture::SWIPE:
4014 case PointerGesture::FREEFORM:
4015 // Fade the pointer when the current gesture manipulates a different
4016 // area and there are spots to guide the user experience.
4017 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4018 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4019 } else {
4020 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4021 }
4022 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004023 }
4024
Jeff Brownace13b12011-03-09 17:39:48 -08004025 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004026 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004027 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004028
4029 // Update last coordinates of pointers that have moved so that we observe the new
4030 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004031 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4032 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4033 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004034 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004035 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4036 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4037 bool moveNeeded = false;
4038 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004039 && !mPointerGesture.lastGestureIdBits.isEmpty()
4040 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004041 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4042 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004043 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004044 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004045 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004046 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4047 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004048 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004049 moveNeeded = true;
4050 }
Jeff Brownace13b12011-03-09 17:39:48 -08004051 }
4052
4053 // Send motion events for all pointers that went up or were canceled.
4054 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4055 if (!dispatchedGestureIdBits.isEmpty()) {
4056 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004057 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004058 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4059 AMOTION_EVENT_EDGE_FLAG_NONE,
4060 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004061 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4062 dispatchedGestureIdBits, -1,
4063 0, 0, mPointerGesture.downTime);
4064
4065 dispatchedGestureIdBits.clear();
4066 } else {
4067 BitSet32 upGestureIdBits;
4068 if (finishPreviousGesture) {
4069 upGestureIdBits = dispatchedGestureIdBits;
4070 } else {
4071 upGestureIdBits.value = dispatchedGestureIdBits.value
4072 & ~mPointerGesture.currentGestureIdBits.value;
4073 }
4074 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004075 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004076
Jeff Brown65fd2512011-08-18 11:20:58 -07004077 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004078 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004079 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4080 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004081 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4082 dispatchedGestureIdBits, id,
4083 0, 0, mPointerGesture.downTime);
4084
4085 dispatchedGestureIdBits.clearBit(id);
4086 }
4087 }
4088 }
4089
4090 // Send motion events for all pointers that moved.
4091 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004092 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004093 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4094 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004095 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4096 dispatchedGestureIdBits, -1,
4097 0, 0, mPointerGesture.downTime);
4098 }
4099
4100 // Send motion events for all pointers that went down.
4101 if (down) {
4102 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4103 & ~dispatchedGestureIdBits.value);
4104 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004105 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004106 dispatchedGestureIdBits.markBit(id);
4107
Jeff Brownace13b12011-03-09 17:39:48 -08004108 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004109 mPointerGesture.downTime = when;
4110 }
4111
Jeff Brown65fd2512011-08-18 11:20:58 -07004112 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004113 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004114 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004115 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4116 dispatchedGestureIdBits, id,
4117 0, 0, mPointerGesture.downTime);
4118 }
4119 }
4120
Jeff Brownace13b12011-03-09 17:39:48 -08004121 // Send motion events for hover.
4122 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004123 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004124 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4125 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4126 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004127 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4128 mPointerGesture.currentGestureIdBits, -1,
4129 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004130 } else if (dispatchedGestureIdBits.isEmpty()
4131 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4132 // Synthesize a hover move event after all pointers go up to indicate that
4133 // the pointer is hovering again even if the user is not currently touching
4134 // the touch pad. This ensures that a view will receive a fresh hover enter
4135 // event after a tap.
4136 float x, y;
4137 mPointerController->getPosition(&x, &y);
4138
4139 PointerProperties pointerProperties;
4140 pointerProperties.clear();
4141 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004142 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004143
4144 PointerCoords pointerCoords;
4145 pointerCoords.clear();
4146 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4147 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4148
Jeff Brown65fd2512011-08-18 11:20:58 -07004149 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004150 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4151 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4152 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004153 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004154 }
4155
4156 // Update state.
4157 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4158 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004159 mPointerGesture.lastGestureIdBits.clear();
4160 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004161 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4162 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004163 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004164 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004165 mPointerGesture.lastGestureProperties[index].copyFrom(
4166 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004167 mPointerGesture.lastGestureCoords[index].copyFrom(
4168 mPointerGesture.currentGestureCoords[index]);
4169 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004170 }
4171 }
4172}
4173
Jeff Brown65fd2512011-08-18 11:20:58 -07004174void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4175 // Cancel previously dispatches pointers.
4176 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4177 int32_t metaState = getContext()->getGlobalMetaState();
4178 int32_t buttonState = mCurrentButtonState;
4179 dispatchMotion(when, policyFlags, mSource,
4180 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4181 AMOTION_EVENT_EDGE_FLAG_NONE,
4182 mPointerGesture.lastGestureProperties,
4183 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4184 mPointerGesture.lastGestureIdBits, -1,
4185 0, 0, mPointerGesture.downTime);
4186 }
4187
4188 // Reset the current pointer gesture.
4189 mPointerGesture.reset();
4190 mPointerVelocityControl.reset();
4191
4192 // Remove any current spots.
4193 if (mPointerController != NULL) {
4194 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4195 mPointerController->clearSpots();
4196 }
4197}
4198
Jeff Brown79ac9692011-04-19 21:20:10 -07004199bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4200 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004201 *outCancelPreviousGesture = false;
4202 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004203
Jeff Brown79ac9692011-04-19 21:20:10 -07004204 // Handle TAP timeout.
4205 if (isTimeout) {
4206#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004207 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004208#endif
4209
4210 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004211 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004212 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004213 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004214 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004215 } else {
4216 // The tap is finished.
4217#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004218 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004219#endif
4220 *outFinishPreviousGesture = true;
4221
4222 mPointerGesture.activeGestureId = -1;
4223 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4224 mPointerGesture.currentGestureIdBits.clear();
4225
Jeff Brown65fd2512011-08-18 11:20:58 -07004226 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004227 return true;
4228 }
4229 }
4230
4231 // We did not handle this timeout.
4232 return false;
4233 }
4234
Jeff Brown65fd2512011-08-18 11:20:58 -07004235 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4236 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4237
Jeff Brownace13b12011-03-09 17:39:48 -08004238 // Update the velocity tracker.
4239 {
4240 VelocityTracker::Position positions[MAX_POINTERS];
4241 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004242 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004243 uint32_t id = idBits.clearFirstMarkedBit();
4244 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004245 positions[count].x = pointer.x * mPointerXMovementScale;
4246 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004247 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004248 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004249 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004250 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004251
Jeff Brownace13b12011-03-09 17:39:48 -08004252 // Pick a new active touch id if needed.
4253 // Choose an arbitrary pointer that just went down, if there is one.
4254 // Otherwise choose an arbitrary remaining pointer.
4255 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004256 // We keep the same active touch id for as long as possible.
4257 bool activeTouchChanged = false;
4258 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4259 int32_t activeTouchId = lastActiveTouchId;
4260 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004261 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004262 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004263 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004264 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004265 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004266 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004267 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004268 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004269 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004270 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004271 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004272 } else {
4273 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004274 }
4275 }
4276
4277 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004278 bool isQuietTime = false;
4279 if (activeTouchId < 0) {
4280 mPointerGesture.resetQuietTime();
4281 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004282 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004283 if (!isQuietTime) {
4284 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4285 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4286 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004287 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004288 // Enter quiet time when exiting swipe or freeform state.
4289 // This is to prevent accidentally entering the hover state and flinging the
4290 // pointer when finishing a swipe and there is still one pointer left onscreen.
4291 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004292 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004293 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004294 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004295 // Enter quiet time when releasing the button and there are still two or more
4296 // fingers down. This may indicate that one finger was used to press the button
4297 // but it has not gone up yet.
4298 isQuietTime = true;
4299 }
4300 if (isQuietTime) {
4301 mPointerGesture.quietTime = when;
4302 }
Jeff Brownace13b12011-03-09 17:39:48 -08004303 }
4304 }
4305
4306 // Switch states based on button and pointer state.
4307 if (isQuietTime) {
4308 // Case 1: Quiet time. (QUIET)
4309#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004310 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004311 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004312#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004313 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4314 *outFinishPreviousGesture = true;
4315 }
Jeff Brownace13b12011-03-09 17:39:48 -08004316
4317 mPointerGesture.activeGestureId = -1;
4318 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004319 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004320
Jeff Brown65fd2512011-08-18 11:20:58 -07004321 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004322 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004323 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004324 // The pointer follows the active touch point.
4325 // Emit DOWN, MOVE, UP events at the pointer location.
4326 //
4327 // Only the active touch matters; other fingers are ignored. This policy helps
4328 // to handle the case where the user places a second finger on the touch pad
4329 // to apply the necessary force to depress an integrated button below the surface.
4330 // We don't want the second finger to be delivered to applications.
4331 //
4332 // For this to work well, we need to make sure to track the pointer that is really
4333 // active. If the user first puts one finger down to click then adds another
4334 // finger to drag then the active pointer should switch to the finger that is
4335 // being dragged.
4336#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004337 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004338 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004339#endif
4340 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004341 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004342 *outFinishPreviousGesture = true;
4343 mPointerGesture.activeGestureId = 0;
4344 }
4345
4346 // Switch pointers if needed.
4347 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004348 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004349 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004350 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004351 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004352 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004353 float vx, vy;
4354 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4355 float speed = hypotf(vx, vy);
4356 if (speed > bestSpeed) {
4357 bestId = id;
4358 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004359 }
Jeff Brown8d608662010-08-30 03:02:23 -07004360 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004361 }
4362 if (bestId >= 0 && bestId != activeTouchId) {
4363 mPointerGesture.activeTouchId = activeTouchId = bestId;
4364 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004365#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004366 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004367 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004368#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004369 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004370 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004371
Jeff Brown65fd2512011-08-18 11:20:58 -07004372 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004373 const RawPointerData::Pointer& currentPointer =
4374 mCurrentRawPointerData.pointerForId(activeTouchId);
4375 const RawPointerData::Pointer& lastPointer =
4376 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004377 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4378 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004379
Jeff Brownbe1aa822011-07-27 16:04:54 -07004380 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004381 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004382
4383 // Move the pointer using a relative motion.
4384 // When using spots, the click will occur at the position of the anchor
4385 // spot and all other spots will move there.
4386 mPointerController->move(deltaX, deltaY);
4387 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004388 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004389 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004390
Jeff Brownace13b12011-03-09 17:39:48 -08004391 float x, y;
4392 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004393
Jeff Brown79ac9692011-04-19 21:20:10 -07004394 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004395 mPointerGesture.currentGestureIdBits.clear();
4396 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4397 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004398 mPointerGesture.currentGestureProperties[0].clear();
4399 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004400 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004401 mPointerGesture.currentGestureCoords[0].clear();
4402 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4403 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4404 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004405 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004406 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004407 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4408 *outFinishPreviousGesture = true;
4409 }
Jeff Brownace13b12011-03-09 17:39:48 -08004410
Jeff Brown79ac9692011-04-19 21:20:10 -07004411 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004412 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004413 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004414 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4415 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004416 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004417 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004418 float x, y;
4419 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004420 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4421 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004422#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004423 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004424#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004425
4426 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004427 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004428 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004429
Jeff Brownace13b12011-03-09 17:39:48 -08004430 mPointerGesture.activeGestureId = 0;
4431 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004432 mPointerGesture.currentGestureIdBits.clear();
4433 mPointerGesture.currentGestureIdBits.markBit(
4434 mPointerGesture.activeGestureId);
4435 mPointerGesture.currentGestureIdToIndex[
4436 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004437 mPointerGesture.currentGestureProperties[0].clear();
4438 mPointerGesture.currentGestureProperties[0].id =
4439 mPointerGesture.activeGestureId;
4440 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004441 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004442 mPointerGesture.currentGestureCoords[0].clear();
4443 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004444 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004445 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004446 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004447 mPointerGesture.currentGestureCoords[0].setAxisValue(
4448 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004449
Jeff Brownace13b12011-03-09 17:39:48 -08004450 tapped = true;
4451 } else {
4452#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004453 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004454 x - mPointerGesture.tapX,
4455 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004456#endif
4457 }
4458 } else {
4459#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004460 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004461 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004462#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004463 }
Jeff Brownace13b12011-03-09 17:39:48 -08004464 }
Jeff Brown2352b972011-04-12 22:39:53 -07004465
Jeff Brown65fd2512011-08-18 11:20:58 -07004466 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004467
Jeff Brownace13b12011-03-09 17:39:48 -08004468 if (!tapped) {
4469#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004470 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004471#endif
4472 mPointerGesture.activeGestureId = -1;
4473 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004474 mPointerGesture.currentGestureIdBits.clear();
4475 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004476 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004477 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004478 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004479 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4480 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004481 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004482
Jeff Brown79ac9692011-04-19 21:20:10 -07004483 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4484 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004485 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004486 float x, y;
4487 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004488 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4489 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004490 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4491 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004492#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004493 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004494 x - mPointerGesture.tapX,
4495 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004496#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004497 }
4498 } else {
4499#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004500 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004501 (when - mPointerGesture.tapUpTime) * 0.000001f);
4502#endif
4503 }
4504 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4505 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4506 }
Jeff Brownace13b12011-03-09 17:39:48 -08004507
Jeff Brown65fd2512011-08-18 11:20:58 -07004508 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004509 const RawPointerData::Pointer& currentPointer =
4510 mCurrentRawPointerData.pointerForId(activeTouchId);
4511 const RawPointerData::Pointer& lastPointer =
4512 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004513 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004514 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004515 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004516 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004517
Jeff Brownbe1aa822011-07-27 16:04:54 -07004518 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004519 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004520
Jeff Brown2352b972011-04-12 22:39:53 -07004521 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004522 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004523 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004524 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004525 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004526 }
4527
Jeff Brown79ac9692011-04-19 21:20:10 -07004528 bool down;
4529 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4530#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004531 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004532#endif
4533 down = true;
4534 } else {
4535#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004536 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004537#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004538 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4539 *outFinishPreviousGesture = true;
4540 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004541 mPointerGesture.activeGestureId = 0;
4542 down = false;
4543 }
Jeff Brownace13b12011-03-09 17:39:48 -08004544
4545 float x, y;
4546 mPointerController->getPosition(&x, &y);
4547
Jeff Brownace13b12011-03-09 17:39:48 -08004548 mPointerGesture.currentGestureIdBits.clear();
4549 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4550 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004551 mPointerGesture.currentGestureProperties[0].clear();
4552 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4553 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004554 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004555 mPointerGesture.currentGestureCoords[0].clear();
4556 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4557 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004558 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4559 down ? 1.0f : 0.0f);
4560
Jeff Brown65fd2512011-08-18 11:20:58 -07004561 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004562 mPointerGesture.resetTap();
4563 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004564 mPointerGesture.tapX = x;
4565 mPointerGesture.tapY = y;
4566 }
Jeff Brownace13b12011-03-09 17:39:48 -08004567 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004568 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4569 // We need to provide feedback for each finger that goes down so we cannot wait
4570 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004571 //
Jeff Brown2352b972011-04-12 22:39:53 -07004572 // The ambiguous case is deciding what to do when there are two fingers down but they
4573 // have not moved enough to determine whether they are part of a drag or part of a
4574 // freeform gesture, or just a press or long-press at the pointer location.
4575 //
4576 // When there are two fingers we start with the PRESS hypothesis and we generate a
4577 // down at the pointer location.
4578 //
4579 // When the two fingers move enough or when additional fingers are added, we make
4580 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004581 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004582
Jeff Brown214eaf42011-05-26 19:17:02 -07004583 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004584 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004585 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004586 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4587 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004588 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004589 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004590 // Additional pointers have gone down but not yet settled.
4591 // Reset the gesture.
4592#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004593 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004594 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004595 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004596 * 0.000001f);
4597#endif
4598 *outCancelPreviousGesture = true;
4599 } else {
4600 // Continue previous gesture.
4601 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4602 }
4603
4604 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004605 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4606 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004607 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004608 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004609
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004610 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004611#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004612 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004613 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004614 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004615 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004616#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004617 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4618 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004619 &mPointerGesture.referenceTouchY);
4620 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4621 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004622 }
Jeff Brownace13b12011-03-09 17:39:48 -08004623
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004624 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004625 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004626 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4627 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004628 mPointerGesture.referenceDeltas[id].dx = 0;
4629 mPointerGesture.referenceDeltas[id].dy = 0;
4630 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004631 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004632
4633 // Add delta for all fingers and calculate a common movement delta.
4634 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004635 BitSet32 commonIdBits(mLastFingerIdBits.value
4636 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004637 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4638 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004639 uint32_t id = idBits.clearFirstMarkedBit();
4640 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4641 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004642 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4643 delta.dx += cpd.x - lpd.x;
4644 delta.dy += cpd.y - lpd.y;
4645
4646 if (first) {
4647 commonDeltaX = delta.dx;
4648 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004649 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004650 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4651 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4652 }
4653 }
Jeff Brownace13b12011-03-09 17:39:48 -08004654
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004655 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4656 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4657 float dist[MAX_POINTER_ID + 1];
4658 int32_t distOverThreshold = 0;
4659 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004660 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004661 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004662 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4663 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004664 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004665 distOverThreshold += 1;
4666 }
4667 }
4668
4669 // Only transition when at least two pointers have moved further than
4670 // the minimum distance threshold.
4671 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004672 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004673 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004674#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004675 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004676 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004677#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004678 *outCancelPreviousGesture = true;
4679 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4680 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004681 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004682 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004683 uint32_t id1 = idBits.clearFirstMarkedBit();
4684 uint32_t id2 = idBits.firstMarkedBit();
4685 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4686 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4687 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4688 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4689 // There are two pointers but they are too far apart for a SWIPE,
4690 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004691#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004692 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004693 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004694#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004695 *outCancelPreviousGesture = true;
4696 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4697 } else {
4698 // There are two pointers. Wait for both pointers to start moving
4699 // before deciding whether this is a SWIPE or FREEFORM gesture.
4700 float dist1 = dist[id1];
4701 float dist2 = dist[id2];
4702 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4703 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4704 // Calculate the dot product of the displacement vectors.
4705 // When the vectors are oriented in approximately the same direction,
4706 // the angle betweeen them is near zero and the cosine of the angle
4707 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4708 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4709 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004710 float dx1 = delta1.dx * mPointerXZoomScale;
4711 float dy1 = delta1.dy * mPointerYZoomScale;
4712 float dx2 = delta2.dx * mPointerXZoomScale;
4713 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004714 float dot = dx1 * dx2 + dy1 * dy2;
4715 float cosine = dot / (dist1 * dist2); // denominator always > 0
4716 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4717 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004718#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004719 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004720 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4721 "cosine %0.3f >= %0.3f",
4722 dist1, mConfig.pointerGestureMultitouchMinDistance,
4723 dist2, mConfig.pointerGestureMultitouchMinDistance,
4724 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004725#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004726 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4727 } else {
4728 // Pointers are moving in different directions. Switch to FREEFORM.
4729#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004730 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004731 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4732 "cosine %0.3f < %0.3f",
4733 dist1, mConfig.pointerGestureMultitouchMinDistance,
4734 dist2, mConfig.pointerGestureMultitouchMinDistance,
4735 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4736#endif
4737 *outCancelPreviousGesture = true;
4738 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4739 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004740 }
Jeff Brownace13b12011-03-09 17:39:48 -08004741 }
4742 }
Jeff Brownace13b12011-03-09 17:39:48 -08004743 }
4744 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004745 // Switch from SWIPE to FREEFORM if additional pointers go down.
4746 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004747 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004748#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004749 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004750 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004751#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004752 *outCancelPreviousGesture = true;
4753 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004754 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004755 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004756
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004757 // Move the reference points based on the overall group motion of the fingers
4758 // except in PRESS mode while waiting for a transition to occur.
4759 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4760 && (commonDeltaX || commonDeltaY)) {
4761 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004762 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004763 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004764 delta.dx = 0;
4765 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004766 }
4767
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004768 mPointerGesture.referenceTouchX += commonDeltaX;
4769 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004770
Jeff Brown65fd2512011-08-18 11:20:58 -07004771 commonDeltaX *= mPointerXMovementScale;
4772 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004773
Jeff Brownbe1aa822011-07-27 16:04:54 -07004774 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004775 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004776
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004777 mPointerGesture.referenceGestureX += commonDeltaX;
4778 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004779 }
4780
4781 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004782 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4783 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4784 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004785#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004786 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004787 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004788 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004789#endif
Steve Blockec193de2012-01-09 18:35:44 +00004790 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07004791
4792 mPointerGesture.currentGestureIdBits.clear();
4793 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4794 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004795 mPointerGesture.currentGestureProperties[0].clear();
4796 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4797 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004798 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004799 mPointerGesture.currentGestureCoords[0].clear();
4800 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4801 mPointerGesture.referenceGestureX);
4802 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4803 mPointerGesture.referenceGestureY);
4804 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004805 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4806 // FREEFORM mode.
4807#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004808 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004809 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004810 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004811#endif
Steve Blockec193de2012-01-09 18:35:44 +00004812 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004813
Jeff Brownace13b12011-03-09 17:39:48 -08004814 mPointerGesture.currentGestureIdBits.clear();
4815
4816 BitSet32 mappedTouchIdBits;
4817 BitSet32 usedGestureIdBits;
4818 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4819 // Initially, assign the active gesture id to the active touch point
4820 // if there is one. No other touch id bits are mapped yet.
4821 if (!*outCancelPreviousGesture) {
4822 mappedTouchIdBits.markBit(activeTouchId);
4823 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4824 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4825 mPointerGesture.activeGestureId;
4826 } else {
4827 mPointerGesture.activeGestureId = -1;
4828 }
4829 } else {
4830 // Otherwise, assume we mapped all touches from the previous frame.
4831 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004832 mappedTouchIdBits.value = mLastFingerIdBits.value
4833 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004834 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4835
4836 // Check whether we need to choose a new active gesture id because the
4837 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004838 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4839 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004840 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004841 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004842 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4843 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4844 mPointerGesture.activeGestureId = -1;
4845 break;
4846 }
4847 }
4848 }
4849
4850#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004851 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08004852 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4853 "activeGestureId=%d",
4854 mappedTouchIdBits.value, usedGestureIdBits.value,
4855 mPointerGesture.activeGestureId);
4856#endif
4857
Jeff Brown65fd2512011-08-18 11:20:58 -07004858 BitSet32 idBits(mCurrentFingerIdBits);
4859 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004860 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004861 uint32_t gestureId;
4862 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004863 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004864 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4865#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004866 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004867 "new mapping for touch id %d -> gesture id %d",
4868 touchId, gestureId);
4869#endif
4870 } else {
4871 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4872#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004873 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004874 "existing mapping for touch id %d -> gesture id %d",
4875 touchId, gestureId);
4876#endif
4877 }
4878 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4879 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4880
Jeff Brownbe1aa822011-07-27 16:04:54 -07004881 const RawPointerData::Pointer& pointer =
4882 mCurrentRawPointerData.pointerForId(touchId);
4883 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004884 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004885 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004886 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004887 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004888
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004889 mPointerGesture.currentGestureProperties[i].clear();
4890 mPointerGesture.currentGestureProperties[i].id = gestureId;
4891 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004892 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004893 mPointerGesture.currentGestureCoords[i].clear();
4894 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004895 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004896 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004897 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004898 mPointerGesture.currentGestureCoords[i].setAxisValue(
4899 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4900 }
4901
4902 if (mPointerGesture.activeGestureId < 0) {
4903 mPointerGesture.activeGestureId =
4904 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4905#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004906 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08004907 "activeGestureId=%d", mPointerGesture.activeGestureId);
4908#endif
4909 }
Jeff Brown2352b972011-04-12 22:39:53 -07004910 }
Jeff Brownace13b12011-03-09 17:39:48 -08004911 }
4912
Jeff Brownbe1aa822011-07-27 16:04:54 -07004913 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004914
Jeff Brownace13b12011-03-09 17:39:48 -08004915#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004916 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004917 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4918 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004919 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004920 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4921 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004922 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004923 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004924 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004925 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004926 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004927 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004928 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4929 id, index, properties.toolType,
4930 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004931 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4932 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4933 }
4934 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004935 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004936 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004937 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004938 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004939 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004940 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4941 id, index, properties.toolType,
4942 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004943 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4944 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4945 }
4946#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004947 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004948}
4949
Jeff Brown65fd2512011-08-18 11:20:58 -07004950void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4951 mPointerSimple.currentCoords.clear();
4952 mPointerSimple.currentProperties.clear();
4953
4954 bool down, hovering;
4955 if (!mCurrentStylusIdBits.isEmpty()) {
4956 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4957 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4958 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4959 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4960 mPointerController->setPosition(x, y);
4961
4962 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4963 down = !hovering;
4964
4965 mPointerController->getPosition(&x, &y);
4966 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4967 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4968 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4969 mPointerSimple.currentProperties.id = 0;
4970 mPointerSimple.currentProperties.toolType =
4971 mCurrentCookedPointerData.pointerProperties[index].toolType;
4972 } else {
4973 down = false;
4974 hovering = false;
4975 }
4976
4977 dispatchPointerSimple(when, policyFlags, down, hovering);
4978}
4979
4980void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4981 abortPointerSimple(when, policyFlags);
4982}
4983
4984void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4985 mPointerSimple.currentCoords.clear();
4986 mPointerSimple.currentProperties.clear();
4987
4988 bool down, hovering;
4989 if (!mCurrentMouseIdBits.isEmpty()) {
4990 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4991 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4992 if (mLastMouseIdBits.hasBit(id)) {
4993 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4994 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4995 - mLastRawPointerData.pointers[lastIndex].x)
4996 * mPointerXMovementScale;
4997 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4998 - mLastRawPointerData.pointers[lastIndex].y)
4999 * mPointerYMovementScale;
5000
5001 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5002 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5003
5004 mPointerController->move(deltaX, deltaY);
5005 } else {
5006 mPointerVelocityControl.reset();
5007 }
5008
5009 down = isPointerDown(mCurrentButtonState);
5010 hovering = !down;
5011
5012 float x, y;
5013 mPointerController->getPosition(&x, &y);
5014 mPointerSimple.currentCoords.copyFrom(
5015 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5016 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5017 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5018 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5019 hovering ? 0.0f : 1.0f);
5020 mPointerSimple.currentProperties.id = 0;
5021 mPointerSimple.currentProperties.toolType =
5022 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5023 } else {
5024 mPointerVelocityControl.reset();
5025
5026 down = false;
5027 hovering = false;
5028 }
5029
5030 dispatchPointerSimple(when, policyFlags, down, hovering);
5031}
5032
5033void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5034 abortPointerSimple(when, policyFlags);
5035
5036 mPointerVelocityControl.reset();
5037}
5038
5039void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5040 bool down, bool hovering) {
5041 int32_t metaState = getContext()->getGlobalMetaState();
5042
5043 if (mPointerController != NULL) {
5044 if (down || hovering) {
5045 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5046 mPointerController->clearSpots();
5047 mPointerController->setButtonState(mCurrentButtonState);
5048 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5049 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5050 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5051 }
5052 }
5053
5054 if (mPointerSimple.down && !down) {
5055 mPointerSimple.down = false;
5056
5057 // Send up.
5058 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5059 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5060 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5061 mOrientedXPrecision, mOrientedYPrecision,
5062 mPointerSimple.downTime);
5063 getListener()->notifyMotion(&args);
5064 }
5065
5066 if (mPointerSimple.hovering && !hovering) {
5067 mPointerSimple.hovering = false;
5068
5069 // Send hover exit.
5070 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5071 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5072 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5073 mOrientedXPrecision, mOrientedYPrecision,
5074 mPointerSimple.downTime);
5075 getListener()->notifyMotion(&args);
5076 }
5077
5078 if (down) {
5079 if (!mPointerSimple.down) {
5080 mPointerSimple.down = true;
5081 mPointerSimple.downTime = when;
5082
5083 // Send down.
5084 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5085 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5086 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5087 mOrientedXPrecision, mOrientedYPrecision,
5088 mPointerSimple.downTime);
5089 getListener()->notifyMotion(&args);
5090 }
5091
5092 // Send move.
5093 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5094 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5095 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5096 mOrientedXPrecision, mOrientedYPrecision,
5097 mPointerSimple.downTime);
5098 getListener()->notifyMotion(&args);
5099 }
5100
5101 if (hovering) {
5102 if (!mPointerSimple.hovering) {
5103 mPointerSimple.hovering = true;
5104
5105 // Send hover enter.
5106 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5107 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5108 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5109 mOrientedXPrecision, mOrientedYPrecision,
5110 mPointerSimple.downTime);
5111 getListener()->notifyMotion(&args);
5112 }
5113
5114 // Send hover move.
5115 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5116 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5117 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5118 mOrientedXPrecision, mOrientedYPrecision,
5119 mPointerSimple.downTime);
5120 getListener()->notifyMotion(&args);
5121 }
5122
5123 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5124 float vscroll = mCurrentRawVScroll;
5125 float hscroll = mCurrentRawHScroll;
5126 mWheelYVelocityControl.move(when, NULL, &vscroll);
5127 mWheelXVelocityControl.move(when, &hscroll, NULL);
5128
5129 // Send scroll.
5130 PointerCoords pointerCoords;
5131 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5132 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5133 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5134
5135 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5136 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5137 1, &mPointerSimple.currentProperties, &pointerCoords,
5138 mOrientedXPrecision, mOrientedYPrecision,
5139 mPointerSimple.downTime);
5140 getListener()->notifyMotion(&args);
5141 }
5142
5143 // Save state.
5144 if (down || hovering) {
5145 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5146 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5147 } else {
5148 mPointerSimple.reset();
5149 }
5150}
5151
5152void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5153 mPointerSimple.currentCoords.clear();
5154 mPointerSimple.currentProperties.clear();
5155
5156 dispatchPointerSimple(when, policyFlags, false, false);
5157}
5158
Jeff Brownace13b12011-03-09 17:39:48 -08005159void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005160 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5161 const PointerProperties* properties, const PointerCoords* coords,
5162 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005163 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5164 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005165 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005166 uint32_t pointerCount = 0;
5167 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005168 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005169 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005170 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005171 pointerCoords[pointerCount].copyFrom(coords[index]);
5172
5173 if (changedId >= 0 && id == uint32_t(changedId)) {
5174 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5175 }
5176
5177 pointerCount += 1;
5178 }
5179
Steve Blockec193de2012-01-09 18:35:44 +00005180 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005181
5182 if (changedId >= 0 && pointerCount == 1) {
5183 // Replace initial down and final up action.
5184 // We can compare the action without masking off the changed pointer index
5185 // because we know the index is 0.
5186 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5187 action = AMOTION_EVENT_ACTION_DOWN;
5188 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5189 action = AMOTION_EVENT_ACTION_UP;
5190 } else {
5191 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005192 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005193 }
5194 }
5195
Jeff Brownbe1aa822011-07-27 16:04:54 -07005196 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005197 action, flags, metaState, buttonState, edgeFlags,
5198 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005199 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005200}
5201
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005202bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005203 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005204 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5205 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005206 bool changed = false;
5207 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005208 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005209 uint32_t inIndex = inIdToIndex[id];
5210 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005211
5212 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005213 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005214 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005215 PointerCoords& curOutCoords = outCoords[outIndex];
5216
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005217 if (curInProperties != curOutProperties) {
5218 curOutProperties.copyFrom(curInProperties);
5219 changed = true;
5220 }
5221
Jeff Brownace13b12011-03-09 17:39:48 -08005222 if (curInCoords != curOutCoords) {
5223 curOutCoords.copyFrom(curInCoords);
5224 changed = true;
5225 }
5226 }
5227 return changed;
5228}
5229
5230void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005231 if (mPointerController != NULL) {
5232 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5233 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005234}
5235
Jeff Brownbe1aa822011-07-27 16:04:54 -07005236bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5237 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5238 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005239}
5240
Jeff Brownbe1aa822011-07-27 16:04:54 -07005241const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005242 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005243 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005244 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005245 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005246
5247#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005248 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005249 "left=%d, top=%d, right=%d, bottom=%d",
5250 x, y,
5251 virtualKey.keyCode, virtualKey.scanCode,
5252 virtualKey.hitLeft, virtualKey.hitTop,
5253 virtualKey.hitRight, virtualKey.hitBottom);
5254#endif
5255
5256 if (virtualKey.isHit(x, y)) {
5257 return & virtualKey;
5258 }
5259 }
5260
5261 return NULL;
5262}
5263
Jeff Brownbe1aa822011-07-27 16:04:54 -07005264void TouchInputMapper::assignPointerIds() {
5265 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5266 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5267
5268 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005269
5270 if (currentPointerCount == 0) {
5271 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005272 return;
5273 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005274
Jeff Brownbe1aa822011-07-27 16:04:54 -07005275 if (lastPointerCount == 0) {
5276 // All pointers are new.
5277 for (uint32_t i = 0; i < currentPointerCount; i++) {
5278 uint32_t id = i;
5279 mCurrentRawPointerData.pointers[i].id = id;
5280 mCurrentRawPointerData.idToIndex[id] = i;
5281 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5282 }
5283 return;
5284 }
5285
5286 if (currentPointerCount == 1 && lastPointerCount == 1
5287 && mCurrentRawPointerData.pointers[0].toolType
5288 == mLastRawPointerData.pointers[0].toolType) {
5289 // Only one pointer and no change in count so it must have the same id as before.
5290 uint32_t id = mLastRawPointerData.pointers[0].id;
5291 mCurrentRawPointerData.pointers[0].id = id;
5292 mCurrentRawPointerData.idToIndex[id] = 0;
5293 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5294 return;
5295 }
5296
5297 // General case.
5298 // We build a heap of squared euclidean distances between current and last pointers
5299 // associated with the current and last pointer indices. Then, we find the best
5300 // match (by distance) for each current pointer.
5301 // The pointers must have the same tool type but it is possible for them to
5302 // transition from hovering to touching or vice-versa while retaining the same id.
5303 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5304
5305 uint32_t heapSize = 0;
5306 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5307 currentPointerIndex++) {
5308 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5309 lastPointerIndex++) {
5310 const RawPointerData::Pointer& currentPointer =
5311 mCurrentRawPointerData.pointers[currentPointerIndex];
5312 const RawPointerData::Pointer& lastPointer =
5313 mLastRawPointerData.pointers[lastPointerIndex];
5314 if (currentPointer.toolType == lastPointer.toolType) {
5315 int64_t deltaX = currentPointer.x - lastPointer.x;
5316 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005317
5318 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5319
5320 // Insert new element into the heap (sift up).
5321 heap[heapSize].currentPointerIndex = currentPointerIndex;
5322 heap[heapSize].lastPointerIndex = lastPointerIndex;
5323 heap[heapSize].distance = distance;
5324 heapSize += 1;
5325 }
5326 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005327 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005328
Jeff Brownbe1aa822011-07-27 16:04:54 -07005329 // Heapify
5330 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5331 startIndex -= 1;
5332 for (uint32_t parentIndex = startIndex; ;) {
5333 uint32_t childIndex = parentIndex * 2 + 1;
5334 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005335 break;
5336 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005337
5338 if (childIndex + 1 < heapSize
5339 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5340 childIndex += 1;
5341 }
5342
5343 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5344 break;
5345 }
5346
5347 swap(heap[parentIndex], heap[childIndex]);
5348 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005349 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005350 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005351
5352#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005353 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005354 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005355 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005356 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5357 heap[i].distance);
5358 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005359#endif
5360
Jeff Brownbe1aa822011-07-27 16:04:54 -07005361 // Pull matches out by increasing order of distance.
5362 // To avoid reassigning pointers that have already been matched, the loop keeps track
5363 // of which last and current pointers have been matched using the matchedXXXBits variables.
5364 // It also tracks the used pointer id bits.
5365 BitSet32 matchedLastBits(0);
5366 BitSet32 matchedCurrentBits(0);
5367 BitSet32 usedIdBits(0);
5368 bool first = true;
5369 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5370 while (heapSize > 0) {
5371 if (first) {
5372 // The first time through the loop, we just consume the root element of
5373 // the heap (the one with smallest distance).
5374 first = false;
5375 } else {
5376 // Previous iterations consumed the root element of the heap.
5377 // Pop root element off of the heap (sift down).
5378 heap[0] = heap[heapSize];
5379 for (uint32_t parentIndex = 0; ;) {
5380 uint32_t childIndex = parentIndex * 2 + 1;
5381 if (childIndex >= heapSize) {
5382 break;
5383 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005384
Jeff Brownbe1aa822011-07-27 16:04:54 -07005385 if (childIndex + 1 < heapSize
5386 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5387 childIndex += 1;
5388 }
5389
5390 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5391 break;
5392 }
5393
5394 swap(heap[parentIndex], heap[childIndex]);
5395 parentIndex = childIndex;
5396 }
5397
5398#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005399 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005400 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005401 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005402 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5403 heap[i].distance);
5404 }
5405#endif
5406 }
5407
5408 heapSize -= 1;
5409
5410 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5411 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5412
5413 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5414 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5415
5416 matchedCurrentBits.markBit(currentPointerIndex);
5417 matchedLastBits.markBit(lastPointerIndex);
5418
5419 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5420 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5421 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5422 mCurrentRawPointerData.markIdBit(id,
5423 mCurrentRawPointerData.isHovering(currentPointerIndex));
5424 usedIdBits.markBit(id);
5425
5426#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005427 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005428 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5429#endif
5430 break;
5431 }
5432 }
5433
5434 // Assign fresh ids to pointers that were not matched in the process.
5435 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5436 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5437 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5438
5439 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5440 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5441 mCurrentRawPointerData.markIdBit(id,
5442 mCurrentRawPointerData.isHovering(currentPointerIndex));
5443
5444#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005445 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005446 currentPointerIndex, id);
5447#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005448 }
5449}
5450
Jeff Brown6d0fec22010-07-23 21:28:06 -07005451int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005452 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5453 return AKEY_STATE_VIRTUAL;
5454 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005455
Jeff Brownbe1aa822011-07-27 16:04:54 -07005456 size_t numVirtualKeys = mVirtualKeys.size();
5457 for (size_t i = 0; i < numVirtualKeys; i++) {
5458 const VirtualKey& virtualKey = mVirtualKeys[i];
5459 if (virtualKey.keyCode == keyCode) {
5460 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005461 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005462 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005463
5464 return AKEY_STATE_UNKNOWN;
5465}
5466
5467int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005468 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5469 return AKEY_STATE_VIRTUAL;
5470 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005471
Jeff Brownbe1aa822011-07-27 16:04:54 -07005472 size_t numVirtualKeys = mVirtualKeys.size();
5473 for (size_t i = 0; i < numVirtualKeys; i++) {
5474 const VirtualKey& virtualKey = mVirtualKeys[i];
5475 if (virtualKey.scanCode == scanCode) {
5476 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005477 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005478 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005479
5480 return AKEY_STATE_UNKNOWN;
5481}
5482
5483bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5484 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005485 size_t numVirtualKeys = mVirtualKeys.size();
5486 for (size_t i = 0; i < numVirtualKeys; i++) {
5487 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005488
Jeff Brownbe1aa822011-07-27 16:04:54 -07005489 for (size_t i = 0; i < numCodes; i++) {
5490 if (virtualKey.keyCode == keyCodes[i]) {
5491 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005492 }
5493 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005494 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005495
5496 return true;
5497}
5498
5499
5500// --- SingleTouchInputMapper ---
5501
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005502SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5503 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005504}
5505
5506SingleTouchInputMapper::~SingleTouchInputMapper() {
5507}
5508
Jeff Brown65fd2512011-08-18 11:20:58 -07005509void SingleTouchInputMapper::reset(nsecs_t when) {
5510 mSingleTouchMotionAccumulator.reset(getDevice());
5511
5512 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005513}
5514
Jeff Brown6d0fec22010-07-23 21:28:06 -07005515void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005516 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005517
Jeff Brown65fd2512011-08-18 11:20:58 -07005518 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005519}
5520
Jeff Brown65fd2512011-08-18 11:20:58 -07005521void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005522 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005523 mCurrentRawPointerData.pointerCount = 1;
5524 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005525
Jeff Brown65fd2512011-08-18 11:20:58 -07005526 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5527 && (mTouchButtonAccumulator.isHovering()
5528 || (mRawPointerAxes.pressure.valid
5529 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005530 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005531
Jeff Brownbe1aa822011-07-27 16:04:54 -07005532 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005533 outPointer.id = 0;
5534 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5535 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5536 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5537 outPointer.touchMajor = 0;
5538 outPointer.touchMinor = 0;
5539 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5540 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5541 outPointer.orientation = 0;
5542 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005543 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5544 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005545 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5546 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5547 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5548 }
5549 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005550 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005551}
5552
Jeff Brownbe1aa822011-07-27 16:04:54 -07005553void SingleTouchInputMapper::configureRawPointerAxes() {
5554 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005555
Jeff Brownbe1aa822011-07-27 16:04:54 -07005556 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5557 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5558 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5559 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5560 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005561 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5562 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005563}
5564
5565
5566// --- MultiTouchInputMapper ---
5567
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005568MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005569 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005570}
5571
5572MultiTouchInputMapper::~MultiTouchInputMapper() {
5573}
5574
Jeff Brown65fd2512011-08-18 11:20:58 -07005575void MultiTouchInputMapper::reset(nsecs_t when) {
5576 mMultiTouchMotionAccumulator.reset(getDevice());
5577
Jeff Brown6894a292011-07-01 17:59:27 -07005578 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005579
Jeff Brown65fd2512011-08-18 11:20:58 -07005580 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005581}
5582
5583void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005584 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005585
Jeff Brown65fd2512011-08-18 11:20:58 -07005586 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005587}
5588
Jeff Brown65fd2512011-08-18 11:20:58 -07005589void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005590 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005591 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005592 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005593
Jeff Brown80fd47c2011-05-24 01:07:44 -07005594 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005595 const MultiTouchMotionAccumulator::Slot* inSlot =
5596 mMultiTouchMotionAccumulator.getSlot(inIndex);
5597 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005598 continue;
5599 }
5600
Jeff Brown80fd47c2011-05-24 01:07:44 -07005601 if (outCount >= MAX_POINTERS) {
5602#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005603 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005604 "ignoring the rest.",
5605 getDeviceName().string(), MAX_POINTERS);
5606#endif
5607 break; // too many fingers!
5608 }
5609
Jeff Brownbe1aa822011-07-27 16:04:54 -07005610 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005611 outPointer.x = inSlot->getX();
5612 outPointer.y = inSlot->getY();
5613 outPointer.pressure = inSlot->getPressure();
5614 outPointer.touchMajor = inSlot->getTouchMajor();
5615 outPointer.touchMinor = inSlot->getTouchMinor();
5616 outPointer.toolMajor = inSlot->getToolMajor();
5617 outPointer.toolMinor = inSlot->getToolMinor();
5618 outPointer.orientation = inSlot->getOrientation();
5619 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005620 outPointer.tiltX = 0;
5621 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005622
Jeff Brown49754db2011-07-01 17:37:58 -07005623 outPointer.toolType = inSlot->getToolType();
5624 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5625 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5626 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5627 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5628 }
Jeff Brown8d608662010-08-30 03:02:23 -07005629 }
5630
Jeff Brown65fd2512011-08-18 11:20:58 -07005631 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5632 && (mTouchButtonAccumulator.isHovering()
5633 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005634 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005635
Jeff Brown8d608662010-08-30 03:02:23 -07005636 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005637 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005638 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005639 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005640 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005641 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005642 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005643 if (mPointerTrackingIdMap[n] == trackingId) {
5644 id = n;
5645 }
5646 }
5647
5648 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005649 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005650 mPointerTrackingIdMap[id] = trackingId;
5651 }
5652 }
5653 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005654 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005655 mCurrentRawPointerData.clearIdBits();
5656 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005657 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005658 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005659 mCurrentRawPointerData.idToIndex[id] = outCount;
5660 mCurrentRawPointerData.markIdBit(id, isHovering);
5661 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005662 }
5663 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005664
Jeff Brown6d0fec22010-07-23 21:28:06 -07005665 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005666 }
5667
Jeff Brownbe1aa822011-07-27 16:04:54 -07005668 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005669 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005670
Jeff Brown65fd2512011-08-18 11:20:58 -07005671 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005672}
5673
Jeff Brownbe1aa822011-07-27 16:04:54 -07005674void MultiTouchInputMapper::configureRawPointerAxes() {
5675 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005676
Jeff Brownbe1aa822011-07-27 16:04:54 -07005677 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5678 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5679 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5680 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5681 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5682 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5683 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5684 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5685 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5686 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5687 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005688
Jeff Brownbe1aa822011-07-27 16:04:54 -07005689 if (mRawPointerAxes.trackingId.valid
5690 && mRawPointerAxes.slot.valid
5691 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5692 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005693 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005694 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005695 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005696 getDeviceName().string(), slotCount, MAX_SLOTS);
5697 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005698 }
Jeff Brown49754db2011-07-01 17:37:58 -07005699 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005700 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005701 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005702 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005703}
5704
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005705
Jeff Browncb1404e2011-01-15 18:14:15 -08005706// --- JoystickInputMapper ---
5707
5708JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5709 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005710}
5711
5712JoystickInputMapper::~JoystickInputMapper() {
5713}
5714
5715uint32_t JoystickInputMapper::getSources() {
5716 return AINPUT_SOURCE_JOYSTICK;
5717}
5718
5719void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5720 InputMapper::populateDeviceInfo(info);
5721
Jeff Brown6f2fba42011-02-19 01:08:02 -08005722 for (size_t i = 0; i < mAxes.size(); i++) {
5723 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005724 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5725 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005726 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005727 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5728 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005729 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005730 }
5731}
5732
5733void JoystickInputMapper::dump(String8& dump) {
5734 dump.append(INDENT2 "Joystick Input Mapper:\n");
5735
Jeff Brown6f2fba42011-02-19 01:08:02 -08005736 dump.append(INDENT3 "Axes:\n");
5737 size_t numAxes = mAxes.size();
5738 for (size_t i = 0; i < numAxes; i++) {
5739 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005740 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005741 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005742 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005743 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005744 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005745 }
Jeff Brown85297452011-03-04 13:07:49 -08005746 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5747 label = getAxisLabel(axis.axisInfo.highAxis);
5748 if (label) {
5749 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5750 } else {
5751 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5752 axis.axisInfo.splitValue);
5753 }
5754 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5755 dump.append(" (invert)");
5756 }
5757
5758 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5759 axis.min, axis.max, axis.flat, axis.fuzz);
5760 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5761 "highScale=%0.5f, highOffset=%0.5f\n",
5762 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005763 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5764 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005765 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005766 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005767 }
5768}
5769
Jeff Brown65fd2512011-08-18 11:20:58 -07005770void JoystickInputMapper::configure(nsecs_t when,
5771 const InputReaderConfiguration* config, uint32_t changes) {
5772 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005773
Jeff Brown474dcb52011-06-14 20:22:50 -07005774 if (!changes) { // first time only
5775 // Collect all axes.
5776 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07005777 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5778 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5779 continue; // axis must be claimed by a different device
5780 }
5781
Jeff Brown474dcb52011-06-14 20:22:50 -07005782 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005783 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005784 if (rawAxisInfo.valid) {
5785 // Map axis.
5786 AxisInfo axisInfo;
5787 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5788 if (!explicitlyMapped) {
5789 // Axis is not explicitly mapped, will choose a generic axis later.
5790 axisInfo.mode = AxisInfo::MODE_NORMAL;
5791 axisInfo.axis = -1;
5792 }
5793
5794 // Apply flat override.
5795 int32_t rawFlat = axisInfo.flatOverride < 0
5796 ? rawAxisInfo.flat : axisInfo.flatOverride;
5797
5798 // Calculate scaling factors and limits.
5799 Axis axis;
5800 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5801 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5802 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5803 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5804 scale, 0.0f, highScale, 0.0f,
5805 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5806 } else if (isCenteredAxis(axisInfo.axis)) {
5807 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5808 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5809 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5810 scale, offset, scale, offset,
5811 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5812 } else {
5813 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5814 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5815 scale, 0.0f, scale, 0.0f,
5816 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5817 }
5818
5819 // To eliminate noise while the joystick is at rest, filter out small variations
5820 // in axis values up front.
5821 axis.filter = axis.flat * 0.25f;
5822
5823 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005824 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005825 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005826
Jeff Brown474dcb52011-06-14 20:22:50 -07005827 // If there are too many axes, start dropping them.
5828 // Prefer to keep explicitly mapped axes.
5829 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00005830 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07005831 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5832 pruneAxes(true);
5833 pruneAxes(false);
5834 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005835
Jeff Brown474dcb52011-06-14 20:22:50 -07005836 // Assign generic axis ids to remaining axes.
5837 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5838 size_t numAxes = mAxes.size();
5839 for (size_t i = 0; i < numAxes; i++) {
5840 Axis& axis = mAxes.editValueAt(i);
5841 if (axis.axisInfo.axis < 0) {
5842 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5843 && haveAxis(nextGenericAxisId)) {
5844 nextGenericAxisId += 1;
5845 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005846
Jeff Brown474dcb52011-06-14 20:22:50 -07005847 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5848 axis.axisInfo.axis = nextGenericAxisId;
5849 nextGenericAxisId += 1;
5850 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00005851 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07005852 "have already been assigned to other axes.",
5853 getDeviceName().string(), mAxes.keyAt(i));
5854 mAxes.removeItemsAt(i--);
5855 numAxes -= 1;
5856 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005857 }
5858 }
5859 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005860}
5861
Jeff Brown85297452011-03-04 13:07:49 -08005862bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005863 size_t numAxes = mAxes.size();
5864 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005865 const Axis& axis = mAxes.valueAt(i);
5866 if (axis.axisInfo.axis == axisId
5867 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5868 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005869 return true;
5870 }
5871 }
5872 return false;
5873}
Jeff Browncb1404e2011-01-15 18:14:15 -08005874
Jeff Brown6f2fba42011-02-19 01:08:02 -08005875void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5876 size_t i = mAxes.size();
5877 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5878 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5879 continue;
5880 }
Steve Block6215d3f2012-01-04 20:05:49 +00005881 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005882 getDeviceName().string(), mAxes.keyAt(i));
5883 mAxes.removeItemsAt(i);
5884 }
5885}
5886
5887bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5888 switch (axis) {
5889 case AMOTION_EVENT_AXIS_X:
5890 case AMOTION_EVENT_AXIS_Y:
5891 case AMOTION_EVENT_AXIS_Z:
5892 case AMOTION_EVENT_AXIS_RX:
5893 case AMOTION_EVENT_AXIS_RY:
5894 case AMOTION_EVENT_AXIS_RZ:
5895 case AMOTION_EVENT_AXIS_HAT_X:
5896 case AMOTION_EVENT_AXIS_HAT_Y:
5897 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005898 case AMOTION_EVENT_AXIS_RUDDER:
5899 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005900 return true;
5901 default:
5902 return false;
5903 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005904}
5905
Jeff Brown65fd2512011-08-18 11:20:58 -07005906void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005907 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005908 size_t numAxes = mAxes.size();
5909 for (size_t i = 0; i < numAxes; i++) {
5910 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005911 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005912 }
5913
Jeff Brown65fd2512011-08-18 11:20:58 -07005914 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005915}
5916
5917void JoystickInputMapper::process(const RawEvent* rawEvent) {
5918 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005919 case EV_ABS: {
5920 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5921 if (index >= 0) {
5922 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005923 float newValue, highNewValue;
5924 switch (axis.axisInfo.mode) {
5925 case AxisInfo::MODE_INVERT:
5926 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5927 * axis.scale + axis.offset;
5928 highNewValue = 0.0f;
5929 break;
5930 case AxisInfo::MODE_SPLIT:
5931 if (rawEvent->value < axis.axisInfo.splitValue) {
5932 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5933 * axis.scale + axis.offset;
5934 highNewValue = 0.0f;
5935 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5936 newValue = 0.0f;
5937 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5938 * axis.highScale + axis.highOffset;
5939 } else {
5940 newValue = 0.0f;
5941 highNewValue = 0.0f;
5942 }
5943 break;
5944 default:
5945 newValue = rawEvent->value * axis.scale + axis.offset;
5946 highNewValue = 0.0f;
5947 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005948 }
Jeff Brown85297452011-03-04 13:07:49 -08005949 axis.newValue = newValue;
5950 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005951 }
5952 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005953 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005954
5955 case EV_SYN:
5956 switch (rawEvent->scanCode) {
5957 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005958 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005959 break;
5960 }
5961 break;
5962 }
5963}
5964
Jeff Brown6f2fba42011-02-19 01:08:02 -08005965void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005966 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005967 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005968 }
5969
5970 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005971 int32_t buttonState = 0;
5972
5973 PointerProperties pointerProperties;
5974 pointerProperties.clear();
5975 pointerProperties.id = 0;
5976 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005977
Jeff Brown6f2fba42011-02-19 01:08:02 -08005978 PointerCoords pointerCoords;
5979 pointerCoords.clear();
5980
5981 size_t numAxes = mAxes.size();
5982 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005983 const Axis& axis = mAxes.valueAt(i);
5984 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5985 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5986 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5987 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005988 }
5989
Jeff Brown56194eb2011-03-02 19:23:13 -08005990 // Moving a joystick axis should not wake the devide because joysticks can
5991 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5992 // button will likely wake the device.
5993 // TODO: Use the input device configuration to control this behavior more finely.
5994 uint32_t policyFlags = 0;
5995
Jeff Brownbe1aa822011-07-27 16:04:54 -07005996 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005997 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5998 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005999 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006000}
6001
Jeff Brown85297452011-03-04 13:07:49 -08006002bool JoystickInputMapper::filterAxes(bool force) {
6003 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006004 size_t numAxes = mAxes.size();
6005 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006006 Axis& axis = mAxes.editValueAt(i);
6007 if (force || hasValueChangedSignificantly(axis.filter,
6008 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6009 axis.currentValue = axis.newValue;
6010 atLeastOneSignificantChange = true;
6011 }
6012 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6013 if (force || hasValueChangedSignificantly(axis.filter,
6014 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6015 axis.highCurrentValue = axis.highNewValue;
6016 atLeastOneSignificantChange = true;
6017 }
6018 }
6019 }
6020 return atLeastOneSignificantChange;
6021}
6022
6023bool JoystickInputMapper::hasValueChangedSignificantly(
6024 float filter, float newValue, float currentValue, float min, float max) {
6025 if (newValue != currentValue) {
6026 // Filter out small changes in value unless the value is converging on the axis
6027 // bounds or center point. This is intended to reduce the amount of information
6028 // sent to applications by particularly noisy joysticks (such as PS3).
6029 if (fabs(newValue - currentValue) > filter
6030 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6031 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6032 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6033 return true;
6034 }
6035 }
6036 return false;
6037}
6038
6039bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6040 float filter, float newValue, float currentValue, float thresholdValue) {
6041 float newDistance = fabs(newValue - thresholdValue);
6042 if (newDistance < filter) {
6043 float oldDistance = fabs(currentValue - thresholdValue);
6044 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006045 return true;
6046 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006047 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006048 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006049}
6050
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006051} // namespace android