blob: 382987b33238a85848154cda3d9ad69577b68dd3 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brownfd035822010-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 Brown46b9ac02010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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);
281
282 if (count) {
283 processEventsLocked(mEventBuffer, count);
284 }
285 if (!count || timeoutMillis == 0) {
286 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700287#if DEBUG_RAW_EVENTS
Jeff Brownbe1aa822011-07-27 16:04:54 -0700288 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700289#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700290 mNextTimeout = LLONG_MAX;
291 timeoutExpiredLocked(now);
292 }
293 } // release lock
294
295 // Flush queued events out to the listener.
296 // This must happen outside of the lock because the listener could potentially call
297 // back into the InputReader's methods, such as getScanCodeState, or become blocked
298 // on another thread similarly waiting to acquire the InputReader lock thereby
299 // resulting in a deadlock. This situation is actually quite plausible because the
300 // listener is actually the input dispatcher, which calls into the window manager,
301 // which occasionally calls into the input reader.
302 mQueuedListener->flush();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700303}
304
Jeff Brownbe1aa822011-07-27 16:04:54 -0700305void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700306 for (const RawEvent* rawEvent = rawEvents; count;) {
307 int32_t type = rawEvent->type;
308 size_t batchSize = 1;
309 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
310 int32_t deviceId = rawEvent->deviceId;
311 while (batchSize < count) {
312 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
313 || rawEvent[batchSize].deviceId != deviceId) {
314 break;
315 }
316 batchSize += 1;
317 }
318#if DEBUG_RAW_EVENTS
319 LOGD("BatchSize: %d Count: %d", batchSize, count);
320#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700321 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700322 } else {
323 switch (rawEvent->type) {
324 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700325 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700326 break;
327 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700329 break;
330 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700331 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700332 break;
333 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700334 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700335 break;
336 }
337 }
338 count -= batchSize;
339 rawEvent += batchSize;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700340 }
341}
342
Jeff Brown65fd2512011-08-18 11:20:58 -0700343void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344 String8 name = mEventHub->getDeviceName(deviceId);
345 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
346
Jeff Brownbe1aa822011-07-27 16:04:54 -0700347 InputDevice* device = createDeviceLocked(deviceId, name, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700348 device->configure(when, &mConfig, 0);
349 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350
Jeff Brown8d608662010-08-30 03:02:23 -0700351 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800352 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700353 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800354 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700355 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700356 }
357
Jeff Brownbe1aa822011-07-27 16:04:54 -0700358 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
359 if (deviceIndex < 0) {
360 mDevices.add(deviceId, device);
361 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
363 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700364 return;
365 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700366}
367
Jeff Brown65fd2512011-08-18 11:20:58 -0700368void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
371 if (deviceIndex >= 0) {
372 device = mDevices.valueAt(deviceIndex);
373 mDevices.removeItemsAt(deviceIndex, 1);
374 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700376 return;
377 }
378
Jeff Brown6d0fec22010-07-23 21:28:06 -0700379 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800380 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381 device->getId(), device->getName().string());
382 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800383 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384 device->getId(), device->getName().string(), device->getSources());
385 }
386
Jeff Brown65fd2512011-08-18 11:20:58 -0700387 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700389}
390
Jeff Brownbe1aa822011-07-27 16:04:54 -0700391InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
392 const String8& name, uint32_t classes) {
Jeff Brown9ee285a2011-08-31 12:56:34 -0700393 InputDevice* device = new InputDevice(&mContext, deviceId, name, classes);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700394
Jeff Brown56194eb2011-03-02 19:23:13 -0800395 // External devices.
396 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
397 device->setExternal(true);
398 }
399
Jeff Brown6d0fec22010-07-23 21:28:06 -0700400 // Switch-like devices.
401 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
402 device->addMapper(new SwitchInputMapper(device));
403 }
404
405 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800406 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
408 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800409 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 }
411 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
412 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
413 }
414 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800415 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700416 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800417 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800418 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800419 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420
Jeff Brownefd32662011-03-08 15:13:06 -0800421 if (keyboardSource != 0) {
422 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 }
424
Jeff Brown83c09682010-12-23 17:50:18 -0800425 // Cursor-like devices.
426 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
427 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700428 }
429
Jeff Brown58a2da82011-01-25 16:02:22 -0800430 // Touchscreens and touchpad devices.
431 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800432 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800433 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800434 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 }
436
Jeff Browncb1404e2011-01-15 18:14:15 -0800437 // Joystick-like devices.
438 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
439 device->addMapper(new JoystickInputMapper(device));
440 }
441
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 return device;
443}
444
Jeff Brownbe1aa822011-07-27 16:04:54 -0700445void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700446 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700447 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
448 if (deviceIndex < 0) {
449 LOGW("Discarding event for unknown deviceId %d.", deviceId);
450 return;
451 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452
Jeff Brownbe1aa822011-07-27 16:04:54 -0700453 InputDevice* device = mDevices.valueAt(deviceIndex);
454 if (device->isIgnored()) {
455 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
456 return;
457 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700458
Jeff Brownbe1aa822011-07-27 16:04:54 -0700459 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460}
461
Jeff Brownbe1aa822011-07-27 16:04:54 -0700462void InputReader::timeoutExpiredLocked(nsecs_t when) {
463 for (size_t i = 0; i < mDevices.size(); i++) {
464 InputDevice* device = mDevices.valueAt(i);
465 if (!device->isIgnored()) {
466 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700467 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700468 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700469}
470
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700473 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700474
475 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700476 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477
478 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700479 NotifyConfigurationChangedArgs args(when);
480 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481}
482
Jeff Brownbe1aa822011-07-27 16:04:54 -0700483void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700484 mPolicy->getReaderConfiguration(&mConfig);
485 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
486
Jeff Brown474dcb52011-06-14 20:22:50 -0700487 if (changes) {
488 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700489 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700490
491 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
492 mEventHub->requestReopenDevices();
493 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700494 for (size_t i = 0; i < mDevices.size(); i++) {
495 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700496 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700497 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700498 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700499 }
500}
501
Jeff Brownbe1aa822011-07-27 16:04:54 -0700502void InputReader::updateGlobalMetaStateLocked() {
503 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504
Jeff Brownbe1aa822011-07-27 16:04:54 -0700505 for (size_t i = 0; i < mDevices.size(); i++) {
506 InputDevice* device = mDevices.valueAt(i);
507 mGlobalMetaState |= device->getMetaState();
508 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509}
510
Jeff Brownbe1aa822011-07-27 16:04:54 -0700511int32_t InputReader::getGlobalMetaStateLocked() {
512 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700513}
514
Jeff Brownbe1aa822011-07-27 16:04:54 -0700515void InputReader::updateInputConfigurationLocked() {
516 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
517 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
518 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
519 InputDeviceInfo deviceInfo;
520 for (size_t i = 0; i < mDevices.size(); i++) {
521 InputDevice* device = mDevices.valueAt(i);
522 device->getDeviceInfo(& deviceInfo);
523 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700524
Jeff Brownbe1aa822011-07-27 16:04:54 -0700525 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
526 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
527 }
528 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
529 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
530 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
531 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
532 }
533 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
534 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
535 }
536 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700537
Jeff Brownbe1aa822011-07-27 16:04:54 -0700538 mInputConfiguration.touchScreen = touchScreenConfig;
539 mInputConfiguration.keyboard = keyboardConfig;
540 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700541}
542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800544 mDisableVirtualKeysTimeout = time;
545}
546
Jeff Brownbe1aa822011-07-27 16:04:54 -0700547bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800548 InputDevice* device, int32_t keyCode, int32_t scanCode) {
549 if (now < mDisableVirtualKeysTimeout) {
550 LOGI("Dropping virtual key from device %s because virtual keys are "
551 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
552 device->getName().string(),
553 (mDisableVirtualKeysTimeout - now) * 0.000001,
554 keyCode, scanCode);
555 return true;
556 } else {
557 return false;
558 }
559}
560
Jeff Brownbe1aa822011-07-27 16:04:54 -0700561void InputReader::fadePointerLocked() {
562 for (size_t i = 0; i < mDevices.size(); i++) {
563 InputDevice* device = mDevices.valueAt(i);
564 device->fadePointer();
565 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800566}
567
Jeff Brownbe1aa822011-07-27 16:04:54 -0700568void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700569 if (when < mNextTimeout) {
570 mNextTimeout = when;
571 }
572}
573
Jeff Brown6d0fec22010-07-23 21:28:06 -0700574void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700575 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700576
Jeff Brownbe1aa822011-07-27 16:04:54 -0700577 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578}
579
580status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582
Jeff Brownbe1aa822011-07-27 16:04:54 -0700583 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
584 if (deviceIndex < 0) {
585 return NAME_NOT_FOUND;
586 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700587
Jeff Brownbe1aa822011-07-27 16:04:54 -0700588 InputDevice* device = mDevices.valueAt(deviceIndex);
589 if (device->isIgnored()) {
590 return NAME_NOT_FOUND;
591 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700592
Jeff Brownbe1aa822011-07-27 16:04:54 -0700593 device->getDeviceInfo(outDeviceInfo);
594 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595}
596
597void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700598 AutoMutex _l(mLock);
599
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 outDeviceIds.clear();
601
Jeff Brownbe1aa822011-07-27 16:04:54 -0700602 size_t numDevices = mDevices.size();
603 for (size_t i = 0; i < numDevices; i++) {
604 InputDevice* device = mDevices.valueAt(i);
605 if (!device->isIgnored()) {
606 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700607 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700608 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700609}
610
611int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
612 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700613 AutoMutex _l(mLock);
614
615 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616}
617
618int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
619 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700620 AutoMutex _l(mLock);
621
622 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700623}
624
625int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700626 AutoMutex _l(mLock);
627
628 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629}
630
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700632 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700633 int32_t result = AKEY_STATE_UNKNOWN;
634 if (deviceId >= 0) {
635 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
636 if (deviceIndex >= 0) {
637 InputDevice* device = mDevices.valueAt(deviceIndex);
638 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
639 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700640 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700641 }
642 } else {
643 size_t numDevices = mDevices.size();
644 for (size_t i = 0; i < numDevices; i++) {
645 InputDevice* device = mDevices.valueAt(i);
646 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800647 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
648 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
649 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
650 if (currentResult >= AKEY_STATE_DOWN) {
651 return currentResult;
652 } else if (currentResult == AKEY_STATE_UP) {
653 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700654 }
655 }
656 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700657 }
658 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700659}
660
661bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
662 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700663 AutoMutex _l(mLock);
664
Jeff Brown6d0fec22010-07-23 21:28:06 -0700665 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700666 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700667}
668
Jeff Brownbe1aa822011-07-27 16:04:54 -0700669bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
670 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
671 bool result = false;
672 if (deviceId >= 0) {
673 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
674 if (deviceIndex >= 0) {
675 InputDevice* device = mDevices.valueAt(deviceIndex);
676 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
677 result = device->markSupportedKeyCodes(sourceMask,
678 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700679 }
680 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700681 } else {
682 size_t numDevices = mDevices.size();
683 for (size_t i = 0; i < numDevices; i++) {
684 InputDevice* device = mDevices.valueAt(i);
685 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
686 result |= device->markSupportedKeyCodes(sourceMask,
687 numCodes, keyCodes, outFlags);
688 }
689 }
690 }
691 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700692}
693
Jeff Brown474dcb52011-06-14 20:22:50 -0700694void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700695 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700696
Jeff Brownbe1aa822011-07-27 16:04:54 -0700697 if (changes) {
698 bool needWake = !mConfigurationChangesToRefresh;
699 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700700
701 if (needWake) {
702 mEventHub->wake();
703 }
704 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700705}
706
Jeff Brownb88102f2010-09-08 11:49:43 -0700707void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700708 AutoMutex _l(mLock);
709
Jeff Brownf2f48712010-10-01 17:46:21 -0700710 mEventHub->dump(dump);
711 dump.append("\n");
712
713 dump.append("Input Reader State:\n");
714
Jeff Brownbe1aa822011-07-27 16:04:54 -0700715 for (size_t i = 0; i < mDevices.size(); i++) {
716 mDevices.valueAt(i)->dump(dump);
717 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700718
719 dump.append(INDENT "Configuration:\n");
720 dump.append(INDENT2 "ExcludedDeviceNames: [");
721 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
722 if (i != 0) {
723 dump.append(", ");
724 }
725 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
726 }
727 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700728 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
729 mConfig.virtualKeyQuietTime * 0.000001f);
730
Jeff Brown19c97d462011-06-01 12:33:19 -0700731 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
732 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
733 mConfig.pointerVelocityControlParameters.scale,
734 mConfig.pointerVelocityControlParameters.lowThreshold,
735 mConfig.pointerVelocityControlParameters.highThreshold,
736 mConfig.pointerVelocityControlParameters.acceleration);
737
738 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
739 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
740 mConfig.wheelVelocityControlParameters.scale,
741 mConfig.wheelVelocityControlParameters.lowThreshold,
742 mConfig.wheelVelocityControlParameters.highThreshold,
743 mConfig.wheelVelocityControlParameters.acceleration);
744
Jeff Brown214eaf42011-05-26 19:17:02 -0700745 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700746 dump.appendFormat(INDENT3 "Enabled: %s\n",
747 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700748 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
749 mConfig.pointerGestureQuietInterval * 0.000001f);
750 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
751 mConfig.pointerGestureDragMinSwitchSpeed);
752 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
753 mConfig.pointerGestureTapInterval * 0.000001f);
754 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
755 mConfig.pointerGestureTapDragInterval * 0.000001f);
756 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
757 mConfig.pointerGestureTapSlop);
758 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
759 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700760 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
761 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700762 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
763 mConfig.pointerGestureSwipeTransitionAngleCosine);
764 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
765 mConfig.pointerGestureSwipeMaxWidthRatio);
766 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
767 mConfig.pointerGestureMovementSpeedRatio);
768 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
769 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700770}
771
Jeff Brown89ef0722011-08-10 16:25:21 -0700772void InputReader::monitor() {
773 // Acquire and release the lock to ensure that the reader has not deadlocked.
774 mLock.lock();
775 mLock.unlock();
776
777 // Check the EventHub
778 mEventHub->monitor();
779}
780
Jeff Brown6d0fec22010-07-23 21:28:06 -0700781
Jeff Brownbe1aa822011-07-27 16:04:54 -0700782// --- InputReader::ContextImpl ---
783
784InputReader::ContextImpl::ContextImpl(InputReader* reader) :
785 mReader(reader) {
786}
787
788void InputReader::ContextImpl::updateGlobalMetaState() {
789 // lock is already held by the input loop
790 mReader->updateGlobalMetaStateLocked();
791}
792
793int32_t InputReader::ContextImpl::getGlobalMetaState() {
794 // lock is already held by the input loop
795 return mReader->getGlobalMetaStateLocked();
796}
797
798void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
799 // lock is already held by the input loop
800 mReader->disableVirtualKeysUntilLocked(time);
801}
802
803bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
804 InputDevice* device, int32_t keyCode, int32_t scanCode) {
805 // lock is already held by the input loop
806 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
807}
808
809void InputReader::ContextImpl::fadePointer() {
810 // lock is already held by the input loop
811 mReader->fadePointerLocked();
812}
813
814void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
815 // lock is already held by the input loop
816 mReader->requestTimeoutAtTimeLocked(when);
817}
818
819InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
820 return mReader->mPolicy.get();
821}
822
823InputListenerInterface* InputReader::ContextImpl::getListener() {
824 return mReader->mQueuedListener.get();
825}
826
827EventHubInterface* InputReader::ContextImpl::getEventHub() {
828 return mReader->mEventHub.get();
829}
830
831
Jeff Brown6d0fec22010-07-23 21:28:06 -0700832// --- InputReaderThread ---
833
834InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
835 Thread(/*canCallJava*/ true), mReader(reader) {
836}
837
838InputReaderThread::~InputReaderThread() {
839}
840
841bool InputReaderThread::threadLoop() {
842 mReader->loopOnce();
843 return true;
844}
845
846
847// --- InputDevice ---
848
Jeff Brown9ee285a2011-08-31 12:56:34 -0700849InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name,
850 uint32_t classes) :
851 mContext(context), mId(id), mName(name), mClasses(classes),
852 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700853}
854
855InputDevice::~InputDevice() {
856 size_t numMappers = mMappers.size();
857 for (size_t i = 0; i < numMappers; i++) {
858 delete mMappers[i];
859 }
860 mMappers.clear();
861}
862
Jeff Brownef3d7e82010-09-30 14:33:04 -0700863void InputDevice::dump(String8& dump) {
864 InputDeviceInfo deviceInfo;
865 getDeviceInfo(& deviceInfo);
866
Jeff Brown90655042010-12-02 13:50:46 -0800867 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700868 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800869 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700870 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
871 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800872
Jeff Brownefd32662011-03-08 15:13:06 -0800873 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800874 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700875 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800876 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800877 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
878 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800879 char name[32];
880 if (label) {
881 strncpy(name, label, sizeof(name));
882 name[sizeof(name) - 1] = '\0';
883 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800884 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800885 }
Jeff Brownefd32662011-03-08 15:13:06 -0800886 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
887 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
888 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800889 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700890 }
891
892 size_t numMappers = mMappers.size();
893 for (size_t i = 0; i < numMappers; i++) {
894 InputMapper* mapper = mMappers[i];
895 mapper->dump(dump);
896 }
897}
898
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899void InputDevice::addMapper(InputMapper* mapper) {
900 mMappers.add(mapper);
901}
902
Jeff Brown65fd2512011-08-18 11:20:58 -0700903void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700904 mSources = 0;
905
Jeff Brown474dcb52011-06-14 20:22:50 -0700906 if (!isIgnored()) {
907 if (!changes) { // first time only
908 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
909 }
910
911 size_t numMappers = mMappers.size();
912 for (size_t i = 0; i < numMappers; i++) {
913 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700914 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700915 mSources |= mapper->getSources();
916 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700917 }
918}
919
Jeff Brown65fd2512011-08-18 11:20:58 -0700920void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700921 size_t numMappers = mMappers.size();
922 for (size_t i = 0; i < numMappers; i++) {
923 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700924 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700926
927 mContext->updateGlobalMetaState();
928
929 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700930}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700931
Jeff Brownb7198742011-03-18 18:14:26 -0700932void InputDevice::process(const RawEvent* rawEvents, size_t count) {
933 // Process all of the events in order for each mapper.
934 // We cannot simply ask each mapper to process them in bulk because mappers may
935 // have side-effects that must be interleaved. For example, joystick movement events and
936 // gamepad button presses are handled by different mappers but they should be dispatched
937 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700938 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700939 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
940#if DEBUG_RAW_EVENTS
941 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700942 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700943 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
944 rawEvent->value, rawEvent->flags);
945#endif
946
Jeff Brown80fd47c2011-05-24 01:07:44 -0700947 if (mDropUntilNextSync) {
948 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
949 mDropUntilNextSync = false;
950#if DEBUG_RAW_EVENTS
951 LOGD("Recovered from input event buffer overrun.");
952#endif
953 } else {
954#if DEBUG_RAW_EVENTS
955 LOGD("Dropped input event while waiting for next input sync.");
956#endif
957 }
958 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
959 LOGI("Detected input event buffer overrun for device %s.", mName.string());
960 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700961 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700962 } else {
963 for (size_t i = 0; i < numMappers; i++) {
964 InputMapper* mapper = mMappers[i];
965 mapper->process(rawEvent);
966 }
Jeff Brownb7198742011-03-18 18:14:26 -0700967 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700968 }
969}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700970
Jeff Brownaa3855d2011-03-17 01:34:19 -0700971void InputDevice::timeoutExpired(nsecs_t when) {
972 size_t numMappers = mMappers.size();
973 for (size_t i = 0; i < numMappers; i++) {
974 InputMapper* mapper = mMappers[i];
975 mapper->timeoutExpired(when);
976 }
977}
978
Jeff Brown6d0fec22010-07-23 21:28:06 -0700979void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
980 outDeviceInfo->initialize(mId, mName);
981
982 size_t numMappers = mMappers.size();
983 for (size_t i = 0; i < numMappers; i++) {
984 InputMapper* mapper = mMappers[i];
985 mapper->populateDeviceInfo(outDeviceInfo);
986 }
987}
988
989int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
990 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
991}
992
993int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
994 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
995}
996
997int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
998 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
999}
1000
1001int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1002 int32_t result = AKEY_STATE_UNKNOWN;
1003 size_t numMappers = mMappers.size();
1004 for (size_t i = 0; i < numMappers; i++) {
1005 InputMapper* mapper = mMappers[i];
1006 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001007 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1008 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1009 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1010 if (currentResult >= AKEY_STATE_DOWN) {
1011 return currentResult;
1012 } else if (currentResult == AKEY_STATE_UP) {
1013 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001014 }
1015 }
1016 }
1017 return result;
1018}
1019
1020bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1021 const int32_t* keyCodes, uint8_t* outFlags) {
1022 bool result = false;
1023 size_t numMappers = mMappers.size();
1024 for (size_t i = 0; i < numMappers; i++) {
1025 InputMapper* mapper = mMappers[i];
1026 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1027 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1028 }
1029 }
1030 return result;
1031}
1032
1033int32_t InputDevice::getMetaState() {
1034 int32_t result = 0;
1035 size_t numMappers = mMappers.size();
1036 for (size_t i = 0; i < numMappers; i++) {
1037 InputMapper* mapper = mMappers[i];
1038 result |= mapper->getMetaState();
1039 }
1040 return result;
1041}
1042
Jeff Brown05dc66a2011-03-02 14:41:58 -08001043void InputDevice::fadePointer() {
1044 size_t numMappers = mMappers.size();
1045 for (size_t i = 0; i < numMappers; i++) {
1046 InputMapper* mapper = mMappers[i];
1047 mapper->fadePointer();
1048 }
1049}
1050
Jeff Brown65fd2512011-08-18 11:20:58 -07001051void InputDevice::notifyReset(nsecs_t when) {
1052 NotifyDeviceResetArgs args(when, mId);
1053 mContext->getListener()->notifyDeviceReset(&args);
1054}
1055
Jeff Brown6d0fec22010-07-23 21:28:06 -07001056
Jeff Brown49754db2011-07-01 17:37:58 -07001057// --- CursorButtonAccumulator ---
1058
1059CursorButtonAccumulator::CursorButtonAccumulator() {
1060 clearButtons();
1061}
1062
Jeff Brown65fd2512011-08-18 11:20:58 -07001063void CursorButtonAccumulator::reset(InputDevice* device) {
1064 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1065 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1066 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1067 mBtnBack = device->isKeyPressed(BTN_BACK);
1068 mBtnSide = device->isKeyPressed(BTN_SIDE);
1069 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1070 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1071 mBtnTask = device->isKeyPressed(BTN_TASK);
1072}
1073
Jeff Brown49754db2011-07-01 17:37:58 -07001074void CursorButtonAccumulator::clearButtons() {
1075 mBtnLeft = 0;
1076 mBtnRight = 0;
1077 mBtnMiddle = 0;
1078 mBtnBack = 0;
1079 mBtnSide = 0;
1080 mBtnForward = 0;
1081 mBtnExtra = 0;
1082 mBtnTask = 0;
1083}
1084
1085void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1086 if (rawEvent->type == EV_KEY) {
1087 switch (rawEvent->scanCode) {
1088 case BTN_LEFT:
1089 mBtnLeft = rawEvent->value;
1090 break;
1091 case BTN_RIGHT:
1092 mBtnRight = rawEvent->value;
1093 break;
1094 case BTN_MIDDLE:
1095 mBtnMiddle = rawEvent->value;
1096 break;
1097 case BTN_BACK:
1098 mBtnBack = rawEvent->value;
1099 break;
1100 case BTN_SIDE:
1101 mBtnSide = rawEvent->value;
1102 break;
1103 case BTN_FORWARD:
1104 mBtnForward = rawEvent->value;
1105 break;
1106 case BTN_EXTRA:
1107 mBtnExtra = rawEvent->value;
1108 break;
1109 case BTN_TASK:
1110 mBtnTask = rawEvent->value;
1111 break;
1112 }
1113 }
1114}
1115
1116uint32_t CursorButtonAccumulator::getButtonState() const {
1117 uint32_t result = 0;
1118 if (mBtnLeft) {
1119 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1120 }
1121 if (mBtnRight) {
1122 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1123 }
1124 if (mBtnMiddle) {
1125 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1126 }
1127 if (mBtnBack || mBtnSide) {
1128 result |= AMOTION_EVENT_BUTTON_BACK;
1129 }
1130 if (mBtnForward || mBtnExtra) {
1131 result |= AMOTION_EVENT_BUTTON_FORWARD;
1132 }
1133 return result;
1134}
1135
1136
1137// --- CursorMotionAccumulator ---
1138
Jeff Brown65fd2512011-08-18 11:20:58 -07001139CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001140 clearRelativeAxes();
1141}
1142
Jeff Brown65fd2512011-08-18 11:20:58 -07001143void CursorMotionAccumulator::reset(InputDevice* device) {
1144 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001145}
1146
1147void CursorMotionAccumulator::clearRelativeAxes() {
1148 mRelX = 0;
1149 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001150}
1151
1152void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1153 if (rawEvent->type == EV_REL) {
1154 switch (rawEvent->scanCode) {
1155 case REL_X:
1156 mRelX = rawEvent->value;
1157 break;
1158 case REL_Y:
1159 mRelY = rawEvent->value;
1160 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001161 }
1162 }
1163}
1164
1165void CursorMotionAccumulator::finishSync() {
1166 clearRelativeAxes();
1167}
1168
1169
1170// --- CursorScrollAccumulator ---
1171
1172CursorScrollAccumulator::CursorScrollAccumulator() :
1173 mHaveRelWheel(false), mHaveRelHWheel(false) {
1174 clearRelativeAxes();
1175}
1176
1177void CursorScrollAccumulator::configure(InputDevice* device) {
1178 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1179 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1180}
1181
1182void CursorScrollAccumulator::reset(InputDevice* device) {
1183 clearRelativeAxes();
1184}
1185
1186void CursorScrollAccumulator::clearRelativeAxes() {
1187 mRelWheel = 0;
1188 mRelHWheel = 0;
1189}
1190
1191void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1192 if (rawEvent->type == EV_REL) {
1193 switch (rawEvent->scanCode) {
Jeff Brown49754db2011-07-01 17:37:58 -07001194 case REL_WHEEL:
1195 mRelWheel = rawEvent->value;
1196 break;
1197 case REL_HWHEEL:
1198 mRelHWheel = rawEvent->value;
1199 break;
1200 }
1201 }
1202}
1203
Jeff Brown65fd2512011-08-18 11:20:58 -07001204void CursorScrollAccumulator::finishSync() {
1205 clearRelativeAxes();
1206}
1207
Jeff Brown49754db2011-07-01 17:37:58 -07001208
1209// --- TouchButtonAccumulator ---
1210
1211TouchButtonAccumulator::TouchButtonAccumulator() :
1212 mHaveBtnTouch(false) {
1213 clearButtons();
1214}
1215
1216void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001217 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1218}
1219
1220void TouchButtonAccumulator::reset(InputDevice* device) {
1221 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1222 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1223 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1224 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1225 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1226 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1227 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1228 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1229 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1230 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1231 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001232 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1233 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1234 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001235}
1236
1237void TouchButtonAccumulator::clearButtons() {
1238 mBtnTouch = 0;
1239 mBtnStylus = 0;
1240 mBtnStylus2 = 0;
1241 mBtnToolFinger = 0;
1242 mBtnToolPen = 0;
1243 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001244 mBtnToolBrush = 0;
1245 mBtnToolPencil = 0;
1246 mBtnToolAirbrush = 0;
1247 mBtnToolMouse = 0;
1248 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001249 mBtnToolDoubleTap = 0;
1250 mBtnToolTripleTap = 0;
1251 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001252}
1253
1254void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1255 if (rawEvent->type == EV_KEY) {
1256 switch (rawEvent->scanCode) {
1257 case BTN_TOUCH:
1258 mBtnTouch = rawEvent->value;
1259 break;
1260 case BTN_STYLUS:
1261 mBtnStylus = rawEvent->value;
1262 break;
1263 case BTN_STYLUS2:
1264 mBtnStylus2 = rawEvent->value;
1265 break;
1266 case BTN_TOOL_FINGER:
1267 mBtnToolFinger = rawEvent->value;
1268 break;
1269 case BTN_TOOL_PEN:
1270 mBtnToolPen = rawEvent->value;
1271 break;
1272 case BTN_TOOL_RUBBER:
1273 mBtnToolRubber = rawEvent->value;
1274 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001275 case BTN_TOOL_BRUSH:
1276 mBtnToolBrush = rawEvent->value;
1277 break;
1278 case BTN_TOOL_PENCIL:
1279 mBtnToolPencil = rawEvent->value;
1280 break;
1281 case BTN_TOOL_AIRBRUSH:
1282 mBtnToolAirbrush = rawEvent->value;
1283 break;
1284 case BTN_TOOL_MOUSE:
1285 mBtnToolMouse = rawEvent->value;
1286 break;
1287 case BTN_TOOL_LENS:
1288 mBtnToolLens = rawEvent->value;
1289 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001290 case BTN_TOOL_DOUBLETAP:
1291 mBtnToolDoubleTap = rawEvent->value;
1292 break;
1293 case BTN_TOOL_TRIPLETAP:
1294 mBtnToolTripleTap = rawEvent->value;
1295 break;
1296 case BTN_TOOL_QUADTAP:
1297 mBtnToolQuadTap = rawEvent->value;
1298 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001299 }
1300 }
1301}
1302
1303uint32_t TouchButtonAccumulator::getButtonState() const {
1304 uint32_t result = 0;
1305 if (mBtnStylus) {
1306 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1307 }
1308 if (mBtnStylus2) {
1309 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1310 }
1311 return result;
1312}
1313
1314int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001315 if (mBtnToolMouse || mBtnToolLens) {
1316 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1317 }
Jeff Brown49754db2011-07-01 17:37:58 -07001318 if (mBtnToolRubber) {
1319 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1320 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001321 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001322 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1323 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001324 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001325 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1326 }
1327 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1328}
1329
Jeff Brownd87c6d52011-08-10 14:55:59 -07001330bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001331 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1332 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001333 || mBtnToolMouse || mBtnToolLens
1334 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001335}
1336
1337bool TouchButtonAccumulator::isHovering() const {
1338 return mHaveBtnTouch && !mBtnTouch;
1339}
1340
1341
Jeff Brownbe1aa822011-07-27 16:04:54 -07001342// --- RawPointerAxes ---
1343
1344RawPointerAxes::RawPointerAxes() {
1345 clear();
1346}
1347
1348void RawPointerAxes::clear() {
1349 x.clear();
1350 y.clear();
1351 pressure.clear();
1352 touchMajor.clear();
1353 touchMinor.clear();
1354 toolMajor.clear();
1355 toolMinor.clear();
1356 orientation.clear();
1357 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001358 tiltX.clear();
1359 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001360 trackingId.clear();
1361 slot.clear();
1362}
1363
1364
1365// --- RawPointerData ---
1366
1367RawPointerData::RawPointerData() {
1368 clear();
1369}
1370
1371void RawPointerData::clear() {
1372 pointerCount = 0;
1373 clearIdBits();
1374}
1375
1376void RawPointerData::copyFrom(const RawPointerData& other) {
1377 pointerCount = other.pointerCount;
1378 hoveringIdBits = other.hoveringIdBits;
1379 touchingIdBits = other.touchingIdBits;
1380
1381 for (uint32_t i = 0; i < pointerCount; i++) {
1382 pointers[i] = other.pointers[i];
1383
1384 int id = pointers[i].id;
1385 idToIndex[id] = other.idToIndex[id];
1386 }
1387}
1388
1389void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1390 float x = 0, y = 0;
1391 uint32_t count = touchingIdBits.count();
1392 if (count) {
1393 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1394 uint32_t id = idBits.clearFirstMarkedBit();
1395 const Pointer& pointer = pointerForId(id);
1396 x += pointer.x;
1397 y += pointer.y;
1398 }
1399 x /= count;
1400 y /= count;
1401 }
1402 *outX = x;
1403 *outY = y;
1404}
1405
1406
1407// --- CookedPointerData ---
1408
1409CookedPointerData::CookedPointerData() {
1410 clear();
1411}
1412
1413void CookedPointerData::clear() {
1414 pointerCount = 0;
1415 hoveringIdBits.clear();
1416 touchingIdBits.clear();
1417}
1418
1419void CookedPointerData::copyFrom(const CookedPointerData& other) {
1420 pointerCount = other.pointerCount;
1421 hoveringIdBits = other.hoveringIdBits;
1422 touchingIdBits = other.touchingIdBits;
1423
1424 for (uint32_t i = 0; i < pointerCount; i++) {
1425 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1426 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1427
1428 int id = pointerProperties[i].id;
1429 idToIndex[id] = other.idToIndex[id];
1430 }
1431}
1432
1433
Jeff Brown49754db2011-07-01 17:37:58 -07001434// --- SingleTouchMotionAccumulator ---
1435
1436SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1437 clearAbsoluteAxes();
1438}
1439
Jeff Brown65fd2512011-08-18 11:20:58 -07001440void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1441 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1442 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1443 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1444 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1445 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1446 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1447 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1448}
1449
Jeff Brown49754db2011-07-01 17:37:58 -07001450void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1451 mAbsX = 0;
1452 mAbsY = 0;
1453 mAbsPressure = 0;
1454 mAbsToolWidth = 0;
1455 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001456 mAbsTiltX = 0;
1457 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001458}
1459
1460void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1461 if (rawEvent->type == EV_ABS) {
1462 switch (rawEvent->scanCode) {
1463 case ABS_X:
1464 mAbsX = rawEvent->value;
1465 break;
1466 case ABS_Y:
1467 mAbsY = rawEvent->value;
1468 break;
1469 case ABS_PRESSURE:
1470 mAbsPressure = rawEvent->value;
1471 break;
1472 case ABS_TOOL_WIDTH:
1473 mAbsToolWidth = rawEvent->value;
1474 break;
1475 case ABS_DISTANCE:
1476 mAbsDistance = rawEvent->value;
1477 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001478 case ABS_TILT_X:
1479 mAbsTiltX = rawEvent->value;
1480 break;
1481 case ABS_TILT_Y:
1482 mAbsTiltY = rawEvent->value;
1483 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001484 }
1485 }
1486}
1487
1488
1489// --- MultiTouchMotionAccumulator ---
1490
1491MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1492 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1493}
1494
1495MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1496 delete[] mSlots;
1497}
1498
1499void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1500 mSlotCount = slotCount;
1501 mUsingSlotsProtocol = usingSlotsProtocol;
1502
1503 delete[] mSlots;
1504 mSlots = new Slot[slotCount];
1505}
1506
Jeff Brown65fd2512011-08-18 11:20:58 -07001507void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1508 // Unfortunately there is no way to read the initial contents of the slots.
1509 // So when we reset the accumulator, we must assume they are all zeroes.
1510 if (mUsingSlotsProtocol) {
1511 // Query the driver for the current slot index and use it as the initial slot
1512 // before we start reading events from the device. It is possible that the
1513 // current slot index will not be the same as it was when the first event was
1514 // written into the evdev buffer, which means the input mapper could start
1515 // out of sync with the initial state of the events in the evdev buffer.
1516 // In the extremely unlikely case that this happens, the data from
1517 // two slots will be confused until the next ABS_MT_SLOT event is received.
1518 // This can cause the touch point to "jump", but at least there will be
1519 // no stuck touches.
1520 int32_t initialSlot;
1521 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1522 ABS_MT_SLOT, &initialSlot);
1523 if (status) {
1524 LOGD("Could not retrieve current multitouch slot index. status=%d", status);
1525 initialSlot = -1;
1526 }
1527 clearSlots(initialSlot);
1528 } else {
1529 clearSlots(-1);
1530 }
1531}
1532
Jeff Brown49754db2011-07-01 17:37:58 -07001533void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001534 if (mSlots) {
1535 for (size_t i = 0; i < mSlotCount; i++) {
1536 mSlots[i].clear();
1537 }
Jeff Brown49754db2011-07-01 17:37:58 -07001538 }
1539 mCurrentSlot = initialSlot;
1540}
1541
1542void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1543 if (rawEvent->type == EV_ABS) {
1544 bool newSlot = false;
1545 if (mUsingSlotsProtocol) {
1546 if (rawEvent->scanCode == ABS_MT_SLOT) {
1547 mCurrentSlot = rawEvent->value;
1548 newSlot = true;
1549 }
1550 } else if (mCurrentSlot < 0) {
1551 mCurrentSlot = 0;
1552 }
1553
1554 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1555#if DEBUG_POINTERS
1556 if (newSlot) {
1557 LOGW("MultiTouch device emitted invalid slot index %d but it "
1558 "should be between 0 and %d; ignoring this slot.",
1559 mCurrentSlot, mSlotCount - 1);
1560 }
1561#endif
1562 } else {
1563 Slot* slot = &mSlots[mCurrentSlot];
1564
1565 switch (rawEvent->scanCode) {
1566 case ABS_MT_POSITION_X:
1567 slot->mInUse = true;
1568 slot->mAbsMTPositionX = rawEvent->value;
1569 break;
1570 case ABS_MT_POSITION_Y:
1571 slot->mInUse = true;
1572 slot->mAbsMTPositionY = rawEvent->value;
1573 break;
1574 case ABS_MT_TOUCH_MAJOR:
1575 slot->mInUse = true;
1576 slot->mAbsMTTouchMajor = rawEvent->value;
1577 break;
1578 case ABS_MT_TOUCH_MINOR:
1579 slot->mInUse = true;
1580 slot->mAbsMTTouchMinor = rawEvent->value;
1581 slot->mHaveAbsMTTouchMinor = true;
1582 break;
1583 case ABS_MT_WIDTH_MAJOR:
1584 slot->mInUse = true;
1585 slot->mAbsMTWidthMajor = rawEvent->value;
1586 break;
1587 case ABS_MT_WIDTH_MINOR:
1588 slot->mInUse = true;
1589 slot->mAbsMTWidthMinor = rawEvent->value;
1590 slot->mHaveAbsMTWidthMinor = true;
1591 break;
1592 case ABS_MT_ORIENTATION:
1593 slot->mInUse = true;
1594 slot->mAbsMTOrientation = rawEvent->value;
1595 break;
1596 case ABS_MT_TRACKING_ID:
1597 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001598 // The slot is no longer in use but it retains its previous contents,
1599 // which may be reused for subsequent touches.
1600 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001601 } else {
1602 slot->mInUse = true;
1603 slot->mAbsMTTrackingId = rawEvent->value;
1604 }
1605 break;
1606 case ABS_MT_PRESSURE:
1607 slot->mInUse = true;
1608 slot->mAbsMTPressure = rawEvent->value;
1609 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001610 case ABS_MT_DISTANCE:
1611 slot->mInUse = true;
1612 slot->mAbsMTDistance = rawEvent->value;
1613 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001614 case ABS_MT_TOOL_TYPE:
1615 slot->mInUse = true;
1616 slot->mAbsMTToolType = rawEvent->value;
1617 slot->mHaveAbsMTToolType = true;
1618 break;
1619 }
1620 }
1621 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1622 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1623 mCurrentSlot += 1;
1624 }
1625}
1626
Jeff Brown65fd2512011-08-18 11:20:58 -07001627void MultiTouchMotionAccumulator::finishSync() {
1628 if (!mUsingSlotsProtocol) {
1629 clearSlots(-1);
1630 }
1631}
1632
Jeff Brown49754db2011-07-01 17:37:58 -07001633
1634// --- MultiTouchMotionAccumulator::Slot ---
1635
1636MultiTouchMotionAccumulator::Slot::Slot() {
1637 clear();
1638}
1639
Jeff Brown49754db2011-07-01 17:37:58 -07001640void MultiTouchMotionAccumulator::Slot::clear() {
1641 mInUse = false;
1642 mHaveAbsMTTouchMinor = false;
1643 mHaveAbsMTWidthMinor = false;
1644 mHaveAbsMTToolType = false;
1645 mAbsMTPositionX = 0;
1646 mAbsMTPositionY = 0;
1647 mAbsMTTouchMajor = 0;
1648 mAbsMTTouchMinor = 0;
1649 mAbsMTWidthMajor = 0;
1650 mAbsMTWidthMinor = 0;
1651 mAbsMTOrientation = 0;
1652 mAbsMTTrackingId = -1;
1653 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001654 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001655 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001656}
1657
1658int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1659 if (mHaveAbsMTToolType) {
1660 switch (mAbsMTToolType) {
1661 case MT_TOOL_FINGER:
1662 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1663 case MT_TOOL_PEN:
1664 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1665 }
1666 }
1667 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1668}
1669
1670
Jeff Brown6d0fec22010-07-23 21:28:06 -07001671// --- InputMapper ---
1672
1673InputMapper::InputMapper(InputDevice* device) :
1674 mDevice(device), mContext(device->getContext()) {
1675}
1676
1677InputMapper::~InputMapper() {
1678}
1679
1680void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1681 info->addSource(getSources());
1682}
1683
Jeff Brownef3d7e82010-09-30 14:33:04 -07001684void InputMapper::dump(String8& dump) {
1685}
1686
Jeff Brown65fd2512011-08-18 11:20:58 -07001687void InputMapper::configure(nsecs_t when,
1688 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001689}
1690
Jeff Brown65fd2512011-08-18 11:20:58 -07001691void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001692}
1693
Jeff Brownaa3855d2011-03-17 01:34:19 -07001694void InputMapper::timeoutExpired(nsecs_t when) {
1695}
1696
Jeff Brown6d0fec22010-07-23 21:28:06 -07001697int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1698 return AKEY_STATE_UNKNOWN;
1699}
1700
1701int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1702 return AKEY_STATE_UNKNOWN;
1703}
1704
1705int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1706 return AKEY_STATE_UNKNOWN;
1707}
1708
1709bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1710 const int32_t* keyCodes, uint8_t* outFlags) {
1711 return false;
1712}
1713
1714int32_t InputMapper::getMetaState() {
1715 return 0;
1716}
1717
Jeff Brown05dc66a2011-03-02 14:41:58 -08001718void InputMapper::fadePointer() {
1719}
1720
Jeff Brownbe1aa822011-07-27 16:04:54 -07001721status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1722 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1723}
1724
Jeff Browncb1404e2011-01-15 18:14:15 -08001725void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1726 const RawAbsoluteAxisInfo& axis, const char* name) {
1727 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001728 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1729 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001730 } else {
1731 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1732 }
1733}
1734
Jeff Brown6d0fec22010-07-23 21:28:06 -07001735
1736// --- SwitchInputMapper ---
1737
1738SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1739 InputMapper(device) {
1740}
1741
1742SwitchInputMapper::~SwitchInputMapper() {
1743}
1744
1745uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001746 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001747}
1748
1749void SwitchInputMapper::process(const RawEvent* rawEvent) {
1750 switch (rawEvent->type) {
1751 case EV_SW:
1752 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1753 break;
1754 }
1755}
1756
1757void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001758 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1759 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001760}
1761
1762int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1763 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1764}
1765
1766
1767// --- KeyboardInputMapper ---
1768
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001769KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001770 uint32_t source, int32_t keyboardType) :
1771 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001772 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001773}
1774
1775KeyboardInputMapper::~KeyboardInputMapper() {
1776}
1777
Jeff Brown6d0fec22010-07-23 21:28:06 -07001778uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001779 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001780}
1781
1782void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1783 InputMapper::populateDeviceInfo(info);
1784
1785 info->setKeyboardType(mKeyboardType);
Jeff Brown1e08fe92011-11-15 17:48:10 -08001786 info->setKeyCharacterMapFile(getEventHub()->getKeyCharacterMapFile(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001787}
1788
Jeff Brownef3d7e82010-09-30 14:33:04 -07001789void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001790 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1791 dumpParameters(dump);
1792 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001793 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001794 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1795 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1796 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001797}
1798
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001799
Jeff Brown65fd2512011-08-18 11:20:58 -07001800void KeyboardInputMapper::configure(nsecs_t when,
1801 const InputReaderConfiguration* config, uint32_t changes) {
1802 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001803
Jeff Brown474dcb52011-06-14 20:22:50 -07001804 if (!changes) { // first time only
1805 // Configure basic parameters.
1806 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001807 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001808
Jeff Brown65fd2512011-08-18 11:20:58 -07001809 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1810 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1811 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1812 false /*external*/, NULL, NULL, &mOrientation)) {
1813 mOrientation = DISPLAY_ORIENTATION_0;
1814 }
1815 } else {
1816 mOrientation = DISPLAY_ORIENTATION_0;
1817 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001818 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001819}
1820
1821void KeyboardInputMapper::configureParameters() {
1822 mParameters.orientationAware = false;
1823 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1824 mParameters.orientationAware);
1825
Jeff Brownbc68a592011-07-25 12:58:12 -07001826 mParameters.associatedDisplayId = -1;
1827 if (mParameters.orientationAware) {
1828 mParameters.associatedDisplayId = 0;
1829 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001830}
1831
1832void KeyboardInputMapper::dumpParameters(String8& dump) {
1833 dump.append(INDENT3 "Parameters:\n");
1834 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1835 mParameters.associatedDisplayId);
1836 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1837 toString(mParameters.orientationAware));
1838}
1839
Jeff Brown65fd2512011-08-18 11:20:58 -07001840void KeyboardInputMapper::reset(nsecs_t when) {
1841 mMetaState = AMETA_NONE;
1842 mDownTime = 0;
1843 mKeyDowns.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001844
Jeff Brownbe1aa822011-07-27 16:04:54 -07001845 resetLedState();
1846
Jeff Brown65fd2512011-08-18 11:20:58 -07001847 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001848}
1849
1850void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1851 switch (rawEvent->type) {
1852 case EV_KEY: {
1853 int32_t scanCode = rawEvent->scanCode;
1854 if (isKeyboardOrGamepadKey(scanCode)) {
1855 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1856 rawEvent->flags);
1857 }
1858 break;
1859 }
1860 }
1861}
1862
1863bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1864 return scanCode < BTN_MOUSE
1865 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001866 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001867 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001868}
1869
Jeff Brown6328cdc2010-07-29 18:18:33 -07001870void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1871 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001872
Jeff Brownbe1aa822011-07-27 16:04:54 -07001873 if (down) {
1874 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001875 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001876 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001877 }
Jeff Brownfe508922011-01-18 15:10:10 -08001878
Jeff Brownbe1aa822011-07-27 16:04:54 -07001879 // Add key down.
1880 ssize_t keyDownIndex = findKeyDown(scanCode);
1881 if (keyDownIndex >= 0) {
1882 // key repeat, be sure to use same keycode as before in case of rotation
1883 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001884 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001885 // key down
1886 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1887 && mContext->shouldDropVirtualKey(when,
1888 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001889 return;
1890 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001891
1892 mKeyDowns.push();
1893 KeyDown& keyDown = mKeyDowns.editTop();
1894 keyDown.keyCode = keyCode;
1895 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001896 }
1897
Jeff Brownbe1aa822011-07-27 16:04:54 -07001898 mDownTime = when;
1899 } else {
1900 // Remove key down.
1901 ssize_t keyDownIndex = findKeyDown(scanCode);
1902 if (keyDownIndex >= 0) {
1903 // key up, be sure to use same keycode as before in case of rotation
1904 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1905 mKeyDowns.removeAt(size_t(keyDownIndex));
1906 } else {
1907 // key was not actually down
1908 LOGI("Dropping key up from device %s because the key was not down. "
1909 "keyCode=%d, scanCode=%d",
1910 getDeviceName().string(), keyCode, scanCode);
1911 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001912 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001913 }
Jeff Brownfd035822010-06-30 16:10:35 -07001914
Jeff Brownbe1aa822011-07-27 16:04:54 -07001915 bool metaStateChanged = false;
1916 int32_t oldMetaState = mMetaState;
1917 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1918 if (oldMetaState != newMetaState) {
1919 mMetaState = newMetaState;
1920 metaStateChanged = true;
1921 updateLedState(false);
1922 }
1923
1924 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001925
Jeff Brown56194eb2011-03-02 19:23:13 -08001926 // Key down on external an keyboard should wake the device.
1927 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1928 // For internal keyboards, the key layout file should specify the policy flags for
1929 // each wake key individually.
1930 // TODO: Use the input device configuration to control this behavior more finely.
1931 if (down && getDevice()->isExternal()
1932 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1933 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1934 }
1935
Jeff Brown6328cdc2010-07-29 18:18:33 -07001936 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001937 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001938 }
1939
Jeff Brown05dc66a2011-03-02 14:41:58 -08001940 if (down && !isMetaKey(keyCode)) {
1941 getContext()->fadePointer();
1942 }
1943
Jeff Brownbe1aa822011-07-27 16:04:54 -07001944 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001945 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1946 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001947 getListener()->notifyKey(&args);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001948}
1949
Jeff Brownbe1aa822011-07-27 16:04:54 -07001950ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1951 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001952 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001953 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001954 return i;
1955 }
1956 }
1957 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001958}
1959
Jeff Brown6d0fec22010-07-23 21:28:06 -07001960int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1961 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1962}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001963
Jeff Brown6d0fec22010-07-23 21:28:06 -07001964int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1965 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1966}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001967
Jeff Brown6d0fec22010-07-23 21:28:06 -07001968bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1969 const int32_t* keyCodes, uint8_t* outFlags) {
1970 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1971}
1972
1973int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001974 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001975}
1976
Jeff Brownbe1aa822011-07-27 16:04:54 -07001977void KeyboardInputMapper::resetLedState() {
1978 initializeLedState(mCapsLockLedState, LED_CAPSL);
1979 initializeLedState(mNumLockLedState, LED_NUML);
1980 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001981
Jeff Brownbe1aa822011-07-27 16:04:54 -07001982 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001983}
1984
Jeff Brownbe1aa822011-07-27 16:04:54 -07001985void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001986 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1987 ledState.on = false;
1988}
1989
Jeff Brownbe1aa822011-07-27 16:04:54 -07001990void KeyboardInputMapper::updateLedState(bool reset) {
1991 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001992 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001993 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001994 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001995 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001996 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001997}
1998
Jeff Brownbe1aa822011-07-27 16:04:54 -07001999void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002000 int32_t led, int32_t modifier, bool reset) {
2001 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002002 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002003 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002004 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2005 ledState.on = desiredState;
2006 }
2007 }
2008}
2009
Jeff Brown6d0fec22010-07-23 21:28:06 -07002010
Jeff Brown83c09682010-12-23 17:50:18 -08002011// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002012
Jeff Brown83c09682010-12-23 17:50:18 -08002013CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002014 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002015}
2016
Jeff Brown83c09682010-12-23 17:50:18 -08002017CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002018}
2019
Jeff Brown83c09682010-12-23 17:50:18 -08002020uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002021 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002022}
2023
Jeff Brown83c09682010-12-23 17:50:18 -08002024void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002025 InputMapper::populateDeviceInfo(info);
2026
Jeff Brown83c09682010-12-23 17:50:18 -08002027 if (mParameters.mode == Parameters::MODE_POINTER) {
2028 float minX, minY, maxX, maxY;
2029 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002030 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2031 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002032 }
2033 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002034 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2035 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002036 }
Jeff Brownefd32662011-03-08 15:13:06 -08002037 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002038
Jeff Brown65fd2512011-08-18 11:20:58 -07002039 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002040 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002041 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002042 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002043 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002044 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002045}
2046
Jeff Brown83c09682010-12-23 17:50:18 -08002047void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002048 dump.append(INDENT2 "Cursor Input Mapper:\n");
2049 dumpParameters(dump);
2050 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2051 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2052 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2053 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2054 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002055 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002056 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002057 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002058 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2059 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002060 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002061 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2062 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2063 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002064}
2065
Jeff Brown65fd2512011-08-18 11:20:58 -07002066void CursorInputMapper::configure(nsecs_t when,
2067 const InputReaderConfiguration* config, uint32_t changes) {
2068 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002069
Jeff Brown474dcb52011-06-14 20:22:50 -07002070 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002071 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002072
Jeff Brown474dcb52011-06-14 20:22:50 -07002073 // Configure basic parameters.
2074 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002075
Jeff Brown474dcb52011-06-14 20:22:50 -07002076 // Configure device mode.
2077 switch (mParameters.mode) {
2078 case Parameters::MODE_POINTER:
2079 mSource = AINPUT_SOURCE_MOUSE;
2080 mXPrecision = 1.0f;
2081 mYPrecision = 1.0f;
2082 mXScale = 1.0f;
2083 mYScale = 1.0f;
2084 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2085 break;
2086 case Parameters::MODE_NAVIGATION:
2087 mSource = AINPUT_SOURCE_TRACKBALL;
2088 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2089 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2090 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2091 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2092 break;
2093 }
2094
2095 mVWheelScale = 1.0f;
2096 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002097 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002098
Jeff Brown474dcb52011-06-14 20:22:50 -07002099 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2100 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2101 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2102 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2103 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002104
2105 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2106 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2107 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2108 false /*external*/, NULL, NULL, &mOrientation)) {
2109 mOrientation = DISPLAY_ORIENTATION_0;
2110 }
2111 } else {
2112 mOrientation = DISPLAY_ORIENTATION_0;
2113 }
2114 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002115}
2116
Jeff Brown83c09682010-12-23 17:50:18 -08002117void CursorInputMapper::configureParameters() {
2118 mParameters.mode = Parameters::MODE_POINTER;
2119 String8 cursorModeString;
2120 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2121 if (cursorModeString == "navigation") {
2122 mParameters.mode = Parameters::MODE_NAVIGATION;
2123 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
2124 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
2125 }
2126 }
2127
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002128 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002129 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002130 mParameters.orientationAware);
2131
Jeff Brownbc68a592011-07-25 12:58:12 -07002132 mParameters.associatedDisplayId = -1;
2133 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2134 mParameters.associatedDisplayId = 0;
2135 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002136}
2137
Jeff Brown83c09682010-12-23 17:50:18 -08002138void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002139 dump.append(INDENT3 "Parameters:\n");
2140 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2141 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002142
2143 switch (mParameters.mode) {
2144 case Parameters::MODE_POINTER:
2145 dump.append(INDENT4 "Mode: pointer\n");
2146 break;
2147 case Parameters::MODE_NAVIGATION:
2148 dump.append(INDENT4 "Mode: navigation\n");
2149 break;
2150 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002151 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002152 }
2153
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002154 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2155 toString(mParameters.orientationAware));
2156}
2157
Jeff Brown65fd2512011-08-18 11:20:58 -07002158void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002159 mButtonState = 0;
2160 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002161
Jeff Brownbe1aa822011-07-27 16:04:54 -07002162 mPointerVelocityControl.reset();
2163 mWheelXVelocityControl.reset();
2164 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002165
Jeff Brown65fd2512011-08-18 11:20:58 -07002166 mCursorButtonAccumulator.reset(getDevice());
2167 mCursorMotionAccumulator.reset(getDevice());
2168 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002169
Jeff Brown65fd2512011-08-18 11:20:58 -07002170 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002171}
Jeff Brown46b9ac02010-04-22 18:58:52 -07002172
Jeff Brown83c09682010-12-23 17:50:18 -08002173void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002174 mCursorButtonAccumulator.process(rawEvent);
2175 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002176 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002177
Jeff Brown49754db2011-07-01 17:37:58 -07002178 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2179 sync(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002180 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002181}
2182
Jeff Brown83c09682010-12-23 17:50:18 -08002183void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002184 int32_t lastButtonState = mButtonState;
2185 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2186 mButtonState = currentButtonState;
2187
2188 bool wasDown = isPointerDown(lastButtonState);
2189 bool down = isPointerDown(currentButtonState);
2190 bool downChanged;
2191 if (!wasDown && down) {
2192 mDownTime = when;
2193 downChanged = true;
2194 } else if (wasDown && !down) {
2195 downChanged = true;
2196 } else {
2197 downChanged = false;
2198 }
2199 nsecs_t downTime = mDownTime;
2200 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002201 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002202
2203 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2204 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2205 bool moved = deltaX != 0 || deltaY != 0;
2206
Jeff Brown65fd2512011-08-18 11:20:58 -07002207 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002208 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2209 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002210 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002211 }
2212
Jeff Brown65fd2512011-08-18 11:20:58 -07002213 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002214 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002215 pointerProperties.clear();
2216 pointerProperties.id = 0;
2217 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2218
Jeff Brown6328cdc2010-07-29 18:18:33 -07002219 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 pointerCoords.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002221
Jeff Brown65fd2512011-08-18 11:20:58 -07002222 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2223 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002224 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002225
Jeff Brownbe1aa822011-07-27 16:04:54 -07002226 mWheelYVelocityControl.move(when, NULL, &vscroll);
2227 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002228
Jeff Brownbe1aa822011-07-27 16:04:54 -07002229 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002230
Jeff Brownbe1aa822011-07-27 16:04:54 -07002231 if (mPointerController != NULL) {
2232 if (moved || scrolled || buttonsChanged) {
2233 mPointerController->setPresentation(
2234 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002235
Jeff Brownbe1aa822011-07-27 16:04:54 -07002236 if (moved) {
2237 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002238 }
2239
Jeff Brownbe1aa822011-07-27 16:04:54 -07002240 if (buttonsChanged) {
2241 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002242 }
Jeff Brownefd32662011-03-08 15:13:06 -08002243
Jeff Brownbe1aa822011-07-27 16:04:54 -07002244 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002245 }
2246
Jeff Brownbe1aa822011-07-27 16:04:54 -07002247 float x, y;
2248 mPointerController->getPosition(&x, &y);
2249 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2250 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2251 } else {
2252 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2253 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2254 }
2255
2256 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002257
Jeff Brown56194eb2011-03-02 19:23:13 -08002258 // Moving an external trackball or mouse should wake the device.
2259 // We don't do this for internal cursor devices to prevent them from waking up
2260 // the device in your pocket.
2261 // TODO: Use the input device configuration to control this behavior more finely.
2262 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002263 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002264 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2265 }
2266
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002267 // Synthesize key down from buttons if needed.
2268 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2269 policyFlags, lastButtonState, currentButtonState);
2270
2271 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002272 if (downChanged || moved || scrolled || buttonsChanged) {
2273 int32_t metaState = mContext->getGlobalMetaState();
2274 int32_t motionEventAction;
2275 if (downChanged) {
2276 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2277 } else if (down || mPointerController == NULL) {
2278 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2279 } else {
2280 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2281 }
Jeff Brownb6997262010-10-08 22:31:17 -07002282
Jeff Brownbe1aa822011-07-27 16:04:54 -07002283 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2284 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002285 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002286 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002287
Jeff Brownbe1aa822011-07-27 16:04:54 -07002288 // Send hover move after UP to tell the application that the mouse is hovering now.
2289 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2290 && mPointerController != NULL) {
2291 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2292 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2293 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2294 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2295 getListener()->notifyMotion(&hoverArgs);
2296 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002297
Jeff Brownbe1aa822011-07-27 16:04:54 -07002298 // Send scroll events.
2299 if (scrolled) {
2300 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2301 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2302
2303 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2304 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2305 AMOTION_EVENT_EDGE_FLAG_NONE,
2306 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2307 getListener()->notifyMotion(&scrollArgs);
2308 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002309 }
Jeff Browna032cc02011-03-07 16:56:21 -08002310
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002311 // Synthesize key up from buttons if needed.
2312 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2313 policyFlags, lastButtonState, currentButtonState);
2314
Jeff Brown65fd2512011-08-18 11:20:58 -07002315 mCursorMotionAccumulator.finishSync();
2316 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002317}
2318
Jeff Brown83c09682010-12-23 17:50:18 -08002319int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002320 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2321 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2322 } else {
2323 return AKEY_STATE_UNKNOWN;
2324 }
2325}
2326
Jeff Brown05dc66a2011-03-02 14:41:58 -08002327void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002328 if (mPointerController != NULL) {
2329 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2330 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002331}
2332
Jeff Brown6d0fec22010-07-23 21:28:06 -07002333
2334// --- TouchInputMapper ---
2335
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002336TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002337 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002338 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002339 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002340}
2341
2342TouchInputMapper::~TouchInputMapper() {
2343}
2344
2345uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002346 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002347}
2348
2349void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2350 InputMapper::populateDeviceInfo(info);
2351
Jeff Brown65fd2512011-08-18 11:20:58 -07002352 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2353 info->addMotionRange(mOrientedRanges.x);
2354 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002355 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002356
Jeff Brown65fd2512011-08-18 11:20:58 -07002357 if (mOrientedRanges.haveSize) {
2358 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002359 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002360
2361 if (mOrientedRanges.haveTouchSize) {
2362 info->addMotionRange(mOrientedRanges.touchMajor);
2363 info->addMotionRange(mOrientedRanges.touchMinor);
2364 }
2365
2366 if (mOrientedRanges.haveToolSize) {
2367 info->addMotionRange(mOrientedRanges.toolMajor);
2368 info->addMotionRange(mOrientedRanges.toolMinor);
2369 }
2370
2371 if (mOrientedRanges.haveOrientation) {
2372 info->addMotionRange(mOrientedRanges.orientation);
2373 }
2374
2375 if (mOrientedRanges.haveDistance) {
2376 info->addMotionRange(mOrientedRanges.distance);
2377 }
2378
2379 if (mOrientedRanges.haveTilt) {
2380 info->addMotionRange(mOrientedRanges.tilt);
2381 }
2382
2383 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2384 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2385 }
2386 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2387 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2388 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002389 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002390}
2391
Jeff Brownef3d7e82010-09-30 14:33:04 -07002392void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002393 dump.append(INDENT2 "Touch Input Mapper:\n");
2394 dumpParameters(dump);
2395 dumpVirtualKeys(dump);
2396 dumpRawPointerAxes(dump);
2397 dumpCalibration(dump);
2398 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002399
Jeff Brownbe1aa822011-07-27 16:04:54 -07002400 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2401 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2402 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2403 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2404 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2405 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002406 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2407 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002408 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002409 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2410 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002411 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2412 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2413 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2414 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2415 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002416
Jeff Brownbe1aa822011-07-27 16:04:54 -07002417 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002418
Jeff Brownbe1aa822011-07-27 16:04:54 -07002419 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2420 mLastRawPointerData.pointerCount);
2421 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2422 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2423 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2424 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002425 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2426 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002427 pointer.id, pointer.x, pointer.y, pointer.pressure,
2428 pointer.touchMajor, pointer.touchMinor,
2429 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002430 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002431 pointer.toolType, toString(pointer.isHovering));
2432 }
2433
2434 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2435 mLastCookedPointerData.pointerCount);
2436 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2437 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2438 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2439 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2440 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002441 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2442 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002443 pointerProperties.id,
2444 pointerCoords.getX(),
2445 pointerCoords.getY(),
2446 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2447 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2448 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2449 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2450 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2451 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002452 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2454 pointerProperties.toolType,
2455 toString(mLastCookedPointerData.isHovering(i)));
2456 }
2457
Jeff Brown65fd2512011-08-18 11:20:58 -07002458 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2460 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002461 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002462 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002463 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002464 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002465 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002466 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002467 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002468 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2469 mPointerGestureMaxSwipeWidth);
2470 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002471}
2472
Jeff Brown65fd2512011-08-18 11:20:58 -07002473void TouchInputMapper::configure(nsecs_t when,
2474 const InputReaderConfiguration* config, uint32_t changes) {
2475 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002476
Jeff Brown474dcb52011-06-14 20:22:50 -07002477 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002478
Jeff Brown474dcb52011-06-14 20:22:50 -07002479 if (!changes) { // first time only
2480 // Configure basic parameters.
2481 configureParameters();
2482
Jeff Brown65fd2512011-08-18 11:20:58 -07002483 // Configure common accumulators.
2484 mCursorScrollAccumulator.configure(getDevice());
2485 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002486
2487 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002488 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002489
2490 // Prepare input device calibration.
2491 parseCalibration();
2492 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002493 }
2494
Jeff Brown474dcb52011-06-14 20:22:50 -07002495 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002496 // Update pointer speed.
2497 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2498 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2499 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002500 }
Jeff Brown8d608662010-08-30 03:02:23 -07002501
Jeff Brown65fd2512011-08-18 11:20:58 -07002502 bool resetNeeded = false;
2503 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002504 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2505 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002506 // Configure device sources, surface dimensions, orientation and
2507 // scaling factors.
2508 configureSurface(when, &resetNeeded);
2509 }
2510
2511 if (changes && resetNeeded) {
2512 // Send reset, unless this is the first time the device has been configured,
2513 // in which case the reader will call reset itself after all mappers are ready.
2514 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002515 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002516}
2517
Jeff Brown8d608662010-08-30 03:02:23 -07002518void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002519 // Use the pointer presentation mode for devices that do not support distinct
2520 // multitouch. The spot-based presentation relies on being able to accurately
2521 // locate two or more fingers on the touch pad.
2522 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2523 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002524
Jeff Brown538881e2011-05-25 18:23:38 -07002525 String8 gestureModeString;
2526 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2527 gestureModeString)) {
2528 if (gestureModeString == "pointer") {
2529 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2530 } else if (gestureModeString == "spots") {
2531 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2532 } else if (gestureModeString != "default") {
2533 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2534 }
2535 }
2536
Jeff Browndeffe072011-08-26 18:38:46 -07002537 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2538 // The device is a touch screen.
2539 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2540 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2541 // The device is a pointing device like a track pad.
2542 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2543 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002544 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2545 // The device is a cursor device with a touch pad attached.
2546 // By default don't use the touch pad to move the pointer.
2547 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2548 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002549 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002550 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2551 }
2552
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002553 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002554 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2555 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002556 if (deviceTypeString == "touchScreen") {
2557 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002558 } else if (deviceTypeString == "touchPad") {
2559 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002560 } else if (deviceTypeString == "pointer") {
2561 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002562 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002563 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2564 }
2565 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002566
Jeff Brownefd32662011-03-08 15:13:06 -08002567 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002568 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2569 mParameters.orientationAware);
2570
Jeff Brownbc68a592011-07-25 12:58:12 -07002571 mParameters.associatedDisplayId = -1;
2572 mParameters.associatedDisplayIsExternal = false;
2573 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002574 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002575 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2576 mParameters.associatedDisplayIsExternal =
2577 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2578 && getDevice()->isExternal();
2579 mParameters.associatedDisplayId = 0;
2580 }
Jeff Brown8d608662010-08-30 03:02:23 -07002581}
2582
Jeff Brownef3d7e82010-09-30 14:33:04 -07002583void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002584 dump.append(INDENT3 "Parameters:\n");
2585
Jeff Brown538881e2011-05-25 18:23:38 -07002586 switch (mParameters.gestureMode) {
2587 case Parameters::GESTURE_MODE_POINTER:
2588 dump.append(INDENT4 "GestureMode: pointer\n");
2589 break;
2590 case Parameters::GESTURE_MODE_SPOTS:
2591 dump.append(INDENT4 "GestureMode: spots\n");
2592 break;
2593 default:
2594 assert(false);
2595 }
2596
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002597 switch (mParameters.deviceType) {
2598 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2599 dump.append(INDENT4 "DeviceType: touchScreen\n");
2600 break;
2601 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2602 dump.append(INDENT4 "DeviceType: touchPad\n");
2603 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002604 case Parameters::DEVICE_TYPE_POINTER:
2605 dump.append(INDENT4 "DeviceType: pointer\n");
2606 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002607 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002608 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002609 }
2610
Jeff Brown65fd2512011-08-18 11:20:58 -07002611 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2612 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002613 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2614 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002615}
2616
Jeff Brownbe1aa822011-07-27 16:04:54 -07002617void TouchInputMapper::configureRawPointerAxes() {
2618 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002619}
2620
Jeff Brownbe1aa822011-07-27 16:04:54 -07002621void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2622 dump.append(INDENT3 "Raw Touch Axes:\n");
2623 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2624 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2625 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2626 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2627 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2628 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2629 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2630 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2631 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002632 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2633 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002634 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2635 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002636}
2637
Jeff Brown65fd2512011-08-18 11:20:58 -07002638void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2639 int32_t oldDeviceMode = mDeviceMode;
2640
2641 // Determine device mode.
2642 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2643 && mConfig.pointerGesturesEnabled) {
2644 mSource = AINPUT_SOURCE_MOUSE;
2645 mDeviceMode = DEVICE_MODE_POINTER;
2646 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2647 && mParameters.associatedDisplayId >= 0) {
2648 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2649 mDeviceMode = DEVICE_MODE_DIRECT;
2650 } else {
2651 mSource = AINPUT_SOURCE_TOUCHPAD;
2652 mDeviceMode = DEVICE_MODE_UNSCALED;
2653 }
2654
Jeff Brown9626b142011-03-03 02:09:54 -08002655 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002656 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002657 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2658 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002659 mDeviceMode = DEVICE_MODE_DISABLED;
2660 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002661 }
2662
Jeff Brown65fd2512011-08-18 11:20:58 -07002663 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002664 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002665 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002666 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002667 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2668 &mAssociatedDisplayOrientation)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002669 LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
2670 "display %d. The device will be inoperable until the display size "
2671 "becomes available.",
2672 getDeviceName().string(), mParameters.associatedDisplayId);
2673 mDeviceMode = DEVICE_MODE_DISABLED;
2674 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002675 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002676 }
2677
Jeff Brown65fd2512011-08-18 11:20:58 -07002678 // Configure dimensions.
2679 int32_t width, height, orientation;
2680 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2681 width = mAssociatedDisplayWidth;
2682 height = mAssociatedDisplayHeight;
2683 orientation = mParameters.orientationAware ?
2684 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2685 } else {
2686 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2687 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2688 orientation = DISPLAY_ORIENTATION_0;
2689 }
2690
2691 // If moving between pointer modes, need to reset some state.
2692 bool deviceModeChanged;
2693 if (mDeviceMode != oldDeviceMode) {
2694 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002695 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002696 }
2697
Jeff Browndaf4a122011-08-26 17:14:14 -07002698 // Create pointer controller if needed.
2699 if (mDeviceMode == DEVICE_MODE_POINTER ||
2700 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2701 if (mPointerController == NULL) {
2702 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2703 }
2704 } else {
2705 mPointerController.clear();
2706 }
2707
Jeff Brownbe1aa822011-07-27 16:04:54 -07002708 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002709 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002710 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002711 }
2712
Jeff Brownbe1aa822011-07-27 16:04:54 -07002713 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002714 if (sizeChanged || deviceModeChanged) {
2715 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
2716 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002717
Jeff Brownbe1aa822011-07-27 16:04:54 -07002718 mSurfaceWidth = width;
2719 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002720
Jeff Brown8d608662010-08-30 03:02:23 -07002721 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002722 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2723 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2724 mXPrecision = 1.0f / mXScale;
2725 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002726
Jeff Brownbe1aa822011-07-27 16:04:54 -07002727 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002728 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002729 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002730 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002731
Jeff Brownbe1aa822011-07-27 16:04:54 -07002732 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002733
Jeff Brown8d608662010-08-30 03:02:23 -07002734 // Scale factor for terms that are not oriented in a particular axis.
2735 // If the pixels are square then xScale == yScale otherwise we fake it
2736 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002737 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002738
Jeff Brown8d608662010-08-30 03:02:23 -07002739 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002740 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002741
Jeff Browna1f89ce2011-08-11 00:05:01 -07002742 // Size factors.
2743 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2744 if (mRawPointerAxes.touchMajor.valid
2745 && mRawPointerAxes.touchMajor.maxValue != 0) {
2746 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2747 } else if (mRawPointerAxes.toolMajor.valid
2748 && mRawPointerAxes.toolMajor.maxValue != 0) {
2749 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2750 } else {
2751 mSizeScale = 0.0f;
2752 }
2753
Jeff Brownbe1aa822011-07-27 16:04:54 -07002754 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002755 mOrientedRanges.haveToolSize = true;
2756 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002757
Jeff Brownbe1aa822011-07-27 16:04:54 -07002758 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002759 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002760 mOrientedRanges.touchMajor.min = 0;
2761 mOrientedRanges.touchMajor.max = diagonalSize;
2762 mOrientedRanges.touchMajor.flat = 0;
2763 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002764
Jeff Brownbe1aa822011-07-27 16:04:54 -07002765 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2766 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002767
Jeff Brownbe1aa822011-07-27 16:04:54 -07002768 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002769 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002770 mOrientedRanges.toolMajor.min = 0;
2771 mOrientedRanges.toolMajor.max = diagonalSize;
2772 mOrientedRanges.toolMajor.flat = 0;
2773 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002774
Jeff Brownbe1aa822011-07-27 16:04:54 -07002775 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2776 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002777
2778 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002779 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002780 mOrientedRanges.size.min = 0;
2781 mOrientedRanges.size.max = 1.0;
2782 mOrientedRanges.size.flat = 0;
2783 mOrientedRanges.size.fuzz = 0;
2784 } else {
2785 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002786 }
2787
2788 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002789 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002790 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2791 || mCalibration.pressureCalibration
2792 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2793 if (mCalibration.havePressureScale) {
2794 mPressureScale = mCalibration.pressureScale;
2795 } else if (mRawPointerAxes.pressure.valid
2796 && mRawPointerAxes.pressure.maxValue != 0) {
2797 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002798 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002799 }
Jeff Brown8d608662010-08-30 03:02:23 -07002800
Jeff Brown65fd2512011-08-18 11:20:58 -07002801 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2802 mOrientedRanges.pressure.source = mSource;
2803 mOrientedRanges.pressure.min = 0;
2804 mOrientedRanges.pressure.max = 1.0;
2805 mOrientedRanges.pressure.flat = 0;
2806 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002807
Jeff Brown65fd2512011-08-18 11:20:58 -07002808 // Tilt
2809 mTiltXCenter = 0;
2810 mTiltXScale = 0;
2811 mTiltYCenter = 0;
2812 mTiltYScale = 0;
2813 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2814 if (mHaveTilt) {
2815 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2816 mRawPointerAxes.tiltX.maxValue);
2817 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2818 mRawPointerAxes.tiltY.maxValue);
2819 mTiltXScale = M_PI / 180;
2820 mTiltYScale = M_PI / 180;
2821
2822 mOrientedRanges.haveTilt = true;
2823
2824 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2825 mOrientedRanges.tilt.source = mSource;
2826 mOrientedRanges.tilt.min = 0;
2827 mOrientedRanges.tilt.max = M_PI_2;
2828 mOrientedRanges.tilt.flat = 0;
2829 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002830 }
2831
Jeff Brown8d608662010-08-30 03:02:23 -07002832 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002833 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002834 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002835 if (mHaveTilt) {
2836 mOrientedRanges.haveOrientation = true;
2837
2838 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2839 mOrientedRanges.orientation.source = mSource;
2840 mOrientedRanges.orientation.min = -M_PI;
2841 mOrientedRanges.orientation.max = M_PI;
2842 mOrientedRanges.orientation.flat = 0;
2843 mOrientedRanges.orientation.fuzz = 0;
2844 } else if (mCalibration.orientationCalibration !=
2845 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002846 if (mCalibration.orientationCalibration
2847 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002848 if (mRawPointerAxes.orientation.valid) {
2849 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2850 mRawPointerAxes.orientation.maxValue);
2851 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2852 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002853 }
2854 }
2855
Jeff Brownbe1aa822011-07-27 16:04:54 -07002856 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002857
Jeff Brownbe1aa822011-07-27 16:04:54 -07002858 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002859 mOrientedRanges.orientation.source = mSource;
2860 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002861 mOrientedRanges.orientation.max = M_PI_2;
2862 mOrientedRanges.orientation.flat = 0;
2863 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002864 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002865
2866 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002867 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002868 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2869 if (mCalibration.distanceCalibration
2870 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2871 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002872 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002873 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002874 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002875 }
2876 }
2877
Jeff Brownbe1aa822011-07-27 16:04:54 -07002878 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002879
Jeff Brownbe1aa822011-07-27 16:04:54 -07002880 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002881 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002882 mOrientedRanges.distance.min =
2883 mRawPointerAxes.distance.minValue * mDistanceScale;
2884 mOrientedRanges.distance.max =
2885 mRawPointerAxes.distance.minValue * mDistanceScale;
2886 mOrientedRanges.distance.flat = 0;
2887 mOrientedRanges.distance.fuzz =
2888 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002889 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002890 }
2891
Jeff Brown65fd2512011-08-18 11:20:58 -07002892 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002893 // Compute oriented surface dimensions, precision, scales and ranges.
2894 // Note that the maximum value reported is an inclusive maximum value so it is one
2895 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002896 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002897 case DISPLAY_ORIENTATION_90:
2898 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002899 mOrientedSurfaceWidth = mSurfaceHeight;
2900 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002901
Jeff Brownbe1aa822011-07-27 16:04:54 -07002902 mOrientedXPrecision = mYPrecision;
2903 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002904
Jeff Brownbe1aa822011-07-27 16:04:54 -07002905 mOrientedRanges.x.min = 0;
2906 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2907 * mYScale;
2908 mOrientedRanges.x.flat = 0;
2909 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002910
Jeff Brownbe1aa822011-07-27 16:04:54 -07002911 mOrientedRanges.y.min = 0;
2912 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2913 * mXScale;
2914 mOrientedRanges.y.flat = 0;
2915 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002916 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002917
Jeff Brown6d0fec22010-07-23 21:28:06 -07002918 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002919 mOrientedSurfaceWidth = mSurfaceWidth;
2920 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002921
Jeff Brownbe1aa822011-07-27 16:04:54 -07002922 mOrientedXPrecision = mXPrecision;
2923 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002924
Jeff Brownbe1aa822011-07-27 16:04:54 -07002925 mOrientedRanges.x.min = 0;
2926 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2927 * mXScale;
2928 mOrientedRanges.x.flat = 0;
2929 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002930
Jeff Brownbe1aa822011-07-27 16:04:54 -07002931 mOrientedRanges.y.min = 0;
2932 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2933 * mYScale;
2934 mOrientedRanges.y.flat = 0;
2935 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002936 break;
2937 }
Jeff Brownace13b12011-03-09 17:39:48 -08002938
2939 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002940 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002941 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2942 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002943 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002944 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2945 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002946
Jeff Brown2352b972011-04-12 22:39:53 -07002947 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002948 // given area relative to the diagonal size of the display when no acceleration
2949 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002950 // Assume that the touch pad has a square aspect ratio such that movements in
2951 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002952 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002953 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002954 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002955
2956 // Scale zooms to cover a smaller range of the display than movements do.
2957 // This value determines the area around the pointer that is affected by freeform
2958 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002959 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002960 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002961 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002962
Jeff Brown2352b972011-04-12 22:39:53 -07002963 // Max width between pointers to detect a swipe gesture is more than some fraction
2964 // of the diagonal axis of the touch pad. Touches that are wider than this are
2965 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002966 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002967 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002968 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002969
Jeff Brown65fd2512011-08-18 11:20:58 -07002970 // Abort current pointer usages because the state has changed.
2971 abortPointerUsage(when, 0 /*policyFlags*/);
2972
2973 // Inform the dispatcher about the changes.
2974 *outResetNeeded = true;
2975 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002976}
2977
Jeff Brownbe1aa822011-07-27 16:04:54 -07002978void TouchInputMapper::dumpSurface(String8& dump) {
2979 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2980 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2981 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002982}
2983
Jeff Brownbe1aa822011-07-27 16:04:54 -07002984void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002985 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002986 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002987
Jeff Brownbe1aa822011-07-27 16:04:54 -07002988 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002989
Jeff Brown6328cdc2010-07-29 18:18:33 -07002990 if (virtualKeyDefinitions.size() == 0) {
2991 return;
2992 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002993
Jeff Brownbe1aa822011-07-27 16:04:54 -07002994 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002995
Jeff Brownbe1aa822011-07-27 16:04:54 -07002996 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2997 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2998 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2999 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003000
3001 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003002 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003003 virtualKeyDefinitions[i];
3004
Jeff Brownbe1aa822011-07-27 16:04:54 -07003005 mVirtualKeys.add();
3006 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003007
3008 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3009 int32_t keyCode;
3010 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08003011 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07003012 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003013 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
3014 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003015 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003016 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003017 }
3018
Jeff Brown6328cdc2010-07-29 18:18:33 -07003019 virtualKey.keyCode = keyCode;
3020 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003021
Jeff Brown6328cdc2010-07-29 18:18:33 -07003022 // convert the key definition's display coordinates into touch coordinates for a hit box
3023 int32_t halfWidth = virtualKeyDefinition.width / 2;
3024 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003025
Jeff Brown6328cdc2010-07-29 18:18:33 -07003026 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003027 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003028 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003029 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003030 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003031 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003032 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003033 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003034 }
3035}
3036
Jeff Brownbe1aa822011-07-27 16:04:54 -07003037void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3038 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003039 dump.append(INDENT3 "Virtual Keys:\n");
3040
Jeff Brownbe1aa822011-07-27 16:04:54 -07003041 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3042 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003043 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3044 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3045 i, virtualKey.scanCode, virtualKey.keyCode,
3046 virtualKey.hitLeft, virtualKey.hitRight,
3047 virtualKey.hitTop, virtualKey.hitBottom);
3048 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003049 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003050}
3051
Jeff Brown8d608662010-08-30 03:02:23 -07003052void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003053 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003054 Calibration& out = mCalibration;
3055
Jeff Browna1f89ce2011-08-11 00:05:01 -07003056 // Size
3057 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3058 String8 sizeCalibrationString;
3059 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3060 if (sizeCalibrationString == "none") {
3061 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3062 } else if (sizeCalibrationString == "geometric") {
3063 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3064 } else if (sizeCalibrationString == "diameter") {
3065 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3066 } else if (sizeCalibrationString == "area") {
3067 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3068 } else if (sizeCalibrationString != "default") {
3069 LOGW("Invalid value for touch.size.calibration: '%s'",
3070 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003071 }
3072 }
3073
Jeff Browna1f89ce2011-08-11 00:05:01 -07003074 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3075 out.sizeScale);
3076 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3077 out.sizeBias);
3078 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3079 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003080
3081 // Pressure
3082 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3083 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003084 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003085 if (pressureCalibrationString == "none") {
3086 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3087 } else if (pressureCalibrationString == "physical") {
3088 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3089 } else if (pressureCalibrationString == "amplitude") {
3090 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3091 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003092 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003093 pressureCalibrationString.string());
3094 }
3095 }
3096
Jeff Brown8d608662010-08-30 03:02:23 -07003097 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3098 out.pressureScale);
3099
Jeff Brown8d608662010-08-30 03:02:23 -07003100 // Orientation
3101 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3102 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003103 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003104 if (orientationCalibrationString == "none") {
3105 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3106 } else if (orientationCalibrationString == "interpolated") {
3107 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003108 } else if (orientationCalibrationString == "vector") {
3109 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003110 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003111 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003112 orientationCalibrationString.string());
3113 }
3114 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003115
3116 // Distance
3117 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3118 String8 distanceCalibrationString;
3119 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3120 if (distanceCalibrationString == "none") {
3121 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3122 } else if (distanceCalibrationString == "scaled") {
3123 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3124 } else if (distanceCalibrationString != "default") {
3125 LOGW("Invalid value for touch.distance.calibration: '%s'",
3126 distanceCalibrationString.string());
3127 }
3128 }
3129
3130 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3131 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003132}
3133
3134void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003135 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003136 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3137 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3138 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003139 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003140 } else {
3141 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3142 }
Jeff Brown8d608662010-08-30 03:02:23 -07003143
Jeff Browna1f89ce2011-08-11 00:05:01 -07003144 // Pressure
3145 if (mRawPointerAxes.pressure.valid) {
3146 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3147 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3148 }
3149 } else {
3150 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003151 }
3152
3153 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003154 if (mRawPointerAxes.orientation.valid) {
3155 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003156 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003157 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003158 } else {
3159 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003160 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003161
3162 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003163 if (mRawPointerAxes.distance.valid) {
3164 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003165 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003166 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003167 } else {
3168 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003169 }
Jeff Brown8d608662010-08-30 03:02:23 -07003170}
3171
Jeff Brownef3d7e82010-09-30 14:33:04 -07003172void TouchInputMapper::dumpCalibration(String8& dump) {
3173 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003174
Jeff Browna1f89ce2011-08-11 00:05:01 -07003175 // Size
3176 switch (mCalibration.sizeCalibration) {
3177 case Calibration::SIZE_CALIBRATION_NONE:
3178 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003179 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003180 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3181 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003182 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003183 case Calibration::SIZE_CALIBRATION_DIAMETER:
3184 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3185 break;
3186 case Calibration::SIZE_CALIBRATION_AREA:
3187 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003188 break;
3189 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003190 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003191 }
3192
Jeff Browna1f89ce2011-08-11 00:05:01 -07003193 if (mCalibration.haveSizeScale) {
3194 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3195 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003196 }
3197
Jeff Browna1f89ce2011-08-11 00:05:01 -07003198 if (mCalibration.haveSizeBias) {
3199 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3200 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003201 }
3202
Jeff Browna1f89ce2011-08-11 00:05:01 -07003203 if (mCalibration.haveSizeIsSummed) {
3204 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3205 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003206 }
3207
3208 // Pressure
3209 switch (mCalibration.pressureCalibration) {
3210 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003211 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003212 break;
3213 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003214 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003215 break;
3216 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003217 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003218 break;
3219 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003220 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003221 }
3222
Jeff Brown8d608662010-08-30 03:02:23 -07003223 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003224 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3225 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003226 }
3227
Jeff Brown8d608662010-08-30 03:02:23 -07003228 // Orientation
3229 switch (mCalibration.orientationCalibration) {
3230 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003231 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003232 break;
3233 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003234 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003235 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003236 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3237 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3238 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003239 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003240 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003241 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003242
3243 // Distance
3244 switch (mCalibration.distanceCalibration) {
3245 case Calibration::DISTANCE_CALIBRATION_NONE:
3246 dump.append(INDENT4 "touch.distance.calibration: none\n");
3247 break;
3248 case Calibration::DISTANCE_CALIBRATION_SCALED:
3249 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3250 break;
3251 default:
3252 LOG_ASSERT(false);
3253 }
3254
3255 if (mCalibration.haveDistanceScale) {
3256 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3257 mCalibration.distanceScale);
3258 }
Jeff Brown8d608662010-08-30 03:02:23 -07003259}
3260
Jeff Brown65fd2512011-08-18 11:20:58 -07003261void TouchInputMapper::reset(nsecs_t when) {
3262 mCursorButtonAccumulator.reset(getDevice());
3263 mCursorScrollAccumulator.reset(getDevice());
3264 mTouchButtonAccumulator.reset(getDevice());
3265
3266 mPointerVelocityControl.reset();
3267 mWheelXVelocityControl.reset();
3268 mWheelYVelocityControl.reset();
3269
Jeff Brownbe1aa822011-07-27 16:04:54 -07003270 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003271 mLastRawPointerData.clear();
3272 mCurrentCookedPointerData.clear();
3273 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003274 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003275 mLastButtonState = 0;
3276 mCurrentRawVScroll = 0;
3277 mCurrentRawHScroll = 0;
3278 mCurrentFingerIdBits.clear();
3279 mLastFingerIdBits.clear();
3280 mCurrentStylusIdBits.clear();
3281 mLastStylusIdBits.clear();
3282 mCurrentMouseIdBits.clear();
3283 mLastMouseIdBits.clear();
3284 mPointerUsage = POINTER_USAGE_NONE;
3285 mSentHoverEnter = false;
3286 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003287
Jeff Brown65fd2512011-08-18 11:20:58 -07003288 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003289
Jeff Brown65fd2512011-08-18 11:20:58 -07003290 mPointerGesture.reset();
3291 mPointerSimple.reset();
3292
3293 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003294 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3295 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003296 }
3297
Jeff Brown65fd2512011-08-18 11:20:58 -07003298 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003299}
3300
Jeff Brown65fd2512011-08-18 11:20:58 -07003301void TouchInputMapper::process(const RawEvent* rawEvent) {
3302 mCursorButtonAccumulator.process(rawEvent);
3303 mCursorScrollAccumulator.process(rawEvent);
3304 mTouchButtonAccumulator.process(rawEvent);
3305
3306 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3307 sync(rawEvent->when);
3308 }
3309}
3310
3311void TouchInputMapper::sync(nsecs_t when) {
3312 // Sync button state.
3313 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3314 | mCursorButtonAccumulator.getButtonState();
3315
3316 // Sync scroll state.
3317 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3318 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3319 mCursorScrollAccumulator.finishSync();
3320
3321 // Sync touch state.
3322 bool havePointerIds = true;
3323 mCurrentRawPointerData.clear();
3324 syncTouch(when, &havePointerIds);
3325
Jeff Brownaa3855d2011-03-17 01:34:19 -07003326#if DEBUG_RAW_EVENTS
3327 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003328 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3329 mLastRawPointerData.pointerCount,
3330 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003331 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003332 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3333 "hovering ids 0x%08x -> 0x%08x",
3334 mLastRawPointerData.pointerCount,
3335 mCurrentRawPointerData.pointerCount,
3336 mLastRawPointerData.touchingIdBits.value,
3337 mCurrentRawPointerData.touchingIdBits.value,
3338 mLastRawPointerData.hoveringIdBits.value,
3339 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003340 }
3341#endif
3342
Jeff Brown65fd2512011-08-18 11:20:58 -07003343 // Reset state that we will compute below.
3344 mCurrentFingerIdBits.clear();
3345 mCurrentStylusIdBits.clear();
3346 mCurrentMouseIdBits.clear();
3347 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003348
Jeff Brown65fd2512011-08-18 11:20:58 -07003349 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3350 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003351 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003352 mCurrentButtonState = 0;
3353 } else {
3354 // Preprocess pointer data.
3355 if (!havePointerIds) {
3356 assignPointerIds();
3357 }
3358
3359 // Handle policy on initial down or hover events.
3360 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003361 bool initialDown = mLastRawPointerData.pointerCount == 0
3362 && mCurrentRawPointerData.pointerCount != 0;
3363 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3364 if (initialDown || buttonsPressed) {
3365 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003366 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003367 getContext()->fadePointer();
3368 }
3369
3370 // Initial downs on external touch devices should wake the device.
3371 // We don't do this for internal touch screens to prevent them from waking
3372 // up in your pocket.
3373 // TODO: Use the input device configuration to control this behavior more finely.
3374 if (getDevice()->isExternal()) {
3375 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3376 }
3377 }
3378
3379 // Synthesize key down from raw buttons if needed.
3380 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3381 policyFlags, mLastButtonState, mCurrentButtonState);
3382
3383 // Consume raw off-screen touches before cooking pointer data.
3384 // If touches are consumed, subsequent code will not receive any pointer data.
3385 if (consumeRawTouches(when, policyFlags)) {
3386 mCurrentRawPointerData.clear();
3387 }
3388
3389 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3390 // with cooked pointer data that has the same ids and indices as the raw data.
3391 // The following code can use either the raw or cooked data, as needed.
3392 cookPointerData();
3393
3394 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003395 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003396 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3397 uint32_t id = idBits.clearFirstMarkedBit();
3398 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3399 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3400 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3401 mCurrentStylusIdBits.markBit(id);
3402 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3403 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3404 mCurrentFingerIdBits.markBit(id);
3405 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3406 mCurrentMouseIdBits.markBit(id);
3407 }
3408 }
3409 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3410 uint32_t id = idBits.clearFirstMarkedBit();
3411 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3412 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3413 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3414 mCurrentStylusIdBits.markBit(id);
3415 }
3416 }
3417
3418 // Stylus takes precedence over all tools, then mouse, then finger.
3419 PointerUsage pointerUsage = mPointerUsage;
3420 if (!mCurrentStylusIdBits.isEmpty()) {
3421 mCurrentMouseIdBits.clear();
3422 mCurrentFingerIdBits.clear();
3423 pointerUsage = POINTER_USAGE_STYLUS;
3424 } else if (!mCurrentMouseIdBits.isEmpty()) {
3425 mCurrentFingerIdBits.clear();
3426 pointerUsage = POINTER_USAGE_MOUSE;
3427 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3428 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003429 }
3430
3431 dispatchPointerUsage(when, policyFlags, pointerUsage);
3432 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003433 if (mDeviceMode == DEVICE_MODE_DIRECT
3434 && mConfig.showTouches && mPointerController != NULL) {
3435 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3436 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3437
3438 mPointerController->setButtonState(mCurrentButtonState);
3439 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3440 mCurrentCookedPointerData.idToIndex,
3441 mCurrentCookedPointerData.touchingIdBits);
3442 }
3443
Jeff Brown65fd2512011-08-18 11:20:58 -07003444 dispatchHoverExit(when, policyFlags);
3445 dispatchTouches(when, policyFlags);
3446 dispatchHoverEnterAndMove(when, policyFlags);
3447 }
3448
3449 // Synthesize key up from raw buttons if needed.
3450 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3451 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003452 }
3453
Jeff Brown6328cdc2010-07-29 18:18:33 -07003454 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003455 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3456 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3457 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003458 mLastFingerIdBits = mCurrentFingerIdBits;
3459 mLastStylusIdBits = mCurrentStylusIdBits;
3460 mLastMouseIdBits = mCurrentMouseIdBits;
3461
3462 // Clear some transient state.
3463 mCurrentRawVScroll = 0;
3464 mCurrentRawHScroll = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003465}
3466
Jeff Brown79ac9692011-04-19 21:20:10 -07003467void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003468 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003469 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3470 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3471 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003472 }
3473}
3474
Jeff Brownbe1aa822011-07-27 16:04:54 -07003475bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3476 // Check for release of a virtual key.
3477 if (mCurrentVirtualKey.down) {
3478 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3479 // Pointer went up while virtual key was down.
3480 mCurrentVirtualKey.down = false;
3481 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003482#if DEBUG_VIRTUAL_KEYS
3483 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003484 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003485#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003486 dispatchVirtualKey(when, policyFlags,
3487 AKEY_EVENT_ACTION_UP,
3488 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003489 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003490 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003491 }
3492
Jeff Brownbe1aa822011-07-27 16:04:54 -07003493 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3494 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3495 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3496 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3497 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3498 // Pointer is still within the space of the virtual key.
3499 return true;
3500 }
3501 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003502
Jeff Brownbe1aa822011-07-27 16:04:54 -07003503 // Pointer left virtual key area or another pointer also went down.
3504 // Send key cancellation but do not consume the touch yet.
3505 // This is useful when the user swipes through from the virtual key area
3506 // into the main display surface.
3507 mCurrentVirtualKey.down = false;
3508 if (!mCurrentVirtualKey.ignored) {
3509#if DEBUG_VIRTUAL_KEYS
3510 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3511 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3512#endif
3513 dispatchVirtualKey(when, policyFlags,
3514 AKEY_EVENT_ACTION_UP,
3515 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3516 | AKEY_EVENT_FLAG_CANCELED);
3517 }
3518 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003519
Jeff Brownbe1aa822011-07-27 16:04:54 -07003520 if (mLastRawPointerData.touchingIdBits.isEmpty()
3521 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3522 // Pointer just went down. Check for virtual key press or off-screen touches.
3523 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3524 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3525 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3526 // If exactly one pointer went down, check for virtual key hit.
3527 // Otherwise we will drop the entire stroke.
3528 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3529 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3530 if (virtualKey) {
3531 mCurrentVirtualKey.down = true;
3532 mCurrentVirtualKey.downTime = when;
3533 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3534 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3535 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3536 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3537
3538 if (!mCurrentVirtualKey.ignored) {
3539#if DEBUG_VIRTUAL_KEYS
3540 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3541 mCurrentVirtualKey.keyCode,
3542 mCurrentVirtualKey.scanCode);
3543#endif
3544 dispatchVirtualKey(when, policyFlags,
3545 AKEY_EVENT_ACTION_DOWN,
3546 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3547 }
3548 }
3549 }
3550 return true;
3551 }
3552 }
3553
Jeff Brownfe508922011-01-18 15:10:10 -08003554 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003555 // most recent touch within the screen area. The idea is to filter out stray
3556 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003557 //
3558 // Problems we're trying to solve:
3559 //
3560 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3561 // virtual key area that is implemented by a separate touch panel and accidentally
3562 // triggers a virtual key.
3563 //
3564 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3565 // area and accidentally triggers a virtual key. This often happens when virtual keys
3566 // are layed out below the screen near to where the on screen keyboard's space bar
3567 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003568 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003569 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003570 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003571 return false;
3572}
3573
3574void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3575 int32_t keyEventAction, int32_t keyEventFlags) {
3576 int32_t keyCode = mCurrentVirtualKey.keyCode;
3577 int32_t scanCode = mCurrentVirtualKey.scanCode;
3578 nsecs_t downTime = mCurrentVirtualKey.downTime;
3579 int32_t metaState = mContext->getGlobalMetaState();
3580 policyFlags |= POLICY_FLAG_VIRTUAL;
3581
3582 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3583 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3584 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003585}
3586
Jeff Brown6d0fec22010-07-23 21:28:06 -07003587void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003588 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3589 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003590 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003591 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003592
3593 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003594 if (!currentIdBits.isEmpty()) {
3595 // No pointer id changes so this is a move event.
3596 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003597 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003598 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3599 AMOTION_EVENT_EDGE_FLAG_NONE,
3600 mCurrentCookedPointerData.pointerProperties,
3601 mCurrentCookedPointerData.pointerCoords,
3602 mCurrentCookedPointerData.idToIndex,
3603 currentIdBits, -1,
3604 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3605 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003606 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003607 // There may be pointers going up and pointers going down and pointers moving
3608 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003609 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3610 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003611 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003612 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003613
Jeff Brownace13b12011-03-09 17:39:48 -08003614 // Update last coordinates of pointers that have moved so that we observe the new
3615 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003616 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003617 mCurrentCookedPointerData.pointerProperties,
3618 mCurrentCookedPointerData.pointerCoords,
3619 mCurrentCookedPointerData.idToIndex,
3620 mLastCookedPointerData.pointerProperties,
3621 mLastCookedPointerData.pointerCoords,
3622 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003623 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003624 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003625 moveNeeded = true;
3626 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003627
Jeff Brownace13b12011-03-09 17:39:48 -08003628 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003629 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003630 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003631
Jeff Brown65fd2512011-08-18 11:20:58 -07003632 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003633 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003634 mLastCookedPointerData.pointerProperties,
3635 mLastCookedPointerData.pointerCoords,
3636 mLastCookedPointerData.idToIndex,
3637 dispatchedIdBits, upId,
3638 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003639 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003640 }
3641
Jeff Brownc3db8582010-10-20 15:33:38 -07003642 // Dispatch move events if any of the remaining pointers moved from their old locations.
3643 // Although applications receive new locations as part of individual pointer up
3644 // events, they do not generally handle them except when presented in a move event.
3645 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003646 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003647 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003648 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003649 mCurrentCookedPointerData.pointerProperties,
3650 mCurrentCookedPointerData.pointerCoords,
3651 mCurrentCookedPointerData.idToIndex,
3652 dispatchedIdBits, -1,
3653 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003654 }
3655
3656 // Dispatch pointer down events using the new pointer locations.
3657 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003658 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003659 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003660
Jeff Brownace13b12011-03-09 17:39:48 -08003661 if (dispatchedIdBits.count() == 1) {
3662 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003663 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003664 }
3665
Jeff Brown65fd2512011-08-18 11:20:58 -07003666 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003667 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003668 mCurrentCookedPointerData.pointerProperties,
3669 mCurrentCookedPointerData.pointerCoords,
3670 mCurrentCookedPointerData.idToIndex,
3671 dispatchedIdBits, downId,
3672 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003673 }
3674 }
Jeff Brownace13b12011-03-09 17:39:48 -08003675}
3676
Jeff Brownbe1aa822011-07-27 16:04:54 -07003677void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3678 if (mSentHoverEnter &&
3679 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3680 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3681 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003682 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003683 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3684 mLastCookedPointerData.pointerProperties,
3685 mLastCookedPointerData.pointerCoords,
3686 mLastCookedPointerData.idToIndex,
3687 mLastCookedPointerData.hoveringIdBits, -1,
3688 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3689 mSentHoverEnter = false;
3690 }
3691}
Jeff Brownace13b12011-03-09 17:39:48 -08003692
Jeff Brownbe1aa822011-07-27 16:04:54 -07003693void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3694 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3695 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3696 int32_t metaState = getContext()->getGlobalMetaState();
3697 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003698 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003699 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3700 mCurrentCookedPointerData.pointerProperties,
3701 mCurrentCookedPointerData.pointerCoords,
3702 mCurrentCookedPointerData.idToIndex,
3703 mCurrentCookedPointerData.hoveringIdBits, -1,
3704 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3705 mSentHoverEnter = true;
3706 }
Jeff Brownace13b12011-03-09 17:39:48 -08003707
Jeff Brown65fd2512011-08-18 11:20:58 -07003708 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003709 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3710 mCurrentCookedPointerData.pointerProperties,
3711 mCurrentCookedPointerData.pointerCoords,
3712 mCurrentCookedPointerData.idToIndex,
3713 mCurrentCookedPointerData.hoveringIdBits, -1,
3714 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3715 }
3716}
3717
3718void TouchInputMapper::cookPointerData() {
3719 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3720
3721 mCurrentCookedPointerData.clear();
3722 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3723 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3724 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3725
3726 // Walk through the the active pointers and map device coordinates onto
3727 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003728 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003729 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003730
Jeff Browna1f89ce2011-08-11 00:05:01 -07003731 // Size
3732 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3733 switch (mCalibration.sizeCalibration) {
3734 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3735 case Calibration::SIZE_CALIBRATION_DIAMETER:
3736 case Calibration::SIZE_CALIBRATION_AREA:
3737 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3738 touchMajor = in.touchMajor;
3739 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3740 toolMajor = in.toolMajor;
3741 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3742 size = mRawPointerAxes.touchMinor.valid
3743 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3744 } else if (mRawPointerAxes.touchMajor.valid) {
3745 toolMajor = touchMajor = in.touchMajor;
3746 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3747 ? in.touchMinor : in.touchMajor;
3748 size = mRawPointerAxes.touchMinor.valid
3749 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3750 } else if (mRawPointerAxes.toolMajor.valid) {
3751 touchMajor = toolMajor = in.toolMajor;
3752 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3753 ? in.toolMinor : in.toolMajor;
3754 size = mRawPointerAxes.toolMinor.valid
3755 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003756 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003757 LOG_ASSERT(false, "No touch or tool axes. "
3758 "Size calibration should have been resolved to NONE.");
3759 touchMajor = 0;
3760 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003761 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003762 toolMinor = 0;
3763 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003764 }
Jeff Brownace13b12011-03-09 17:39:48 -08003765
Jeff Browna1f89ce2011-08-11 00:05:01 -07003766 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3767 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3768 if (touchingCount > 1) {
3769 touchMajor /= touchingCount;
3770 touchMinor /= touchingCount;
3771 toolMajor /= touchingCount;
3772 toolMinor /= touchingCount;
3773 size /= touchingCount;
3774 }
3775 }
Jeff Brownace13b12011-03-09 17:39:48 -08003776
Jeff Browna1f89ce2011-08-11 00:05:01 -07003777 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3778 touchMajor *= mGeometricScale;
3779 touchMinor *= mGeometricScale;
3780 toolMajor *= mGeometricScale;
3781 toolMinor *= mGeometricScale;
3782 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3783 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003784 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003785 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3786 toolMinor = toolMajor;
3787 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3788 touchMinor = touchMajor;
3789 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003790 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003791
3792 mCalibration.applySizeScaleAndBias(&touchMajor);
3793 mCalibration.applySizeScaleAndBias(&touchMinor);
3794 mCalibration.applySizeScaleAndBias(&toolMajor);
3795 mCalibration.applySizeScaleAndBias(&toolMinor);
3796 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003797 break;
3798 default:
3799 touchMajor = 0;
3800 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003801 toolMajor = 0;
3802 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003803 size = 0;
3804 break;
3805 }
3806
Jeff Browna1f89ce2011-08-11 00:05:01 -07003807 // Pressure
3808 float pressure;
3809 switch (mCalibration.pressureCalibration) {
3810 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3811 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3812 pressure = in.pressure * mPressureScale;
3813 break;
3814 default:
3815 pressure = in.isHovering ? 0 : 1;
3816 break;
3817 }
3818
Jeff Brown65fd2512011-08-18 11:20:58 -07003819 // Tilt and Orientation
3820 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003821 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003822 if (mHaveTilt) {
3823 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3824 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3825 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3826 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3827 } else {
3828 tilt = 0;
3829
3830 switch (mCalibration.orientationCalibration) {
3831 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3832 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3833 break;
3834 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3835 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3836 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3837 if (c1 != 0 || c2 != 0) {
3838 orientation = atan2f(c1, c2) * 0.5f;
3839 float confidence = hypotf(c1, c2);
3840 float scale = 1.0f + confidence / 16.0f;
3841 touchMajor *= scale;
3842 touchMinor /= scale;
3843 toolMajor *= scale;
3844 toolMinor /= scale;
3845 } else {
3846 orientation = 0;
3847 }
3848 break;
3849 }
3850 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003851 orientation = 0;
3852 }
Jeff Brownace13b12011-03-09 17:39:48 -08003853 }
3854
Jeff Brown80fd47c2011-05-24 01:07:44 -07003855 // Distance
3856 float distance;
3857 switch (mCalibration.distanceCalibration) {
3858 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003859 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003860 break;
3861 default:
3862 distance = 0;
3863 }
3864
Jeff Brownace13b12011-03-09 17:39:48 -08003865 // X and Y
3866 // Adjust coords for surface orientation.
3867 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003868 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003869 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003870 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3871 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003872 orientation -= M_PI_2;
3873 if (orientation < - M_PI_2) {
3874 orientation += M_PI;
3875 }
3876 break;
3877 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003878 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3879 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003880 break;
3881 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003882 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3883 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003884 orientation += M_PI_2;
3885 if (orientation > M_PI_2) {
3886 orientation -= M_PI;
3887 }
3888 break;
3889 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003890 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3891 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003892 break;
3893 }
3894
3895 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003896 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003897 out.clear();
3898 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3899 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3900 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3901 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3902 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3903 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3904 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3905 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3906 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003907 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003908 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003909
3910 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003911 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3912 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003913 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003914 properties.id = id;
3915 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003916
Jeff Brownbe1aa822011-07-27 16:04:54 -07003917 // Write id index.
3918 mCurrentCookedPointerData.idToIndex[id] = i;
3919 }
Jeff Brownace13b12011-03-09 17:39:48 -08003920}
3921
Jeff Brown65fd2512011-08-18 11:20:58 -07003922void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3923 PointerUsage pointerUsage) {
3924 if (pointerUsage != mPointerUsage) {
3925 abortPointerUsage(when, policyFlags);
3926 mPointerUsage = pointerUsage;
3927 }
3928
3929 switch (mPointerUsage) {
3930 case POINTER_USAGE_GESTURES:
3931 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3932 break;
3933 case POINTER_USAGE_STYLUS:
3934 dispatchPointerStylus(when, policyFlags);
3935 break;
3936 case POINTER_USAGE_MOUSE:
3937 dispatchPointerMouse(when, policyFlags);
3938 break;
3939 default:
3940 break;
3941 }
3942}
3943
3944void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3945 switch (mPointerUsage) {
3946 case POINTER_USAGE_GESTURES:
3947 abortPointerGestures(when, policyFlags);
3948 break;
3949 case POINTER_USAGE_STYLUS:
3950 abortPointerStylus(when, policyFlags);
3951 break;
3952 case POINTER_USAGE_MOUSE:
3953 abortPointerMouse(when, policyFlags);
3954 break;
3955 default:
3956 break;
3957 }
3958
3959 mPointerUsage = POINTER_USAGE_NONE;
3960}
3961
Jeff Brown79ac9692011-04-19 21:20:10 -07003962void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3963 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003964 // Update current gesture coordinates.
3965 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003966 bool sendEvents = preparePointerGestures(when,
3967 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3968 if (!sendEvents) {
3969 return;
3970 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003971 if (finishPreviousGesture) {
3972 cancelPreviousGesture = false;
3973 }
Jeff Brownace13b12011-03-09 17:39:48 -08003974
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003975 // Update the pointer presentation and spots.
3976 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3977 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3978 if (finishPreviousGesture || cancelPreviousGesture) {
3979 mPointerController->clearSpots();
3980 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003981 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3982 mPointerGesture.currentGestureIdToIndex,
3983 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003984 } else {
3985 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3986 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003987
Jeff Brown538881e2011-05-25 18:23:38 -07003988 // Show or hide the pointer if needed.
3989 switch (mPointerGesture.currentGestureMode) {
3990 case PointerGesture::NEUTRAL:
3991 case PointerGesture::QUIET:
3992 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3993 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3994 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3995 // Remind the user of where the pointer is after finishing a gesture with spots.
3996 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3997 }
3998 break;
3999 case PointerGesture::TAP:
4000 case PointerGesture::TAP_DRAG:
4001 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4002 case PointerGesture::HOVER:
4003 case PointerGesture::PRESS:
4004 // Unfade the pointer when the current gesture manipulates the
4005 // area directly under the pointer.
4006 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4007 break;
4008 case PointerGesture::SWIPE:
4009 case PointerGesture::FREEFORM:
4010 // Fade the pointer when the current gesture manipulates a different
4011 // area and there are spots to guide the user experience.
4012 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4013 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4014 } else {
4015 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4016 }
4017 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004018 }
4019
Jeff Brownace13b12011-03-09 17:39:48 -08004020 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004021 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004022 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004023
4024 // Update last coordinates of pointers that have moved so that we observe the new
4025 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004026 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4027 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4028 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004029 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004030 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4031 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4032 bool moveNeeded = false;
4033 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004034 && !mPointerGesture.lastGestureIdBits.isEmpty()
4035 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004036 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4037 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004038 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004039 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004040 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004041 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4042 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004043 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004044 moveNeeded = true;
4045 }
Jeff Brownace13b12011-03-09 17:39:48 -08004046 }
4047
4048 // Send motion events for all pointers that went up or were canceled.
4049 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4050 if (!dispatchedGestureIdBits.isEmpty()) {
4051 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004052 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004053 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4054 AMOTION_EVENT_EDGE_FLAG_NONE,
4055 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004056 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4057 dispatchedGestureIdBits, -1,
4058 0, 0, mPointerGesture.downTime);
4059
4060 dispatchedGestureIdBits.clear();
4061 } else {
4062 BitSet32 upGestureIdBits;
4063 if (finishPreviousGesture) {
4064 upGestureIdBits = dispatchedGestureIdBits;
4065 } else {
4066 upGestureIdBits.value = dispatchedGestureIdBits.value
4067 & ~mPointerGesture.currentGestureIdBits.value;
4068 }
4069 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004070 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004071
Jeff Brown65fd2512011-08-18 11:20:58 -07004072 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004073 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004074 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4075 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004076 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4077 dispatchedGestureIdBits, id,
4078 0, 0, mPointerGesture.downTime);
4079
4080 dispatchedGestureIdBits.clearBit(id);
4081 }
4082 }
4083 }
4084
4085 // Send motion events for all pointers that moved.
4086 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004087 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004088 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4089 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004090 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4091 dispatchedGestureIdBits, -1,
4092 0, 0, mPointerGesture.downTime);
4093 }
4094
4095 // Send motion events for all pointers that went down.
4096 if (down) {
4097 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4098 & ~dispatchedGestureIdBits.value);
4099 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004100 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004101 dispatchedGestureIdBits.markBit(id);
4102
Jeff Brownace13b12011-03-09 17:39:48 -08004103 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004104 mPointerGesture.downTime = when;
4105 }
4106
Jeff Brown65fd2512011-08-18 11:20:58 -07004107 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004108 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004109 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004110 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4111 dispatchedGestureIdBits, id,
4112 0, 0, mPointerGesture.downTime);
4113 }
4114 }
4115
Jeff Brownace13b12011-03-09 17:39:48 -08004116 // Send motion events for hover.
4117 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004118 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004119 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4120 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4121 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004122 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4123 mPointerGesture.currentGestureIdBits, -1,
4124 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004125 } else if (dispatchedGestureIdBits.isEmpty()
4126 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4127 // Synthesize a hover move event after all pointers go up to indicate that
4128 // the pointer is hovering again even if the user is not currently touching
4129 // the touch pad. This ensures that a view will receive a fresh hover enter
4130 // event after a tap.
4131 float x, y;
4132 mPointerController->getPosition(&x, &y);
4133
4134 PointerProperties pointerProperties;
4135 pointerProperties.clear();
4136 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004137 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004138
4139 PointerCoords pointerCoords;
4140 pointerCoords.clear();
4141 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4142 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4143
Jeff Brown65fd2512011-08-18 11:20:58 -07004144 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004145 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4146 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4147 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004148 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004149 }
4150
4151 // Update state.
4152 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4153 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004154 mPointerGesture.lastGestureIdBits.clear();
4155 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004156 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4157 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004158 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004159 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004160 mPointerGesture.lastGestureProperties[index].copyFrom(
4161 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004162 mPointerGesture.lastGestureCoords[index].copyFrom(
4163 mPointerGesture.currentGestureCoords[index]);
4164 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004165 }
4166 }
4167}
4168
Jeff Brown65fd2512011-08-18 11:20:58 -07004169void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4170 // Cancel previously dispatches pointers.
4171 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4172 int32_t metaState = getContext()->getGlobalMetaState();
4173 int32_t buttonState = mCurrentButtonState;
4174 dispatchMotion(when, policyFlags, mSource,
4175 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4176 AMOTION_EVENT_EDGE_FLAG_NONE,
4177 mPointerGesture.lastGestureProperties,
4178 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4179 mPointerGesture.lastGestureIdBits, -1,
4180 0, 0, mPointerGesture.downTime);
4181 }
4182
4183 // Reset the current pointer gesture.
4184 mPointerGesture.reset();
4185 mPointerVelocityControl.reset();
4186
4187 // Remove any current spots.
4188 if (mPointerController != NULL) {
4189 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4190 mPointerController->clearSpots();
4191 }
4192}
4193
Jeff Brown79ac9692011-04-19 21:20:10 -07004194bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4195 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004196 *outCancelPreviousGesture = false;
4197 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004198
Jeff Brown79ac9692011-04-19 21:20:10 -07004199 // Handle TAP timeout.
4200 if (isTimeout) {
4201#if DEBUG_GESTURES
4202 LOGD("Gestures: Processing timeout");
4203#endif
4204
4205 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004206 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004207 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004208 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004209 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004210 } else {
4211 // The tap is finished.
4212#if DEBUG_GESTURES
4213 LOGD("Gestures: TAP finished");
4214#endif
4215 *outFinishPreviousGesture = true;
4216
4217 mPointerGesture.activeGestureId = -1;
4218 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4219 mPointerGesture.currentGestureIdBits.clear();
4220
Jeff Brown65fd2512011-08-18 11:20:58 -07004221 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004222 return true;
4223 }
4224 }
4225
4226 // We did not handle this timeout.
4227 return false;
4228 }
4229
Jeff Brown65fd2512011-08-18 11:20:58 -07004230 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4231 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4232
Jeff Brownace13b12011-03-09 17:39:48 -08004233 // Update the velocity tracker.
4234 {
4235 VelocityTracker::Position positions[MAX_POINTERS];
4236 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004237 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004238 uint32_t id = idBits.clearFirstMarkedBit();
4239 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004240 positions[count].x = pointer.x * mPointerXMovementScale;
4241 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004242 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004243 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004244 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004245 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004246
Jeff Brownace13b12011-03-09 17:39:48 -08004247 // Pick a new active touch id if needed.
4248 // Choose an arbitrary pointer that just went down, if there is one.
4249 // Otherwise choose an arbitrary remaining pointer.
4250 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004251 // We keep the same active touch id for as long as possible.
4252 bool activeTouchChanged = false;
4253 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4254 int32_t activeTouchId = lastActiveTouchId;
4255 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004256 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004257 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004258 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004259 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004260 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004261 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004262 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004263 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004264 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004265 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004266 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004267 } else {
4268 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004269 }
4270 }
4271
4272 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004273 bool isQuietTime = false;
4274 if (activeTouchId < 0) {
4275 mPointerGesture.resetQuietTime();
4276 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004277 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004278 if (!isQuietTime) {
4279 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4280 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4281 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004282 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004283 // Enter quiet time when exiting swipe or freeform state.
4284 // This is to prevent accidentally entering the hover state and flinging the
4285 // pointer when finishing a swipe and there is still one pointer left onscreen.
4286 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004287 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004288 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004289 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004290 // Enter quiet time when releasing the button and there are still two or more
4291 // fingers down. This may indicate that one finger was used to press the button
4292 // but it has not gone up yet.
4293 isQuietTime = true;
4294 }
4295 if (isQuietTime) {
4296 mPointerGesture.quietTime = when;
4297 }
Jeff Brownace13b12011-03-09 17:39:48 -08004298 }
4299 }
4300
4301 // Switch states based on button and pointer state.
4302 if (isQuietTime) {
4303 // Case 1: Quiet time. (QUIET)
4304#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004305 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004306 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004307#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004308 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4309 *outFinishPreviousGesture = true;
4310 }
Jeff Brownace13b12011-03-09 17:39:48 -08004311
4312 mPointerGesture.activeGestureId = -1;
4313 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004314 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004315
Jeff Brown65fd2512011-08-18 11:20:58 -07004316 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004317 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004318 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004319 // The pointer follows the active touch point.
4320 // Emit DOWN, MOVE, UP events at the pointer location.
4321 //
4322 // Only the active touch matters; other fingers are ignored. This policy helps
4323 // to handle the case where the user places a second finger on the touch pad
4324 // to apply the necessary force to depress an integrated button below the surface.
4325 // We don't want the second finger to be delivered to applications.
4326 //
4327 // For this to work well, we need to make sure to track the pointer that is really
4328 // active. If the user first puts one finger down to click then adds another
4329 // finger to drag then the active pointer should switch to the finger that is
4330 // being dragged.
4331#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004332 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004333 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004334#endif
4335 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004336 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004337 *outFinishPreviousGesture = true;
4338 mPointerGesture.activeGestureId = 0;
4339 }
4340
4341 // Switch pointers if needed.
4342 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004343 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004344 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004345 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004346 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004347 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004348 float vx, vy;
4349 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4350 float speed = hypotf(vx, vy);
4351 if (speed > bestSpeed) {
4352 bestId = id;
4353 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004354 }
Jeff Brown8d608662010-08-30 03:02:23 -07004355 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004356 }
4357 if (bestId >= 0 && bestId != activeTouchId) {
4358 mPointerGesture.activeTouchId = activeTouchId = bestId;
4359 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004360#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004361 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4362 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004363#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004364 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004365 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004366
Jeff Brown65fd2512011-08-18 11:20:58 -07004367 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004368 const RawPointerData::Pointer& currentPointer =
4369 mCurrentRawPointerData.pointerForId(activeTouchId);
4370 const RawPointerData::Pointer& lastPointer =
4371 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004372 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4373 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004374
Jeff Brownbe1aa822011-07-27 16:04:54 -07004375 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004376 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004377
4378 // Move the pointer using a relative motion.
4379 // When using spots, the click will occur at the position of the anchor
4380 // spot and all other spots will move there.
4381 mPointerController->move(deltaX, deltaY);
4382 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004383 mPointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004384 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004385
Jeff Brownace13b12011-03-09 17:39:48 -08004386 float x, y;
4387 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004388
Jeff Brown79ac9692011-04-19 21:20:10 -07004389 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004390 mPointerGesture.currentGestureIdBits.clear();
4391 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4392 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004393 mPointerGesture.currentGestureProperties[0].clear();
4394 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004395 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004396 mPointerGesture.currentGestureCoords[0].clear();
4397 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4398 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4399 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004400 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004401 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004402 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4403 *outFinishPreviousGesture = true;
4404 }
Jeff Brownace13b12011-03-09 17:39:48 -08004405
Jeff Brown79ac9692011-04-19 21:20:10 -07004406 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004407 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004408 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004409 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4410 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004411 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004412 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004413 float x, y;
4414 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004415 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4416 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004417#if DEBUG_GESTURES
4418 LOGD("Gestures: TAP");
4419#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004420
4421 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004422 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004423 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004424
Jeff Brownace13b12011-03-09 17:39:48 -08004425 mPointerGesture.activeGestureId = 0;
4426 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004427 mPointerGesture.currentGestureIdBits.clear();
4428 mPointerGesture.currentGestureIdBits.markBit(
4429 mPointerGesture.activeGestureId);
4430 mPointerGesture.currentGestureIdToIndex[
4431 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004432 mPointerGesture.currentGestureProperties[0].clear();
4433 mPointerGesture.currentGestureProperties[0].id =
4434 mPointerGesture.activeGestureId;
4435 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004436 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004437 mPointerGesture.currentGestureCoords[0].clear();
4438 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004439 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004440 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004441 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004442 mPointerGesture.currentGestureCoords[0].setAxisValue(
4443 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004444
Jeff Brownace13b12011-03-09 17:39:48 -08004445 tapped = true;
4446 } else {
4447#if DEBUG_GESTURES
4448 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004449 x - mPointerGesture.tapX,
4450 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004451#endif
4452 }
4453 } else {
4454#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004455 LOGD("Gestures: Not a TAP, %0.3fms since down",
4456 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004457#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004458 }
Jeff Brownace13b12011-03-09 17:39:48 -08004459 }
Jeff Brown2352b972011-04-12 22:39:53 -07004460
Jeff Brown65fd2512011-08-18 11:20:58 -07004461 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004462
Jeff Brownace13b12011-03-09 17:39:48 -08004463 if (!tapped) {
4464#if DEBUG_GESTURES
4465 LOGD("Gestures: NEUTRAL");
4466#endif
4467 mPointerGesture.activeGestureId = -1;
4468 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004469 mPointerGesture.currentGestureIdBits.clear();
4470 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004471 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004472 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004473 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004474 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4475 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004476 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004477
Jeff Brown79ac9692011-04-19 21:20:10 -07004478 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4479 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004480 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004481 float x, y;
4482 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004483 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4484 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004485 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4486 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004487#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004488 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4489 x - mPointerGesture.tapX,
4490 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004491#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004492 }
4493 } else {
4494#if DEBUG_GESTURES
4495 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4496 (when - mPointerGesture.tapUpTime) * 0.000001f);
4497#endif
4498 }
4499 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4500 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4501 }
Jeff Brownace13b12011-03-09 17:39:48 -08004502
Jeff Brown65fd2512011-08-18 11:20:58 -07004503 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004504 const RawPointerData::Pointer& currentPointer =
4505 mCurrentRawPointerData.pointerForId(activeTouchId);
4506 const RawPointerData::Pointer& lastPointer =
4507 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004508 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004509 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004510 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004511 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004512
Jeff Brownbe1aa822011-07-27 16:04:54 -07004513 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004514 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004515
Jeff Brown2352b972011-04-12 22:39:53 -07004516 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004517 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004518 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004519 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004520 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004521 }
4522
Jeff Brown79ac9692011-04-19 21:20:10 -07004523 bool down;
4524 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4525#if DEBUG_GESTURES
4526 LOGD("Gestures: TAP_DRAG");
4527#endif
4528 down = true;
4529 } else {
4530#if DEBUG_GESTURES
4531 LOGD("Gestures: HOVER");
4532#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004533 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4534 *outFinishPreviousGesture = true;
4535 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004536 mPointerGesture.activeGestureId = 0;
4537 down = false;
4538 }
Jeff Brownace13b12011-03-09 17:39:48 -08004539
4540 float x, y;
4541 mPointerController->getPosition(&x, &y);
4542
Jeff Brownace13b12011-03-09 17:39:48 -08004543 mPointerGesture.currentGestureIdBits.clear();
4544 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4545 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004546 mPointerGesture.currentGestureProperties[0].clear();
4547 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4548 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004549 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004550 mPointerGesture.currentGestureCoords[0].clear();
4551 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4552 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004553 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4554 down ? 1.0f : 0.0f);
4555
Jeff Brown65fd2512011-08-18 11:20:58 -07004556 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004557 mPointerGesture.resetTap();
4558 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004559 mPointerGesture.tapX = x;
4560 mPointerGesture.tapY = y;
4561 }
Jeff Brownace13b12011-03-09 17:39:48 -08004562 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004563 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4564 // We need to provide feedback for each finger that goes down so we cannot wait
4565 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004566 //
Jeff Brown2352b972011-04-12 22:39:53 -07004567 // The ambiguous case is deciding what to do when there are two fingers down but they
4568 // have not moved enough to determine whether they are part of a drag or part of a
4569 // freeform gesture, or just a press or long-press at the pointer location.
4570 //
4571 // When there are two fingers we start with the PRESS hypothesis and we generate a
4572 // down at the pointer location.
4573 //
4574 // When the two fingers move enough or when additional fingers are added, we make
4575 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004576 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004577
Jeff Brown214eaf42011-05-26 19:17:02 -07004578 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004579 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004580 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004581 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4582 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004583 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004584 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004585 // Additional pointers have gone down but not yet settled.
4586 // Reset the gesture.
4587#if DEBUG_GESTURES
4588 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004589 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004590 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004591 * 0.000001f);
4592#endif
4593 *outCancelPreviousGesture = true;
4594 } else {
4595 // Continue previous gesture.
4596 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4597 }
4598
4599 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004600 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4601 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004602 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004603 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004604
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004605 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004606#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004607 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4608 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004609 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004610 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004611#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004612 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4613 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004614 &mPointerGesture.referenceTouchY);
4615 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4616 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004617 }
Jeff Brownace13b12011-03-09 17:39:48 -08004618
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004619 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004620 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004621 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4622 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004623 mPointerGesture.referenceDeltas[id].dx = 0;
4624 mPointerGesture.referenceDeltas[id].dy = 0;
4625 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004626 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004627
4628 // Add delta for all fingers and calculate a common movement delta.
4629 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004630 BitSet32 commonIdBits(mLastFingerIdBits.value
4631 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004632 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4633 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004634 uint32_t id = idBits.clearFirstMarkedBit();
4635 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4636 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004637 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4638 delta.dx += cpd.x - lpd.x;
4639 delta.dy += cpd.y - lpd.y;
4640
4641 if (first) {
4642 commonDeltaX = delta.dx;
4643 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004644 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004645 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4646 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4647 }
4648 }
Jeff Brownace13b12011-03-09 17:39:48 -08004649
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004650 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4651 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4652 float dist[MAX_POINTER_ID + 1];
4653 int32_t distOverThreshold = 0;
4654 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004655 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004656 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004657 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4658 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004659 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004660 distOverThreshold += 1;
4661 }
4662 }
4663
4664 // Only transition when at least two pointers have moved further than
4665 // the minimum distance threshold.
4666 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004667 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004668 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004669#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004670 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004671 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004672#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004673 *outCancelPreviousGesture = true;
4674 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4675 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004676 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004677 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004678 uint32_t id1 = idBits.clearFirstMarkedBit();
4679 uint32_t id2 = idBits.firstMarkedBit();
4680 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4681 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4682 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4683 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4684 // There are two pointers but they are too far apart for a SWIPE,
4685 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004686#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004687 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4688 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004689#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004690 *outCancelPreviousGesture = true;
4691 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4692 } else {
4693 // There are two pointers. Wait for both pointers to start moving
4694 // before deciding whether this is a SWIPE or FREEFORM gesture.
4695 float dist1 = dist[id1];
4696 float dist2 = dist[id2];
4697 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4698 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4699 // Calculate the dot product of the displacement vectors.
4700 // When the vectors are oriented in approximately the same direction,
4701 // the angle betweeen them is near zero and the cosine of the angle
4702 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4703 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4704 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004705 float dx1 = delta1.dx * mPointerXZoomScale;
4706 float dy1 = delta1.dy * mPointerYZoomScale;
4707 float dx2 = delta2.dx * mPointerXZoomScale;
4708 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004709 float dot = dx1 * dx2 + dy1 * dy2;
4710 float cosine = dot / (dist1 * dist2); // denominator always > 0
4711 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4712 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004713#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004714 LOGD("Gestures: PRESS transitioned to SWIPE, "
4715 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4716 "cosine %0.3f >= %0.3f",
4717 dist1, mConfig.pointerGestureMultitouchMinDistance,
4718 dist2, mConfig.pointerGestureMultitouchMinDistance,
4719 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004720#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004721 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4722 } else {
4723 // Pointers are moving in different directions. Switch to FREEFORM.
4724#if DEBUG_GESTURES
4725 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4726 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4727 "cosine %0.3f < %0.3f",
4728 dist1, mConfig.pointerGestureMultitouchMinDistance,
4729 dist2, mConfig.pointerGestureMultitouchMinDistance,
4730 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4731#endif
4732 *outCancelPreviousGesture = true;
4733 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4734 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004735 }
Jeff Brownace13b12011-03-09 17:39:48 -08004736 }
4737 }
Jeff Brownace13b12011-03-09 17:39:48 -08004738 }
4739 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004740 // Switch from SWIPE to FREEFORM if additional pointers go down.
4741 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004742 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004743#if DEBUG_GESTURES
4744 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004745 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004746#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004747 *outCancelPreviousGesture = true;
4748 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004749 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004750 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004751
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004752 // Move the reference points based on the overall group motion of the fingers
4753 // except in PRESS mode while waiting for a transition to occur.
4754 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4755 && (commonDeltaX || commonDeltaY)) {
4756 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004757 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004758 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004759 delta.dx = 0;
4760 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004761 }
4762
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004763 mPointerGesture.referenceTouchX += commonDeltaX;
4764 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004765
Jeff Brown65fd2512011-08-18 11:20:58 -07004766 commonDeltaX *= mPointerXMovementScale;
4767 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004768
Jeff Brownbe1aa822011-07-27 16:04:54 -07004769 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004770 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004771
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004772 mPointerGesture.referenceGestureX += commonDeltaX;
4773 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004774 }
4775
4776 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004777 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4778 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4779 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004780#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004781 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004782 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004783 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004784#endif
4785 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4786
4787 mPointerGesture.currentGestureIdBits.clear();
4788 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4789 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004790 mPointerGesture.currentGestureProperties[0].clear();
4791 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4792 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004793 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004794 mPointerGesture.currentGestureCoords[0].clear();
4795 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4796 mPointerGesture.referenceGestureX);
4797 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4798 mPointerGesture.referenceGestureY);
4799 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004800 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4801 // FREEFORM mode.
4802#if DEBUG_GESTURES
4803 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4804 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004805 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004806#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004807 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004808
Jeff Brownace13b12011-03-09 17:39:48 -08004809 mPointerGesture.currentGestureIdBits.clear();
4810
4811 BitSet32 mappedTouchIdBits;
4812 BitSet32 usedGestureIdBits;
4813 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4814 // Initially, assign the active gesture id to the active touch point
4815 // if there is one. No other touch id bits are mapped yet.
4816 if (!*outCancelPreviousGesture) {
4817 mappedTouchIdBits.markBit(activeTouchId);
4818 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4819 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4820 mPointerGesture.activeGestureId;
4821 } else {
4822 mPointerGesture.activeGestureId = -1;
4823 }
4824 } else {
4825 // Otherwise, assume we mapped all touches from the previous frame.
4826 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004827 mappedTouchIdBits.value = mLastFingerIdBits.value
4828 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004829 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4830
4831 // Check whether we need to choose a new active gesture id because the
4832 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004833 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4834 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004835 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004836 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004837 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4838 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4839 mPointerGesture.activeGestureId = -1;
4840 break;
4841 }
4842 }
4843 }
4844
4845#if DEBUG_GESTURES
4846 LOGD("Gestures: FREEFORM follow up "
4847 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4848 "activeGestureId=%d",
4849 mappedTouchIdBits.value, usedGestureIdBits.value,
4850 mPointerGesture.activeGestureId);
4851#endif
4852
Jeff Brown65fd2512011-08-18 11:20:58 -07004853 BitSet32 idBits(mCurrentFingerIdBits);
4854 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004855 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004856 uint32_t gestureId;
4857 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004858 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004859 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4860#if DEBUG_GESTURES
4861 LOGD("Gestures: FREEFORM "
4862 "new mapping for touch id %d -> gesture id %d",
4863 touchId, gestureId);
4864#endif
4865 } else {
4866 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4867#if DEBUG_GESTURES
4868 LOGD("Gestures: FREEFORM "
4869 "existing mapping for touch id %d -> gesture id %d",
4870 touchId, gestureId);
4871#endif
4872 }
4873 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4874 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4875
Jeff Brownbe1aa822011-07-27 16:04:54 -07004876 const RawPointerData::Pointer& pointer =
4877 mCurrentRawPointerData.pointerForId(touchId);
4878 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004879 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004880 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004881 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004882 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004883
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004884 mPointerGesture.currentGestureProperties[i].clear();
4885 mPointerGesture.currentGestureProperties[i].id = gestureId;
4886 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004887 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004888 mPointerGesture.currentGestureCoords[i].clear();
4889 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004890 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004891 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004892 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004893 mPointerGesture.currentGestureCoords[i].setAxisValue(
4894 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4895 }
4896
4897 if (mPointerGesture.activeGestureId < 0) {
4898 mPointerGesture.activeGestureId =
4899 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4900#if DEBUG_GESTURES
4901 LOGD("Gestures: FREEFORM new "
4902 "activeGestureId=%d", mPointerGesture.activeGestureId);
4903#endif
4904 }
Jeff Brown2352b972011-04-12 22:39:53 -07004905 }
Jeff Brownace13b12011-03-09 17:39:48 -08004906 }
4907
Jeff Brownbe1aa822011-07-27 16:04:54 -07004908 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004909
Jeff Brownace13b12011-03-09 17:39:48 -08004910#if DEBUG_GESTURES
4911 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004912 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4913 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004914 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004915 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4916 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004917 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004918 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004919 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004920 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004921 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004922 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4923 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4924 id, index, properties.toolType,
4925 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004926 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4927 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4928 }
4929 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004930 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004931 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004932 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004933 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004934 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4935 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4936 id, index, properties.toolType,
4937 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004938 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4939 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4940 }
4941#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004942 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004943}
4944
Jeff Brown65fd2512011-08-18 11:20:58 -07004945void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4946 mPointerSimple.currentCoords.clear();
4947 mPointerSimple.currentProperties.clear();
4948
4949 bool down, hovering;
4950 if (!mCurrentStylusIdBits.isEmpty()) {
4951 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4952 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4953 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4954 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4955 mPointerController->setPosition(x, y);
4956
4957 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4958 down = !hovering;
4959
4960 mPointerController->getPosition(&x, &y);
4961 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4962 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4963 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4964 mPointerSimple.currentProperties.id = 0;
4965 mPointerSimple.currentProperties.toolType =
4966 mCurrentCookedPointerData.pointerProperties[index].toolType;
4967 } else {
4968 down = false;
4969 hovering = false;
4970 }
4971
4972 dispatchPointerSimple(when, policyFlags, down, hovering);
4973}
4974
4975void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4976 abortPointerSimple(when, policyFlags);
4977}
4978
4979void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4980 mPointerSimple.currentCoords.clear();
4981 mPointerSimple.currentProperties.clear();
4982
4983 bool down, hovering;
4984 if (!mCurrentMouseIdBits.isEmpty()) {
4985 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4986 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4987 if (mLastMouseIdBits.hasBit(id)) {
4988 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4989 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4990 - mLastRawPointerData.pointers[lastIndex].x)
4991 * mPointerXMovementScale;
4992 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4993 - mLastRawPointerData.pointers[lastIndex].y)
4994 * mPointerYMovementScale;
4995
4996 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4997 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4998
4999 mPointerController->move(deltaX, deltaY);
5000 } else {
5001 mPointerVelocityControl.reset();
5002 }
5003
5004 down = isPointerDown(mCurrentButtonState);
5005 hovering = !down;
5006
5007 float x, y;
5008 mPointerController->getPosition(&x, &y);
5009 mPointerSimple.currentCoords.copyFrom(
5010 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5011 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5012 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5013 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5014 hovering ? 0.0f : 1.0f);
5015 mPointerSimple.currentProperties.id = 0;
5016 mPointerSimple.currentProperties.toolType =
5017 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5018 } else {
5019 mPointerVelocityControl.reset();
5020
5021 down = false;
5022 hovering = false;
5023 }
5024
5025 dispatchPointerSimple(when, policyFlags, down, hovering);
5026}
5027
5028void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5029 abortPointerSimple(when, policyFlags);
5030
5031 mPointerVelocityControl.reset();
5032}
5033
5034void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5035 bool down, bool hovering) {
5036 int32_t metaState = getContext()->getGlobalMetaState();
5037
5038 if (mPointerController != NULL) {
5039 if (down || hovering) {
5040 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5041 mPointerController->clearSpots();
5042 mPointerController->setButtonState(mCurrentButtonState);
5043 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5044 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5045 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5046 }
5047 }
5048
5049 if (mPointerSimple.down && !down) {
5050 mPointerSimple.down = false;
5051
5052 // Send up.
5053 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5054 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5055 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5056 mOrientedXPrecision, mOrientedYPrecision,
5057 mPointerSimple.downTime);
5058 getListener()->notifyMotion(&args);
5059 }
5060
5061 if (mPointerSimple.hovering && !hovering) {
5062 mPointerSimple.hovering = false;
5063
5064 // Send hover exit.
5065 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5066 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5067 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5068 mOrientedXPrecision, mOrientedYPrecision,
5069 mPointerSimple.downTime);
5070 getListener()->notifyMotion(&args);
5071 }
5072
5073 if (down) {
5074 if (!mPointerSimple.down) {
5075 mPointerSimple.down = true;
5076 mPointerSimple.downTime = when;
5077
5078 // Send down.
5079 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5080 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5081 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5082 mOrientedXPrecision, mOrientedYPrecision,
5083 mPointerSimple.downTime);
5084 getListener()->notifyMotion(&args);
5085 }
5086
5087 // Send move.
5088 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5089 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5090 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5091 mOrientedXPrecision, mOrientedYPrecision,
5092 mPointerSimple.downTime);
5093 getListener()->notifyMotion(&args);
5094 }
5095
5096 if (hovering) {
5097 if (!mPointerSimple.hovering) {
5098 mPointerSimple.hovering = true;
5099
5100 // Send hover enter.
5101 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5102 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5103 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5104 mOrientedXPrecision, mOrientedYPrecision,
5105 mPointerSimple.downTime);
5106 getListener()->notifyMotion(&args);
5107 }
5108
5109 // Send hover move.
5110 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5111 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5112 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5113 mOrientedXPrecision, mOrientedYPrecision,
5114 mPointerSimple.downTime);
5115 getListener()->notifyMotion(&args);
5116 }
5117
5118 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5119 float vscroll = mCurrentRawVScroll;
5120 float hscroll = mCurrentRawHScroll;
5121 mWheelYVelocityControl.move(when, NULL, &vscroll);
5122 mWheelXVelocityControl.move(when, &hscroll, NULL);
5123
5124 // Send scroll.
5125 PointerCoords pointerCoords;
5126 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5127 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5128 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5129
5130 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5131 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5132 1, &mPointerSimple.currentProperties, &pointerCoords,
5133 mOrientedXPrecision, mOrientedYPrecision,
5134 mPointerSimple.downTime);
5135 getListener()->notifyMotion(&args);
5136 }
5137
5138 // Save state.
5139 if (down || hovering) {
5140 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5141 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5142 } else {
5143 mPointerSimple.reset();
5144 }
5145}
5146
5147void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5148 mPointerSimple.currentCoords.clear();
5149 mPointerSimple.currentProperties.clear();
5150
5151 dispatchPointerSimple(when, policyFlags, false, false);
5152}
5153
Jeff Brownace13b12011-03-09 17:39:48 -08005154void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005155 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5156 const PointerProperties* properties, const PointerCoords* coords,
5157 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005158 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5159 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005160 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005161 uint32_t pointerCount = 0;
5162 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005163 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005164 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005165 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005166 pointerCoords[pointerCount].copyFrom(coords[index]);
5167
5168 if (changedId >= 0 && id == uint32_t(changedId)) {
5169 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5170 }
5171
5172 pointerCount += 1;
5173 }
5174
Jeff Brownb6110c22011-04-01 16:15:13 -07005175 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005176
5177 if (changedId >= 0 && pointerCount == 1) {
5178 // Replace initial down and final up action.
5179 // We can compare the action without masking off the changed pointer index
5180 // because we know the index is 0.
5181 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5182 action = AMOTION_EVENT_ACTION_DOWN;
5183 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5184 action = AMOTION_EVENT_ACTION_UP;
5185 } else {
5186 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005187 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005188 }
5189 }
5190
Jeff Brownbe1aa822011-07-27 16:04:54 -07005191 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005192 action, flags, metaState, buttonState, edgeFlags,
5193 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005194 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005195}
5196
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005197bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005198 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005199 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5200 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005201 bool changed = false;
5202 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005203 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005204 uint32_t inIndex = inIdToIndex[id];
5205 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005206
5207 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005208 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005209 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005210 PointerCoords& curOutCoords = outCoords[outIndex];
5211
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005212 if (curInProperties != curOutProperties) {
5213 curOutProperties.copyFrom(curInProperties);
5214 changed = true;
5215 }
5216
Jeff Brownace13b12011-03-09 17:39:48 -08005217 if (curInCoords != curOutCoords) {
5218 curOutCoords.copyFrom(curInCoords);
5219 changed = true;
5220 }
5221 }
5222 return changed;
5223}
5224
5225void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005226 if (mPointerController != NULL) {
5227 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5228 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005229}
5230
Jeff Brownbe1aa822011-07-27 16:04:54 -07005231bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5232 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5233 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005234}
5235
Jeff Brownbe1aa822011-07-27 16:04:54 -07005236const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005237 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005238 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005239 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005240 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005241
5242#if DEBUG_VIRTUAL_KEYS
5243 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5244 "left=%d, top=%d, right=%d, bottom=%d",
5245 x, y,
5246 virtualKey.keyCode, virtualKey.scanCode,
5247 virtualKey.hitLeft, virtualKey.hitTop,
5248 virtualKey.hitRight, virtualKey.hitBottom);
5249#endif
5250
5251 if (virtualKey.isHit(x, y)) {
5252 return & virtualKey;
5253 }
5254 }
5255
5256 return NULL;
5257}
5258
Jeff Brownbe1aa822011-07-27 16:04:54 -07005259void TouchInputMapper::assignPointerIds() {
5260 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5261 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5262
5263 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005264
5265 if (currentPointerCount == 0) {
5266 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005267 return;
5268 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005269
Jeff Brownbe1aa822011-07-27 16:04:54 -07005270 if (lastPointerCount == 0) {
5271 // All pointers are new.
5272 for (uint32_t i = 0; i < currentPointerCount; i++) {
5273 uint32_t id = i;
5274 mCurrentRawPointerData.pointers[i].id = id;
5275 mCurrentRawPointerData.idToIndex[id] = i;
5276 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5277 }
5278 return;
5279 }
5280
5281 if (currentPointerCount == 1 && lastPointerCount == 1
5282 && mCurrentRawPointerData.pointers[0].toolType
5283 == mLastRawPointerData.pointers[0].toolType) {
5284 // Only one pointer and no change in count so it must have the same id as before.
5285 uint32_t id = mLastRawPointerData.pointers[0].id;
5286 mCurrentRawPointerData.pointers[0].id = id;
5287 mCurrentRawPointerData.idToIndex[id] = 0;
5288 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5289 return;
5290 }
5291
5292 // General case.
5293 // We build a heap of squared euclidean distances between current and last pointers
5294 // associated with the current and last pointer indices. Then, we find the best
5295 // match (by distance) for each current pointer.
5296 // The pointers must have the same tool type but it is possible for them to
5297 // transition from hovering to touching or vice-versa while retaining the same id.
5298 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5299
5300 uint32_t heapSize = 0;
5301 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5302 currentPointerIndex++) {
5303 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5304 lastPointerIndex++) {
5305 const RawPointerData::Pointer& currentPointer =
5306 mCurrentRawPointerData.pointers[currentPointerIndex];
5307 const RawPointerData::Pointer& lastPointer =
5308 mLastRawPointerData.pointers[lastPointerIndex];
5309 if (currentPointer.toolType == lastPointer.toolType) {
5310 int64_t deltaX = currentPointer.x - lastPointer.x;
5311 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005312
5313 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5314
5315 // Insert new element into the heap (sift up).
5316 heap[heapSize].currentPointerIndex = currentPointerIndex;
5317 heap[heapSize].lastPointerIndex = lastPointerIndex;
5318 heap[heapSize].distance = distance;
5319 heapSize += 1;
5320 }
5321 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005322 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005323
Jeff Brownbe1aa822011-07-27 16:04:54 -07005324 // Heapify
5325 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5326 startIndex -= 1;
5327 for (uint32_t parentIndex = startIndex; ;) {
5328 uint32_t childIndex = parentIndex * 2 + 1;
5329 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005330 break;
5331 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005332
5333 if (childIndex + 1 < heapSize
5334 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5335 childIndex += 1;
5336 }
5337
5338 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5339 break;
5340 }
5341
5342 swap(heap[parentIndex], heap[childIndex]);
5343 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005344 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005345 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005346
5347#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005348 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5349 for (size_t i = 0; i < heapSize; i++) {
5350 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5351 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5352 heap[i].distance);
5353 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005354#endif
5355
Jeff Brownbe1aa822011-07-27 16:04:54 -07005356 // Pull matches out by increasing order of distance.
5357 // To avoid reassigning pointers that have already been matched, the loop keeps track
5358 // of which last and current pointers have been matched using the matchedXXXBits variables.
5359 // It also tracks the used pointer id bits.
5360 BitSet32 matchedLastBits(0);
5361 BitSet32 matchedCurrentBits(0);
5362 BitSet32 usedIdBits(0);
5363 bool first = true;
5364 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5365 while (heapSize > 0) {
5366 if (first) {
5367 // The first time through the loop, we just consume the root element of
5368 // the heap (the one with smallest distance).
5369 first = false;
5370 } else {
5371 // Previous iterations consumed the root element of the heap.
5372 // Pop root element off of the heap (sift down).
5373 heap[0] = heap[heapSize];
5374 for (uint32_t parentIndex = 0; ;) {
5375 uint32_t childIndex = parentIndex * 2 + 1;
5376 if (childIndex >= heapSize) {
5377 break;
5378 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005379
Jeff Brownbe1aa822011-07-27 16:04:54 -07005380 if (childIndex + 1 < heapSize
5381 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5382 childIndex += 1;
5383 }
5384
5385 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5386 break;
5387 }
5388
5389 swap(heap[parentIndex], heap[childIndex]);
5390 parentIndex = childIndex;
5391 }
5392
5393#if DEBUG_POINTER_ASSIGNMENT
5394 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5395 for (size_t i = 0; i < heapSize; i++) {
5396 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5397 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5398 heap[i].distance);
5399 }
5400#endif
5401 }
5402
5403 heapSize -= 1;
5404
5405 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5406 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5407
5408 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5409 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5410
5411 matchedCurrentBits.markBit(currentPointerIndex);
5412 matchedLastBits.markBit(lastPointerIndex);
5413
5414 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5415 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5416 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5417 mCurrentRawPointerData.markIdBit(id,
5418 mCurrentRawPointerData.isHovering(currentPointerIndex));
5419 usedIdBits.markBit(id);
5420
5421#if DEBUG_POINTER_ASSIGNMENT
5422 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5423 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5424#endif
5425 break;
5426 }
5427 }
5428
5429 // Assign fresh ids to pointers that were not matched in the process.
5430 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5431 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5432 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5433
5434 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5435 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5436 mCurrentRawPointerData.markIdBit(id,
5437 mCurrentRawPointerData.isHovering(currentPointerIndex));
5438
5439#if DEBUG_POINTER_ASSIGNMENT
5440 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5441 currentPointerIndex, id);
5442#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005443 }
5444}
5445
Jeff Brown6d0fec22010-07-23 21:28:06 -07005446int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005447 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5448 return AKEY_STATE_VIRTUAL;
5449 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005450
Jeff Brownbe1aa822011-07-27 16:04:54 -07005451 size_t numVirtualKeys = mVirtualKeys.size();
5452 for (size_t i = 0; i < numVirtualKeys; i++) {
5453 const VirtualKey& virtualKey = mVirtualKeys[i];
5454 if (virtualKey.keyCode == keyCode) {
5455 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005456 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005457 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005458
5459 return AKEY_STATE_UNKNOWN;
5460}
5461
5462int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005463 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5464 return AKEY_STATE_VIRTUAL;
5465 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005466
Jeff Brownbe1aa822011-07-27 16:04:54 -07005467 size_t numVirtualKeys = mVirtualKeys.size();
5468 for (size_t i = 0; i < numVirtualKeys; i++) {
5469 const VirtualKey& virtualKey = mVirtualKeys[i];
5470 if (virtualKey.scanCode == scanCode) {
5471 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005472 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005473 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005474
5475 return AKEY_STATE_UNKNOWN;
5476}
5477
5478bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5479 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005480 size_t numVirtualKeys = mVirtualKeys.size();
5481 for (size_t i = 0; i < numVirtualKeys; i++) {
5482 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005483
Jeff Brownbe1aa822011-07-27 16:04:54 -07005484 for (size_t i = 0; i < numCodes; i++) {
5485 if (virtualKey.keyCode == keyCodes[i]) {
5486 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005487 }
5488 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005489 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005490
5491 return true;
5492}
5493
5494
5495// --- SingleTouchInputMapper ---
5496
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005497SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5498 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005499}
5500
5501SingleTouchInputMapper::~SingleTouchInputMapper() {
5502}
5503
Jeff Brown65fd2512011-08-18 11:20:58 -07005504void SingleTouchInputMapper::reset(nsecs_t when) {
5505 mSingleTouchMotionAccumulator.reset(getDevice());
5506
5507 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005508}
5509
Jeff Brown6d0fec22010-07-23 21:28:06 -07005510void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005511 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005512
Jeff Brown65fd2512011-08-18 11:20:58 -07005513 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005514}
5515
Jeff Brown65fd2512011-08-18 11:20:58 -07005516void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005517 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005518 mCurrentRawPointerData.pointerCount = 1;
5519 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005520
Jeff Brown65fd2512011-08-18 11:20:58 -07005521 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5522 && (mTouchButtonAccumulator.isHovering()
5523 || (mRawPointerAxes.pressure.valid
5524 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005525 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005526
Jeff Brownbe1aa822011-07-27 16:04:54 -07005527 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005528 outPointer.id = 0;
5529 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5530 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5531 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5532 outPointer.touchMajor = 0;
5533 outPointer.touchMinor = 0;
5534 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5535 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5536 outPointer.orientation = 0;
5537 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005538 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5539 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005540 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5541 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5542 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5543 }
5544 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005545 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005546}
5547
Jeff Brownbe1aa822011-07-27 16:04:54 -07005548void SingleTouchInputMapper::configureRawPointerAxes() {
5549 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005550
Jeff Brownbe1aa822011-07-27 16:04:54 -07005551 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5552 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5553 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5554 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5555 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005556 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5557 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005558}
5559
5560
5561// --- MultiTouchInputMapper ---
5562
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005563MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005564 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005565}
5566
5567MultiTouchInputMapper::~MultiTouchInputMapper() {
5568}
5569
Jeff Brown65fd2512011-08-18 11:20:58 -07005570void MultiTouchInputMapper::reset(nsecs_t when) {
5571 mMultiTouchMotionAccumulator.reset(getDevice());
5572
Jeff Brown6894a292011-07-01 17:59:27 -07005573 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005574
Jeff Brown65fd2512011-08-18 11:20:58 -07005575 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005576}
5577
5578void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005579 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005580
Jeff Brown65fd2512011-08-18 11:20:58 -07005581 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005582}
5583
Jeff Brown65fd2512011-08-18 11:20:58 -07005584void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005585 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005586 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005587 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005588
Jeff Brown80fd47c2011-05-24 01:07:44 -07005589 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005590 const MultiTouchMotionAccumulator::Slot* inSlot =
5591 mMultiTouchMotionAccumulator.getSlot(inIndex);
5592 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005593 continue;
5594 }
5595
Jeff Brown80fd47c2011-05-24 01:07:44 -07005596 if (outCount >= MAX_POINTERS) {
5597#if DEBUG_POINTERS
5598 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5599 "ignoring the rest.",
5600 getDeviceName().string(), MAX_POINTERS);
5601#endif
5602 break; // too many fingers!
5603 }
5604
Jeff Brownbe1aa822011-07-27 16:04:54 -07005605 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005606 outPointer.x = inSlot->getX();
5607 outPointer.y = inSlot->getY();
5608 outPointer.pressure = inSlot->getPressure();
5609 outPointer.touchMajor = inSlot->getTouchMajor();
5610 outPointer.touchMinor = inSlot->getTouchMinor();
5611 outPointer.toolMajor = inSlot->getToolMajor();
5612 outPointer.toolMinor = inSlot->getToolMinor();
5613 outPointer.orientation = inSlot->getOrientation();
5614 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005615 outPointer.tiltX = 0;
5616 outPointer.tiltY = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005617
Jeff Brown49754db2011-07-01 17:37:58 -07005618 outPointer.toolType = inSlot->getToolType();
5619 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5620 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5621 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5622 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5623 }
Jeff Brown8d608662010-08-30 03:02:23 -07005624 }
5625
Jeff Brown65fd2512011-08-18 11:20:58 -07005626 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5627 && (mTouchButtonAccumulator.isHovering()
5628 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005629 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005630
Jeff Brown8d608662010-08-30 03:02:23 -07005631 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005632 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005633 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005634 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005635 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005636 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005637 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005638 if (mPointerTrackingIdMap[n] == trackingId) {
5639 id = n;
5640 }
5641 }
5642
5643 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005644 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005645 mPointerTrackingIdMap[id] = trackingId;
5646 }
5647 }
5648 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005649 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005650 mCurrentRawPointerData.clearIdBits();
5651 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005652 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005653 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005654 mCurrentRawPointerData.idToIndex[id] = outCount;
5655 mCurrentRawPointerData.markIdBit(id, isHovering);
5656 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005657 }
5658 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005659
Jeff Brown6d0fec22010-07-23 21:28:06 -07005660 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005661 }
5662
Jeff Brownbe1aa822011-07-27 16:04:54 -07005663 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005664 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005665
Jeff Brown65fd2512011-08-18 11:20:58 -07005666 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005667}
5668
Jeff Brownbe1aa822011-07-27 16:04:54 -07005669void MultiTouchInputMapper::configureRawPointerAxes() {
5670 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005671
Jeff Brownbe1aa822011-07-27 16:04:54 -07005672 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5673 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5674 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5675 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5676 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5677 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5678 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5679 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5680 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5681 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5682 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005683
Jeff Brownbe1aa822011-07-27 16:04:54 -07005684 if (mRawPointerAxes.trackingId.valid
5685 && mRawPointerAxes.slot.valid
5686 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5687 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005688 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005689 LOGW("MultiTouch Device %s reported %d slots but the framework "
5690 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005691 getDeviceName().string(), slotCount, MAX_SLOTS);
5692 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005693 }
Jeff Brown49754db2011-07-01 17:37:58 -07005694 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005695 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005696 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005697 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005698}
5699
Jeff Brown46b9ac02010-04-22 18:58:52 -07005700
Jeff Browncb1404e2011-01-15 18:14:15 -08005701// --- JoystickInputMapper ---
5702
5703JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5704 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005705}
5706
5707JoystickInputMapper::~JoystickInputMapper() {
5708}
5709
5710uint32_t JoystickInputMapper::getSources() {
5711 return AINPUT_SOURCE_JOYSTICK;
5712}
5713
5714void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5715 InputMapper::populateDeviceInfo(info);
5716
Jeff Brown6f2fba42011-02-19 01:08:02 -08005717 for (size_t i = 0; i < mAxes.size(); i++) {
5718 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005719 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5720 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005721 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005722 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5723 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005724 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005725 }
5726}
5727
5728void JoystickInputMapper::dump(String8& dump) {
5729 dump.append(INDENT2 "Joystick Input Mapper:\n");
5730
Jeff Brown6f2fba42011-02-19 01:08:02 -08005731 dump.append(INDENT3 "Axes:\n");
5732 size_t numAxes = mAxes.size();
5733 for (size_t i = 0; i < numAxes; i++) {
5734 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005735 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005736 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005737 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005738 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005739 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005740 }
Jeff Brown85297452011-03-04 13:07:49 -08005741 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5742 label = getAxisLabel(axis.axisInfo.highAxis);
5743 if (label) {
5744 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5745 } else {
5746 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5747 axis.axisInfo.splitValue);
5748 }
5749 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5750 dump.append(" (invert)");
5751 }
5752
5753 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5754 axis.min, axis.max, axis.flat, axis.fuzz);
5755 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5756 "highScale=%0.5f, highOffset=%0.5f\n",
5757 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005758 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5759 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005760 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005761 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005762 }
5763}
5764
Jeff Brown65fd2512011-08-18 11:20:58 -07005765void JoystickInputMapper::configure(nsecs_t when,
5766 const InputReaderConfiguration* config, uint32_t changes) {
5767 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005768
Jeff Brown474dcb52011-06-14 20:22:50 -07005769 if (!changes) { // first time only
5770 // Collect all axes.
5771 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285a2011-08-31 12:56:34 -07005772 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5773 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5774 continue; // axis must be claimed by a different device
5775 }
5776
Jeff Brown474dcb52011-06-14 20:22:50 -07005777 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005778 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005779 if (rawAxisInfo.valid) {
5780 // Map axis.
5781 AxisInfo axisInfo;
5782 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5783 if (!explicitlyMapped) {
5784 // Axis is not explicitly mapped, will choose a generic axis later.
5785 axisInfo.mode = AxisInfo::MODE_NORMAL;
5786 axisInfo.axis = -1;
5787 }
5788
5789 // Apply flat override.
5790 int32_t rawFlat = axisInfo.flatOverride < 0
5791 ? rawAxisInfo.flat : axisInfo.flatOverride;
5792
5793 // Calculate scaling factors and limits.
5794 Axis axis;
5795 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5796 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5797 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5798 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5799 scale, 0.0f, highScale, 0.0f,
5800 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5801 } else if (isCenteredAxis(axisInfo.axis)) {
5802 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5803 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5804 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5805 scale, offset, scale, offset,
5806 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5807 } else {
5808 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5809 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5810 scale, 0.0f, scale, 0.0f,
5811 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5812 }
5813
5814 // To eliminate noise while the joystick is at rest, filter out small variations
5815 // in axis values up front.
5816 axis.filter = axis.flat * 0.25f;
5817
5818 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005819 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005820 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005821
Jeff Brown474dcb52011-06-14 20:22:50 -07005822 // If there are too many axes, start dropping them.
5823 // Prefer to keep explicitly mapped axes.
5824 if (mAxes.size() > PointerCoords::MAX_AXES) {
5825 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5826 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5827 pruneAxes(true);
5828 pruneAxes(false);
5829 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005830
Jeff Brown474dcb52011-06-14 20:22:50 -07005831 // Assign generic axis ids to remaining axes.
5832 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5833 size_t numAxes = mAxes.size();
5834 for (size_t i = 0; i < numAxes; i++) {
5835 Axis& axis = mAxes.editValueAt(i);
5836 if (axis.axisInfo.axis < 0) {
5837 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5838 && haveAxis(nextGenericAxisId)) {
5839 nextGenericAxisId += 1;
5840 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005841
Jeff Brown474dcb52011-06-14 20:22:50 -07005842 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5843 axis.axisInfo.axis = nextGenericAxisId;
5844 nextGenericAxisId += 1;
5845 } else {
5846 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5847 "have already been assigned to other axes.",
5848 getDeviceName().string(), mAxes.keyAt(i));
5849 mAxes.removeItemsAt(i--);
5850 numAxes -= 1;
5851 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005852 }
5853 }
5854 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005855}
5856
Jeff Brown85297452011-03-04 13:07:49 -08005857bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005858 size_t numAxes = mAxes.size();
5859 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005860 const Axis& axis = mAxes.valueAt(i);
5861 if (axis.axisInfo.axis == axisId
5862 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5863 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005864 return true;
5865 }
5866 }
5867 return false;
5868}
Jeff Browncb1404e2011-01-15 18:14:15 -08005869
Jeff Brown6f2fba42011-02-19 01:08:02 -08005870void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5871 size_t i = mAxes.size();
5872 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5873 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5874 continue;
5875 }
5876 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5877 getDeviceName().string(), mAxes.keyAt(i));
5878 mAxes.removeItemsAt(i);
5879 }
5880}
5881
5882bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5883 switch (axis) {
5884 case AMOTION_EVENT_AXIS_X:
5885 case AMOTION_EVENT_AXIS_Y:
5886 case AMOTION_EVENT_AXIS_Z:
5887 case AMOTION_EVENT_AXIS_RX:
5888 case AMOTION_EVENT_AXIS_RY:
5889 case AMOTION_EVENT_AXIS_RZ:
5890 case AMOTION_EVENT_AXIS_HAT_X:
5891 case AMOTION_EVENT_AXIS_HAT_Y:
5892 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005893 case AMOTION_EVENT_AXIS_RUDDER:
5894 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005895 return true;
5896 default:
5897 return false;
5898 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005899}
5900
Jeff Brown65fd2512011-08-18 11:20:58 -07005901void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005902 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005903 size_t numAxes = mAxes.size();
5904 for (size_t i = 0; i < numAxes; i++) {
5905 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005906 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005907 }
5908
Jeff Brown65fd2512011-08-18 11:20:58 -07005909 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005910}
5911
5912void JoystickInputMapper::process(const RawEvent* rawEvent) {
5913 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005914 case EV_ABS: {
5915 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5916 if (index >= 0) {
5917 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005918 float newValue, highNewValue;
5919 switch (axis.axisInfo.mode) {
5920 case AxisInfo::MODE_INVERT:
5921 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5922 * axis.scale + axis.offset;
5923 highNewValue = 0.0f;
5924 break;
5925 case AxisInfo::MODE_SPLIT:
5926 if (rawEvent->value < axis.axisInfo.splitValue) {
5927 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5928 * axis.scale + axis.offset;
5929 highNewValue = 0.0f;
5930 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5931 newValue = 0.0f;
5932 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5933 * axis.highScale + axis.highOffset;
5934 } else {
5935 newValue = 0.0f;
5936 highNewValue = 0.0f;
5937 }
5938 break;
5939 default:
5940 newValue = rawEvent->value * axis.scale + axis.offset;
5941 highNewValue = 0.0f;
5942 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005943 }
Jeff Brown85297452011-03-04 13:07:49 -08005944 axis.newValue = newValue;
5945 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005946 }
5947 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005948 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005949
5950 case EV_SYN:
5951 switch (rawEvent->scanCode) {
5952 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005953 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005954 break;
5955 }
5956 break;
5957 }
5958}
5959
Jeff Brown6f2fba42011-02-19 01:08:02 -08005960void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005961 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005962 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005963 }
5964
5965 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005966 int32_t buttonState = 0;
5967
5968 PointerProperties pointerProperties;
5969 pointerProperties.clear();
5970 pointerProperties.id = 0;
5971 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005972
Jeff Brown6f2fba42011-02-19 01:08:02 -08005973 PointerCoords pointerCoords;
5974 pointerCoords.clear();
5975
5976 size_t numAxes = mAxes.size();
5977 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005978 const Axis& axis = mAxes.valueAt(i);
5979 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5980 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5981 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5982 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005983 }
5984
Jeff Brown56194eb2011-03-02 19:23:13 -08005985 // Moving a joystick axis should not wake the devide because joysticks can
5986 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5987 // button will likely wake the device.
5988 // TODO: Use the input device configuration to control this behavior more finely.
5989 uint32_t policyFlags = 0;
5990
Jeff Brownbe1aa822011-07-27 16:04:54 -07005991 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005992 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5993 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005994 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005995}
5996
Jeff Brown85297452011-03-04 13:07:49 -08005997bool JoystickInputMapper::filterAxes(bool force) {
5998 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005999 size_t numAxes = mAxes.size();
6000 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006001 Axis& axis = mAxes.editValueAt(i);
6002 if (force || hasValueChangedSignificantly(axis.filter,
6003 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6004 axis.currentValue = axis.newValue;
6005 atLeastOneSignificantChange = true;
6006 }
6007 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6008 if (force || hasValueChangedSignificantly(axis.filter,
6009 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6010 axis.highCurrentValue = axis.highNewValue;
6011 atLeastOneSignificantChange = true;
6012 }
6013 }
6014 }
6015 return atLeastOneSignificantChange;
6016}
6017
6018bool JoystickInputMapper::hasValueChangedSignificantly(
6019 float filter, float newValue, float currentValue, float min, float max) {
6020 if (newValue != currentValue) {
6021 // Filter out small changes in value unless the value is converging on the axis
6022 // bounds or center point. This is intended to reduce the amount of information
6023 // sent to applications by particularly noisy joysticks (such as PS3).
6024 if (fabs(newValue - currentValue) > filter
6025 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6026 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6027 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6028 return true;
6029 }
6030 }
6031 return false;
6032}
6033
6034bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6035 float filter, float newValue, float currentValue, float thresholdValue) {
6036 float newDistance = fabs(newValue - thresholdValue);
6037 if (newDistance < filter) {
6038 float oldDistance = fabs(currentValue - thresholdValue);
6039 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006040 return true;
6041 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006042 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006043 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006044}
6045
Jeff Brown46b9ac02010-04-22 18:58:52 -07006046} // namespace android