blob: 88c19a417f8ad400b79bb82645f8ec03fffdc024 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041#include <cutils/log.h>
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 Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700183 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700185 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 }
187}
188
189static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
190 nsecs_t when, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
192 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
193 lastButtonState, currentButtonState,
194 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
198}
199
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700200
Jeff Brown65fd2512011-08-18 11:20:58 -0700201// --- InputReaderConfiguration ---
202
203bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,
204 int32_t* width, int32_t* height, int32_t* orientation) const {
205 if (displayId == 0) {
206 const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
207 if (info.width > 0 && info.height > 0) {
208 if (width) {
209 *width = info.width;
210 }
211 if (height) {
212 *height = info.height;
213 }
214 if (orientation) {
215 *orientation = info.orientation;
216 }
217 return true;
218 }
219 }
220 return false;
221}
222
223void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,
224 int32_t width, int32_t height, int32_t orientation) {
225 if (displayId == 0) {
226 DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
227 info.width = width;
228 info.height = height;
229 info.orientation = orientation;
230 }
231}
232
233
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234// --- InputReader ---
235
236InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700238 const sp<InputListenerInterface>& listener) :
239 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700240 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700241 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700242 mQueuedListener = new QueuedInputListener(listener);
243
244 { // acquire lock
245 AutoMutex _l(mLock);
246
247 refreshConfigurationLocked(0);
248 updateGlobalMetaStateLocked();
249 updateInputConfigurationLocked();
250 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700251}
252
253InputReader::~InputReader() {
254 for (size_t i = 0; i < mDevices.size(); i++) {
255 delete mDevices.valueAt(i);
256 }
257}
258
259void InputReader::loopOnce() {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700260 int32_t timeoutMillis;
Jeff Brown474dcb52011-06-14 20:22:50 -0700261 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700262 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700263
Jeff Brownbe1aa822011-07-27 16:04:54 -0700264 uint32_t changes = mConfigurationChangesToRefresh;
265 if (changes) {
266 mConfigurationChangesToRefresh = 0;
267 refreshConfigurationLocked(changes);
268 }
269
270 timeoutMillis = -1;
271 if (mNextTimeout != LLONG_MAX) {
272 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
273 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
274 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700275 } // release lock
276
Jeff Brownb7198742011-03-18 18:14:26 -0700277 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700278
279 { // acquire lock
280 AutoMutex _l(mLock);
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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -0700364 return;
365 }
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown9ee285af2011-08-31 12:56:34 -0700393 InputDevice* device = new InputDevice(&mContext, deviceId, name, classes);
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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)) {
647 result = (device->*getStateFunc)(sourceMask, code);
648 if (result >= AKEY_STATE_DOWN) {
649 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700650 }
651 }
652 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700653 }
654 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700655}
656
657bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
658 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700659 AutoMutex _l(mLock);
660
Jeff Brown6d0fec22010-07-23 21:28:06 -0700661 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700662 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700663}
664
Jeff Brownbe1aa822011-07-27 16:04:54 -0700665bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
666 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
667 bool result = false;
668 if (deviceId >= 0) {
669 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
670 if (deviceIndex >= 0) {
671 InputDevice* device = mDevices.valueAt(deviceIndex);
672 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
673 result = device->markSupportedKeyCodes(sourceMask,
674 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700675 }
676 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700677 } else {
678 size_t numDevices = mDevices.size();
679 for (size_t i = 0; i < numDevices; i++) {
680 InputDevice* device = mDevices.valueAt(i);
681 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
682 result |= device->markSupportedKeyCodes(sourceMask,
683 numCodes, keyCodes, outFlags);
684 }
685 }
686 }
687 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700688}
689
Jeff Brown474dcb52011-06-14 20:22:50 -0700690void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700691 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700692
Jeff Brownbe1aa822011-07-27 16:04:54 -0700693 if (changes) {
694 bool needWake = !mConfigurationChangesToRefresh;
695 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700696
697 if (needWake) {
698 mEventHub->wake();
699 }
700 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700701}
702
Jeff Brownb88102f2010-09-08 11:49:43 -0700703void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700704 AutoMutex _l(mLock);
705
Jeff Brownf2f487182010-10-01 17:46:21 -0700706 mEventHub->dump(dump);
707 dump.append("\n");
708
709 dump.append("Input Reader State:\n");
710
Jeff Brownbe1aa822011-07-27 16:04:54 -0700711 for (size_t i = 0; i < mDevices.size(); i++) {
712 mDevices.valueAt(i)->dump(dump);
713 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700714
715 dump.append(INDENT "Configuration:\n");
716 dump.append(INDENT2 "ExcludedDeviceNames: [");
717 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
718 if (i != 0) {
719 dump.append(", ");
720 }
721 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
722 }
723 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700724 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
725 mConfig.virtualKeyQuietTime * 0.000001f);
726
Jeff Brown19c97d462011-06-01 12:33:19 -0700727 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
728 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
729 mConfig.pointerVelocityControlParameters.scale,
730 mConfig.pointerVelocityControlParameters.lowThreshold,
731 mConfig.pointerVelocityControlParameters.highThreshold,
732 mConfig.pointerVelocityControlParameters.acceleration);
733
734 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
735 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
736 mConfig.wheelVelocityControlParameters.scale,
737 mConfig.wheelVelocityControlParameters.lowThreshold,
738 mConfig.wheelVelocityControlParameters.highThreshold,
739 mConfig.wheelVelocityControlParameters.acceleration);
740
Jeff Brown214eaf42011-05-26 19:17:02 -0700741 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700742 dump.appendFormat(INDENT3 "Enabled: %s\n",
743 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700744 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
745 mConfig.pointerGestureQuietInterval * 0.000001f);
746 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
747 mConfig.pointerGestureDragMinSwitchSpeed);
748 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
749 mConfig.pointerGestureTapInterval * 0.000001f);
750 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
751 mConfig.pointerGestureTapDragInterval * 0.000001f);
752 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
753 mConfig.pointerGestureTapSlop);
754 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
755 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700756 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
757 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700758 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
759 mConfig.pointerGestureSwipeTransitionAngleCosine);
760 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
761 mConfig.pointerGestureSwipeMaxWidthRatio);
762 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
763 mConfig.pointerGestureMovementSpeedRatio);
764 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
765 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700766}
767
Jeff Brown89ef0722011-08-10 16:25:21 -0700768void InputReader::monitor() {
769 // Acquire and release the lock to ensure that the reader has not deadlocked.
770 mLock.lock();
771 mLock.unlock();
772
773 // Check the EventHub
774 mEventHub->monitor();
775}
776
Jeff Brown6d0fec22010-07-23 21:28:06 -0700777
Jeff Brownbe1aa822011-07-27 16:04:54 -0700778// --- InputReader::ContextImpl ---
779
780InputReader::ContextImpl::ContextImpl(InputReader* reader) :
781 mReader(reader) {
782}
783
784void InputReader::ContextImpl::updateGlobalMetaState() {
785 // lock is already held by the input loop
786 mReader->updateGlobalMetaStateLocked();
787}
788
789int32_t InputReader::ContextImpl::getGlobalMetaState() {
790 // lock is already held by the input loop
791 return mReader->getGlobalMetaStateLocked();
792}
793
794void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
795 // lock is already held by the input loop
796 mReader->disableVirtualKeysUntilLocked(time);
797}
798
799bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
800 InputDevice* device, int32_t keyCode, int32_t scanCode) {
801 // lock is already held by the input loop
802 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
803}
804
805void InputReader::ContextImpl::fadePointer() {
806 // lock is already held by the input loop
807 mReader->fadePointerLocked();
808}
809
810void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
811 // lock is already held by the input loop
812 mReader->requestTimeoutAtTimeLocked(when);
813}
814
815InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
816 return mReader->mPolicy.get();
817}
818
819InputListenerInterface* InputReader::ContextImpl::getListener() {
820 return mReader->mQueuedListener.get();
821}
822
823EventHubInterface* InputReader::ContextImpl::getEventHub() {
824 return mReader->mEventHub.get();
825}
826
827
Jeff Brown6d0fec22010-07-23 21:28:06 -0700828// --- InputReaderThread ---
829
830InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
831 Thread(/*canCallJava*/ true), mReader(reader) {
832}
833
834InputReaderThread::~InputReaderThread() {
835}
836
837bool InputReaderThread::threadLoop() {
838 mReader->loopOnce();
839 return true;
840}
841
842
843// --- InputDevice ---
844
Jeff Brown9ee285af2011-08-31 12:56:34 -0700845InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name,
846 uint32_t classes) :
847 mContext(context), mId(id), mName(name), mClasses(classes),
848 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700849}
850
851InputDevice::~InputDevice() {
852 size_t numMappers = mMappers.size();
853 for (size_t i = 0; i < numMappers; i++) {
854 delete mMappers[i];
855 }
856 mMappers.clear();
857}
858
Jeff Brownef3d7e82010-09-30 14:33:04 -0700859void InputDevice::dump(String8& dump) {
860 InputDeviceInfo deviceInfo;
861 getDeviceInfo(& deviceInfo);
862
Jeff Brown90655042010-12-02 13:50:46 -0800863 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700864 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800865 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700866 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
867 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800868
Jeff Brownefd32662011-03-08 15:13:06 -0800869 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800870 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700871 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800872 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800873 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
874 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800875 char name[32];
876 if (label) {
877 strncpy(name, label, sizeof(name));
878 name[sizeof(name) - 1] = '\0';
879 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800880 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800881 }
Jeff Brownefd32662011-03-08 15:13:06 -0800882 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
883 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
884 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800885 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700886 }
887
888 size_t numMappers = mMappers.size();
889 for (size_t i = 0; i < numMappers; i++) {
890 InputMapper* mapper = mMappers[i];
891 mapper->dump(dump);
892 }
893}
894
Jeff Brown6d0fec22010-07-23 21:28:06 -0700895void InputDevice::addMapper(InputMapper* mapper) {
896 mMappers.add(mapper);
897}
898
Jeff Brown65fd2512011-08-18 11:20:58 -0700899void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700900 mSources = 0;
901
Jeff Brown474dcb52011-06-14 20:22:50 -0700902 if (!isIgnored()) {
903 if (!changes) { // first time only
904 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
905 }
906
907 size_t numMappers = mMappers.size();
908 for (size_t i = 0; i < numMappers; i++) {
909 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700910 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700911 mSources |= mapper->getSources();
912 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700913 }
914}
915
Jeff Brown65fd2512011-08-18 11:20:58 -0700916void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700917 size_t numMappers = mMappers.size();
918 for (size_t i = 0; i < numMappers; i++) {
919 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700920 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700921 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700922
923 mContext->updateGlobalMetaState();
924
925 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700926}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700927
Jeff Brownb7198742011-03-18 18:14:26 -0700928void InputDevice::process(const RawEvent* rawEvents, size_t count) {
929 // Process all of the events in order for each mapper.
930 // We cannot simply ask each mapper to process them in bulk because mappers may
931 // have side-effects that must be interleaved. For example, joystick movement events and
932 // gamepad button presses are handled by different mappers but they should be dispatched
933 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700934 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700935 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
936#if DEBUG_RAW_EVENTS
937 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700938 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700939 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
940 rawEvent->value, rawEvent->flags);
941#endif
942
Jeff Brown80fd47c2011-05-24 01:07:44 -0700943 if (mDropUntilNextSync) {
944 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
945 mDropUntilNextSync = false;
946#if DEBUG_RAW_EVENTS
947 LOGD("Recovered from input event buffer overrun.");
948#endif
949 } else {
950#if DEBUG_RAW_EVENTS
951 LOGD("Dropped input event while waiting for next input sync.");
952#endif
953 }
954 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
955 LOGI("Detected input event buffer overrun for device %s.", mName.string());
956 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700957 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700958 } else {
959 for (size_t i = 0; i < numMappers; i++) {
960 InputMapper* mapper = mMappers[i];
961 mapper->process(rawEvent);
962 }
Jeff Brownb7198742011-03-18 18:14:26 -0700963 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700964 }
965}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700966
Jeff Brownaa3855d2011-03-17 01:34:19 -0700967void InputDevice::timeoutExpired(nsecs_t when) {
968 size_t numMappers = mMappers.size();
969 for (size_t i = 0; i < numMappers; i++) {
970 InputMapper* mapper = mMappers[i];
971 mapper->timeoutExpired(when);
972 }
973}
974
Jeff Brown6d0fec22010-07-23 21:28:06 -0700975void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
976 outDeviceInfo->initialize(mId, mName);
977
978 size_t numMappers = mMappers.size();
979 for (size_t i = 0; i < numMappers; i++) {
980 InputMapper* mapper = mMappers[i];
981 mapper->populateDeviceInfo(outDeviceInfo);
982 }
983}
984
985int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
986 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
987}
988
989int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
990 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
991}
992
993int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
994 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
995}
996
997int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
998 int32_t result = AKEY_STATE_UNKNOWN;
999 size_t numMappers = mMappers.size();
1000 for (size_t i = 0; i < numMappers; i++) {
1001 InputMapper* mapper = mMappers[i];
1002 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1003 result = (mapper->*getStateFunc)(sourceMask, code);
1004 if (result >= AKEY_STATE_DOWN) {
1005 return result;
1006 }
1007 }
1008 }
1009 return result;
1010}
1011
1012bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1013 const int32_t* keyCodes, uint8_t* outFlags) {
1014 bool result = false;
1015 size_t numMappers = mMappers.size();
1016 for (size_t i = 0; i < numMappers; i++) {
1017 InputMapper* mapper = mMappers[i];
1018 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1019 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1020 }
1021 }
1022 return result;
1023}
1024
1025int32_t InputDevice::getMetaState() {
1026 int32_t result = 0;
1027 size_t numMappers = mMappers.size();
1028 for (size_t i = 0; i < numMappers; i++) {
1029 InputMapper* mapper = mMappers[i];
1030 result |= mapper->getMetaState();
1031 }
1032 return result;
1033}
1034
Jeff Brown05dc66a2011-03-02 14:41:58 -08001035void InputDevice::fadePointer() {
1036 size_t numMappers = mMappers.size();
1037 for (size_t i = 0; i < numMappers; i++) {
1038 InputMapper* mapper = mMappers[i];
1039 mapper->fadePointer();
1040 }
1041}
1042
Jeff Brown65fd2512011-08-18 11:20:58 -07001043void InputDevice::notifyReset(nsecs_t when) {
1044 NotifyDeviceResetArgs args(when, mId);
1045 mContext->getListener()->notifyDeviceReset(&args);
1046}
1047
Jeff Brown6d0fec22010-07-23 21:28:06 -07001048
Jeff Brown49754db2011-07-01 17:37:58 -07001049// --- CursorButtonAccumulator ---
1050
1051CursorButtonAccumulator::CursorButtonAccumulator() {
1052 clearButtons();
1053}
1054
Jeff Brown65fd2512011-08-18 11:20:58 -07001055void CursorButtonAccumulator::reset(InputDevice* device) {
1056 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1057 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1058 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1059 mBtnBack = device->isKeyPressed(BTN_BACK);
1060 mBtnSide = device->isKeyPressed(BTN_SIDE);
1061 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1062 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1063 mBtnTask = device->isKeyPressed(BTN_TASK);
1064}
1065
Jeff Brown49754db2011-07-01 17:37:58 -07001066void CursorButtonAccumulator::clearButtons() {
1067 mBtnLeft = 0;
1068 mBtnRight = 0;
1069 mBtnMiddle = 0;
1070 mBtnBack = 0;
1071 mBtnSide = 0;
1072 mBtnForward = 0;
1073 mBtnExtra = 0;
1074 mBtnTask = 0;
1075}
1076
1077void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1078 if (rawEvent->type == EV_KEY) {
1079 switch (rawEvent->scanCode) {
1080 case BTN_LEFT:
1081 mBtnLeft = rawEvent->value;
1082 break;
1083 case BTN_RIGHT:
1084 mBtnRight = rawEvent->value;
1085 break;
1086 case BTN_MIDDLE:
1087 mBtnMiddle = rawEvent->value;
1088 break;
1089 case BTN_BACK:
1090 mBtnBack = rawEvent->value;
1091 break;
1092 case BTN_SIDE:
1093 mBtnSide = rawEvent->value;
1094 break;
1095 case BTN_FORWARD:
1096 mBtnForward = rawEvent->value;
1097 break;
1098 case BTN_EXTRA:
1099 mBtnExtra = rawEvent->value;
1100 break;
1101 case BTN_TASK:
1102 mBtnTask = rawEvent->value;
1103 break;
1104 }
1105 }
1106}
1107
1108uint32_t CursorButtonAccumulator::getButtonState() const {
1109 uint32_t result = 0;
1110 if (mBtnLeft) {
1111 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1112 }
1113 if (mBtnRight) {
1114 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1115 }
1116 if (mBtnMiddle) {
1117 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1118 }
1119 if (mBtnBack || mBtnSide) {
1120 result |= AMOTION_EVENT_BUTTON_BACK;
1121 }
1122 if (mBtnForward || mBtnExtra) {
1123 result |= AMOTION_EVENT_BUTTON_FORWARD;
1124 }
1125 return result;
1126}
1127
1128
1129// --- CursorMotionAccumulator ---
1130
Jeff Brown65fd2512011-08-18 11:20:58 -07001131CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001132 clearRelativeAxes();
1133}
1134
Jeff Brown65fd2512011-08-18 11:20:58 -07001135void CursorMotionAccumulator::reset(InputDevice* device) {
1136 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001137}
1138
1139void CursorMotionAccumulator::clearRelativeAxes() {
1140 mRelX = 0;
1141 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001142}
1143
1144void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1145 if (rawEvent->type == EV_REL) {
1146 switch (rawEvent->scanCode) {
1147 case REL_X:
1148 mRelX = rawEvent->value;
1149 break;
1150 case REL_Y:
1151 mRelY = rawEvent->value;
1152 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001153 }
1154 }
1155}
1156
1157void CursorMotionAccumulator::finishSync() {
1158 clearRelativeAxes();
1159}
1160
1161
1162// --- CursorScrollAccumulator ---
1163
1164CursorScrollAccumulator::CursorScrollAccumulator() :
1165 mHaveRelWheel(false), mHaveRelHWheel(false) {
1166 clearRelativeAxes();
1167}
1168
1169void CursorScrollAccumulator::configure(InputDevice* device) {
1170 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1171 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1172}
1173
1174void CursorScrollAccumulator::reset(InputDevice* device) {
1175 clearRelativeAxes();
1176}
1177
1178void CursorScrollAccumulator::clearRelativeAxes() {
1179 mRelWheel = 0;
1180 mRelHWheel = 0;
1181}
1182
1183void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1184 if (rawEvent->type == EV_REL) {
1185 switch (rawEvent->scanCode) {
Jeff Brown49754db2011-07-01 17:37:58 -07001186 case REL_WHEEL:
1187 mRelWheel = rawEvent->value;
1188 break;
1189 case REL_HWHEEL:
1190 mRelHWheel = rawEvent->value;
1191 break;
1192 }
1193 }
1194}
1195
Jeff Brown65fd2512011-08-18 11:20:58 -07001196void CursorScrollAccumulator::finishSync() {
1197 clearRelativeAxes();
1198}
1199
Jeff Brown49754db2011-07-01 17:37:58 -07001200
1201// --- TouchButtonAccumulator ---
1202
1203TouchButtonAccumulator::TouchButtonAccumulator() :
1204 mHaveBtnTouch(false) {
1205 clearButtons();
1206}
1207
1208void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001209 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1210}
1211
1212void TouchButtonAccumulator::reset(InputDevice* device) {
1213 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1214 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1215 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1216 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1217 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1218 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1219 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1220 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1221 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1222 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1223 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001224 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1225 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1226 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001227}
1228
1229void TouchButtonAccumulator::clearButtons() {
1230 mBtnTouch = 0;
1231 mBtnStylus = 0;
1232 mBtnStylus2 = 0;
1233 mBtnToolFinger = 0;
1234 mBtnToolPen = 0;
1235 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001236 mBtnToolBrush = 0;
1237 mBtnToolPencil = 0;
1238 mBtnToolAirbrush = 0;
1239 mBtnToolMouse = 0;
1240 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001241 mBtnToolDoubleTap = 0;
1242 mBtnToolTripleTap = 0;
1243 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001244}
1245
1246void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1247 if (rawEvent->type == EV_KEY) {
1248 switch (rawEvent->scanCode) {
1249 case BTN_TOUCH:
1250 mBtnTouch = rawEvent->value;
1251 break;
1252 case BTN_STYLUS:
1253 mBtnStylus = rawEvent->value;
1254 break;
1255 case BTN_STYLUS2:
1256 mBtnStylus2 = rawEvent->value;
1257 break;
1258 case BTN_TOOL_FINGER:
1259 mBtnToolFinger = rawEvent->value;
1260 break;
1261 case BTN_TOOL_PEN:
1262 mBtnToolPen = rawEvent->value;
1263 break;
1264 case BTN_TOOL_RUBBER:
1265 mBtnToolRubber = rawEvent->value;
1266 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001267 case BTN_TOOL_BRUSH:
1268 mBtnToolBrush = rawEvent->value;
1269 break;
1270 case BTN_TOOL_PENCIL:
1271 mBtnToolPencil = rawEvent->value;
1272 break;
1273 case BTN_TOOL_AIRBRUSH:
1274 mBtnToolAirbrush = rawEvent->value;
1275 break;
1276 case BTN_TOOL_MOUSE:
1277 mBtnToolMouse = rawEvent->value;
1278 break;
1279 case BTN_TOOL_LENS:
1280 mBtnToolLens = rawEvent->value;
1281 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001282 case BTN_TOOL_DOUBLETAP:
1283 mBtnToolDoubleTap = rawEvent->value;
1284 break;
1285 case BTN_TOOL_TRIPLETAP:
1286 mBtnToolTripleTap = rawEvent->value;
1287 break;
1288 case BTN_TOOL_QUADTAP:
1289 mBtnToolQuadTap = rawEvent->value;
1290 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001291 }
1292 }
1293}
1294
1295uint32_t TouchButtonAccumulator::getButtonState() const {
1296 uint32_t result = 0;
1297 if (mBtnStylus) {
1298 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1299 }
1300 if (mBtnStylus2) {
1301 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1302 }
1303 return result;
1304}
1305
1306int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001307 if (mBtnToolMouse || mBtnToolLens) {
1308 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1309 }
Jeff Brown49754db2011-07-01 17:37:58 -07001310 if (mBtnToolRubber) {
1311 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1312 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001313 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001314 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1315 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001316 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001317 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1318 }
1319 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1320}
1321
Jeff Brownd87c6d52011-08-10 14:55:59 -07001322bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001323 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1324 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001325 || mBtnToolMouse || mBtnToolLens
1326 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001327}
1328
1329bool TouchButtonAccumulator::isHovering() const {
1330 return mHaveBtnTouch && !mBtnTouch;
1331}
1332
1333
Jeff Brownbe1aa822011-07-27 16:04:54 -07001334// --- RawPointerAxes ---
1335
1336RawPointerAxes::RawPointerAxes() {
1337 clear();
1338}
1339
1340void RawPointerAxes::clear() {
1341 x.clear();
1342 y.clear();
1343 pressure.clear();
1344 touchMajor.clear();
1345 touchMinor.clear();
1346 toolMajor.clear();
1347 toolMinor.clear();
1348 orientation.clear();
1349 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001350 tiltX.clear();
1351 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001352 trackingId.clear();
1353 slot.clear();
1354}
1355
1356
1357// --- RawPointerData ---
1358
1359RawPointerData::RawPointerData() {
1360 clear();
1361}
1362
1363void RawPointerData::clear() {
1364 pointerCount = 0;
1365 clearIdBits();
1366}
1367
1368void RawPointerData::copyFrom(const RawPointerData& other) {
1369 pointerCount = other.pointerCount;
1370 hoveringIdBits = other.hoveringIdBits;
1371 touchingIdBits = other.touchingIdBits;
1372
1373 for (uint32_t i = 0; i < pointerCount; i++) {
1374 pointers[i] = other.pointers[i];
1375
1376 int id = pointers[i].id;
1377 idToIndex[id] = other.idToIndex[id];
1378 }
1379}
1380
1381void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1382 float x = 0, y = 0;
1383 uint32_t count = touchingIdBits.count();
1384 if (count) {
1385 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1386 uint32_t id = idBits.clearFirstMarkedBit();
1387 const Pointer& pointer = pointerForId(id);
1388 x += pointer.x;
1389 y += pointer.y;
1390 }
1391 x /= count;
1392 y /= count;
1393 }
1394 *outX = x;
1395 *outY = y;
1396}
1397
1398
1399// --- CookedPointerData ---
1400
1401CookedPointerData::CookedPointerData() {
1402 clear();
1403}
1404
1405void CookedPointerData::clear() {
1406 pointerCount = 0;
1407 hoveringIdBits.clear();
1408 touchingIdBits.clear();
1409}
1410
1411void CookedPointerData::copyFrom(const CookedPointerData& other) {
1412 pointerCount = other.pointerCount;
1413 hoveringIdBits = other.hoveringIdBits;
1414 touchingIdBits = other.touchingIdBits;
1415
1416 for (uint32_t i = 0; i < pointerCount; i++) {
1417 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1418 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1419
1420 int id = pointerProperties[i].id;
1421 idToIndex[id] = other.idToIndex[id];
1422 }
1423}
1424
1425
Jeff Brown49754db2011-07-01 17:37:58 -07001426// --- SingleTouchMotionAccumulator ---
1427
1428SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1429 clearAbsoluteAxes();
1430}
1431
Jeff Brown65fd2512011-08-18 11:20:58 -07001432void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1433 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1434 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1435 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1436 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1437 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1438 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1439 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1440}
1441
Jeff Brown49754db2011-07-01 17:37:58 -07001442void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1443 mAbsX = 0;
1444 mAbsY = 0;
1445 mAbsPressure = 0;
1446 mAbsToolWidth = 0;
1447 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001448 mAbsTiltX = 0;
1449 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001450}
1451
1452void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1453 if (rawEvent->type == EV_ABS) {
1454 switch (rawEvent->scanCode) {
1455 case ABS_X:
1456 mAbsX = rawEvent->value;
1457 break;
1458 case ABS_Y:
1459 mAbsY = rawEvent->value;
1460 break;
1461 case ABS_PRESSURE:
1462 mAbsPressure = rawEvent->value;
1463 break;
1464 case ABS_TOOL_WIDTH:
1465 mAbsToolWidth = rawEvent->value;
1466 break;
1467 case ABS_DISTANCE:
1468 mAbsDistance = rawEvent->value;
1469 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001470 case ABS_TILT_X:
1471 mAbsTiltX = rawEvent->value;
1472 break;
1473 case ABS_TILT_Y:
1474 mAbsTiltY = rawEvent->value;
1475 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001476 }
1477 }
1478}
1479
1480
1481// --- MultiTouchMotionAccumulator ---
1482
1483MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1484 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1485}
1486
1487MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1488 delete[] mSlots;
1489}
1490
1491void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1492 mSlotCount = slotCount;
1493 mUsingSlotsProtocol = usingSlotsProtocol;
1494
1495 delete[] mSlots;
1496 mSlots = new Slot[slotCount];
1497}
1498
Jeff Brown65fd2512011-08-18 11:20:58 -07001499void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1500 // Unfortunately there is no way to read the initial contents of the slots.
1501 // So when we reset the accumulator, we must assume they are all zeroes.
1502 if (mUsingSlotsProtocol) {
1503 // Query the driver for the current slot index and use it as the initial slot
1504 // before we start reading events from the device. It is possible that the
1505 // current slot index will not be the same as it was when the first event was
1506 // written into the evdev buffer, which means the input mapper could start
1507 // out of sync with the initial state of the events in the evdev buffer.
1508 // In the extremely unlikely case that this happens, the data from
1509 // two slots will be confused until the next ABS_MT_SLOT event is received.
1510 // This can cause the touch point to "jump", but at least there will be
1511 // no stuck touches.
1512 int32_t initialSlot;
1513 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1514 ABS_MT_SLOT, &initialSlot);
1515 if (status) {
1516 LOGD("Could not retrieve current multitouch slot index. status=%d", status);
1517 initialSlot = -1;
1518 }
1519 clearSlots(initialSlot);
1520 } else {
1521 clearSlots(-1);
1522 }
1523}
1524
Jeff Brown49754db2011-07-01 17:37:58 -07001525void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001526 if (mSlots) {
1527 for (size_t i = 0; i < mSlotCount; i++) {
1528 mSlots[i].clear();
1529 }
Jeff Brown49754db2011-07-01 17:37:58 -07001530 }
1531 mCurrentSlot = initialSlot;
1532}
1533
1534void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1535 if (rawEvent->type == EV_ABS) {
1536 bool newSlot = false;
1537 if (mUsingSlotsProtocol) {
1538 if (rawEvent->scanCode == ABS_MT_SLOT) {
1539 mCurrentSlot = rawEvent->value;
1540 newSlot = true;
1541 }
1542 } else if (mCurrentSlot < 0) {
1543 mCurrentSlot = 0;
1544 }
1545
1546 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1547#if DEBUG_POINTERS
1548 if (newSlot) {
1549 LOGW("MultiTouch device emitted invalid slot index %d but it "
1550 "should be between 0 and %d; ignoring this slot.",
1551 mCurrentSlot, mSlotCount - 1);
1552 }
1553#endif
1554 } else {
1555 Slot* slot = &mSlots[mCurrentSlot];
1556
1557 switch (rawEvent->scanCode) {
1558 case ABS_MT_POSITION_X:
1559 slot->mInUse = true;
1560 slot->mAbsMTPositionX = rawEvent->value;
1561 break;
1562 case ABS_MT_POSITION_Y:
1563 slot->mInUse = true;
1564 slot->mAbsMTPositionY = rawEvent->value;
1565 break;
1566 case ABS_MT_TOUCH_MAJOR:
1567 slot->mInUse = true;
1568 slot->mAbsMTTouchMajor = rawEvent->value;
1569 break;
1570 case ABS_MT_TOUCH_MINOR:
1571 slot->mInUse = true;
1572 slot->mAbsMTTouchMinor = rawEvent->value;
1573 slot->mHaveAbsMTTouchMinor = true;
1574 break;
1575 case ABS_MT_WIDTH_MAJOR:
1576 slot->mInUse = true;
1577 slot->mAbsMTWidthMajor = rawEvent->value;
1578 break;
1579 case ABS_MT_WIDTH_MINOR:
1580 slot->mInUse = true;
1581 slot->mAbsMTWidthMinor = rawEvent->value;
1582 slot->mHaveAbsMTWidthMinor = true;
1583 break;
1584 case ABS_MT_ORIENTATION:
1585 slot->mInUse = true;
1586 slot->mAbsMTOrientation = rawEvent->value;
1587 break;
1588 case ABS_MT_TRACKING_ID:
1589 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001590 // The slot is no longer in use but it retains its previous contents,
1591 // which may be reused for subsequent touches.
1592 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001593 } else {
1594 slot->mInUse = true;
1595 slot->mAbsMTTrackingId = rawEvent->value;
1596 }
1597 break;
1598 case ABS_MT_PRESSURE:
1599 slot->mInUse = true;
1600 slot->mAbsMTPressure = rawEvent->value;
1601 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001602 case ABS_MT_DISTANCE:
1603 slot->mInUse = true;
1604 slot->mAbsMTDistance = rawEvent->value;
1605 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001606 case ABS_MT_TOOL_TYPE:
1607 slot->mInUse = true;
1608 slot->mAbsMTToolType = rawEvent->value;
1609 slot->mHaveAbsMTToolType = true;
1610 break;
1611 }
1612 }
1613 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1614 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1615 mCurrentSlot += 1;
1616 }
1617}
1618
Jeff Brown65fd2512011-08-18 11:20:58 -07001619void MultiTouchMotionAccumulator::finishSync() {
1620 if (!mUsingSlotsProtocol) {
1621 clearSlots(-1);
1622 }
1623}
1624
Jeff Brown49754db2011-07-01 17:37:58 -07001625
1626// --- MultiTouchMotionAccumulator::Slot ---
1627
1628MultiTouchMotionAccumulator::Slot::Slot() {
1629 clear();
1630}
1631
Jeff Brown49754db2011-07-01 17:37:58 -07001632void MultiTouchMotionAccumulator::Slot::clear() {
1633 mInUse = false;
1634 mHaveAbsMTTouchMinor = false;
1635 mHaveAbsMTWidthMinor = false;
1636 mHaveAbsMTToolType = false;
1637 mAbsMTPositionX = 0;
1638 mAbsMTPositionY = 0;
1639 mAbsMTTouchMajor = 0;
1640 mAbsMTTouchMinor = 0;
1641 mAbsMTWidthMajor = 0;
1642 mAbsMTWidthMinor = 0;
1643 mAbsMTOrientation = 0;
1644 mAbsMTTrackingId = -1;
1645 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001646 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001647 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001648}
1649
1650int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1651 if (mHaveAbsMTToolType) {
1652 switch (mAbsMTToolType) {
1653 case MT_TOOL_FINGER:
1654 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1655 case MT_TOOL_PEN:
1656 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1657 }
1658 }
1659 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1660}
1661
1662
Jeff Brown6d0fec22010-07-23 21:28:06 -07001663// --- InputMapper ---
1664
1665InputMapper::InputMapper(InputDevice* device) :
1666 mDevice(device), mContext(device->getContext()) {
1667}
1668
1669InputMapper::~InputMapper() {
1670}
1671
1672void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1673 info->addSource(getSources());
1674}
1675
Jeff Brownef3d7e82010-09-30 14:33:04 -07001676void InputMapper::dump(String8& dump) {
1677}
1678
Jeff Brown65fd2512011-08-18 11:20:58 -07001679void InputMapper::configure(nsecs_t when,
1680 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001681}
1682
Jeff Brown65fd2512011-08-18 11:20:58 -07001683void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001684}
1685
Jeff Brownaa3855d2011-03-17 01:34:19 -07001686void InputMapper::timeoutExpired(nsecs_t when) {
1687}
1688
Jeff Brown6d0fec22010-07-23 21:28:06 -07001689int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1690 return AKEY_STATE_UNKNOWN;
1691}
1692
1693int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1694 return AKEY_STATE_UNKNOWN;
1695}
1696
1697int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1698 return AKEY_STATE_UNKNOWN;
1699}
1700
1701bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1702 const int32_t* keyCodes, uint8_t* outFlags) {
1703 return false;
1704}
1705
1706int32_t InputMapper::getMetaState() {
1707 return 0;
1708}
1709
Jeff Brown05dc66a2011-03-02 14:41:58 -08001710void InputMapper::fadePointer() {
1711}
1712
Jeff Brownbe1aa822011-07-27 16:04:54 -07001713status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1714 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1715}
1716
Jeff Browncb1404e2011-01-15 18:14:15 -08001717void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1718 const RawAbsoluteAxisInfo& axis, const char* name) {
1719 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001720 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1721 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001722 } else {
1723 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1724 }
1725}
1726
Jeff Brown6d0fec22010-07-23 21:28:06 -07001727
1728// --- SwitchInputMapper ---
1729
1730SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1731 InputMapper(device) {
1732}
1733
1734SwitchInputMapper::~SwitchInputMapper() {
1735}
1736
1737uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001738 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001739}
1740
1741void SwitchInputMapper::process(const RawEvent* rawEvent) {
1742 switch (rawEvent->type) {
1743 case EV_SW:
1744 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1745 break;
1746 }
1747}
1748
1749void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001750 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1751 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001752}
1753
1754int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1755 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1756}
1757
1758
1759// --- KeyboardInputMapper ---
1760
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001761KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001762 uint32_t source, int32_t keyboardType) :
1763 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001764 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001765}
1766
1767KeyboardInputMapper::~KeyboardInputMapper() {
1768}
1769
Jeff Brown6d0fec22010-07-23 21:28:06 -07001770uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001771 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001772}
1773
1774void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1775 InputMapper::populateDeviceInfo(info);
1776
1777 info->setKeyboardType(mKeyboardType);
1778}
1779
Jeff Brownef3d7e82010-09-30 14:33:04 -07001780void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001781 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1782 dumpParameters(dump);
1783 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001784 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001785 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1786 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1787 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001788}
1789
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001790
Jeff Brown65fd2512011-08-18 11:20:58 -07001791void KeyboardInputMapper::configure(nsecs_t when,
1792 const InputReaderConfiguration* config, uint32_t changes) {
1793 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001794
Jeff Brown474dcb52011-06-14 20:22:50 -07001795 if (!changes) { // first time only
1796 // Configure basic parameters.
1797 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001798 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001799
Jeff Brown65fd2512011-08-18 11:20:58 -07001800 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1801 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1802 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1803 false /*external*/, NULL, NULL, &mOrientation)) {
1804 mOrientation = DISPLAY_ORIENTATION_0;
1805 }
1806 } else {
1807 mOrientation = DISPLAY_ORIENTATION_0;
1808 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001809 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001810}
1811
1812void KeyboardInputMapper::configureParameters() {
1813 mParameters.orientationAware = false;
1814 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1815 mParameters.orientationAware);
1816
Jeff Brownbc68a592011-07-25 12:58:12 -07001817 mParameters.associatedDisplayId = -1;
1818 if (mParameters.orientationAware) {
1819 mParameters.associatedDisplayId = 0;
1820 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001821}
1822
1823void KeyboardInputMapper::dumpParameters(String8& dump) {
1824 dump.append(INDENT3 "Parameters:\n");
1825 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1826 mParameters.associatedDisplayId);
1827 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1828 toString(mParameters.orientationAware));
1829}
1830
Jeff Brown65fd2512011-08-18 11:20:58 -07001831void KeyboardInputMapper::reset(nsecs_t when) {
1832 mMetaState = AMETA_NONE;
1833 mDownTime = 0;
1834 mKeyDowns.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001835
Jeff Brownbe1aa822011-07-27 16:04:54 -07001836 resetLedState();
1837
Jeff Brown65fd2512011-08-18 11:20:58 -07001838 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001839}
1840
1841void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1842 switch (rawEvent->type) {
1843 case EV_KEY: {
1844 int32_t scanCode = rawEvent->scanCode;
1845 if (isKeyboardOrGamepadKey(scanCode)) {
1846 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1847 rawEvent->flags);
1848 }
1849 break;
1850 }
1851 }
1852}
1853
1854bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1855 return scanCode < BTN_MOUSE
1856 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001857 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001858 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001859}
1860
Jeff Brown6328cdc2010-07-29 18:18:33 -07001861void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1862 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001863
Jeff Brownbe1aa822011-07-27 16:04:54 -07001864 if (down) {
1865 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001866 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001867 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001868 }
Jeff Brownfe508922011-01-18 15:10:10 -08001869
Jeff Brownbe1aa822011-07-27 16:04:54 -07001870 // Add key down.
1871 ssize_t keyDownIndex = findKeyDown(scanCode);
1872 if (keyDownIndex >= 0) {
1873 // key repeat, be sure to use same keycode as before in case of rotation
1874 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001875 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001876 // key down
1877 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1878 && mContext->shouldDropVirtualKey(when,
1879 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001880 return;
1881 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001882
1883 mKeyDowns.push();
1884 KeyDown& keyDown = mKeyDowns.editTop();
1885 keyDown.keyCode = keyCode;
1886 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001887 }
1888
Jeff Brownbe1aa822011-07-27 16:04:54 -07001889 mDownTime = when;
1890 } else {
1891 // Remove key down.
1892 ssize_t keyDownIndex = findKeyDown(scanCode);
1893 if (keyDownIndex >= 0) {
1894 // key up, be sure to use same keycode as before in case of rotation
1895 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1896 mKeyDowns.removeAt(size_t(keyDownIndex));
1897 } else {
1898 // key was not actually down
1899 LOGI("Dropping key up from device %s because the key was not down. "
1900 "keyCode=%d, scanCode=%d",
1901 getDeviceName().string(), keyCode, scanCode);
1902 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001903 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001904 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001905
Jeff Brownbe1aa822011-07-27 16:04:54 -07001906 bool metaStateChanged = false;
1907 int32_t oldMetaState = mMetaState;
1908 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1909 if (oldMetaState != newMetaState) {
1910 mMetaState = newMetaState;
1911 metaStateChanged = true;
1912 updateLedState(false);
1913 }
1914
1915 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001916
Jeff Brown56194eb2011-03-02 19:23:13 -08001917 // Key down on external an keyboard should wake the device.
1918 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1919 // For internal keyboards, the key layout file should specify the policy flags for
1920 // each wake key individually.
1921 // TODO: Use the input device configuration to control this behavior more finely.
1922 if (down && getDevice()->isExternal()
1923 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1924 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1925 }
1926
Jeff Brown6328cdc2010-07-29 18:18:33 -07001927 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001928 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001929 }
1930
Jeff Brown05dc66a2011-03-02 14:41:58 -08001931 if (down && !isMetaKey(keyCode)) {
1932 getContext()->fadePointer();
1933 }
1934
Jeff Brownbe1aa822011-07-27 16:04:54 -07001935 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001936 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1937 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001938 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001939}
1940
Jeff Brownbe1aa822011-07-27 16:04:54 -07001941ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1942 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001943 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001944 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001945 return i;
1946 }
1947 }
1948 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001949}
1950
Jeff Brown6d0fec22010-07-23 21:28:06 -07001951int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1952 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1953}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001954
Jeff Brown6d0fec22010-07-23 21:28:06 -07001955int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1956 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1957}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001958
Jeff Brown6d0fec22010-07-23 21:28:06 -07001959bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1960 const int32_t* keyCodes, uint8_t* outFlags) {
1961 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1962}
1963
1964int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001965 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001966}
1967
Jeff Brownbe1aa822011-07-27 16:04:54 -07001968void KeyboardInputMapper::resetLedState() {
1969 initializeLedState(mCapsLockLedState, LED_CAPSL);
1970 initializeLedState(mNumLockLedState, LED_NUML);
1971 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001972
Jeff Brownbe1aa822011-07-27 16:04:54 -07001973 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001974}
1975
Jeff Brownbe1aa822011-07-27 16:04:54 -07001976void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001977 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1978 ledState.on = false;
1979}
1980
Jeff Brownbe1aa822011-07-27 16:04:54 -07001981void KeyboardInputMapper::updateLedState(bool reset) {
1982 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001983 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001984 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001985 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001986 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001987 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001988}
1989
Jeff Brownbe1aa822011-07-27 16:04:54 -07001990void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001991 int32_t led, int32_t modifier, bool reset) {
1992 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001993 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001994 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001995 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1996 ledState.on = desiredState;
1997 }
1998 }
1999}
2000
Jeff Brown6d0fec22010-07-23 21:28:06 -07002001
Jeff Brown83c09682010-12-23 17:50:18 -08002002// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002003
Jeff Brown83c09682010-12-23 17:50:18 -08002004CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002005 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002006}
2007
Jeff Brown83c09682010-12-23 17:50:18 -08002008CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002009}
2010
Jeff Brown83c09682010-12-23 17:50:18 -08002011uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002012 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002013}
2014
Jeff Brown83c09682010-12-23 17:50:18 -08002015void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002016 InputMapper::populateDeviceInfo(info);
2017
Jeff Brown83c09682010-12-23 17:50:18 -08002018 if (mParameters.mode == Parameters::MODE_POINTER) {
2019 float minX, minY, maxX, maxY;
2020 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002021 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2022 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002023 }
2024 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002025 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2026 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002027 }
Jeff Brownefd32662011-03-08 15:13:06 -08002028 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002029
Jeff Brown65fd2512011-08-18 11:20:58 -07002030 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002031 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002032 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002033 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002034 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002035 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002036}
2037
Jeff Brown83c09682010-12-23 17:50:18 -08002038void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002039 dump.append(INDENT2 "Cursor Input Mapper:\n");
2040 dumpParameters(dump);
2041 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2042 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2043 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2044 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2045 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002046 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002047 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002048 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002049 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2050 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002051 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002052 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2053 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2054 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002055}
2056
Jeff Brown65fd2512011-08-18 11:20:58 -07002057void CursorInputMapper::configure(nsecs_t when,
2058 const InputReaderConfiguration* config, uint32_t changes) {
2059 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002060
Jeff Brown474dcb52011-06-14 20:22:50 -07002061 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002062 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002063
Jeff Brown474dcb52011-06-14 20:22:50 -07002064 // Configure basic parameters.
2065 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002066
Jeff Brown474dcb52011-06-14 20:22:50 -07002067 // Configure device mode.
2068 switch (mParameters.mode) {
2069 case Parameters::MODE_POINTER:
2070 mSource = AINPUT_SOURCE_MOUSE;
2071 mXPrecision = 1.0f;
2072 mYPrecision = 1.0f;
2073 mXScale = 1.0f;
2074 mYScale = 1.0f;
2075 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2076 break;
2077 case Parameters::MODE_NAVIGATION:
2078 mSource = AINPUT_SOURCE_TRACKBALL;
2079 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2080 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2081 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2082 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2083 break;
2084 }
2085
2086 mVWheelScale = 1.0f;
2087 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002088 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002089
Jeff Brown474dcb52011-06-14 20:22:50 -07002090 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2091 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2092 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2093 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2094 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002095
2096 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2097 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2098 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2099 false /*external*/, NULL, NULL, &mOrientation)) {
2100 mOrientation = DISPLAY_ORIENTATION_0;
2101 }
2102 } else {
2103 mOrientation = DISPLAY_ORIENTATION_0;
2104 }
2105 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002106}
2107
Jeff Brown83c09682010-12-23 17:50:18 -08002108void CursorInputMapper::configureParameters() {
2109 mParameters.mode = Parameters::MODE_POINTER;
2110 String8 cursorModeString;
2111 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2112 if (cursorModeString == "navigation") {
2113 mParameters.mode = Parameters::MODE_NAVIGATION;
2114 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
2115 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
2116 }
2117 }
2118
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002119 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002120 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002121 mParameters.orientationAware);
2122
Jeff Brownbc68a592011-07-25 12:58:12 -07002123 mParameters.associatedDisplayId = -1;
2124 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2125 mParameters.associatedDisplayId = 0;
2126 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002127}
2128
Jeff Brown83c09682010-12-23 17:50:18 -08002129void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002130 dump.append(INDENT3 "Parameters:\n");
2131 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2132 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002133
2134 switch (mParameters.mode) {
2135 case Parameters::MODE_POINTER:
2136 dump.append(INDENT4 "Mode: pointer\n");
2137 break;
2138 case Parameters::MODE_NAVIGATION:
2139 dump.append(INDENT4 "Mode: navigation\n");
2140 break;
2141 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002142 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002143 }
2144
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002145 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2146 toString(mParameters.orientationAware));
2147}
2148
Jeff Brown65fd2512011-08-18 11:20:58 -07002149void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002150 mButtonState = 0;
2151 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002152
Jeff Brownbe1aa822011-07-27 16:04:54 -07002153 mPointerVelocityControl.reset();
2154 mWheelXVelocityControl.reset();
2155 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002156
Jeff Brown65fd2512011-08-18 11:20:58 -07002157 mCursorButtonAccumulator.reset(getDevice());
2158 mCursorMotionAccumulator.reset(getDevice());
2159 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002160
Jeff Brown65fd2512011-08-18 11:20:58 -07002161 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002162}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002163
Jeff Brown83c09682010-12-23 17:50:18 -08002164void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002165 mCursorButtonAccumulator.process(rawEvent);
2166 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002167 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002168
Jeff Brown49754db2011-07-01 17:37:58 -07002169 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2170 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002171 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002172}
2173
Jeff Brown83c09682010-12-23 17:50:18 -08002174void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002175 int32_t lastButtonState = mButtonState;
2176 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2177 mButtonState = currentButtonState;
2178
2179 bool wasDown = isPointerDown(lastButtonState);
2180 bool down = isPointerDown(currentButtonState);
2181 bool downChanged;
2182 if (!wasDown && down) {
2183 mDownTime = when;
2184 downChanged = true;
2185 } else if (wasDown && !down) {
2186 downChanged = true;
2187 } else {
2188 downChanged = false;
2189 }
2190 nsecs_t downTime = mDownTime;
2191 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002192 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002193
2194 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2195 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2196 bool moved = deltaX != 0 || deltaY != 0;
2197
Jeff Brown65fd2512011-08-18 11:20:58 -07002198 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002199 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2200 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002201 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002202 }
2203
Jeff Brown65fd2512011-08-18 11:20:58 -07002204 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002205 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002206 pointerProperties.clear();
2207 pointerProperties.id = 0;
2208 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2209
Jeff Brown6328cdc2010-07-29 18:18:33 -07002210 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002211 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002212
Jeff Brown65fd2512011-08-18 11:20:58 -07002213 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2214 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002215 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002216
Jeff Brownbe1aa822011-07-27 16:04:54 -07002217 mWheelYVelocityControl.move(when, NULL, &vscroll);
2218 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002219
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002221
Jeff Brownbe1aa822011-07-27 16:04:54 -07002222 if (mPointerController != NULL) {
2223 if (moved || scrolled || buttonsChanged) {
2224 mPointerController->setPresentation(
2225 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002226
Jeff Brownbe1aa822011-07-27 16:04:54 -07002227 if (moved) {
2228 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002229 }
2230
Jeff Brownbe1aa822011-07-27 16:04:54 -07002231 if (buttonsChanged) {
2232 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002233 }
Jeff Brownefd32662011-03-08 15:13:06 -08002234
Jeff Brownbe1aa822011-07-27 16:04:54 -07002235 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002236 }
2237
Jeff Brownbe1aa822011-07-27 16:04:54 -07002238 float x, y;
2239 mPointerController->getPosition(&x, &y);
2240 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2241 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2242 } else {
2243 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2244 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2245 }
2246
2247 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002248
Jeff Brown56194eb2011-03-02 19:23:13 -08002249 // Moving an external trackball or mouse should wake the device.
2250 // We don't do this for internal cursor devices to prevent them from waking up
2251 // the device in your pocket.
2252 // TODO: Use the input device configuration to control this behavior more finely.
2253 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002254 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002255 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2256 }
2257
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002258 // Synthesize key down from buttons if needed.
2259 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2260 policyFlags, lastButtonState, currentButtonState);
2261
2262 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002263 if (downChanged || moved || scrolled || buttonsChanged) {
2264 int32_t metaState = mContext->getGlobalMetaState();
2265 int32_t motionEventAction;
2266 if (downChanged) {
2267 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2268 } else if (down || mPointerController == NULL) {
2269 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2270 } else {
2271 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2272 }
Jeff Brownb6997262010-10-08 22:31:17 -07002273
Jeff Brownbe1aa822011-07-27 16:04:54 -07002274 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2275 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002276 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002277 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002278
Jeff Brownbe1aa822011-07-27 16:04:54 -07002279 // Send hover move after UP to tell the application that the mouse is hovering now.
2280 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2281 && mPointerController != NULL) {
2282 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2283 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2284 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2285 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2286 getListener()->notifyMotion(&hoverArgs);
2287 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002288
Jeff Brownbe1aa822011-07-27 16:04:54 -07002289 // Send scroll events.
2290 if (scrolled) {
2291 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2292 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2293
2294 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2295 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2296 AMOTION_EVENT_EDGE_FLAG_NONE,
2297 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2298 getListener()->notifyMotion(&scrollArgs);
2299 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002300 }
Jeff Browna032cc02011-03-07 16:56:21 -08002301
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002302 // Synthesize key up from buttons if needed.
2303 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2304 policyFlags, lastButtonState, currentButtonState);
2305
Jeff Brown65fd2512011-08-18 11:20:58 -07002306 mCursorMotionAccumulator.finishSync();
2307 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002308}
2309
Jeff Brown83c09682010-12-23 17:50:18 -08002310int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002311 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2312 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2313 } else {
2314 return AKEY_STATE_UNKNOWN;
2315 }
2316}
2317
Jeff Brown05dc66a2011-03-02 14:41:58 -08002318void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002319 if (mPointerController != NULL) {
2320 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2321 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002322}
2323
Jeff Brown6d0fec22010-07-23 21:28:06 -07002324
2325// --- TouchInputMapper ---
2326
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002327TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002328 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002329 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002330 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002331}
2332
2333TouchInputMapper::~TouchInputMapper() {
2334}
2335
2336uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002337 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002338}
2339
2340void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2341 InputMapper::populateDeviceInfo(info);
2342
Jeff Brown65fd2512011-08-18 11:20:58 -07002343 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2344 info->addMotionRange(mOrientedRanges.x);
2345 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002346 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002347
Jeff Brown65fd2512011-08-18 11:20:58 -07002348 if (mOrientedRanges.haveSize) {
2349 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002350 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002351
2352 if (mOrientedRanges.haveTouchSize) {
2353 info->addMotionRange(mOrientedRanges.touchMajor);
2354 info->addMotionRange(mOrientedRanges.touchMinor);
2355 }
2356
2357 if (mOrientedRanges.haveToolSize) {
2358 info->addMotionRange(mOrientedRanges.toolMajor);
2359 info->addMotionRange(mOrientedRanges.toolMinor);
2360 }
2361
2362 if (mOrientedRanges.haveOrientation) {
2363 info->addMotionRange(mOrientedRanges.orientation);
2364 }
2365
2366 if (mOrientedRanges.haveDistance) {
2367 info->addMotionRange(mOrientedRanges.distance);
2368 }
2369
2370 if (mOrientedRanges.haveTilt) {
2371 info->addMotionRange(mOrientedRanges.tilt);
2372 }
2373
2374 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2375 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2376 }
2377 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2378 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2379 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002380 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002381}
2382
Jeff Brownef3d7e82010-09-30 14:33:04 -07002383void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002384 dump.append(INDENT2 "Touch Input Mapper:\n");
2385 dumpParameters(dump);
2386 dumpVirtualKeys(dump);
2387 dumpRawPointerAxes(dump);
2388 dumpCalibration(dump);
2389 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002390
Jeff Brownbe1aa822011-07-27 16:04:54 -07002391 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2392 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2393 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2394 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2395 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2396 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002397 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2398 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002399 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002400 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2401 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002402 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2403 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2404 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2405 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2406 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002407
Jeff Brownbe1aa822011-07-27 16:04:54 -07002408 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002409
Jeff Brownbe1aa822011-07-27 16:04:54 -07002410 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2411 mLastRawPointerData.pointerCount);
2412 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2413 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2414 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2415 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002416 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2417 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002418 pointer.id, pointer.x, pointer.y, pointer.pressure,
2419 pointer.touchMajor, pointer.touchMinor,
2420 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002421 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002422 pointer.toolType, toString(pointer.isHovering));
2423 }
2424
2425 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2426 mLastCookedPointerData.pointerCount);
2427 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2428 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2429 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2430 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2431 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002432 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2433 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002434 pointerProperties.id,
2435 pointerCoords.getX(),
2436 pointerCoords.getY(),
2437 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2438 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2439 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2440 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2441 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2442 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002443 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002444 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2445 pointerProperties.toolType,
2446 toString(mLastCookedPointerData.isHovering(i)));
2447 }
2448
Jeff Brown65fd2512011-08-18 11:20:58 -07002449 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002450 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2451 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002452 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002454 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002455 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002456 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002457 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002458 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2460 mPointerGestureMaxSwipeWidth);
2461 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002462}
2463
Jeff Brown65fd2512011-08-18 11:20:58 -07002464void TouchInputMapper::configure(nsecs_t when,
2465 const InputReaderConfiguration* config, uint32_t changes) {
2466 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002467
Jeff Brown474dcb52011-06-14 20:22:50 -07002468 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002469
Jeff Brown474dcb52011-06-14 20:22:50 -07002470 if (!changes) { // first time only
2471 // Configure basic parameters.
2472 configureParameters();
2473
Jeff Brown65fd2512011-08-18 11:20:58 -07002474 // Configure common accumulators.
2475 mCursorScrollAccumulator.configure(getDevice());
2476 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002477
2478 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002479 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002480
2481 // Prepare input device calibration.
2482 parseCalibration();
2483 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002484 }
2485
Jeff Brown474dcb52011-06-14 20:22:50 -07002486 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002487 // Update pointer speed.
2488 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2489 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2490 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002491 }
Jeff Brown8d608662010-08-30 03:02:23 -07002492
Jeff Brown65fd2512011-08-18 11:20:58 -07002493 bool resetNeeded = false;
2494 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002495 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2496 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002497 // Configure device sources, surface dimensions, orientation and
2498 // scaling factors.
2499 configureSurface(when, &resetNeeded);
2500 }
2501
2502 if (changes && resetNeeded) {
2503 // Send reset, unless this is the first time the device has been configured,
2504 // in which case the reader will call reset itself after all mappers are ready.
2505 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002506 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002507}
2508
Jeff Brown8d608662010-08-30 03:02:23 -07002509void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002510 // Use the pointer presentation mode for devices that do not support distinct
2511 // multitouch. The spot-based presentation relies on being able to accurately
2512 // locate two or more fingers on the touch pad.
2513 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2514 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002515
Jeff Brown538881e2011-05-25 18:23:38 -07002516 String8 gestureModeString;
2517 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2518 gestureModeString)) {
2519 if (gestureModeString == "pointer") {
2520 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2521 } else if (gestureModeString == "spots") {
2522 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2523 } else if (gestureModeString != "default") {
2524 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2525 }
2526 }
2527
Jeff Browndeffe072011-08-26 18:38:46 -07002528 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2529 // The device is a touch screen.
2530 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2531 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2532 // The device is a pointing device like a track pad.
2533 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2534 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002535 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2536 // The device is a cursor device with a touch pad attached.
2537 // By default don't use the touch pad to move the pointer.
2538 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2539 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002540 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002541 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2542 }
2543
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002544 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002545 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2546 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002547 if (deviceTypeString == "touchScreen") {
2548 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002549 } else if (deviceTypeString == "touchPad") {
2550 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002551 } else if (deviceTypeString == "pointer") {
2552 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002553 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002554 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2555 }
2556 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002557
Jeff Brownefd32662011-03-08 15:13:06 -08002558 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002559 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2560 mParameters.orientationAware);
2561
Jeff Brownbc68a592011-07-25 12:58:12 -07002562 mParameters.associatedDisplayId = -1;
2563 mParameters.associatedDisplayIsExternal = false;
2564 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002565 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002566 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2567 mParameters.associatedDisplayIsExternal =
2568 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2569 && getDevice()->isExternal();
2570 mParameters.associatedDisplayId = 0;
2571 }
Jeff Brown8d608662010-08-30 03:02:23 -07002572}
2573
Jeff Brownef3d7e82010-09-30 14:33:04 -07002574void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002575 dump.append(INDENT3 "Parameters:\n");
2576
Jeff Brown538881e2011-05-25 18:23:38 -07002577 switch (mParameters.gestureMode) {
2578 case Parameters::GESTURE_MODE_POINTER:
2579 dump.append(INDENT4 "GestureMode: pointer\n");
2580 break;
2581 case Parameters::GESTURE_MODE_SPOTS:
2582 dump.append(INDENT4 "GestureMode: spots\n");
2583 break;
2584 default:
2585 assert(false);
2586 }
2587
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002588 switch (mParameters.deviceType) {
2589 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2590 dump.append(INDENT4 "DeviceType: touchScreen\n");
2591 break;
2592 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2593 dump.append(INDENT4 "DeviceType: touchPad\n");
2594 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002595 case Parameters::DEVICE_TYPE_POINTER:
2596 dump.append(INDENT4 "DeviceType: pointer\n");
2597 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002598 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002599 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002600 }
2601
Jeff Brown65fd2512011-08-18 11:20:58 -07002602 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2603 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002604 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2605 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002606}
2607
Jeff Brownbe1aa822011-07-27 16:04:54 -07002608void TouchInputMapper::configureRawPointerAxes() {
2609 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002610}
2611
Jeff Brownbe1aa822011-07-27 16:04:54 -07002612void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2613 dump.append(INDENT3 "Raw Touch Axes:\n");
2614 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2615 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2616 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2617 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2618 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2619 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2620 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2621 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2622 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002623 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2624 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002625 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2626 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002627}
2628
Jeff Brown65fd2512011-08-18 11:20:58 -07002629void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2630 int32_t oldDeviceMode = mDeviceMode;
2631
2632 // Determine device mode.
2633 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2634 && mConfig.pointerGesturesEnabled) {
2635 mSource = AINPUT_SOURCE_MOUSE;
2636 mDeviceMode = DEVICE_MODE_POINTER;
2637 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2638 && mParameters.associatedDisplayId >= 0) {
2639 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2640 mDeviceMode = DEVICE_MODE_DIRECT;
2641 } else {
2642 mSource = AINPUT_SOURCE_TOUCHPAD;
2643 mDeviceMode = DEVICE_MODE_UNSCALED;
2644 }
2645
Jeff Brown9626b142011-03-03 02:09:54 -08002646 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002647 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002648 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2649 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002650 mDeviceMode = DEVICE_MODE_DISABLED;
2651 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002652 }
2653
Jeff Brown65fd2512011-08-18 11:20:58 -07002654 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002655 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002656 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002657 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002658 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2659 &mAssociatedDisplayOrientation)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002660 LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
2661 "display %d. The device will be inoperable until the display size "
2662 "becomes available.",
2663 getDeviceName().string(), mParameters.associatedDisplayId);
2664 mDeviceMode = DEVICE_MODE_DISABLED;
2665 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002666 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002667 }
2668
Jeff Brown65fd2512011-08-18 11:20:58 -07002669 // Configure dimensions.
2670 int32_t width, height, orientation;
2671 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2672 width = mAssociatedDisplayWidth;
2673 height = mAssociatedDisplayHeight;
2674 orientation = mParameters.orientationAware ?
2675 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2676 } else {
2677 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2678 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2679 orientation = DISPLAY_ORIENTATION_0;
2680 }
2681
2682 // If moving between pointer modes, need to reset some state.
2683 bool deviceModeChanged;
2684 if (mDeviceMode != oldDeviceMode) {
2685 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002686 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002687 }
2688
Jeff Browndaf4a122011-08-26 17:14:14 -07002689 // Create pointer controller if needed.
2690 if (mDeviceMode == DEVICE_MODE_POINTER ||
2691 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2692 if (mPointerController == NULL) {
2693 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2694 }
2695 } else {
2696 mPointerController.clear();
2697 }
2698
Jeff Brownbe1aa822011-07-27 16:04:54 -07002699 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002700 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002701 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002702 }
2703
Jeff Brownbe1aa822011-07-27 16:04:54 -07002704 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002705 if (sizeChanged || deviceModeChanged) {
2706 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
2707 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002708
Jeff Brownbe1aa822011-07-27 16:04:54 -07002709 mSurfaceWidth = width;
2710 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002711
Jeff Brown8d608662010-08-30 03:02:23 -07002712 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002713 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2714 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2715 mXPrecision = 1.0f / mXScale;
2716 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002717
Jeff Brownbe1aa822011-07-27 16:04:54 -07002718 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002719 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002720 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002721 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002722
Jeff Brownbe1aa822011-07-27 16:04:54 -07002723 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002724
Jeff Brown8d608662010-08-30 03:02:23 -07002725 // Scale factor for terms that are not oriented in a particular axis.
2726 // If the pixels are square then xScale == yScale otherwise we fake it
2727 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002728 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002729
Jeff Brown8d608662010-08-30 03:02:23 -07002730 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002731 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002732
Jeff Browna1f89ce2011-08-11 00:05:01 -07002733 // Size factors.
2734 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2735 if (mRawPointerAxes.touchMajor.valid
2736 && mRawPointerAxes.touchMajor.maxValue != 0) {
2737 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2738 } else if (mRawPointerAxes.toolMajor.valid
2739 && mRawPointerAxes.toolMajor.maxValue != 0) {
2740 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2741 } else {
2742 mSizeScale = 0.0f;
2743 }
2744
Jeff Brownbe1aa822011-07-27 16:04:54 -07002745 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002746 mOrientedRanges.haveToolSize = true;
2747 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002748
Jeff Brownbe1aa822011-07-27 16:04:54 -07002749 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002750 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002751 mOrientedRanges.touchMajor.min = 0;
2752 mOrientedRanges.touchMajor.max = diagonalSize;
2753 mOrientedRanges.touchMajor.flat = 0;
2754 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002755
Jeff Brownbe1aa822011-07-27 16:04:54 -07002756 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2757 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002758
Jeff Brownbe1aa822011-07-27 16:04:54 -07002759 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002760 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002761 mOrientedRanges.toolMajor.min = 0;
2762 mOrientedRanges.toolMajor.max = diagonalSize;
2763 mOrientedRanges.toolMajor.flat = 0;
2764 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002765
Jeff Brownbe1aa822011-07-27 16:04:54 -07002766 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2767 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002768
2769 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002770 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002771 mOrientedRanges.size.min = 0;
2772 mOrientedRanges.size.max = 1.0;
2773 mOrientedRanges.size.flat = 0;
2774 mOrientedRanges.size.fuzz = 0;
2775 } else {
2776 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002777 }
2778
2779 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002780 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002781 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2782 || mCalibration.pressureCalibration
2783 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2784 if (mCalibration.havePressureScale) {
2785 mPressureScale = mCalibration.pressureScale;
2786 } else if (mRawPointerAxes.pressure.valid
2787 && mRawPointerAxes.pressure.maxValue != 0) {
2788 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002789 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002790 }
Jeff Brown8d608662010-08-30 03:02:23 -07002791
Jeff Brown65fd2512011-08-18 11:20:58 -07002792 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2793 mOrientedRanges.pressure.source = mSource;
2794 mOrientedRanges.pressure.min = 0;
2795 mOrientedRanges.pressure.max = 1.0;
2796 mOrientedRanges.pressure.flat = 0;
2797 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002798
Jeff Brown65fd2512011-08-18 11:20:58 -07002799 // Tilt
2800 mTiltXCenter = 0;
2801 mTiltXScale = 0;
2802 mTiltYCenter = 0;
2803 mTiltYScale = 0;
2804 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2805 if (mHaveTilt) {
2806 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2807 mRawPointerAxes.tiltX.maxValue);
2808 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2809 mRawPointerAxes.tiltY.maxValue);
2810 mTiltXScale = M_PI / 180;
2811 mTiltYScale = M_PI / 180;
2812
2813 mOrientedRanges.haveTilt = true;
2814
2815 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2816 mOrientedRanges.tilt.source = mSource;
2817 mOrientedRanges.tilt.min = 0;
2818 mOrientedRanges.tilt.max = M_PI_2;
2819 mOrientedRanges.tilt.flat = 0;
2820 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002821 }
2822
Jeff Brown8d608662010-08-30 03:02:23 -07002823 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002824 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002825 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002826 if (mHaveTilt) {
2827 mOrientedRanges.haveOrientation = true;
2828
2829 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2830 mOrientedRanges.orientation.source = mSource;
2831 mOrientedRanges.orientation.min = -M_PI;
2832 mOrientedRanges.orientation.max = M_PI;
2833 mOrientedRanges.orientation.flat = 0;
2834 mOrientedRanges.orientation.fuzz = 0;
2835 } else if (mCalibration.orientationCalibration !=
2836 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002837 if (mCalibration.orientationCalibration
2838 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002839 if (mRawPointerAxes.orientation.valid) {
2840 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2841 mRawPointerAxes.orientation.maxValue);
2842 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2843 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002844 }
2845 }
2846
Jeff Brownbe1aa822011-07-27 16:04:54 -07002847 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002848
Jeff Brownbe1aa822011-07-27 16:04:54 -07002849 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002850 mOrientedRanges.orientation.source = mSource;
2851 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002852 mOrientedRanges.orientation.max = M_PI_2;
2853 mOrientedRanges.orientation.flat = 0;
2854 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002855 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002856
2857 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002858 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002859 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2860 if (mCalibration.distanceCalibration
2861 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2862 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002863 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002864 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002865 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002866 }
2867 }
2868
Jeff Brownbe1aa822011-07-27 16:04:54 -07002869 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002870
Jeff Brownbe1aa822011-07-27 16:04:54 -07002871 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002872 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002873 mOrientedRanges.distance.min =
2874 mRawPointerAxes.distance.minValue * mDistanceScale;
2875 mOrientedRanges.distance.max =
2876 mRawPointerAxes.distance.minValue * mDistanceScale;
2877 mOrientedRanges.distance.flat = 0;
2878 mOrientedRanges.distance.fuzz =
2879 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002880 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002881 }
2882
Jeff Brown65fd2512011-08-18 11:20:58 -07002883 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002884 // Compute oriented surface dimensions, precision, scales and ranges.
2885 // Note that the maximum value reported is an inclusive maximum value so it is one
2886 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002887 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002888 case DISPLAY_ORIENTATION_90:
2889 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002890 mOrientedSurfaceWidth = mSurfaceHeight;
2891 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002892
Jeff Brownbe1aa822011-07-27 16:04:54 -07002893 mOrientedXPrecision = mYPrecision;
2894 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002895
Jeff Brownbe1aa822011-07-27 16:04:54 -07002896 mOrientedRanges.x.min = 0;
2897 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2898 * mYScale;
2899 mOrientedRanges.x.flat = 0;
2900 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002901
Jeff Brownbe1aa822011-07-27 16:04:54 -07002902 mOrientedRanges.y.min = 0;
2903 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2904 * mXScale;
2905 mOrientedRanges.y.flat = 0;
2906 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002907 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002908
Jeff Brown6d0fec22010-07-23 21:28:06 -07002909 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002910 mOrientedSurfaceWidth = mSurfaceWidth;
2911 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002912
Jeff Brownbe1aa822011-07-27 16:04:54 -07002913 mOrientedXPrecision = mXPrecision;
2914 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002915
Jeff Brownbe1aa822011-07-27 16:04:54 -07002916 mOrientedRanges.x.min = 0;
2917 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2918 * mXScale;
2919 mOrientedRanges.x.flat = 0;
2920 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002921
Jeff Brownbe1aa822011-07-27 16:04:54 -07002922 mOrientedRanges.y.min = 0;
2923 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2924 * mYScale;
2925 mOrientedRanges.y.flat = 0;
2926 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002927 break;
2928 }
Jeff Brownace13b12011-03-09 17:39:48 -08002929
2930 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002931 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002932 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2933 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002934 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002935 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2936 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002937
Jeff Brown2352b972011-04-12 22:39:53 -07002938 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002939 // given area relative to the diagonal size of the display when no acceleration
2940 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002941 // Assume that the touch pad has a square aspect ratio such that movements in
2942 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002943 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002944 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002945 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002946
2947 // Scale zooms to cover a smaller range of the display than movements do.
2948 // This value determines the area around the pointer that is affected by freeform
2949 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002950 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002951 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002952 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002953
Jeff Brown2352b972011-04-12 22:39:53 -07002954 // Max width between pointers to detect a swipe gesture is more than some fraction
2955 // of the diagonal axis of the touch pad. Touches that are wider than this are
2956 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002957 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002958 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002959 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002960
Jeff Brown65fd2512011-08-18 11:20:58 -07002961 // Abort current pointer usages because the state has changed.
2962 abortPointerUsage(when, 0 /*policyFlags*/);
2963
2964 // Inform the dispatcher about the changes.
2965 *outResetNeeded = true;
2966 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002967}
2968
Jeff Brownbe1aa822011-07-27 16:04:54 -07002969void TouchInputMapper::dumpSurface(String8& dump) {
2970 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2971 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2972 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002973}
2974
Jeff Brownbe1aa822011-07-27 16:04:54 -07002975void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002976 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002977 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002978
Jeff Brownbe1aa822011-07-27 16:04:54 -07002979 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002980
Jeff Brown6328cdc2010-07-29 18:18:33 -07002981 if (virtualKeyDefinitions.size() == 0) {
2982 return;
2983 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002984
Jeff Brownbe1aa822011-07-27 16:04:54 -07002985 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002986
Jeff Brownbe1aa822011-07-27 16:04:54 -07002987 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2988 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2989 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2990 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002991
2992 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002993 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002994 virtualKeyDefinitions[i];
2995
Jeff Brownbe1aa822011-07-27 16:04:54 -07002996 mVirtualKeys.add();
2997 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002998
2999 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3000 int32_t keyCode;
3001 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08003002 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07003003 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003004 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
3005 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003006 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003007 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003008 }
3009
Jeff Brown6328cdc2010-07-29 18:18:33 -07003010 virtualKey.keyCode = keyCode;
3011 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003012
Jeff Brown6328cdc2010-07-29 18:18:33 -07003013 // convert the key definition's display coordinates into touch coordinates for a hit box
3014 int32_t halfWidth = virtualKeyDefinition.width / 2;
3015 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003016
Jeff Brown6328cdc2010-07-29 18:18:33 -07003017 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003018 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003019 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003020 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003021 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003022 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003023 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003024 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003025 }
3026}
3027
Jeff Brownbe1aa822011-07-27 16:04:54 -07003028void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3029 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003030 dump.append(INDENT3 "Virtual Keys:\n");
3031
Jeff Brownbe1aa822011-07-27 16:04:54 -07003032 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3033 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003034 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3035 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3036 i, virtualKey.scanCode, virtualKey.keyCode,
3037 virtualKey.hitLeft, virtualKey.hitRight,
3038 virtualKey.hitTop, virtualKey.hitBottom);
3039 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003040 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003041}
3042
Jeff Brown8d608662010-08-30 03:02:23 -07003043void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003044 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003045 Calibration& out = mCalibration;
3046
Jeff Browna1f89ce2011-08-11 00:05:01 -07003047 // Size
3048 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3049 String8 sizeCalibrationString;
3050 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3051 if (sizeCalibrationString == "none") {
3052 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3053 } else if (sizeCalibrationString == "geometric") {
3054 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3055 } else if (sizeCalibrationString == "diameter") {
3056 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3057 } else if (sizeCalibrationString == "area") {
3058 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3059 } else if (sizeCalibrationString != "default") {
3060 LOGW("Invalid value for touch.size.calibration: '%s'",
3061 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003062 }
3063 }
3064
Jeff Browna1f89ce2011-08-11 00:05:01 -07003065 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3066 out.sizeScale);
3067 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3068 out.sizeBias);
3069 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3070 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003071
3072 // Pressure
3073 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3074 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003075 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003076 if (pressureCalibrationString == "none") {
3077 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3078 } else if (pressureCalibrationString == "physical") {
3079 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3080 } else if (pressureCalibrationString == "amplitude") {
3081 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3082 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003083 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003084 pressureCalibrationString.string());
3085 }
3086 }
3087
Jeff Brown8d608662010-08-30 03:02:23 -07003088 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3089 out.pressureScale);
3090
Jeff Brown8d608662010-08-30 03:02:23 -07003091 // Orientation
3092 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3093 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003094 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003095 if (orientationCalibrationString == "none") {
3096 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3097 } else if (orientationCalibrationString == "interpolated") {
3098 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003099 } else if (orientationCalibrationString == "vector") {
3100 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003101 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003102 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003103 orientationCalibrationString.string());
3104 }
3105 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003106
3107 // Distance
3108 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3109 String8 distanceCalibrationString;
3110 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3111 if (distanceCalibrationString == "none") {
3112 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3113 } else if (distanceCalibrationString == "scaled") {
3114 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3115 } else if (distanceCalibrationString != "default") {
3116 LOGW("Invalid value for touch.distance.calibration: '%s'",
3117 distanceCalibrationString.string());
3118 }
3119 }
3120
3121 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3122 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003123}
3124
3125void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003126 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003127 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3128 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3129 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003130 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003131 } else {
3132 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3133 }
Jeff Brown8d608662010-08-30 03:02:23 -07003134
Jeff Browna1f89ce2011-08-11 00:05:01 -07003135 // Pressure
3136 if (mRawPointerAxes.pressure.valid) {
3137 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3138 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3139 }
3140 } else {
3141 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003142 }
3143
3144 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003145 if (mRawPointerAxes.orientation.valid) {
3146 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003147 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003148 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003149 } else {
3150 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003151 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003152
3153 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003154 if (mRawPointerAxes.distance.valid) {
3155 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003156 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003157 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003158 } else {
3159 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003160 }
Jeff Brown8d608662010-08-30 03:02:23 -07003161}
3162
Jeff Brownef3d7e82010-09-30 14:33:04 -07003163void TouchInputMapper::dumpCalibration(String8& dump) {
3164 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003165
Jeff Browna1f89ce2011-08-11 00:05:01 -07003166 // Size
3167 switch (mCalibration.sizeCalibration) {
3168 case Calibration::SIZE_CALIBRATION_NONE:
3169 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003170 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003171 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3172 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003173 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003174 case Calibration::SIZE_CALIBRATION_DIAMETER:
3175 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3176 break;
3177 case Calibration::SIZE_CALIBRATION_AREA:
3178 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003179 break;
3180 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003181 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003182 }
3183
Jeff Browna1f89ce2011-08-11 00:05:01 -07003184 if (mCalibration.haveSizeScale) {
3185 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3186 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003187 }
3188
Jeff Browna1f89ce2011-08-11 00:05:01 -07003189 if (mCalibration.haveSizeBias) {
3190 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3191 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003192 }
3193
Jeff Browna1f89ce2011-08-11 00:05:01 -07003194 if (mCalibration.haveSizeIsSummed) {
3195 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3196 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003197 }
3198
3199 // Pressure
3200 switch (mCalibration.pressureCalibration) {
3201 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003202 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003203 break;
3204 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003205 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003206 break;
3207 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003208 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003209 break;
3210 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003211 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003212 }
3213
Jeff Brown8d608662010-08-30 03:02:23 -07003214 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003215 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3216 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003217 }
3218
Jeff Brown8d608662010-08-30 03:02:23 -07003219 // Orientation
3220 switch (mCalibration.orientationCalibration) {
3221 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003222 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003223 break;
3224 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003225 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003226 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003227 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3228 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3229 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003230 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003231 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003232 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003233
3234 // Distance
3235 switch (mCalibration.distanceCalibration) {
3236 case Calibration::DISTANCE_CALIBRATION_NONE:
3237 dump.append(INDENT4 "touch.distance.calibration: none\n");
3238 break;
3239 case Calibration::DISTANCE_CALIBRATION_SCALED:
3240 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3241 break;
3242 default:
3243 LOG_ASSERT(false);
3244 }
3245
3246 if (mCalibration.haveDistanceScale) {
3247 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3248 mCalibration.distanceScale);
3249 }
Jeff Brown8d608662010-08-30 03:02:23 -07003250}
3251
Jeff Brown65fd2512011-08-18 11:20:58 -07003252void TouchInputMapper::reset(nsecs_t when) {
3253 mCursorButtonAccumulator.reset(getDevice());
3254 mCursorScrollAccumulator.reset(getDevice());
3255 mTouchButtonAccumulator.reset(getDevice());
3256
3257 mPointerVelocityControl.reset();
3258 mWheelXVelocityControl.reset();
3259 mWheelYVelocityControl.reset();
3260
Jeff Brownbe1aa822011-07-27 16:04:54 -07003261 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003262 mLastRawPointerData.clear();
3263 mCurrentCookedPointerData.clear();
3264 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003265 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003266 mLastButtonState = 0;
3267 mCurrentRawVScroll = 0;
3268 mCurrentRawHScroll = 0;
3269 mCurrentFingerIdBits.clear();
3270 mLastFingerIdBits.clear();
3271 mCurrentStylusIdBits.clear();
3272 mLastStylusIdBits.clear();
3273 mCurrentMouseIdBits.clear();
3274 mLastMouseIdBits.clear();
3275 mPointerUsage = POINTER_USAGE_NONE;
3276 mSentHoverEnter = false;
3277 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003278
Jeff Brown65fd2512011-08-18 11:20:58 -07003279 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003280
Jeff Brown65fd2512011-08-18 11:20:58 -07003281 mPointerGesture.reset();
3282 mPointerSimple.reset();
3283
3284 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003285 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3286 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003287 }
3288
Jeff Brown65fd2512011-08-18 11:20:58 -07003289 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003290}
3291
Jeff Brown65fd2512011-08-18 11:20:58 -07003292void TouchInputMapper::process(const RawEvent* rawEvent) {
3293 mCursorButtonAccumulator.process(rawEvent);
3294 mCursorScrollAccumulator.process(rawEvent);
3295 mTouchButtonAccumulator.process(rawEvent);
3296
3297 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3298 sync(rawEvent->when);
3299 }
3300}
3301
3302void TouchInputMapper::sync(nsecs_t when) {
3303 // Sync button state.
3304 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3305 | mCursorButtonAccumulator.getButtonState();
3306
3307 // Sync scroll state.
3308 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3309 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3310 mCursorScrollAccumulator.finishSync();
3311
3312 // Sync touch state.
3313 bool havePointerIds = true;
3314 mCurrentRawPointerData.clear();
3315 syncTouch(when, &havePointerIds);
3316
Jeff Brownaa3855d2011-03-17 01:34:19 -07003317#if DEBUG_RAW_EVENTS
3318 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003319 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3320 mLastRawPointerData.pointerCount,
3321 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003322 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003323 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3324 "hovering ids 0x%08x -> 0x%08x",
3325 mLastRawPointerData.pointerCount,
3326 mCurrentRawPointerData.pointerCount,
3327 mLastRawPointerData.touchingIdBits.value,
3328 mCurrentRawPointerData.touchingIdBits.value,
3329 mLastRawPointerData.hoveringIdBits.value,
3330 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003331 }
3332#endif
3333
Jeff Brown65fd2512011-08-18 11:20:58 -07003334 // Reset state that we will compute below.
3335 mCurrentFingerIdBits.clear();
3336 mCurrentStylusIdBits.clear();
3337 mCurrentMouseIdBits.clear();
3338 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003339
Jeff Brown65fd2512011-08-18 11:20:58 -07003340 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3341 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003342 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003343 mCurrentButtonState = 0;
3344 } else {
3345 // Preprocess pointer data.
3346 if (!havePointerIds) {
3347 assignPointerIds();
3348 }
3349
3350 // Handle policy on initial down or hover events.
3351 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003352 bool initialDown = mLastRawPointerData.pointerCount == 0
3353 && mCurrentRawPointerData.pointerCount != 0;
3354 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3355 if (initialDown || buttonsPressed) {
3356 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003357 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003358 getContext()->fadePointer();
3359 }
3360
3361 // Initial downs on external touch devices should wake the device.
3362 // We don't do this for internal touch screens to prevent them from waking
3363 // up in your pocket.
3364 // TODO: Use the input device configuration to control this behavior more finely.
3365 if (getDevice()->isExternal()) {
3366 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3367 }
3368 }
3369
3370 // Synthesize key down from raw buttons if needed.
3371 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3372 policyFlags, mLastButtonState, mCurrentButtonState);
3373
3374 // Consume raw off-screen touches before cooking pointer data.
3375 // If touches are consumed, subsequent code will not receive any pointer data.
3376 if (consumeRawTouches(when, policyFlags)) {
3377 mCurrentRawPointerData.clear();
3378 }
3379
3380 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3381 // with cooked pointer data that has the same ids and indices as the raw data.
3382 // The following code can use either the raw or cooked data, as needed.
3383 cookPointerData();
3384
3385 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003386 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003387 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3388 uint32_t id = idBits.clearFirstMarkedBit();
3389 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3390 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3391 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3392 mCurrentStylusIdBits.markBit(id);
3393 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3394 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3395 mCurrentFingerIdBits.markBit(id);
3396 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3397 mCurrentMouseIdBits.markBit(id);
3398 }
3399 }
3400 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3401 uint32_t id = idBits.clearFirstMarkedBit();
3402 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3403 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3404 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3405 mCurrentStylusIdBits.markBit(id);
3406 }
3407 }
3408
3409 // Stylus takes precedence over all tools, then mouse, then finger.
3410 PointerUsage pointerUsage = mPointerUsage;
3411 if (!mCurrentStylusIdBits.isEmpty()) {
3412 mCurrentMouseIdBits.clear();
3413 mCurrentFingerIdBits.clear();
3414 pointerUsage = POINTER_USAGE_STYLUS;
3415 } else if (!mCurrentMouseIdBits.isEmpty()) {
3416 mCurrentFingerIdBits.clear();
3417 pointerUsage = POINTER_USAGE_MOUSE;
3418 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3419 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003420 }
3421
3422 dispatchPointerUsage(when, policyFlags, pointerUsage);
3423 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003424 if (mDeviceMode == DEVICE_MODE_DIRECT
3425 && mConfig.showTouches && mPointerController != NULL) {
3426 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3427 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3428
3429 mPointerController->setButtonState(mCurrentButtonState);
3430 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3431 mCurrentCookedPointerData.idToIndex,
3432 mCurrentCookedPointerData.touchingIdBits);
3433 }
3434
Jeff Brown65fd2512011-08-18 11:20:58 -07003435 dispatchHoverExit(when, policyFlags);
3436 dispatchTouches(when, policyFlags);
3437 dispatchHoverEnterAndMove(when, policyFlags);
3438 }
3439
3440 // Synthesize key up from raw buttons if needed.
3441 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3442 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003443 }
3444
Jeff Brown6328cdc2010-07-29 18:18:33 -07003445 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003446 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3447 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3448 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003449 mLastFingerIdBits = mCurrentFingerIdBits;
3450 mLastStylusIdBits = mCurrentStylusIdBits;
3451 mLastMouseIdBits = mCurrentMouseIdBits;
3452
3453 // Clear some transient state.
3454 mCurrentRawVScroll = 0;
3455 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003456}
3457
Jeff Brown79ac9692011-04-19 21:20:10 -07003458void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003459 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003460 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3461 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3462 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003463 }
3464}
3465
Jeff Brownbe1aa822011-07-27 16:04:54 -07003466bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3467 // Check for release of a virtual key.
3468 if (mCurrentVirtualKey.down) {
3469 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3470 // Pointer went up while virtual key was down.
3471 mCurrentVirtualKey.down = false;
3472 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003473#if DEBUG_VIRTUAL_KEYS
3474 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003475 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003476#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003477 dispatchVirtualKey(when, policyFlags,
3478 AKEY_EVENT_ACTION_UP,
3479 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003480 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003481 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003482 }
3483
Jeff Brownbe1aa822011-07-27 16:04:54 -07003484 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3485 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3486 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3487 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3488 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3489 // Pointer is still within the space of the virtual key.
3490 return true;
3491 }
3492 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003493
Jeff Brownbe1aa822011-07-27 16:04:54 -07003494 // Pointer left virtual key area or another pointer also went down.
3495 // Send key cancellation but do not consume the touch yet.
3496 // This is useful when the user swipes through from the virtual key area
3497 // into the main display surface.
3498 mCurrentVirtualKey.down = false;
3499 if (!mCurrentVirtualKey.ignored) {
3500#if DEBUG_VIRTUAL_KEYS
3501 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3502 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3503#endif
3504 dispatchVirtualKey(when, policyFlags,
3505 AKEY_EVENT_ACTION_UP,
3506 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3507 | AKEY_EVENT_FLAG_CANCELED);
3508 }
3509 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003510
Jeff Brownbe1aa822011-07-27 16:04:54 -07003511 if (mLastRawPointerData.touchingIdBits.isEmpty()
3512 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3513 // Pointer just went down. Check for virtual key press or off-screen touches.
3514 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3515 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3516 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3517 // If exactly one pointer went down, check for virtual key hit.
3518 // Otherwise we will drop the entire stroke.
3519 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3520 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3521 if (virtualKey) {
3522 mCurrentVirtualKey.down = true;
3523 mCurrentVirtualKey.downTime = when;
3524 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3525 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3526 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3527 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3528
3529 if (!mCurrentVirtualKey.ignored) {
3530#if DEBUG_VIRTUAL_KEYS
3531 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3532 mCurrentVirtualKey.keyCode,
3533 mCurrentVirtualKey.scanCode);
3534#endif
3535 dispatchVirtualKey(when, policyFlags,
3536 AKEY_EVENT_ACTION_DOWN,
3537 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3538 }
3539 }
3540 }
3541 return true;
3542 }
3543 }
3544
Jeff Brownfe508922011-01-18 15:10:10 -08003545 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003546 // most recent touch within the screen area. The idea is to filter out stray
3547 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003548 //
3549 // Problems we're trying to solve:
3550 //
3551 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3552 // virtual key area that is implemented by a separate touch panel and accidentally
3553 // triggers a virtual key.
3554 //
3555 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3556 // area and accidentally triggers a virtual key. This often happens when virtual keys
3557 // are layed out below the screen near to where the on screen keyboard's space bar
3558 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003559 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003560 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003561 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003562 return false;
3563}
3564
3565void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3566 int32_t keyEventAction, int32_t keyEventFlags) {
3567 int32_t keyCode = mCurrentVirtualKey.keyCode;
3568 int32_t scanCode = mCurrentVirtualKey.scanCode;
3569 nsecs_t downTime = mCurrentVirtualKey.downTime;
3570 int32_t metaState = mContext->getGlobalMetaState();
3571 policyFlags |= POLICY_FLAG_VIRTUAL;
3572
3573 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3574 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3575 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003576}
3577
Jeff Brown6d0fec22010-07-23 21:28:06 -07003578void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003579 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3580 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003581 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003582 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003583
3584 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003585 if (!currentIdBits.isEmpty()) {
3586 // No pointer id changes so this is a move event.
3587 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003588 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003589 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3590 AMOTION_EVENT_EDGE_FLAG_NONE,
3591 mCurrentCookedPointerData.pointerProperties,
3592 mCurrentCookedPointerData.pointerCoords,
3593 mCurrentCookedPointerData.idToIndex,
3594 currentIdBits, -1,
3595 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3596 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003597 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003598 // There may be pointers going up and pointers going down and pointers moving
3599 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003600 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3601 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003602 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003603 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003604
Jeff Brownace13b12011-03-09 17:39:48 -08003605 // Update last coordinates of pointers that have moved so that we observe the new
3606 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003607 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003608 mCurrentCookedPointerData.pointerProperties,
3609 mCurrentCookedPointerData.pointerCoords,
3610 mCurrentCookedPointerData.idToIndex,
3611 mLastCookedPointerData.pointerProperties,
3612 mLastCookedPointerData.pointerCoords,
3613 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003614 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003615 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003616 moveNeeded = true;
3617 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003618
Jeff Brownace13b12011-03-09 17:39:48 -08003619 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003620 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003621 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003622
Jeff Brown65fd2512011-08-18 11:20:58 -07003623 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003624 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003625 mLastCookedPointerData.pointerProperties,
3626 mLastCookedPointerData.pointerCoords,
3627 mLastCookedPointerData.idToIndex,
3628 dispatchedIdBits, upId,
3629 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003630 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003631 }
3632
Jeff Brownc3db8582010-10-20 15:33:38 -07003633 // Dispatch move events if any of the remaining pointers moved from their old locations.
3634 // Although applications receive new locations as part of individual pointer up
3635 // events, they do not generally handle them except when presented in a move event.
3636 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003637 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003638 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003639 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003640 mCurrentCookedPointerData.pointerProperties,
3641 mCurrentCookedPointerData.pointerCoords,
3642 mCurrentCookedPointerData.idToIndex,
3643 dispatchedIdBits, -1,
3644 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003645 }
3646
3647 // Dispatch pointer down events using the new pointer locations.
3648 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003649 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003650 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003651
Jeff Brownace13b12011-03-09 17:39:48 -08003652 if (dispatchedIdBits.count() == 1) {
3653 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003654 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003655 }
3656
Jeff Brown65fd2512011-08-18 11:20:58 -07003657 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003658 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003659 mCurrentCookedPointerData.pointerProperties,
3660 mCurrentCookedPointerData.pointerCoords,
3661 mCurrentCookedPointerData.idToIndex,
3662 dispatchedIdBits, downId,
3663 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003664 }
3665 }
Jeff Brownace13b12011-03-09 17:39:48 -08003666}
3667
Jeff Brownbe1aa822011-07-27 16:04:54 -07003668void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3669 if (mSentHoverEnter &&
3670 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3671 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3672 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003673 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003674 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3675 mLastCookedPointerData.pointerProperties,
3676 mLastCookedPointerData.pointerCoords,
3677 mLastCookedPointerData.idToIndex,
3678 mLastCookedPointerData.hoveringIdBits, -1,
3679 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3680 mSentHoverEnter = false;
3681 }
3682}
Jeff Brownace13b12011-03-09 17:39:48 -08003683
Jeff Brownbe1aa822011-07-27 16:04:54 -07003684void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3685 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3686 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3687 int32_t metaState = getContext()->getGlobalMetaState();
3688 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003689 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003690 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3691 mCurrentCookedPointerData.pointerProperties,
3692 mCurrentCookedPointerData.pointerCoords,
3693 mCurrentCookedPointerData.idToIndex,
3694 mCurrentCookedPointerData.hoveringIdBits, -1,
3695 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3696 mSentHoverEnter = true;
3697 }
Jeff Brownace13b12011-03-09 17:39:48 -08003698
Jeff Brown65fd2512011-08-18 11:20:58 -07003699 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003700 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3701 mCurrentCookedPointerData.pointerProperties,
3702 mCurrentCookedPointerData.pointerCoords,
3703 mCurrentCookedPointerData.idToIndex,
3704 mCurrentCookedPointerData.hoveringIdBits, -1,
3705 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3706 }
3707}
3708
3709void TouchInputMapper::cookPointerData() {
3710 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3711
3712 mCurrentCookedPointerData.clear();
3713 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3714 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3715 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3716
3717 // Walk through the the active pointers and map device coordinates onto
3718 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003719 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003720 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003721
Jeff Browna1f89ce2011-08-11 00:05:01 -07003722 // Size
3723 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3724 switch (mCalibration.sizeCalibration) {
3725 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3726 case Calibration::SIZE_CALIBRATION_DIAMETER:
3727 case Calibration::SIZE_CALIBRATION_AREA:
3728 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3729 touchMajor = in.touchMajor;
3730 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3731 toolMajor = in.toolMajor;
3732 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3733 size = mRawPointerAxes.touchMinor.valid
3734 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3735 } else if (mRawPointerAxes.touchMajor.valid) {
3736 toolMajor = touchMajor = in.touchMajor;
3737 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3738 ? in.touchMinor : in.touchMajor;
3739 size = mRawPointerAxes.touchMinor.valid
3740 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3741 } else if (mRawPointerAxes.toolMajor.valid) {
3742 touchMajor = toolMajor = in.toolMajor;
3743 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3744 ? in.toolMinor : in.toolMajor;
3745 size = mRawPointerAxes.toolMinor.valid
3746 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003747 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003748 LOG_ASSERT(false, "No touch or tool axes. "
3749 "Size calibration should have been resolved to NONE.");
3750 touchMajor = 0;
3751 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003752 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003753 toolMinor = 0;
3754 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003755 }
Jeff Brownace13b12011-03-09 17:39:48 -08003756
Jeff Browna1f89ce2011-08-11 00:05:01 -07003757 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3758 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3759 if (touchingCount > 1) {
3760 touchMajor /= touchingCount;
3761 touchMinor /= touchingCount;
3762 toolMajor /= touchingCount;
3763 toolMinor /= touchingCount;
3764 size /= touchingCount;
3765 }
3766 }
Jeff Brownace13b12011-03-09 17:39:48 -08003767
Jeff Browna1f89ce2011-08-11 00:05:01 -07003768 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3769 touchMajor *= mGeometricScale;
3770 touchMinor *= mGeometricScale;
3771 toolMajor *= mGeometricScale;
3772 toolMinor *= mGeometricScale;
3773 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3774 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003775 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003776 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3777 toolMinor = toolMajor;
3778 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3779 touchMinor = touchMajor;
3780 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003781 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003782
3783 mCalibration.applySizeScaleAndBias(&touchMajor);
3784 mCalibration.applySizeScaleAndBias(&touchMinor);
3785 mCalibration.applySizeScaleAndBias(&toolMajor);
3786 mCalibration.applySizeScaleAndBias(&toolMinor);
3787 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003788 break;
3789 default:
3790 touchMajor = 0;
3791 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003792 toolMajor = 0;
3793 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003794 size = 0;
3795 break;
3796 }
3797
Jeff Browna1f89ce2011-08-11 00:05:01 -07003798 // Pressure
3799 float pressure;
3800 switch (mCalibration.pressureCalibration) {
3801 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3802 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3803 pressure = in.pressure * mPressureScale;
3804 break;
3805 default:
3806 pressure = in.isHovering ? 0 : 1;
3807 break;
3808 }
3809
Jeff Brown65fd2512011-08-18 11:20:58 -07003810 // Tilt and Orientation
3811 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003812 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003813 if (mHaveTilt) {
3814 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3815 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3816 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3817 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3818 } else {
3819 tilt = 0;
3820
3821 switch (mCalibration.orientationCalibration) {
3822 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3823 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3824 break;
3825 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3826 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3827 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3828 if (c1 != 0 || c2 != 0) {
3829 orientation = atan2f(c1, c2) * 0.5f;
3830 float confidence = hypotf(c1, c2);
3831 float scale = 1.0f + confidence / 16.0f;
3832 touchMajor *= scale;
3833 touchMinor /= scale;
3834 toolMajor *= scale;
3835 toolMinor /= scale;
3836 } else {
3837 orientation = 0;
3838 }
3839 break;
3840 }
3841 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003842 orientation = 0;
3843 }
Jeff Brownace13b12011-03-09 17:39:48 -08003844 }
3845
Jeff Brown80fd47c2011-05-24 01:07:44 -07003846 // Distance
3847 float distance;
3848 switch (mCalibration.distanceCalibration) {
3849 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003850 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003851 break;
3852 default:
3853 distance = 0;
3854 }
3855
Jeff Brownace13b12011-03-09 17:39:48 -08003856 // X and Y
3857 // Adjust coords for surface orientation.
3858 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003859 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003860 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003861 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3862 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003863 orientation -= M_PI_2;
3864 if (orientation < - M_PI_2) {
3865 orientation += M_PI;
3866 }
3867 break;
3868 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003869 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3870 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003871 break;
3872 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003873 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3874 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003875 orientation += M_PI_2;
3876 if (orientation > M_PI_2) {
3877 orientation -= M_PI;
3878 }
3879 break;
3880 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003881 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3882 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003883 break;
3884 }
3885
3886 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003887 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003888 out.clear();
3889 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3890 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3891 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3892 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3893 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3894 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3895 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3896 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3897 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003898 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003899 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003900
3901 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003902 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3903 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003904 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003905 properties.id = id;
3906 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003907
Jeff Brownbe1aa822011-07-27 16:04:54 -07003908 // Write id index.
3909 mCurrentCookedPointerData.idToIndex[id] = i;
3910 }
Jeff Brownace13b12011-03-09 17:39:48 -08003911}
3912
Jeff Brown65fd2512011-08-18 11:20:58 -07003913void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3914 PointerUsage pointerUsage) {
3915 if (pointerUsage != mPointerUsage) {
3916 abortPointerUsage(when, policyFlags);
3917 mPointerUsage = pointerUsage;
3918 }
3919
3920 switch (mPointerUsage) {
3921 case POINTER_USAGE_GESTURES:
3922 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3923 break;
3924 case POINTER_USAGE_STYLUS:
3925 dispatchPointerStylus(when, policyFlags);
3926 break;
3927 case POINTER_USAGE_MOUSE:
3928 dispatchPointerMouse(when, policyFlags);
3929 break;
3930 default:
3931 break;
3932 }
3933}
3934
3935void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3936 switch (mPointerUsage) {
3937 case POINTER_USAGE_GESTURES:
3938 abortPointerGestures(when, policyFlags);
3939 break;
3940 case POINTER_USAGE_STYLUS:
3941 abortPointerStylus(when, policyFlags);
3942 break;
3943 case POINTER_USAGE_MOUSE:
3944 abortPointerMouse(when, policyFlags);
3945 break;
3946 default:
3947 break;
3948 }
3949
3950 mPointerUsage = POINTER_USAGE_NONE;
3951}
3952
Jeff Brown79ac9692011-04-19 21:20:10 -07003953void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3954 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003955 // Update current gesture coordinates.
3956 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003957 bool sendEvents = preparePointerGestures(when,
3958 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3959 if (!sendEvents) {
3960 return;
3961 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003962 if (finishPreviousGesture) {
3963 cancelPreviousGesture = false;
3964 }
Jeff Brownace13b12011-03-09 17:39:48 -08003965
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003966 // Update the pointer presentation and spots.
3967 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3968 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3969 if (finishPreviousGesture || cancelPreviousGesture) {
3970 mPointerController->clearSpots();
3971 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003972 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3973 mPointerGesture.currentGestureIdToIndex,
3974 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003975 } else {
3976 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3977 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003978
Jeff Brown538881e2011-05-25 18:23:38 -07003979 // Show or hide the pointer if needed.
3980 switch (mPointerGesture.currentGestureMode) {
3981 case PointerGesture::NEUTRAL:
3982 case PointerGesture::QUIET:
3983 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3984 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3985 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3986 // Remind the user of where the pointer is after finishing a gesture with spots.
3987 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3988 }
3989 break;
3990 case PointerGesture::TAP:
3991 case PointerGesture::TAP_DRAG:
3992 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3993 case PointerGesture::HOVER:
3994 case PointerGesture::PRESS:
3995 // Unfade the pointer when the current gesture manipulates the
3996 // area directly under the pointer.
3997 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3998 break;
3999 case PointerGesture::SWIPE:
4000 case PointerGesture::FREEFORM:
4001 // Fade the pointer when the current gesture manipulates a different
4002 // area and there are spots to guide the user experience.
4003 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4004 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4005 } else {
4006 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4007 }
4008 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004009 }
4010
Jeff Brownace13b12011-03-09 17:39:48 -08004011 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004012 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004013 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004014
4015 // Update last coordinates of pointers that have moved so that we observe the new
4016 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004017 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4018 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4019 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004020 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004021 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4022 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4023 bool moveNeeded = false;
4024 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004025 && !mPointerGesture.lastGestureIdBits.isEmpty()
4026 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004027 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4028 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004029 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004030 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004031 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004032 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4033 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004034 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004035 moveNeeded = true;
4036 }
Jeff Brownace13b12011-03-09 17:39:48 -08004037 }
4038
4039 // Send motion events for all pointers that went up or were canceled.
4040 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4041 if (!dispatchedGestureIdBits.isEmpty()) {
4042 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004043 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004044 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4045 AMOTION_EVENT_EDGE_FLAG_NONE,
4046 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004047 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4048 dispatchedGestureIdBits, -1,
4049 0, 0, mPointerGesture.downTime);
4050
4051 dispatchedGestureIdBits.clear();
4052 } else {
4053 BitSet32 upGestureIdBits;
4054 if (finishPreviousGesture) {
4055 upGestureIdBits = dispatchedGestureIdBits;
4056 } else {
4057 upGestureIdBits.value = dispatchedGestureIdBits.value
4058 & ~mPointerGesture.currentGestureIdBits.value;
4059 }
4060 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004061 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004062
Jeff Brown65fd2512011-08-18 11:20:58 -07004063 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004064 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004065 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4066 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004067 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4068 dispatchedGestureIdBits, id,
4069 0, 0, mPointerGesture.downTime);
4070
4071 dispatchedGestureIdBits.clearBit(id);
4072 }
4073 }
4074 }
4075
4076 // Send motion events for all pointers that moved.
4077 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004078 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004079 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4080 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004081 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4082 dispatchedGestureIdBits, -1,
4083 0, 0, mPointerGesture.downTime);
4084 }
4085
4086 // Send motion events for all pointers that went down.
4087 if (down) {
4088 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4089 & ~dispatchedGestureIdBits.value);
4090 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004091 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004092 dispatchedGestureIdBits.markBit(id);
4093
Jeff Brownace13b12011-03-09 17:39:48 -08004094 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004095 mPointerGesture.downTime = when;
4096 }
4097
Jeff Brown65fd2512011-08-18 11:20:58 -07004098 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004099 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004100 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004101 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4102 dispatchedGestureIdBits, id,
4103 0, 0, mPointerGesture.downTime);
4104 }
4105 }
4106
Jeff Brownace13b12011-03-09 17:39:48 -08004107 // Send motion events for hover.
4108 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004109 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004110 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4111 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4112 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004113 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4114 mPointerGesture.currentGestureIdBits, -1,
4115 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004116 } else if (dispatchedGestureIdBits.isEmpty()
4117 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4118 // Synthesize a hover move event after all pointers go up to indicate that
4119 // the pointer is hovering again even if the user is not currently touching
4120 // the touch pad. This ensures that a view will receive a fresh hover enter
4121 // event after a tap.
4122 float x, y;
4123 mPointerController->getPosition(&x, &y);
4124
4125 PointerProperties pointerProperties;
4126 pointerProperties.clear();
4127 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004128 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004129
4130 PointerCoords pointerCoords;
4131 pointerCoords.clear();
4132 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4133 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4134
Jeff Brown65fd2512011-08-18 11:20:58 -07004135 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004136 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4137 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4138 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004139 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004140 }
4141
4142 // Update state.
4143 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4144 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004145 mPointerGesture.lastGestureIdBits.clear();
4146 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004147 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4148 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004149 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004150 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004151 mPointerGesture.lastGestureProperties[index].copyFrom(
4152 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004153 mPointerGesture.lastGestureCoords[index].copyFrom(
4154 mPointerGesture.currentGestureCoords[index]);
4155 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004156 }
4157 }
4158}
4159
Jeff Brown65fd2512011-08-18 11:20:58 -07004160void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4161 // Cancel previously dispatches pointers.
4162 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4163 int32_t metaState = getContext()->getGlobalMetaState();
4164 int32_t buttonState = mCurrentButtonState;
4165 dispatchMotion(when, policyFlags, mSource,
4166 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4167 AMOTION_EVENT_EDGE_FLAG_NONE,
4168 mPointerGesture.lastGestureProperties,
4169 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4170 mPointerGesture.lastGestureIdBits, -1,
4171 0, 0, mPointerGesture.downTime);
4172 }
4173
4174 // Reset the current pointer gesture.
4175 mPointerGesture.reset();
4176 mPointerVelocityControl.reset();
4177
4178 // Remove any current spots.
4179 if (mPointerController != NULL) {
4180 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4181 mPointerController->clearSpots();
4182 }
4183}
4184
Jeff Brown79ac9692011-04-19 21:20:10 -07004185bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4186 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004187 *outCancelPreviousGesture = false;
4188 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004189
Jeff Brown79ac9692011-04-19 21:20:10 -07004190 // Handle TAP timeout.
4191 if (isTimeout) {
4192#if DEBUG_GESTURES
4193 LOGD("Gestures: Processing timeout");
4194#endif
4195
4196 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004197 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004198 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004199 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004200 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004201 } else {
4202 // The tap is finished.
4203#if DEBUG_GESTURES
4204 LOGD("Gestures: TAP finished");
4205#endif
4206 *outFinishPreviousGesture = true;
4207
4208 mPointerGesture.activeGestureId = -1;
4209 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4210 mPointerGesture.currentGestureIdBits.clear();
4211
Jeff Brown65fd2512011-08-18 11:20:58 -07004212 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004213 return true;
4214 }
4215 }
4216
4217 // We did not handle this timeout.
4218 return false;
4219 }
4220
Jeff Brown65fd2512011-08-18 11:20:58 -07004221 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4222 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4223
Jeff Brownace13b12011-03-09 17:39:48 -08004224 // Update the velocity tracker.
4225 {
4226 VelocityTracker::Position positions[MAX_POINTERS];
4227 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004228 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004229 uint32_t id = idBits.clearFirstMarkedBit();
4230 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004231 positions[count].x = pointer.x * mPointerXMovementScale;
4232 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004233 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004234 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004235 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004236 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004237
Jeff Brownace13b12011-03-09 17:39:48 -08004238 // Pick a new active touch id if needed.
4239 // Choose an arbitrary pointer that just went down, if there is one.
4240 // Otherwise choose an arbitrary remaining pointer.
4241 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004242 // We keep the same active touch id for as long as possible.
4243 bool activeTouchChanged = false;
4244 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4245 int32_t activeTouchId = lastActiveTouchId;
4246 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004247 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004248 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004249 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004250 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004251 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004252 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004253 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004254 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004255 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004256 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004257 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004258 } else {
4259 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004260 }
4261 }
4262
4263 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004264 bool isQuietTime = false;
4265 if (activeTouchId < 0) {
4266 mPointerGesture.resetQuietTime();
4267 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004268 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004269 if (!isQuietTime) {
4270 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4271 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4272 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004273 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004274 // Enter quiet time when exiting swipe or freeform state.
4275 // This is to prevent accidentally entering the hover state and flinging the
4276 // pointer when finishing a swipe and there is still one pointer left onscreen.
4277 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004278 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004279 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004280 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004281 // Enter quiet time when releasing the button and there are still two or more
4282 // fingers down. This may indicate that one finger was used to press the button
4283 // but it has not gone up yet.
4284 isQuietTime = true;
4285 }
4286 if (isQuietTime) {
4287 mPointerGesture.quietTime = when;
4288 }
Jeff Brownace13b12011-03-09 17:39:48 -08004289 }
4290 }
4291
4292 // Switch states based on button and pointer state.
4293 if (isQuietTime) {
4294 // Case 1: Quiet time. (QUIET)
4295#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004296 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004297 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004298#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004299 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4300 *outFinishPreviousGesture = true;
4301 }
Jeff Brownace13b12011-03-09 17:39:48 -08004302
4303 mPointerGesture.activeGestureId = -1;
4304 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004305 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004306
Jeff Brown65fd2512011-08-18 11:20:58 -07004307 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004308 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004309 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004310 // The pointer follows the active touch point.
4311 // Emit DOWN, MOVE, UP events at the pointer location.
4312 //
4313 // Only the active touch matters; other fingers are ignored. This policy helps
4314 // to handle the case where the user places a second finger on the touch pad
4315 // to apply the necessary force to depress an integrated button below the surface.
4316 // We don't want the second finger to be delivered to applications.
4317 //
4318 // For this to work well, we need to make sure to track the pointer that is really
4319 // active. If the user first puts one finger down to click then adds another
4320 // finger to drag then the active pointer should switch to the finger that is
4321 // being dragged.
4322#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004323 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004324 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004325#endif
4326 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004327 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004328 *outFinishPreviousGesture = true;
4329 mPointerGesture.activeGestureId = 0;
4330 }
4331
4332 // Switch pointers if needed.
4333 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004334 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004335 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004336 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004337 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004338 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004339 float vx, vy;
4340 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4341 float speed = hypotf(vx, vy);
4342 if (speed > bestSpeed) {
4343 bestId = id;
4344 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004345 }
Jeff Brown8d608662010-08-30 03:02:23 -07004346 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004347 }
4348 if (bestId >= 0 && bestId != activeTouchId) {
4349 mPointerGesture.activeTouchId = activeTouchId = bestId;
4350 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004351#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004352 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4353 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004354#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004355 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004356 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004357
Jeff Brown65fd2512011-08-18 11:20:58 -07004358 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004359 const RawPointerData::Pointer& currentPointer =
4360 mCurrentRawPointerData.pointerForId(activeTouchId);
4361 const RawPointerData::Pointer& lastPointer =
4362 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004363 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4364 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004365
Jeff Brownbe1aa822011-07-27 16:04:54 -07004366 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004367 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004368
4369 // Move the pointer using a relative motion.
4370 // When using spots, the click will occur at the position of the anchor
4371 // spot and all other spots will move there.
4372 mPointerController->move(deltaX, deltaY);
4373 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004374 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004375 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004376
Jeff Brownace13b12011-03-09 17:39:48 -08004377 float x, y;
4378 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004379
Jeff Brown79ac9692011-04-19 21:20:10 -07004380 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004381 mPointerGesture.currentGestureIdBits.clear();
4382 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4383 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004384 mPointerGesture.currentGestureProperties[0].clear();
4385 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004386 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004387 mPointerGesture.currentGestureCoords[0].clear();
4388 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4389 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4390 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004391 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004392 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004393 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4394 *outFinishPreviousGesture = true;
4395 }
Jeff Brownace13b12011-03-09 17:39:48 -08004396
Jeff Brown79ac9692011-04-19 21:20:10 -07004397 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004398 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004399 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004400 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4401 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004402 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004403 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004404 float x, y;
4405 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004406 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4407 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004408#if DEBUG_GESTURES
4409 LOGD("Gestures: TAP");
4410#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004411
4412 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004413 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004414 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004415
Jeff Brownace13b12011-03-09 17:39:48 -08004416 mPointerGesture.activeGestureId = 0;
4417 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004418 mPointerGesture.currentGestureIdBits.clear();
4419 mPointerGesture.currentGestureIdBits.markBit(
4420 mPointerGesture.activeGestureId);
4421 mPointerGesture.currentGestureIdToIndex[
4422 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004423 mPointerGesture.currentGestureProperties[0].clear();
4424 mPointerGesture.currentGestureProperties[0].id =
4425 mPointerGesture.activeGestureId;
4426 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004427 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004428 mPointerGesture.currentGestureCoords[0].clear();
4429 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004430 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004431 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004432 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004433 mPointerGesture.currentGestureCoords[0].setAxisValue(
4434 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004435
Jeff Brownace13b12011-03-09 17:39:48 -08004436 tapped = true;
4437 } else {
4438#if DEBUG_GESTURES
4439 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004440 x - mPointerGesture.tapX,
4441 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004442#endif
4443 }
4444 } else {
4445#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004446 LOGD("Gestures: Not a TAP, %0.3fms since down",
4447 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004448#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004449 }
Jeff Brownace13b12011-03-09 17:39:48 -08004450 }
Jeff Brown2352b972011-04-12 22:39:53 -07004451
Jeff Brown65fd2512011-08-18 11:20:58 -07004452 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004453
Jeff Brownace13b12011-03-09 17:39:48 -08004454 if (!tapped) {
4455#if DEBUG_GESTURES
4456 LOGD("Gestures: NEUTRAL");
4457#endif
4458 mPointerGesture.activeGestureId = -1;
4459 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004460 mPointerGesture.currentGestureIdBits.clear();
4461 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004462 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004463 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004464 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004465 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4466 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004467 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004468
Jeff Brown79ac9692011-04-19 21:20:10 -07004469 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4470 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004471 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004472 float x, y;
4473 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004474 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4475 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004476 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4477 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004478#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004479 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4480 x - mPointerGesture.tapX,
4481 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004482#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004483 }
4484 } else {
4485#if DEBUG_GESTURES
4486 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4487 (when - mPointerGesture.tapUpTime) * 0.000001f);
4488#endif
4489 }
4490 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4491 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4492 }
Jeff Brownace13b12011-03-09 17:39:48 -08004493
Jeff Brown65fd2512011-08-18 11:20:58 -07004494 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004495 const RawPointerData::Pointer& currentPointer =
4496 mCurrentRawPointerData.pointerForId(activeTouchId);
4497 const RawPointerData::Pointer& lastPointer =
4498 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004499 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004500 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004501 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004502 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004503
Jeff Brownbe1aa822011-07-27 16:04:54 -07004504 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004505 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004506
Jeff Brown2352b972011-04-12 22:39:53 -07004507 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004508 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004509 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004510 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004511 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004512 }
4513
Jeff Brown79ac9692011-04-19 21:20:10 -07004514 bool down;
4515 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4516#if DEBUG_GESTURES
4517 LOGD("Gestures: TAP_DRAG");
4518#endif
4519 down = true;
4520 } else {
4521#if DEBUG_GESTURES
4522 LOGD("Gestures: HOVER");
4523#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004524 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4525 *outFinishPreviousGesture = true;
4526 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004527 mPointerGesture.activeGestureId = 0;
4528 down = false;
4529 }
Jeff Brownace13b12011-03-09 17:39:48 -08004530
4531 float x, y;
4532 mPointerController->getPosition(&x, &y);
4533
Jeff Brownace13b12011-03-09 17:39:48 -08004534 mPointerGesture.currentGestureIdBits.clear();
4535 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4536 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004537 mPointerGesture.currentGestureProperties[0].clear();
4538 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4539 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004540 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004541 mPointerGesture.currentGestureCoords[0].clear();
4542 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4543 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004544 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4545 down ? 1.0f : 0.0f);
4546
Jeff Brown65fd2512011-08-18 11:20:58 -07004547 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004548 mPointerGesture.resetTap();
4549 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004550 mPointerGesture.tapX = x;
4551 mPointerGesture.tapY = y;
4552 }
Jeff Brownace13b12011-03-09 17:39:48 -08004553 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004554 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4555 // We need to provide feedback for each finger that goes down so we cannot wait
4556 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004557 //
Jeff Brown2352b972011-04-12 22:39:53 -07004558 // The ambiguous case is deciding what to do when there are two fingers down but they
4559 // have not moved enough to determine whether they are part of a drag or part of a
4560 // freeform gesture, or just a press or long-press at the pointer location.
4561 //
4562 // When there are two fingers we start with the PRESS hypothesis and we generate a
4563 // down at the pointer location.
4564 //
4565 // When the two fingers move enough or when additional fingers are added, we make
4566 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004567 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004568
Jeff Brown214eaf42011-05-26 19:17:02 -07004569 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004570 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004571 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004572 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4573 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004574 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004575 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004576 // Additional pointers have gone down but not yet settled.
4577 // Reset the gesture.
4578#if DEBUG_GESTURES
4579 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004580 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004581 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004582 * 0.000001f);
4583#endif
4584 *outCancelPreviousGesture = true;
4585 } else {
4586 // Continue previous gesture.
4587 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4588 }
4589
4590 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004591 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4592 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004593 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004594 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004595
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004596 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004597#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004598 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4599 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004600 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004601 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004602#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004603 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4604 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004605 &mPointerGesture.referenceTouchY);
4606 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4607 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004608 }
Jeff Brownace13b12011-03-09 17:39:48 -08004609
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004610 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004611 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004612 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4613 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004614 mPointerGesture.referenceDeltas[id].dx = 0;
4615 mPointerGesture.referenceDeltas[id].dy = 0;
4616 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004617 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004618
4619 // Add delta for all fingers and calculate a common movement delta.
4620 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004621 BitSet32 commonIdBits(mLastFingerIdBits.value
4622 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004623 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4624 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004625 uint32_t id = idBits.clearFirstMarkedBit();
4626 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4627 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004628 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4629 delta.dx += cpd.x - lpd.x;
4630 delta.dy += cpd.y - lpd.y;
4631
4632 if (first) {
4633 commonDeltaX = delta.dx;
4634 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004635 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004636 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4637 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4638 }
4639 }
Jeff Brownace13b12011-03-09 17:39:48 -08004640
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004641 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4642 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4643 float dist[MAX_POINTER_ID + 1];
4644 int32_t distOverThreshold = 0;
4645 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004646 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004647 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004648 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4649 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004650 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004651 distOverThreshold += 1;
4652 }
4653 }
4654
4655 // Only transition when at least two pointers have moved further than
4656 // the minimum distance threshold.
4657 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004658 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004659 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004660#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004661 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004662 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004663#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004664 *outCancelPreviousGesture = true;
4665 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4666 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004667 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004668 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004669 uint32_t id1 = idBits.clearFirstMarkedBit();
4670 uint32_t id2 = idBits.firstMarkedBit();
4671 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4672 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4673 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4674 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4675 // There are two pointers but they are too far apart for a SWIPE,
4676 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004677#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004678 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4679 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004680#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004681 *outCancelPreviousGesture = true;
4682 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4683 } else {
4684 // There are two pointers. Wait for both pointers to start moving
4685 // before deciding whether this is a SWIPE or FREEFORM gesture.
4686 float dist1 = dist[id1];
4687 float dist2 = dist[id2];
4688 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4689 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4690 // Calculate the dot product of the displacement vectors.
4691 // When the vectors are oriented in approximately the same direction,
4692 // the angle betweeen them is near zero and the cosine of the angle
4693 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4694 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4695 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004696 float dx1 = delta1.dx * mPointerXZoomScale;
4697 float dy1 = delta1.dy * mPointerYZoomScale;
4698 float dx2 = delta2.dx * mPointerXZoomScale;
4699 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004700 float dot = dx1 * dx2 + dy1 * dy2;
4701 float cosine = dot / (dist1 * dist2); // denominator always > 0
4702 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4703 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004704#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004705 LOGD("Gestures: PRESS transitioned to SWIPE, "
4706 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4707 "cosine %0.3f >= %0.3f",
4708 dist1, mConfig.pointerGestureMultitouchMinDistance,
4709 dist2, mConfig.pointerGestureMultitouchMinDistance,
4710 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004711#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004712 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4713 } else {
4714 // Pointers are moving in different directions. Switch to FREEFORM.
4715#if DEBUG_GESTURES
4716 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4717 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4718 "cosine %0.3f < %0.3f",
4719 dist1, mConfig.pointerGestureMultitouchMinDistance,
4720 dist2, mConfig.pointerGestureMultitouchMinDistance,
4721 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4722#endif
4723 *outCancelPreviousGesture = true;
4724 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4725 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004726 }
Jeff Brownace13b12011-03-09 17:39:48 -08004727 }
4728 }
Jeff Brownace13b12011-03-09 17:39:48 -08004729 }
4730 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004731 // Switch from SWIPE to FREEFORM if additional pointers go down.
4732 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004733 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004734#if DEBUG_GESTURES
4735 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004736 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004737#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004738 *outCancelPreviousGesture = true;
4739 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004740 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004741 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004742
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004743 // Move the reference points based on the overall group motion of the fingers
4744 // except in PRESS mode while waiting for a transition to occur.
4745 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4746 && (commonDeltaX || commonDeltaY)) {
4747 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004748 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004749 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004750 delta.dx = 0;
4751 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004752 }
4753
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004754 mPointerGesture.referenceTouchX += commonDeltaX;
4755 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004756
Jeff Brown65fd2512011-08-18 11:20:58 -07004757 commonDeltaX *= mPointerXMovementScale;
4758 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004759
Jeff Brownbe1aa822011-07-27 16:04:54 -07004760 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004761 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004762
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004763 mPointerGesture.referenceGestureX += commonDeltaX;
4764 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004765 }
4766
4767 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004768 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4769 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4770 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004771#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004772 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004773 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004774 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004775#endif
4776 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4777
4778 mPointerGesture.currentGestureIdBits.clear();
4779 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4780 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004781 mPointerGesture.currentGestureProperties[0].clear();
4782 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4783 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004784 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004785 mPointerGesture.currentGestureCoords[0].clear();
4786 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4787 mPointerGesture.referenceGestureX);
4788 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4789 mPointerGesture.referenceGestureY);
4790 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004791 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4792 // FREEFORM mode.
4793#if DEBUG_GESTURES
4794 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4795 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004796 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004797#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004798 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004799
Jeff Brownace13b12011-03-09 17:39:48 -08004800 mPointerGesture.currentGestureIdBits.clear();
4801
4802 BitSet32 mappedTouchIdBits;
4803 BitSet32 usedGestureIdBits;
4804 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4805 // Initially, assign the active gesture id to the active touch point
4806 // if there is one. No other touch id bits are mapped yet.
4807 if (!*outCancelPreviousGesture) {
4808 mappedTouchIdBits.markBit(activeTouchId);
4809 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4810 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4811 mPointerGesture.activeGestureId;
4812 } else {
4813 mPointerGesture.activeGestureId = -1;
4814 }
4815 } else {
4816 // Otherwise, assume we mapped all touches from the previous frame.
4817 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004818 mappedTouchIdBits.value = mLastFingerIdBits.value
4819 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004820 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4821
4822 // Check whether we need to choose a new active gesture id because the
4823 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004824 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4825 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004826 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004827 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004828 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4829 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4830 mPointerGesture.activeGestureId = -1;
4831 break;
4832 }
4833 }
4834 }
4835
4836#if DEBUG_GESTURES
4837 LOGD("Gestures: FREEFORM follow up "
4838 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4839 "activeGestureId=%d",
4840 mappedTouchIdBits.value, usedGestureIdBits.value,
4841 mPointerGesture.activeGestureId);
4842#endif
4843
Jeff Brown65fd2512011-08-18 11:20:58 -07004844 BitSet32 idBits(mCurrentFingerIdBits);
4845 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004846 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004847 uint32_t gestureId;
4848 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004849 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004850 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4851#if DEBUG_GESTURES
4852 LOGD("Gestures: FREEFORM "
4853 "new mapping for touch id %d -> gesture id %d",
4854 touchId, gestureId);
4855#endif
4856 } else {
4857 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4858#if DEBUG_GESTURES
4859 LOGD("Gestures: FREEFORM "
4860 "existing mapping for touch id %d -> gesture id %d",
4861 touchId, gestureId);
4862#endif
4863 }
4864 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4865 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4866
Jeff Brownbe1aa822011-07-27 16:04:54 -07004867 const RawPointerData::Pointer& pointer =
4868 mCurrentRawPointerData.pointerForId(touchId);
4869 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004870 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004871 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004872 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004873 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004874
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004875 mPointerGesture.currentGestureProperties[i].clear();
4876 mPointerGesture.currentGestureProperties[i].id = gestureId;
4877 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004878 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004879 mPointerGesture.currentGestureCoords[i].clear();
4880 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004881 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004882 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004883 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004884 mPointerGesture.currentGestureCoords[i].setAxisValue(
4885 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4886 }
4887
4888 if (mPointerGesture.activeGestureId < 0) {
4889 mPointerGesture.activeGestureId =
4890 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4891#if DEBUG_GESTURES
4892 LOGD("Gestures: FREEFORM new "
4893 "activeGestureId=%d", mPointerGesture.activeGestureId);
4894#endif
4895 }
Jeff Brown2352b972011-04-12 22:39:53 -07004896 }
Jeff Brownace13b12011-03-09 17:39:48 -08004897 }
4898
Jeff Brownbe1aa822011-07-27 16:04:54 -07004899 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004900
Jeff Brownace13b12011-03-09 17:39:48 -08004901#if DEBUG_GESTURES
4902 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004903 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4904 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004905 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004906 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4907 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004908 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004909 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004910 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004911 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004912 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004913 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4914 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4915 id, index, properties.toolType,
4916 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004917 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4918 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4919 }
4920 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004921 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004922 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004923 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004924 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004925 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4926 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4927 id, index, properties.toolType,
4928 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004929 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4930 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4931 }
4932#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004933 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004934}
4935
Jeff Brown65fd2512011-08-18 11:20:58 -07004936void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4937 mPointerSimple.currentCoords.clear();
4938 mPointerSimple.currentProperties.clear();
4939
4940 bool down, hovering;
4941 if (!mCurrentStylusIdBits.isEmpty()) {
4942 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4943 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4944 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4945 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4946 mPointerController->setPosition(x, y);
4947
4948 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4949 down = !hovering;
4950
4951 mPointerController->getPosition(&x, &y);
4952 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4953 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4954 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4955 mPointerSimple.currentProperties.id = 0;
4956 mPointerSimple.currentProperties.toolType =
4957 mCurrentCookedPointerData.pointerProperties[index].toolType;
4958 } else {
4959 down = false;
4960 hovering = false;
4961 }
4962
4963 dispatchPointerSimple(when, policyFlags, down, hovering);
4964}
4965
4966void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4967 abortPointerSimple(when, policyFlags);
4968}
4969
4970void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4971 mPointerSimple.currentCoords.clear();
4972 mPointerSimple.currentProperties.clear();
4973
4974 bool down, hovering;
4975 if (!mCurrentMouseIdBits.isEmpty()) {
4976 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4977 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4978 if (mLastMouseIdBits.hasBit(id)) {
4979 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4980 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4981 - mLastRawPointerData.pointers[lastIndex].x)
4982 * mPointerXMovementScale;
4983 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4984 - mLastRawPointerData.pointers[lastIndex].y)
4985 * mPointerYMovementScale;
4986
4987 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4988 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4989
4990 mPointerController->move(deltaX, deltaY);
4991 } else {
4992 mPointerVelocityControl.reset();
4993 }
4994
4995 down = isPointerDown(mCurrentButtonState);
4996 hovering = !down;
4997
4998 float x, y;
4999 mPointerController->getPosition(&x, &y);
5000 mPointerSimple.currentCoords.copyFrom(
5001 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5002 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5003 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5004 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5005 hovering ? 0.0f : 1.0f);
5006 mPointerSimple.currentProperties.id = 0;
5007 mPointerSimple.currentProperties.toolType =
5008 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5009 } else {
5010 mPointerVelocityControl.reset();
5011
5012 down = false;
5013 hovering = false;
5014 }
5015
5016 dispatchPointerSimple(when, policyFlags, down, hovering);
5017}
5018
5019void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5020 abortPointerSimple(when, policyFlags);
5021
5022 mPointerVelocityControl.reset();
5023}
5024
5025void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5026 bool down, bool hovering) {
5027 int32_t metaState = getContext()->getGlobalMetaState();
5028
5029 if (mPointerController != NULL) {
5030 if (down || hovering) {
5031 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5032 mPointerController->clearSpots();
5033 mPointerController->setButtonState(mCurrentButtonState);
5034 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5035 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5036 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5037 }
5038 }
5039
5040 if (mPointerSimple.down && !down) {
5041 mPointerSimple.down = false;
5042
5043 // Send up.
5044 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5045 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5046 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5047 mOrientedXPrecision, mOrientedYPrecision,
5048 mPointerSimple.downTime);
5049 getListener()->notifyMotion(&args);
5050 }
5051
5052 if (mPointerSimple.hovering && !hovering) {
5053 mPointerSimple.hovering = false;
5054
5055 // Send hover exit.
5056 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5057 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5058 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5059 mOrientedXPrecision, mOrientedYPrecision,
5060 mPointerSimple.downTime);
5061 getListener()->notifyMotion(&args);
5062 }
5063
5064 if (down) {
5065 if (!mPointerSimple.down) {
5066 mPointerSimple.down = true;
5067 mPointerSimple.downTime = when;
5068
5069 // Send down.
5070 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5071 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5072 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5073 mOrientedXPrecision, mOrientedYPrecision,
5074 mPointerSimple.downTime);
5075 getListener()->notifyMotion(&args);
5076 }
5077
5078 // Send move.
5079 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5080 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5081 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5082 mOrientedXPrecision, mOrientedYPrecision,
5083 mPointerSimple.downTime);
5084 getListener()->notifyMotion(&args);
5085 }
5086
5087 if (hovering) {
5088 if (!mPointerSimple.hovering) {
5089 mPointerSimple.hovering = true;
5090
5091 // Send hover enter.
5092 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5093 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5094 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5095 mOrientedXPrecision, mOrientedYPrecision,
5096 mPointerSimple.downTime);
5097 getListener()->notifyMotion(&args);
5098 }
5099
5100 // Send hover move.
5101 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5102 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5103 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5104 mOrientedXPrecision, mOrientedYPrecision,
5105 mPointerSimple.downTime);
5106 getListener()->notifyMotion(&args);
5107 }
5108
5109 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5110 float vscroll = mCurrentRawVScroll;
5111 float hscroll = mCurrentRawHScroll;
5112 mWheelYVelocityControl.move(when, NULL, &vscroll);
5113 mWheelXVelocityControl.move(when, &hscroll, NULL);
5114
5115 // Send scroll.
5116 PointerCoords pointerCoords;
5117 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5118 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5119 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5120
5121 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5122 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5123 1, &mPointerSimple.currentProperties, &pointerCoords,
5124 mOrientedXPrecision, mOrientedYPrecision,
5125 mPointerSimple.downTime);
5126 getListener()->notifyMotion(&args);
5127 }
5128
5129 // Save state.
5130 if (down || hovering) {
5131 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5132 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5133 } else {
5134 mPointerSimple.reset();
5135 }
5136}
5137
5138void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5139 mPointerSimple.currentCoords.clear();
5140 mPointerSimple.currentProperties.clear();
5141
5142 dispatchPointerSimple(when, policyFlags, false, false);
5143}
5144
Jeff Brownace13b12011-03-09 17:39:48 -08005145void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005146 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5147 const PointerProperties* properties, const PointerCoords* coords,
5148 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005149 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5150 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005151 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005152 uint32_t pointerCount = 0;
5153 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005154 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005155 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005156 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005157 pointerCoords[pointerCount].copyFrom(coords[index]);
5158
5159 if (changedId >= 0 && id == uint32_t(changedId)) {
5160 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5161 }
5162
5163 pointerCount += 1;
5164 }
5165
Jeff Brownb6110c22011-04-01 16:15:13 -07005166 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005167
5168 if (changedId >= 0 && pointerCount == 1) {
5169 // Replace initial down and final up action.
5170 // We can compare the action without masking off the changed pointer index
5171 // because we know the index is 0.
5172 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5173 action = AMOTION_EVENT_ACTION_DOWN;
5174 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5175 action = AMOTION_EVENT_ACTION_UP;
5176 } else {
5177 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005178 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005179 }
5180 }
5181
Jeff Brownbe1aa822011-07-27 16:04:54 -07005182 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005183 action, flags, metaState, buttonState, edgeFlags,
5184 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005185 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005186}
5187
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005188bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005189 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005190 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5191 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005192 bool changed = false;
5193 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005194 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005195 uint32_t inIndex = inIdToIndex[id];
5196 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005197
5198 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005199 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005200 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005201 PointerCoords& curOutCoords = outCoords[outIndex];
5202
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005203 if (curInProperties != curOutProperties) {
5204 curOutProperties.copyFrom(curInProperties);
5205 changed = true;
5206 }
5207
Jeff Brownace13b12011-03-09 17:39:48 -08005208 if (curInCoords != curOutCoords) {
5209 curOutCoords.copyFrom(curInCoords);
5210 changed = true;
5211 }
5212 }
5213 return changed;
5214}
5215
5216void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005217 if (mPointerController != NULL) {
5218 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5219 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005220}
5221
Jeff Brownbe1aa822011-07-27 16:04:54 -07005222bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5223 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5224 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005225}
5226
Jeff Brownbe1aa822011-07-27 16:04:54 -07005227const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005228 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005229 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005230 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005231 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005232
5233#if DEBUG_VIRTUAL_KEYS
5234 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5235 "left=%d, top=%d, right=%d, bottom=%d",
5236 x, y,
5237 virtualKey.keyCode, virtualKey.scanCode,
5238 virtualKey.hitLeft, virtualKey.hitTop,
5239 virtualKey.hitRight, virtualKey.hitBottom);
5240#endif
5241
5242 if (virtualKey.isHit(x, y)) {
5243 return & virtualKey;
5244 }
5245 }
5246
5247 return NULL;
5248}
5249
Jeff Brownbe1aa822011-07-27 16:04:54 -07005250void TouchInputMapper::assignPointerIds() {
5251 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5252 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5253
5254 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005255
5256 if (currentPointerCount == 0) {
5257 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005258 return;
5259 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005260
Jeff Brownbe1aa822011-07-27 16:04:54 -07005261 if (lastPointerCount == 0) {
5262 // All pointers are new.
5263 for (uint32_t i = 0; i < currentPointerCount; i++) {
5264 uint32_t id = i;
5265 mCurrentRawPointerData.pointers[i].id = id;
5266 mCurrentRawPointerData.idToIndex[id] = i;
5267 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5268 }
5269 return;
5270 }
5271
5272 if (currentPointerCount == 1 && lastPointerCount == 1
5273 && mCurrentRawPointerData.pointers[0].toolType
5274 == mLastRawPointerData.pointers[0].toolType) {
5275 // Only one pointer and no change in count so it must have the same id as before.
5276 uint32_t id = mLastRawPointerData.pointers[0].id;
5277 mCurrentRawPointerData.pointers[0].id = id;
5278 mCurrentRawPointerData.idToIndex[id] = 0;
5279 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5280 return;
5281 }
5282
5283 // General case.
5284 // We build a heap of squared euclidean distances between current and last pointers
5285 // associated with the current and last pointer indices. Then, we find the best
5286 // match (by distance) for each current pointer.
5287 // The pointers must have the same tool type but it is possible for them to
5288 // transition from hovering to touching or vice-versa while retaining the same id.
5289 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5290
5291 uint32_t heapSize = 0;
5292 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5293 currentPointerIndex++) {
5294 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5295 lastPointerIndex++) {
5296 const RawPointerData::Pointer& currentPointer =
5297 mCurrentRawPointerData.pointers[currentPointerIndex];
5298 const RawPointerData::Pointer& lastPointer =
5299 mLastRawPointerData.pointers[lastPointerIndex];
5300 if (currentPointer.toolType == lastPointer.toolType) {
5301 int64_t deltaX = currentPointer.x - lastPointer.x;
5302 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005303
5304 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5305
5306 // Insert new element into the heap (sift up).
5307 heap[heapSize].currentPointerIndex = currentPointerIndex;
5308 heap[heapSize].lastPointerIndex = lastPointerIndex;
5309 heap[heapSize].distance = distance;
5310 heapSize += 1;
5311 }
5312 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005313 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005314
Jeff Brownbe1aa822011-07-27 16:04:54 -07005315 // Heapify
5316 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5317 startIndex -= 1;
5318 for (uint32_t parentIndex = startIndex; ;) {
5319 uint32_t childIndex = parentIndex * 2 + 1;
5320 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005321 break;
5322 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005323
5324 if (childIndex + 1 < heapSize
5325 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5326 childIndex += 1;
5327 }
5328
5329 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5330 break;
5331 }
5332
5333 swap(heap[parentIndex], heap[childIndex]);
5334 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005335 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005336 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005337
5338#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005339 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5340 for (size_t i = 0; i < heapSize; i++) {
5341 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5342 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5343 heap[i].distance);
5344 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005345#endif
5346
Jeff Brownbe1aa822011-07-27 16:04:54 -07005347 // Pull matches out by increasing order of distance.
5348 // To avoid reassigning pointers that have already been matched, the loop keeps track
5349 // of which last and current pointers have been matched using the matchedXXXBits variables.
5350 // It also tracks the used pointer id bits.
5351 BitSet32 matchedLastBits(0);
5352 BitSet32 matchedCurrentBits(0);
5353 BitSet32 usedIdBits(0);
5354 bool first = true;
5355 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5356 while (heapSize > 0) {
5357 if (first) {
5358 // The first time through the loop, we just consume the root element of
5359 // the heap (the one with smallest distance).
5360 first = false;
5361 } else {
5362 // Previous iterations consumed the root element of the heap.
5363 // Pop root element off of the heap (sift down).
5364 heap[0] = heap[heapSize];
5365 for (uint32_t parentIndex = 0; ;) {
5366 uint32_t childIndex = parentIndex * 2 + 1;
5367 if (childIndex >= heapSize) {
5368 break;
5369 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005370
Jeff Brownbe1aa822011-07-27 16:04:54 -07005371 if (childIndex + 1 < heapSize
5372 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5373 childIndex += 1;
5374 }
5375
5376 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5377 break;
5378 }
5379
5380 swap(heap[parentIndex], heap[childIndex]);
5381 parentIndex = childIndex;
5382 }
5383
5384#if DEBUG_POINTER_ASSIGNMENT
5385 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5386 for (size_t i = 0; i < heapSize; i++) {
5387 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5388 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5389 heap[i].distance);
5390 }
5391#endif
5392 }
5393
5394 heapSize -= 1;
5395
5396 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5397 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5398
5399 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5400 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5401
5402 matchedCurrentBits.markBit(currentPointerIndex);
5403 matchedLastBits.markBit(lastPointerIndex);
5404
5405 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5406 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5407 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5408 mCurrentRawPointerData.markIdBit(id,
5409 mCurrentRawPointerData.isHovering(currentPointerIndex));
5410 usedIdBits.markBit(id);
5411
5412#if DEBUG_POINTER_ASSIGNMENT
5413 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5414 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5415#endif
5416 break;
5417 }
5418 }
5419
5420 // Assign fresh ids to pointers that were not matched in the process.
5421 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5422 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5423 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5424
5425 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5426 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5427 mCurrentRawPointerData.markIdBit(id,
5428 mCurrentRawPointerData.isHovering(currentPointerIndex));
5429
5430#if DEBUG_POINTER_ASSIGNMENT
5431 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5432 currentPointerIndex, id);
5433#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005434 }
5435}
5436
Jeff Brown6d0fec22010-07-23 21:28:06 -07005437int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005438 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5439 return AKEY_STATE_VIRTUAL;
5440 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005441
Jeff Brownbe1aa822011-07-27 16:04:54 -07005442 size_t numVirtualKeys = mVirtualKeys.size();
5443 for (size_t i = 0; i < numVirtualKeys; i++) {
5444 const VirtualKey& virtualKey = mVirtualKeys[i];
5445 if (virtualKey.keyCode == keyCode) {
5446 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005447 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005448 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005449
5450 return AKEY_STATE_UNKNOWN;
5451}
5452
5453int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005454 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5455 return AKEY_STATE_VIRTUAL;
5456 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005457
Jeff Brownbe1aa822011-07-27 16:04:54 -07005458 size_t numVirtualKeys = mVirtualKeys.size();
5459 for (size_t i = 0; i < numVirtualKeys; i++) {
5460 const VirtualKey& virtualKey = mVirtualKeys[i];
5461 if (virtualKey.scanCode == scanCode) {
5462 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005463 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005464 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005465
5466 return AKEY_STATE_UNKNOWN;
5467}
5468
5469bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5470 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005471 size_t numVirtualKeys = mVirtualKeys.size();
5472 for (size_t i = 0; i < numVirtualKeys; i++) {
5473 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005474
Jeff Brownbe1aa822011-07-27 16:04:54 -07005475 for (size_t i = 0; i < numCodes; i++) {
5476 if (virtualKey.keyCode == keyCodes[i]) {
5477 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005478 }
5479 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005480 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005481
5482 return true;
5483}
5484
5485
5486// --- SingleTouchInputMapper ---
5487
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005488SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5489 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005490}
5491
5492SingleTouchInputMapper::~SingleTouchInputMapper() {
5493}
5494
Jeff Brown65fd2512011-08-18 11:20:58 -07005495void SingleTouchInputMapper::reset(nsecs_t when) {
5496 mSingleTouchMotionAccumulator.reset(getDevice());
5497
5498 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005499}
5500
Jeff Brown6d0fec22010-07-23 21:28:06 -07005501void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005502 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005503
Jeff Brown65fd2512011-08-18 11:20:58 -07005504 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005505}
5506
Jeff Brown65fd2512011-08-18 11:20:58 -07005507void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005508 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005509 mCurrentRawPointerData.pointerCount = 1;
5510 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005511
Jeff Brown65fd2512011-08-18 11:20:58 -07005512 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5513 && (mTouchButtonAccumulator.isHovering()
5514 || (mRawPointerAxes.pressure.valid
5515 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005516 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005517
Jeff Brownbe1aa822011-07-27 16:04:54 -07005518 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005519 outPointer.id = 0;
5520 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5521 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5522 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5523 outPointer.touchMajor = 0;
5524 outPointer.touchMinor = 0;
5525 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5526 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5527 outPointer.orientation = 0;
5528 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005529 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5530 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005531 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5532 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5533 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5534 }
5535 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005536 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005537}
5538
Jeff Brownbe1aa822011-07-27 16:04:54 -07005539void SingleTouchInputMapper::configureRawPointerAxes() {
5540 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005541
Jeff Brownbe1aa822011-07-27 16:04:54 -07005542 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5543 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5544 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5545 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5546 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005547 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5548 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005549}
5550
5551
5552// --- MultiTouchInputMapper ---
5553
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005554MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005555 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005556}
5557
5558MultiTouchInputMapper::~MultiTouchInputMapper() {
5559}
5560
Jeff Brown65fd2512011-08-18 11:20:58 -07005561void MultiTouchInputMapper::reset(nsecs_t when) {
5562 mMultiTouchMotionAccumulator.reset(getDevice());
5563
Jeff Brown6894a292011-07-01 17:59:27 -07005564 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005565
Jeff Brown65fd2512011-08-18 11:20:58 -07005566 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005567}
5568
5569void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005570 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005571
Jeff Brown65fd2512011-08-18 11:20:58 -07005572 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005573}
5574
Jeff Brown65fd2512011-08-18 11:20:58 -07005575void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005576 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005577 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005578 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005579
Jeff Brown80fd47c2011-05-24 01:07:44 -07005580 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005581 const MultiTouchMotionAccumulator::Slot* inSlot =
5582 mMultiTouchMotionAccumulator.getSlot(inIndex);
5583 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005584 continue;
5585 }
5586
Jeff Brown80fd47c2011-05-24 01:07:44 -07005587 if (outCount >= MAX_POINTERS) {
5588#if DEBUG_POINTERS
5589 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5590 "ignoring the rest.",
5591 getDeviceName().string(), MAX_POINTERS);
5592#endif
5593 break; // too many fingers!
5594 }
5595
Jeff Brownbe1aa822011-07-27 16:04:54 -07005596 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005597 outPointer.x = inSlot->getX();
5598 outPointer.y = inSlot->getY();
5599 outPointer.pressure = inSlot->getPressure();
5600 outPointer.touchMajor = inSlot->getTouchMajor();
5601 outPointer.touchMinor = inSlot->getTouchMinor();
5602 outPointer.toolMajor = inSlot->getToolMajor();
5603 outPointer.toolMinor = inSlot->getToolMinor();
5604 outPointer.orientation = inSlot->getOrientation();
5605 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005606 outPointer.tiltX = 0;
5607 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005608
Jeff Brown49754db2011-07-01 17:37:58 -07005609 outPointer.toolType = inSlot->getToolType();
5610 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5611 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5612 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5613 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5614 }
Jeff Brown8d608662010-08-30 03:02:23 -07005615 }
5616
Jeff Brown65fd2512011-08-18 11:20:58 -07005617 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5618 && (mTouchButtonAccumulator.isHovering()
5619 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005620 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005621
Jeff Brown8d608662010-08-30 03:02:23 -07005622 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005623 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005624 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005625 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005626 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005627 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005628 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005629 if (mPointerTrackingIdMap[n] == trackingId) {
5630 id = n;
5631 }
5632 }
5633
5634 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005635 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005636 mPointerTrackingIdMap[id] = trackingId;
5637 }
5638 }
5639 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005640 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005641 mCurrentRawPointerData.clearIdBits();
5642 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005643 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005644 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005645 mCurrentRawPointerData.idToIndex[id] = outCount;
5646 mCurrentRawPointerData.markIdBit(id, isHovering);
5647 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005648 }
5649 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005650
Jeff Brown6d0fec22010-07-23 21:28:06 -07005651 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005652 }
5653
Jeff Brownbe1aa822011-07-27 16:04:54 -07005654 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005655 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005656
Jeff Brown65fd2512011-08-18 11:20:58 -07005657 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005658}
5659
Jeff Brownbe1aa822011-07-27 16:04:54 -07005660void MultiTouchInputMapper::configureRawPointerAxes() {
5661 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005662
Jeff Brownbe1aa822011-07-27 16:04:54 -07005663 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5664 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5665 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5666 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5667 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5668 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5669 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5670 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5671 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5672 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5673 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005674
Jeff Brownbe1aa822011-07-27 16:04:54 -07005675 if (mRawPointerAxes.trackingId.valid
5676 && mRawPointerAxes.slot.valid
5677 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5678 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005679 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005680 LOGW("MultiTouch Device %s reported %d slots but the framework "
5681 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005682 getDeviceName().string(), slotCount, MAX_SLOTS);
5683 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005684 }
Jeff Brown49754db2011-07-01 17:37:58 -07005685 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005686 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005687 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005688 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005689}
5690
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005691
Jeff Browncb1404e2011-01-15 18:14:15 -08005692// --- JoystickInputMapper ---
5693
5694JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5695 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005696}
5697
5698JoystickInputMapper::~JoystickInputMapper() {
5699}
5700
5701uint32_t JoystickInputMapper::getSources() {
5702 return AINPUT_SOURCE_JOYSTICK;
5703}
5704
5705void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5706 InputMapper::populateDeviceInfo(info);
5707
Jeff Brown6f2fba42011-02-19 01:08:02 -08005708 for (size_t i = 0; i < mAxes.size(); i++) {
5709 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005710 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5711 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005712 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005713 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5714 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005715 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005716 }
5717}
5718
5719void JoystickInputMapper::dump(String8& dump) {
5720 dump.append(INDENT2 "Joystick Input Mapper:\n");
5721
Jeff Brown6f2fba42011-02-19 01:08:02 -08005722 dump.append(INDENT3 "Axes:\n");
5723 size_t numAxes = mAxes.size();
5724 for (size_t i = 0; i < numAxes; i++) {
5725 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005726 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005727 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005728 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005729 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005730 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005731 }
Jeff Brown85297452011-03-04 13:07:49 -08005732 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5733 label = getAxisLabel(axis.axisInfo.highAxis);
5734 if (label) {
5735 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5736 } else {
5737 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5738 axis.axisInfo.splitValue);
5739 }
5740 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5741 dump.append(" (invert)");
5742 }
5743
5744 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5745 axis.min, axis.max, axis.flat, axis.fuzz);
5746 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5747 "highScale=%0.5f, highOffset=%0.5f\n",
5748 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005749 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5750 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005751 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005752 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005753 }
5754}
5755
Jeff Brown65fd2512011-08-18 11:20:58 -07005756void JoystickInputMapper::configure(nsecs_t when,
5757 const InputReaderConfiguration* config, uint32_t changes) {
5758 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005759
Jeff Brown474dcb52011-06-14 20:22:50 -07005760 if (!changes) { // first time only
5761 // Collect all axes.
5762 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07005763 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5764 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5765 continue; // axis must be claimed by a different device
5766 }
5767
Jeff Brown474dcb52011-06-14 20:22:50 -07005768 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005769 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005770 if (rawAxisInfo.valid) {
5771 // Map axis.
5772 AxisInfo axisInfo;
5773 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5774 if (!explicitlyMapped) {
5775 // Axis is not explicitly mapped, will choose a generic axis later.
5776 axisInfo.mode = AxisInfo::MODE_NORMAL;
5777 axisInfo.axis = -1;
5778 }
5779
5780 // Apply flat override.
5781 int32_t rawFlat = axisInfo.flatOverride < 0
5782 ? rawAxisInfo.flat : axisInfo.flatOverride;
5783
5784 // Calculate scaling factors and limits.
5785 Axis axis;
5786 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5787 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5788 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5789 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5790 scale, 0.0f, highScale, 0.0f,
5791 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5792 } else if (isCenteredAxis(axisInfo.axis)) {
5793 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5794 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5795 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5796 scale, offset, scale, offset,
5797 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5798 } else {
5799 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5800 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5801 scale, 0.0f, scale, 0.0f,
5802 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5803 }
5804
5805 // To eliminate noise while the joystick is at rest, filter out small variations
5806 // in axis values up front.
5807 axis.filter = axis.flat * 0.25f;
5808
5809 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005810 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005811 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005812
Jeff Brown474dcb52011-06-14 20:22:50 -07005813 // If there are too many axes, start dropping them.
5814 // Prefer to keep explicitly mapped axes.
5815 if (mAxes.size() > PointerCoords::MAX_AXES) {
5816 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5817 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5818 pruneAxes(true);
5819 pruneAxes(false);
5820 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005821
Jeff Brown474dcb52011-06-14 20:22:50 -07005822 // Assign generic axis ids to remaining axes.
5823 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5824 size_t numAxes = mAxes.size();
5825 for (size_t i = 0; i < numAxes; i++) {
5826 Axis& axis = mAxes.editValueAt(i);
5827 if (axis.axisInfo.axis < 0) {
5828 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5829 && haveAxis(nextGenericAxisId)) {
5830 nextGenericAxisId += 1;
5831 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005832
Jeff Brown474dcb52011-06-14 20:22:50 -07005833 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5834 axis.axisInfo.axis = nextGenericAxisId;
5835 nextGenericAxisId += 1;
5836 } else {
5837 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5838 "have already been assigned to other axes.",
5839 getDeviceName().string(), mAxes.keyAt(i));
5840 mAxes.removeItemsAt(i--);
5841 numAxes -= 1;
5842 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005843 }
5844 }
5845 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005846}
5847
Jeff Brown85297452011-03-04 13:07:49 -08005848bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005849 size_t numAxes = mAxes.size();
5850 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005851 const Axis& axis = mAxes.valueAt(i);
5852 if (axis.axisInfo.axis == axisId
5853 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5854 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005855 return true;
5856 }
5857 }
5858 return false;
5859}
Jeff Browncb1404e2011-01-15 18:14:15 -08005860
Jeff Brown6f2fba42011-02-19 01:08:02 -08005861void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5862 size_t i = mAxes.size();
5863 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5864 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5865 continue;
5866 }
5867 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5868 getDeviceName().string(), mAxes.keyAt(i));
5869 mAxes.removeItemsAt(i);
5870 }
5871}
5872
5873bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5874 switch (axis) {
5875 case AMOTION_EVENT_AXIS_X:
5876 case AMOTION_EVENT_AXIS_Y:
5877 case AMOTION_EVENT_AXIS_Z:
5878 case AMOTION_EVENT_AXIS_RX:
5879 case AMOTION_EVENT_AXIS_RY:
5880 case AMOTION_EVENT_AXIS_RZ:
5881 case AMOTION_EVENT_AXIS_HAT_X:
5882 case AMOTION_EVENT_AXIS_HAT_Y:
5883 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005884 case AMOTION_EVENT_AXIS_RUDDER:
5885 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005886 return true;
5887 default:
5888 return false;
5889 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005890}
5891
Jeff Brown65fd2512011-08-18 11:20:58 -07005892void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005893 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005894 size_t numAxes = mAxes.size();
5895 for (size_t i = 0; i < numAxes; i++) {
5896 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005897 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005898 }
5899
Jeff Brown65fd2512011-08-18 11:20:58 -07005900 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005901}
5902
5903void JoystickInputMapper::process(const RawEvent* rawEvent) {
5904 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005905 case EV_ABS: {
5906 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5907 if (index >= 0) {
5908 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005909 float newValue, highNewValue;
5910 switch (axis.axisInfo.mode) {
5911 case AxisInfo::MODE_INVERT:
5912 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5913 * axis.scale + axis.offset;
5914 highNewValue = 0.0f;
5915 break;
5916 case AxisInfo::MODE_SPLIT:
5917 if (rawEvent->value < axis.axisInfo.splitValue) {
5918 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5919 * axis.scale + axis.offset;
5920 highNewValue = 0.0f;
5921 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5922 newValue = 0.0f;
5923 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5924 * axis.highScale + axis.highOffset;
5925 } else {
5926 newValue = 0.0f;
5927 highNewValue = 0.0f;
5928 }
5929 break;
5930 default:
5931 newValue = rawEvent->value * axis.scale + axis.offset;
5932 highNewValue = 0.0f;
5933 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005934 }
Jeff Brown85297452011-03-04 13:07:49 -08005935 axis.newValue = newValue;
5936 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005937 }
5938 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005939 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005940
5941 case EV_SYN:
5942 switch (rawEvent->scanCode) {
5943 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005944 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005945 break;
5946 }
5947 break;
5948 }
5949}
5950
Jeff Brown6f2fba42011-02-19 01:08:02 -08005951void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005952 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005953 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005954 }
5955
5956 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005957 int32_t buttonState = 0;
5958
5959 PointerProperties pointerProperties;
5960 pointerProperties.clear();
5961 pointerProperties.id = 0;
5962 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005963
Jeff Brown6f2fba42011-02-19 01:08:02 -08005964 PointerCoords pointerCoords;
5965 pointerCoords.clear();
5966
5967 size_t numAxes = mAxes.size();
5968 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005969 const Axis& axis = mAxes.valueAt(i);
5970 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5971 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5972 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5973 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005974 }
5975
Jeff Brown56194eb2011-03-02 19:23:13 -08005976 // Moving a joystick axis should not wake the devide because joysticks can
5977 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5978 // button will likely wake the device.
5979 // TODO: Use the input device configuration to control this behavior more finely.
5980 uint32_t policyFlags = 0;
5981
Jeff Brownbe1aa822011-07-27 16:04:54 -07005982 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005983 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5984 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005985 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005986}
5987
Jeff Brown85297452011-03-04 13:07:49 -08005988bool JoystickInputMapper::filterAxes(bool force) {
5989 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005990 size_t numAxes = mAxes.size();
5991 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005992 Axis& axis = mAxes.editValueAt(i);
5993 if (force || hasValueChangedSignificantly(axis.filter,
5994 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5995 axis.currentValue = axis.newValue;
5996 atLeastOneSignificantChange = true;
5997 }
5998 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5999 if (force || hasValueChangedSignificantly(axis.filter,
6000 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6001 axis.highCurrentValue = axis.highNewValue;
6002 atLeastOneSignificantChange = true;
6003 }
6004 }
6005 }
6006 return atLeastOneSignificantChange;
6007}
6008
6009bool JoystickInputMapper::hasValueChangedSignificantly(
6010 float filter, float newValue, float currentValue, float min, float max) {
6011 if (newValue != currentValue) {
6012 // Filter out small changes in value unless the value is converging on the axis
6013 // bounds or center point. This is intended to reduce the amount of information
6014 // sent to applications by particularly noisy joysticks (such as PS3).
6015 if (fabs(newValue - currentValue) > filter
6016 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6017 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6018 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6019 return true;
6020 }
6021 }
6022 return false;
6023}
6024
6025bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6026 float filter, float newValue, float currentValue, float thresholdValue) {
6027 float newDistance = fabs(newValue - thresholdValue);
6028 if (newDistance < filter) {
6029 float oldDistance = fabs(currentValue - thresholdValue);
6030 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006031 return true;
6032 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006033 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006034 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006035}
6036
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006037} // namespace android