blob: 80e754756ab27f6f115739fcae4bb87901b1edf8 [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 Brown46b9ac0a2010-04-22 18:58:52 -0700201// --- InputReader ---
202
203InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700204 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700205 const sp<InputListenerInterface>& listener) :
206 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700207 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700208 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700209 mQueuedListener = new QueuedInputListener(listener);
210
211 { // acquire lock
212 AutoMutex _l(mLock);
213
214 refreshConfigurationLocked(0);
215 updateGlobalMetaStateLocked();
216 updateInputConfigurationLocked();
217 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700218}
219
220InputReader::~InputReader() {
221 for (size_t i = 0; i < mDevices.size(); i++) {
222 delete mDevices.valueAt(i);
223 }
224}
225
226void InputReader::loopOnce() {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700227 int32_t timeoutMillis;
Jeff Brown474dcb52011-06-14 20:22:50 -0700228 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700229 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700230
Jeff Brownbe1aa822011-07-27 16:04:54 -0700231 uint32_t changes = mConfigurationChangesToRefresh;
232 if (changes) {
233 mConfigurationChangesToRefresh = 0;
234 refreshConfigurationLocked(changes);
235 }
236
237 timeoutMillis = -1;
238 if (mNextTimeout != LLONG_MAX) {
239 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
240 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
241 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700242 } // release lock
243
Jeff Brownb7198742011-03-18 18:14:26 -0700244 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700245
246 { // acquire lock
247 AutoMutex _l(mLock);
248
249 if (count) {
250 processEventsLocked(mEventBuffer, count);
251 }
252 if (!count || timeoutMillis == 0) {
253 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700254#if DEBUG_RAW_EVENTS
Jeff Brownbe1aa822011-07-27 16:04:54 -0700255 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700256#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700257 mNextTimeout = LLONG_MAX;
258 timeoutExpiredLocked(now);
259 }
260 } // release lock
261
262 // Flush queued events out to the listener.
263 // This must happen outside of the lock because the listener could potentially call
264 // back into the InputReader's methods, such as getScanCodeState, or become blocked
265 // on another thread similarly waiting to acquire the InputReader lock thereby
266 // resulting in a deadlock. This situation is actually quite plausible because the
267 // listener is actually the input dispatcher, which calls into the window manager,
268 // which occasionally calls into the input reader.
269 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700270}
271
Jeff Brownbe1aa822011-07-27 16:04:54 -0700272void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700273 for (const RawEvent* rawEvent = rawEvents; count;) {
274 int32_t type = rawEvent->type;
275 size_t batchSize = 1;
276 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
277 int32_t deviceId = rawEvent->deviceId;
278 while (batchSize < count) {
279 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
280 || rawEvent[batchSize].deviceId != deviceId) {
281 break;
282 }
283 batchSize += 1;
284 }
285#if DEBUG_RAW_EVENTS
286 LOGD("BatchSize: %d Count: %d", batchSize, count);
287#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700288 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700289 } else {
290 switch (rawEvent->type) {
291 case EventHubInterface::DEVICE_ADDED:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700292 addDeviceLocked(rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700293 break;
294 case EventHubInterface::DEVICE_REMOVED:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700295 removeDeviceLocked(rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700296 break;
297 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700298 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700299 break;
300 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700301 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700302 break;
303 }
304 }
305 count -= batchSize;
306 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700307 }
308}
309
Jeff Brownbe1aa822011-07-27 16:04:54 -0700310void InputReader::addDeviceLocked(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700311 String8 name = mEventHub->getDeviceName(deviceId);
312 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
313
Jeff Brownbe1aa822011-07-27 16:04:54 -0700314 InputDevice* device = createDeviceLocked(deviceId, name, classes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700315 device->configure(&mConfig, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700316
Jeff Brown8d608662010-08-30 03:02:23 -0700317 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800318 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700319 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800320 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700321 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700322 }
323
Jeff Brownbe1aa822011-07-27 16:04:54 -0700324 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
325 if (deviceIndex < 0) {
326 mDevices.add(deviceId, device);
327 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700328 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
329 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700330 return;
331 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700332}
333
Jeff Brownbe1aa822011-07-27 16:04:54 -0700334void InputReader::removeDeviceLocked(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700335 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700336 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
337 if (deviceIndex >= 0) {
338 device = mDevices.valueAt(deviceIndex);
339 mDevices.removeItemsAt(deviceIndex, 1);
340 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700341 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342 return;
343 }
344
Jeff Brown6d0fec22010-07-23 21:28:06 -0700345 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800346 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700347 device->getId(), device->getName().string());
348 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800349 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350 device->getId(), device->getName().string(), device->getSources());
351 }
352
Jeff Brown8d608662010-08-30 03:02:23 -0700353 device->reset();
354
Jeff Brown6d0fec22010-07-23 21:28:06 -0700355 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700356}
357
Jeff Brownbe1aa822011-07-27 16:04:54 -0700358InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
359 const String8& name, uint32_t classes) {
360 InputDevice* device = new InputDevice(&mContext, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700361
Jeff Brown56194eb2011-03-02 19:23:13 -0800362 // External devices.
363 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
364 device->setExternal(true);
365 }
366
Jeff Brown6d0fec22010-07-23 21:28:06 -0700367 // Switch-like devices.
368 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
369 device->addMapper(new SwitchInputMapper(device));
370 }
371
372 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800373 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
375 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800376 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377 }
378 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
379 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
380 }
381 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800382 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800384 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800385 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800386 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387
Jeff Brownefd32662011-03-08 15:13:06 -0800388 if (keyboardSource != 0) {
389 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700390 }
391
Jeff Brown83c09682010-12-23 17:50:18 -0800392 // Cursor-like devices.
393 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
394 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 }
396
Jeff Brown58a2da82011-01-25 16:02:22 -0800397 // Touchscreens and touchpad devices.
398 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800399 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800400 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800401 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 }
403
Jeff Browncb1404e2011-01-15 18:14:15 -0800404 // Joystick-like devices.
405 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
406 device->addMapper(new JoystickInputMapper(device));
407 }
408
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 return device;
410}
411
Jeff Brownbe1aa822011-07-27 16:04:54 -0700412void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700413 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700414 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
415 if (deviceIndex < 0) {
416 LOGW("Discarding event for unknown deviceId %d.", deviceId);
417 return;
418 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700419
Jeff Brownbe1aa822011-07-27 16:04:54 -0700420 InputDevice* device = mDevices.valueAt(deviceIndex);
421 if (device->isIgnored()) {
422 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
423 return;
424 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700425
Jeff Brownbe1aa822011-07-27 16:04:54 -0700426 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700427}
428
Jeff Brownbe1aa822011-07-27 16:04:54 -0700429void InputReader::timeoutExpiredLocked(nsecs_t when) {
430 for (size_t i = 0; i < mDevices.size(); i++) {
431 InputDevice* device = mDevices.valueAt(i);
432 if (!device->isIgnored()) {
433 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700434 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700435 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700436}
437
Jeff Brownbe1aa822011-07-27 16:04:54 -0700438void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700440 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441
442 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700443 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444
445 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700446 NotifyConfigurationChangedArgs args(when);
447 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700448}
449
Jeff Brownbe1aa822011-07-27 16:04:54 -0700450void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700451 mPolicy->getReaderConfiguration(&mConfig);
452 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
453
Jeff Brown474dcb52011-06-14 20:22:50 -0700454 if (changes) {
455 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
456
457 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
458 mEventHub->requestReopenDevices();
459 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700460 for (size_t i = 0; i < mDevices.size(); i++) {
461 InputDevice* device = mDevices.valueAt(i);
462 device->configure(&mConfig, changes);
463 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700464 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465 }
466}
467
Jeff Brownbe1aa822011-07-27 16:04:54 -0700468void InputReader::updateGlobalMetaStateLocked() {
469 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700470
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471 for (size_t i = 0; i < mDevices.size(); i++) {
472 InputDevice* device = mDevices.valueAt(i);
473 mGlobalMetaState |= device->getMetaState();
474 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700475}
476
Jeff Brownbe1aa822011-07-27 16:04:54 -0700477int32_t InputReader::getGlobalMetaStateLocked() {
478 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700479}
480
Jeff Brownbe1aa822011-07-27 16:04:54 -0700481void InputReader::updateInputConfigurationLocked() {
482 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
483 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
484 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
485 InputDeviceInfo deviceInfo;
486 for (size_t i = 0; i < mDevices.size(); i++) {
487 InputDevice* device = mDevices.valueAt(i);
488 device->getDeviceInfo(& deviceInfo);
489 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490
Jeff Brownbe1aa822011-07-27 16:04:54 -0700491 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
492 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
493 }
494 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
495 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
496 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
497 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
498 }
499 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
500 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
501 }
502 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700503
Jeff Brownbe1aa822011-07-27 16:04:54 -0700504 mInputConfiguration.touchScreen = touchScreenConfig;
505 mInputConfiguration.keyboard = keyboardConfig;
506 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507}
508
Jeff Brownbe1aa822011-07-27 16:04:54 -0700509void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800510 mDisableVirtualKeysTimeout = time;
511}
512
Jeff Brownbe1aa822011-07-27 16:04:54 -0700513bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800514 InputDevice* device, int32_t keyCode, int32_t scanCode) {
515 if (now < mDisableVirtualKeysTimeout) {
516 LOGI("Dropping virtual key from device %s because virtual keys are "
517 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
518 device->getName().string(),
519 (mDisableVirtualKeysTimeout - now) * 0.000001,
520 keyCode, scanCode);
521 return true;
522 } else {
523 return false;
524 }
525}
526
Jeff Brownbe1aa822011-07-27 16:04:54 -0700527void InputReader::fadePointerLocked() {
528 for (size_t i = 0; i < mDevices.size(); i++) {
529 InputDevice* device = mDevices.valueAt(i);
530 device->fadePointer();
531 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800532}
533
Jeff Brownbe1aa822011-07-27 16:04:54 -0700534void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700535 if (when < mNextTimeout) {
536 mNextTimeout = when;
537 }
538}
539
Jeff Brown6d0fec22010-07-23 21:28:06 -0700540void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700541 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700544}
545
546status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700547 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700548
Jeff Brownbe1aa822011-07-27 16:04:54 -0700549 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
550 if (deviceIndex < 0) {
551 return NAME_NOT_FOUND;
552 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700553
Jeff Brownbe1aa822011-07-27 16:04:54 -0700554 InputDevice* device = mDevices.valueAt(deviceIndex);
555 if (device->isIgnored()) {
556 return NAME_NOT_FOUND;
557 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700558
Jeff Brownbe1aa822011-07-27 16:04:54 -0700559 device->getDeviceInfo(outDeviceInfo);
560 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700561}
562
563void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700564 AutoMutex _l(mLock);
565
Jeff Brown6d0fec22010-07-23 21:28:06 -0700566 outDeviceIds.clear();
567
Jeff Brownbe1aa822011-07-27 16:04:54 -0700568 size_t numDevices = mDevices.size();
569 for (size_t i = 0; i < numDevices; i++) {
570 InputDevice* device = mDevices.valueAt(i);
571 if (!device->isIgnored()) {
572 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700573 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700574 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575}
576
577int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
578 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700579 AutoMutex _l(mLock);
580
581 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582}
583
584int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
585 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700586 AutoMutex _l(mLock);
587
588 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700589}
590
591int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700592 AutoMutex _l(mLock);
593
594 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595}
596
Jeff Brownbe1aa822011-07-27 16:04:54 -0700597int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700599 int32_t result = AKEY_STATE_UNKNOWN;
600 if (deviceId >= 0) {
601 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
602 if (deviceIndex >= 0) {
603 InputDevice* device = mDevices.valueAt(deviceIndex);
604 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
605 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700606 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700607 }
608 } else {
609 size_t numDevices = mDevices.size();
610 for (size_t i = 0; i < numDevices; i++) {
611 InputDevice* device = mDevices.valueAt(i);
612 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
613 result = (device->*getStateFunc)(sourceMask, code);
614 if (result >= AKEY_STATE_DOWN) {
615 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616 }
617 }
618 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700619 }
620 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700621}
622
623bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
624 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700625 AutoMutex _l(mLock);
626
Jeff Brown6d0fec22010-07-23 21:28:06 -0700627 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700628 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629}
630
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
632 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
633 bool result = false;
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->markSupportedKeyCodes(sourceMask,
640 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700641 }
642 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700643 } else {
644 size_t numDevices = mDevices.size();
645 for (size_t i = 0; i < numDevices; i++) {
646 InputDevice* device = mDevices.valueAt(i);
647 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
648 result |= device->markSupportedKeyCodes(sourceMask,
649 numCodes, keyCodes, outFlags);
650 }
651 }
652 }
653 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700654}
655
Jeff Brown474dcb52011-06-14 20:22:50 -0700656void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700657 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700658
Jeff Brownbe1aa822011-07-27 16:04:54 -0700659 if (changes) {
660 bool needWake = !mConfigurationChangesToRefresh;
661 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700662
663 if (needWake) {
664 mEventHub->wake();
665 }
666 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700667}
668
Jeff Brownb88102f2010-09-08 11:49:43 -0700669void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700670 AutoMutex _l(mLock);
671
Jeff Brownf2f487182010-10-01 17:46:21 -0700672 mEventHub->dump(dump);
673 dump.append("\n");
674
675 dump.append("Input Reader State:\n");
676
Jeff Brownbe1aa822011-07-27 16:04:54 -0700677 for (size_t i = 0; i < mDevices.size(); i++) {
678 mDevices.valueAt(i)->dump(dump);
679 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700680
681 dump.append(INDENT "Configuration:\n");
682 dump.append(INDENT2 "ExcludedDeviceNames: [");
683 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
684 if (i != 0) {
685 dump.append(", ");
686 }
687 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
688 }
689 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700690 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
691 mConfig.virtualKeyQuietTime * 0.000001f);
692
Jeff Brown19c97d462011-06-01 12:33:19 -0700693 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
694 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
695 mConfig.pointerVelocityControlParameters.scale,
696 mConfig.pointerVelocityControlParameters.lowThreshold,
697 mConfig.pointerVelocityControlParameters.highThreshold,
698 mConfig.pointerVelocityControlParameters.acceleration);
699
700 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
701 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
702 mConfig.wheelVelocityControlParameters.scale,
703 mConfig.wheelVelocityControlParameters.lowThreshold,
704 mConfig.wheelVelocityControlParameters.highThreshold,
705 mConfig.wheelVelocityControlParameters.acceleration);
706
Jeff Brown214eaf42011-05-26 19:17:02 -0700707 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700708 dump.appendFormat(INDENT3 "Enabled: %s\n",
709 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700710 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
711 mConfig.pointerGestureQuietInterval * 0.000001f);
712 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
713 mConfig.pointerGestureDragMinSwitchSpeed);
714 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
715 mConfig.pointerGestureTapInterval * 0.000001f);
716 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
717 mConfig.pointerGestureTapDragInterval * 0.000001f);
718 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
719 mConfig.pointerGestureTapSlop);
720 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
721 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700722 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
723 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700724 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
725 mConfig.pointerGestureSwipeTransitionAngleCosine);
726 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
727 mConfig.pointerGestureSwipeMaxWidthRatio);
728 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
729 mConfig.pointerGestureMovementSpeedRatio);
730 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
731 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700732}
733
Jeff Brown89ef0722011-08-10 16:25:21 -0700734void InputReader::monitor() {
735 // Acquire and release the lock to ensure that the reader has not deadlocked.
736 mLock.lock();
737 mLock.unlock();
738
739 // Check the EventHub
740 mEventHub->monitor();
741}
742
Jeff Brown6d0fec22010-07-23 21:28:06 -0700743
Jeff Brownbe1aa822011-07-27 16:04:54 -0700744// --- InputReader::ContextImpl ---
745
746InputReader::ContextImpl::ContextImpl(InputReader* reader) :
747 mReader(reader) {
748}
749
750void InputReader::ContextImpl::updateGlobalMetaState() {
751 // lock is already held by the input loop
752 mReader->updateGlobalMetaStateLocked();
753}
754
755int32_t InputReader::ContextImpl::getGlobalMetaState() {
756 // lock is already held by the input loop
757 return mReader->getGlobalMetaStateLocked();
758}
759
760void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
761 // lock is already held by the input loop
762 mReader->disableVirtualKeysUntilLocked(time);
763}
764
765bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
766 InputDevice* device, int32_t keyCode, int32_t scanCode) {
767 // lock is already held by the input loop
768 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
769}
770
771void InputReader::ContextImpl::fadePointer() {
772 // lock is already held by the input loop
773 mReader->fadePointerLocked();
774}
775
776void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
777 // lock is already held by the input loop
778 mReader->requestTimeoutAtTimeLocked(when);
779}
780
781InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
782 return mReader->mPolicy.get();
783}
784
785InputListenerInterface* InputReader::ContextImpl::getListener() {
786 return mReader->mQueuedListener.get();
787}
788
789EventHubInterface* InputReader::ContextImpl::getEventHub() {
790 return mReader->mEventHub.get();
791}
792
793
Jeff Brown6d0fec22010-07-23 21:28:06 -0700794// --- InputReaderThread ---
795
796InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
797 Thread(/*canCallJava*/ true), mReader(reader) {
798}
799
800InputReaderThread::~InputReaderThread() {
801}
802
803bool InputReaderThread::threadLoop() {
804 mReader->loopOnce();
805 return true;
806}
807
808
809// --- InputDevice ---
810
811InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700812 mContext(context), mId(id), mName(name), mSources(0),
813 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700814}
815
816InputDevice::~InputDevice() {
817 size_t numMappers = mMappers.size();
818 for (size_t i = 0; i < numMappers; i++) {
819 delete mMappers[i];
820 }
821 mMappers.clear();
822}
823
Jeff Brownef3d7e82010-09-30 14:33:04 -0700824void InputDevice::dump(String8& dump) {
825 InputDeviceInfo deviceInfo;
826 getDeviceInfo(& deviceInfo);
827
Jeff Brown90655042010-12-02 13:50:46 -0800828 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700829 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800830 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700831 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
832 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800833
Jeff Brownefd32662011-03-08 15:13:06 -0800834 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800835 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700836 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800837 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800838 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
839 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800840 char name[32];
841 if (label) {
842 strncpy(name, label, sizeof(name));
843 name[sizeof(name) - 1] = '\0';
844 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800845 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800846 }
Jeff Brownefd32662011-03-08 15:13:06 -0800847 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
848 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
849 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800850 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700851 }
852
853 size_t numMappers = mMappers.size();
854 for (size_t i = 0; i < numMappers; i++) {
855 InputMapper* mapper = mMappers[i];
856 mapper->dump(dump);
857 }
858}
859
Jeff Brown6d0fec22010-07-23 21:28:06 -0700860void InputDevice::addMapper(InputMapper* mapper) {
861 mMappers.add(mapper);
862}
863
Jeff Brown474dcb52011-06-14 20:22:50 -0700864void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700865 mSources = 0;
866
Jeff Brown474dcb52011-06-14 20:22:50 -0700867 if (!isIgnored()) {
868 if (!changes) { // first time only
869 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
870 }
871
872 size_t numMappers = mMappers.size();
873 for (size_t i = 0; i < numMappers; i++) {
874 InputMapper* mapper = mMappers[i];
875 mapper->configure(config, changes);
876 mSources |= mapper->getSources();
877 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700878 }
879}
880
Jeff Brown6d0fec22010-07-23 21:28:06 -0700881void InputDevice::reset() {
882 size_t numMappers = mMappers.size();
883 for (size_t i = 0; i < numMappers; i++) {
884 InputMapper* mapper = mMappers[i];
885 mapper->reset();
886 }
887}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700888
Jeff Brownb7198742011-03-18 18:14:26 -0700889void InputDevice::process(const RawEvent* rawEvents, size_t count) {
890 // Process all of the events in order for each mapper.
891 // We cannot simply ask each mapper to process them in bulk because mappers may
892 // have side-effects that must be interleaved. For example, joystick movement events and
893 // gamepad button presses are handled by different mappers but they should be dispatched
894 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700895 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700896 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
897#if DEBUG_RAW_EVENTS
898 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700899 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700900 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
901 rawEvent->value, rawEvent->flags);
902#endif
903
Jeff Brown80fd47c2011-05-24 01:07:44 -0700904 if (mDropUntilNextSync) {
905 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
906 mDropUntilNextSync = false;
907#if DEBUG_RAW_EVENTS
908 LOGD("Recovered from input event buffer overrun.");
909#endif
910 } else {
911#if DEBUG_RAW_EVENTS
912 LOGD("Dropped input event while waiting for next input sync.");
913#endif
914 }
915 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
916 LOGI("Detected input event buffer overrun for device %s.", mName.string());
917 mDropUntilNextSync = true;
918 reset();
919 } else {
920 for (size_t i = 0; i < numMappers; i++) {
921 InputMapper* mapper = mMappers[i];
922 mapper->process(rawEvent);
923 }
Jeff Brownb7198742011-03-18 18:14:26 -0700924 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925 }
926}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700927
Jeff Brownaa3855d2011-03-17 01:34:19 -0700928void InputDevice::timeoutExpired(nsecs_t when) {
929 size_t numMappers = mMappers.size();
930 for (size_t i = 0; i < numMappers; i++) {
931 InputMapper* mapper = mMappers[i];
932 mapper->timeoutExpired(when);
933 }
934}
935
Jeff Brown6d0fec22010-07-23 21:28:06 -0700936void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
937 outDeviceInfo->initialize(mId, mName);
938
939 size_t numMappers = mMappers.size();
940 for (size_t i = 0; i < numMappers; i++) {
941 InputMapper* mapper = mMappers[i];
942 mapper->populateDeviceInfo(outDeviceInfo);
943 }
944}
945
946int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
947 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
948}
949
950int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
951 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
952}
953
954int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
955 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
956}
957
958int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
959 int32_t result = AKEY_STATE_UNKNOWN;
960 size_t numMappers = mMappers.size();
961 for (size_t i = 0; i < numMappers; i++) {
962 InputMapper* mapper = mMappers[i];
963 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
964 result = (mapper->*getStateFunc)(sourceMask, code);
965 if (result >= AKEY_STATE_DOWN) {
966 return result;
967 }
968 }
969 }
970 return result;
971}
972
973bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
974 const int32_t* keyCodes, uint8_t* outFlags) {
975 bool result = false;
976 size_t numMappers = mMappers.size();
977 for (size_t i = 0; i < numMappers; i++) {
978 InputMapper* mapper = mMappers[i];
979 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
980 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
981 }
982 }
983 return result;
984}
985
986int32_t InputDevice::getMetaState() {
987 int32_t result = 0;
988 size_t numMappers = mMappers.size();
989 for (size_t i = 0; i < numMappers; i++) {
990 InputMapper* mapper = mMappers[i];
991 result |= mapper->getMetaState();
992 }
993 return result;
994}
995
Jeff Brown05dc66a2011-03-02 14:41:58 -0800996void InputDevice::fadePointer() {
997 size_t numMappers = mMappers.size();
998 for (size_t i = 0; i < numMappers; i++) {
999 InputMapper* mapper = mMappers[i];
1000 mapper->fadePointer();
1001 }
1002}
1003
Jeff Brown6d0fec22010-07-23 21:28:06 -07001004
Jeff Brown49754db2011-07-01 17:37:58 -07001005// --- CursorButtonAccumulator ---
1006
1007CursorButtonAccumulator::CursorButtonAccumulator() {
1008 clearButtons();
1009}
1010
1011void CursorButtonAccumulator::clearButtons() {
1012 mBtnLeft = 0;
1013 mBtnRight = 0;
1014 mBtnMiddle = 0;
1015 mBtnBack = 0;
1016 mBtnSide = 0;
1017 mBtnForward = 0;
1018 mBtnExtra = 0;
1019 mBtnTask = 0;
1020}
1021
1022void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1023 if (rawEvent->type == EV_KEY) {
1024 switch (rawEvent->scanCode) {
1025 case BTN_LEFT:
1026 mBtnLeft = rawEvent->value;
1027 break;
1028 case BTN_RIGHT:
1029 mBtnRight = rawEvent->value;
1030 break;
1031 case BTN_MIDDLE:
1032 mBtnMiddle = rawEvent->value;
1033 break;
1034 case BTN_BACK:
1035 mBtnBack = rawEvent->value;
1036 break;
1037 case BTN_SIDE:
1038 mBtnSide = rawEvent->value;
1039 break;
1040 case BTN_FORWARD:
1041 mBtnForward = rawEvent->value;
1042 break;
1043 case BTN_EXTRA:
1044 mBtnExtra = rawEvent->value;
1045 break;
1046 case BTN_TASK:
1047 mBtnTask = rawEvent->value;
1048 break;
1049 }
1050 }
1051}
1052
1053uint32_t CursorButtonAccumulator::getButtonState() const {
1054 uint32_t result = 0;
1055 if (mBtnLeft) {
1056 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1057 }
1058 if (mBtnRight) {
1059 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1060 }
1061 if (mBtnMiddle) {
1062 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1063 }
1064 if (mBtnBack || mBtnSide) {
1065 result |= AMOTION_EVENT_BUTTON_BACK;
1066 }
1067 if (mBtnForward || mBtnExtra) {
1068 result |= AMOTION_EVENT_BUTTON_FORWARD;
1069 }
1070 return result;
1071}
1072
1073
1074// --- CursorMotionAccumulator ---
1075
1076CursorMotionAccumulator::CursorMotionAccumulator() :
1077 mHaveRelWheel(false), mHaveRelHWheel(false) {
1078 clearRelativeAxes();
1079}
1080
1081void CursorMotionAccumulator::configure(InputDevice* device) {
1082 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1083 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1084}
1085
1086void CursorMotionAccumulator::clearRelativeAxes() {
1087 mRelX = 0;
1088 mRelY = 0;
1089 mRelWheel = 0;
1090 mRelHWheel = 0;
1091}
1092
1093void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1094 if (rawEvent->type == EV_REL) {
1095 switch (rawEvent->scanCode) {
1096 case REL_X:
1097 mRelX = rawEvent->value;
1098 break;
1099 case REL_Y:
1100 mRelY = rawEvent->value;
1101 break;
1102 case REL_WHEEL:
1103 mRelWheel = rawEvent->value;
1104 break;
1105 case REL_HWHEEL:
1106 mRelHWheel = rawEvent->value;
1107 break;
1108 }
1109 }
1110}
1111
1112
1113// --- TouchButtonAccumulator ---
1114
1115TouchButtonAccumulator::TouchButtonAccumulator() :
1116 mHaveBtnTouch(false) {
1117 clearButtons();
1118}
1119
1120void TouchButtonAccumulator::configure(InputDevice* device) {
1121 mHaveBtnTouch = device->getEventHub()->hasScanCode(device->getId(), BTN_TOUCH);
1122}
1123
1124void TouchButtonAccumulator::clearButtons() {
1125 mBtnTouch = 0;
1126 mBtnStylus = 0;
1127 mBtnStylus2 = 0;
1128 mBtnToolFinger = 0;
1129 mBtnToolPen = 0;
1130 mBtnToolRubber = 0;
1131}
1132
1133void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1134 if (rawEvent->type == EV_KEY) {
1135 switch (rawEvent->scanCode) {
1136 case BTN_TOUCH:
1137 mBtnTouch = rawEvent->value;
1138 break;
1139 case BTN_STYLUS:
1140 mBtnStylus = rawEvent->value;
1141 break;
1142 case BTN_STYLUS2:
1143 mBtnStylus2 = rawEvent->value;
1144 break;
1145 case BTN_TOOL_FINGER:
1146 mBtnToolFinger = rawEvent->value;
1147 break;
1148 case BTN_TOOL_PEN:
1149 mBtnToolPen = rawEvent->value;
1150 break;
1151 case BTN_TOOL_RUBBER:
1152 mBtnToolRubber = rawEvent->value;
1153 break;
1154 }
1155 }
1156}
1157
1158uint32_t TouchButtonAccumulator::getButtonState() const {
1159 uint32_t result = 0;
1160 if (mBtnStylus) {
1161 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1162 }
1163 if (mBtnStylus2) {
1164 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1165 }
1166 return result;
1167}
1168
1169int32_t TouchButtonAccumulator::getToolType() const {
1170 if (mBtnToolRubber) {
1171 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1172 }
1173 if (mBtnToolPen) {
1174 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1175 }
1176 if (mBtnToolFinger) {
1177 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1178 }
1179 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1180}
1181
Jeff Brownd87c6d52011-08-10 14:55:59 -07001182bool TouchButtonAccumulator::isToolActive() const {
1183 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber;
Jeff Brown49754db2011-07-01 17:37:58 -07001184}
1185
1186bool TouchButtonAccumulator::isHovering() const {
1187 return mHaveBtnTouch && !mBtnTouch;
1188}
1189
1190
Jeff Brownbe1aa822011-07-27 16:04:54 -07001191// --- RawPointerAxes ---
1192
1193RawPointerAxes::RawPointerAxes() {
1194 clear();
1195}
1196
1197void RawPointerAxes::clear() {
1198 x.clear();
1199 y.clear();
1200 pressure.clear();
1201 touchMajor.clear();
1202 touchMinor.clear();
1203 toolMajor.clear();
1204 toolMinor.clear();
1205 orientation.clear();
1206 distance.clear();
1207 trackingId.clear();
1208 slot.clear();
1209}
1210
1211
1212// --- RawPointerData ---
1213
1214RawPointerData::RawPointerData() {
1215 clear();
1216}
1217
1218void RawPointerData::clear() {
1219 pointerCount = 0;
1220 clearIdBits();
1221}
1222
1223void RawPointerData::copyFrom(const RawPointerData& other) {
1224 pointerCount = other.pointerCount;
1225 hoveringIdBits = other.hoveringIdBits;
1226 touchingIdBits = other.touchingIdBits;
1227
1228 for (uint32_t i = 0; i < pointerCount; i++) {
1229 pointers[i] = other.pointers[i];
1230
1231 int id = pointers[i].id;
1232 idToIndex[id] = other.idToIndex[id];
1233 }
1234}
1235
1236void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1237 float x = 0, y = 0;
1238 uint32_t count = touchingIdBits.count();
1239 if (count) {
1240 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1241 uint32_t id = idBits.clearFirstMarkedBit();
1242 const Pointer& pointer = pointerForId(id);
1243 x += pointer.x;
1244 y += pointer.y;
1245 }
1246 x /= count;
1247 y /= count;
1248 }
1249 *outX = x;
1250 *outY = y;
1251}
1252
1253
1254// --- CookedPointerData ---
1255
1256CookedPointerData::CookedPointerData() {
1257 clear();
1258}
1259
1260void CookedPointerData::clear() {
1261 pointerCount = 0;
1262 hoveringIdBits.clear();
1263 touchingIdBits.clear();
1264}
1265
1266void CookedPointerData::copyFrom(const CookedPointerData& other) {
1267 pointerCount = other.pointerCount;
1268 hoveringIdBits = other.hoveringIdBits;
1269 touchingIdBits = other.touchingIdBits;
1270
1271 for (uint32_t i = 0; i < pointerCount; i++) {
1272 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1273 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1274
1275 int id = pointerProperties[i].id;
1276 idToIndex[id] = other.idToIndex[id];
1277 }
1278}
1279
1280
Jeff Brown49754db2011-07-01 17:37:58 -07001281// --- SingleTouchMotionAccumulator ---
1282
1283SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1284 clearAbsoluteAxes();
1285}
1286
1287void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1288 mAbsX = 0;
1289 mAbsY = 0;
1290 mAbsPressure = 0;
1291 mAbsToolWidth = 0;
1292 mAbsDistance = 0;
1293}
1294
1295void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1296 if (rawEvent->type == EV_ABS) {
1297 switch (rawEvent->scanCode) {
1298 case ABS_X:
1299 mAbsX = rawEvent->value;
1300 break;
1301 case ABS_Y:
1302 mAbsY = rawEvent->value;
1303 break;
1304 case ABS_PRESSURE:
1305 mAbsPressure = rawEvent->value;
1306 break;
1307 case ABS_TOOL_WIDTH:
1308 mAbsToolWidth = rawEvent->value;
1309 break;
1310 case ABS_DISTANCE:
1311 mAbsDistance = rawEvent->value;
1312 break;
1313 }
1314 }
1315}
1316
1317
1318// --- MultiTouchMotionAccumulator ---
1319
1320MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1321 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1322}
1323
1324MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1325 delete[] mSlots;
1326}
1327
1328void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1329 mSlotCount = slotCount;
1330 mUsingSlotsProtocol = usingSlotsProtocol;
1331
1332 delete[] mSlots;
1333 mSlots = new Slot[slotCount];
1334}
1335
1336void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
1337 for (size_t i = 0; i < mSlotCount; i++) {
1338 mSlots[i].clearIfInUse();
1339 }
1340 mCurrentSlot = initialSlot;
1341}
1342
1343void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1344 if (rawEvent->type == EV_ABS) {
1345 bool newSlot = false;
1346 if (mUsingSlotsProtocol) {
1347 if (rawEvent->scanCode == ABS_MT_SLOT) {
1348 mCurrentSlot = rawEvent->value;
1349 newSlot = true;
1350 }
1351 } else if (mCurrentSlot < 0) {
1352 mCurrentSlot = 0;
1353 }
1354
1355 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1356#if DEBUG_POINTERS
1357 if (newSlot) {
1358 LOGW("MultiTouch device emitted invalid slot index %d but it "
1359 "should be between 0 and %d; ignoring this slot.",
1360 mCurrentSlot, mSlotCount - 1);
1361 }
1362#endif
1363 } else {
1364 Slot* slot = &mSlots[mCurrentSlot];
1365
1366 switch (rawEvent->scanCode) {
1367 case ABS_MT_POSITION_X:
1368 slot->mInUse = true;
1369 slot->mAbsMTPositionX = rawEvent->value;
1370 break;
1371 case ABS_MT_POSITION_Y:
1372 slot->mInUse = true;
1373 slot->mAbsMTPositionY = rawEvent->value;
1374 break;
1375 case ABS_MT_TOUCH_MAJOR:
1376 slot->mInUse = true;
1377 slot->mAbsMTTouchMajor = rawEvent->value;
1378 break;
1379 case ABS_MT_TOUCH_MINOR:
1380 slot->mInUse = true;
1381 slot->mAbsMTTouchMinor = rawEvent->value;
1382 slot->mHaveAbsMTTouchMinor = true;
1383 break;
1384 case ABS_MT_WIDTH_MAJOR:
1385 slot->mInUse = true;
1386 slot->mAbsMTWidthMajor = rawEvent->value;
1387 break;
1388 case ABS_MT_WIDTH_MINOR:
1389 slot->mInUse = true;
1390 slot->mAbsMTWidthMinor = rawEvent->value;
1391 slot->mHaveAbsMTWidthMinor = true;
1392 break;
1393 case ABS_MT_ORIENTATION:
1394 slot->mInUse = true;
1395 slot->mAbsMTOrientation = rawEvent->value;
1396 break;
1397 case ABS_MT_TRACKING_ID:
1398 if (mUsingSlotsProtocol && rawEvent->value < 0) {
1399 slot->clearIfInUse();
1400 } else {
1401 slot->mInUse = true;
1402 slot->mAbsMTTrackingId = rawEvent->value;
1403 }
1404 break;
1405 case ABS_MT_PRESSURE:
1406 slot->mInUse = true;
1407 slot->mAbsMTPressure = rawEvent->value;
1408 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001409 case ABS_MT_DISTANCE:
1410 slot->mInUse = true;
1411 slot->mAbsMTDistance = rawEvent->value;
1412 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001413 case ABS_MT_TOOL_TYPE:
1414 slot->mInUse = true;
1415 slot->mAbsMTToolType = rawEvent->value;
1416 slot->mHaveAbsMTToolType = true;
1417 break;
1418 }
1419 }
1420 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1421 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1422 mCurrentSlot += 1;
1423 }
1424}
1425
1426
1427// --- MultiTouchMotionAccumulator::Slot ---
1428
1429MultiTouchMotionAccumulator::Slot::Slot() {
1430 clear();
1431}
1432
1433void MultiTouchMotionAccumulator::Slot::clearIfInUse() {
1434 if (mInUse) {
1435 clear();
1436 }
1437}
1438
1439void MultiTouchMotionAccumulator::Slot::clear() {
1440 mInUse = false;
1441 mHaveAbsMTTouchMinor = false;
1442 mHaveAbsMTWidthMinor = false;
1443 mHaveAbsMTToolType = false;
1444 mAbsMTPositionX = 0;
1445 mAbsMTPositionY = 0;
1446 mAbsMTTouchMajor = 0;
1447 mAbsMTTouchMinor = 0;
1448 mAbsMTWidthMajor = 0;
1449 mAbsMTWidthMinor = 0;
1450 mAbsMTOrientation = 0;
1451 mAbsMTTrackingId = -1;
1452 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001453 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001454 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001455}
1456
1457int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1458 if (mHaveAbsMTToolType) {
1459 switch (mAbsMTToolType) {
1460 case MT_TOOL_FINGER:
1461 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1462 case MT_TOOL_PEN:
1463 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1464 }
1465 }
1466 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1467}
1468
1469
Jeff Brown6d0fec22010-07-23 21:28:06 -07001470// --- InputMapper ---
1471
1472InputMapper::InputMapper(InputDevice* device) :
1473 mDevice(device), mContext(device->getContext()) {
1474}
1475
1476InputMapper::~InputMapper() {
1477}
1478
1479void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1480 info->addSource(getSources());
1481}
1482
Jeff Brownef3d7e82010-09-30 14:33:04 -07001483void InputMapper::dump(String8& dump) {
1484}
1485
Jeff Brown474dcb52011-06-14 20:22:50 -07001486void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001487}
1488
1489void InputMapper::reset() {
1490}
1491
Jeff Brownaa3855d2011-03-17 01:34:19 -07001492void InputMapper::timeoutExpired(nsecs_t when) {
1493}
1494
Jeff Brown6d0fec22010-07-23 21:28:06 -07001495int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1496 return AKEY_STATE_UNKNOWN;
1497}
1498
1499int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1500 return AKEY_STATE_UNKNOWN;
1501}
1502
1503int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1504 return AKEY_STATE_UNKNOWN;
1505}
1506
1507bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1508 const int32_t* keyCodes, uint8_t* outFlags) {
1509 return false;
1510}
1511
1512int32_t InputMapper::getMetaState() {
1513 return 0;
1514}
1515
Jeff Brown05dc66a2011-03-02 14:41:58 -08001516void InputMapper::fadePointer() {
1517}
1518
Jeff Brownbe1aa822011-07-27 16:04:54 -07001519status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1520 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1521}
1522
Jeff Browncb1404e2011-01-15 18:14:15 -08001523void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1524 const RawAbsoluteAxisInfo& axis, const char* name) {
1525 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001526 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1527 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001528 } else {
1529 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1530 }
1531}
1532
Jeff Brown6d0fec22010-07-23 21:28:06 -07001533
1534// --- SwitchInputMapper ---
1535
1536SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1537 InputMapper(device) {
1538}
1539
1540SwitchInputMapper::~SwitchInputMapper() {
1541}
1542
1543uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001544 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001545}
1546
1547void SwitchInputMapper::process(const RawEvent* rawEvent) {
1548 switch (rawEvent->type) {
1549 case EV_SW:
1550 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1551 break;
1552 }
1553}
1554
1555void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001556 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1557 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001558}
1559
1560int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1561 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1562}
1563
1564
1565// --- KeyboardInputMapper ---
1566
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001567KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001568 uint32_t source, int32_t keyboardType) :
1569 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001570 mKeyboardType(keyboardType) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001571 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001572}
1573
1574KeyboardInputMapper::~KeyboardInputMapper() {
1575}
1576
Jeff Brownbe1aa822011-07-27 16:04:54 -07001577void KeyboardInputMapper::initialize() {
1578 mMetaState = AMETA_NONE;
1579 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001580}
1581
1582uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001583 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001584}
1585
1586void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1587 InputMapper::populateDeviceInfo(info);
1588
1589 info->setKeyboardType(mKeyboardType);
1590}
1591
Jeff Brownef3d7e82010-09-30 14:33:04 -07001592void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001593 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1594 dumpParameters(dump);
1595 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1596 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1597 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1598 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001599}
1600
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001601
Jeff Brown474dcb52011-06-14 20:22:50 -07001602void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1603 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001604
Jeff Brown474dcb52011-06-14 20:22:50 -07001605 if (!changes) { // first time only
1606 // Configure basic parameters.
1607 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001608
Jeff Brown474dcb52011-06-14 20:22:50 -07001609 // Reset LEDs.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001610 resetLedState();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001611 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001612}
1613
1614void KeyboardInputMapper::configureParameters() {
1615 mParameters.orientationAware = false;
1616 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1617 mParameters.orientationAware);
1618
Jeff Brownbc68a592011-07-25 12:58:12 -07001619 mParameters.associatedDisplayId = -1;
1620 if (mParameters.orientationAware) {
1621 mParameters.associatedDisplayId = 0;
1622 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001623}
1624
1625void KeyboardInputMapper::dumpParameters(String8& dump) {
1626 dump.append(INDENT3 "Parameters:\n");
1627 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1628 mParameters.associatedDisplayId);
1629 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1630 toString(mParameters.orientationAware));
1631}
1632
Jeff Brown6d0fec22010-07-23 21:28:06 -07001633void KeyboardInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001634 // Synthesize key up event on reset if keys are currently down.
1635 while (!mKeyDowns.isEmpty()) {
1636 const KeyDown& keyDown = mKeyDowns.top();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001637 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001638 processKey(when, false, keyDown.keyCode, keyDown.scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001639 }
1640
Jeff Brownbe1aa822011-07-27 16:04:54 -07001641 initialize();
1642 resetLedState();
1643
Jeff Brown6d0fec22010-07-23 21:28:06 -07001644 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001645 getContext()->updateGlobalMetaState();
1646}
1647
1648void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1649 switch (rawEvent->type) {
1650 case EV_KEY: {
1651 int32_t scanCode = rawEvent->scanCode;
1652 if (isKeyboardOrGamepadKey(scanCode)) {
1653 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1654 rawEvent->flags);
1655 }
1656 break;
1657 }
1658 }
1659}
1660
1661bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1662 return scanCode < BTN_MOUSE
1663 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001664 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001665 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001666}
1667
Jeff Brown6328cdc2010-07-29 18:18:33 -07001668void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1669 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001670
Jeff Brownbe1aa822011-07-27 16:04:54 -07001671 if (down) {
1672 // Rotate key codes according to orientation if needed.
1673 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1674 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1675 int32_t orientation;
1676 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1677 false /*external*/, NULL, NULL, & orientation)) {
1678 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001679 }
1680
Jeff Brownbe1aa822011-07-27 16:04:54 -07001681 keyCode = rotateKeyCode(keyCode, orientation);
1682 }
Jeff Brownfe508922011-01-18 15:10:10 -08001683
Jeff Brownbe1aa822011-07-27 16:04:54 -07001684 // Add key down.
1685 ssize_t keyDownIndex = findKeyDown(scanCode);
1686 if (keyDownIndex >= 0) {
1687 // key repeat, be sure to use same keycode as before in case of rotation
1688 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001689 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001690 // key down
1691 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1692 && mContext->shouldDropVirtualKey(when,
1693 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001694 return;
1695 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001696
1697 mKeyDowns.push();
1698 KeyDown& keyDown = mKeyDowns.editTop();
1699 keyDown.keyCode = keyCode;
1700 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001701 }
1702
Jeff Brownbe1aa822011-07-27 16:04:54 -07001703 mDownTime = when;
1704 } else {
1705 // Remove key down.
1706 ssize_t keyDownIndex = findKeyDown(scanCode);
1707 if (keyDownIndex >= 0) {
1708 // key up, be sure to use same keycode as before in case of rotation
1709 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1710 mKeyDowns.removeAt(size_t(keyDownIndex));
1711 } else {
1712 // key was not actually down
1713 LOGI("Dropping key up from device %s because the key was not down. "
1714 "keyCode=%d, scanCode=%d",
1715 getDeviceName().string(), keyCode, scanCode);
1716 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001717 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001718 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001719
Jeff Brownbe1aa822011-07-27 16:04:54 -07001720 bool metaStateChanged = false;
1721 int32_t oldMetaState = mMetaState;
1722 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1723 if (oldMetaState != newMetaState) {
1724 mMetaState = newMetaState;
1725 metaStateChanged = true;
1726 updateLedState(false);
1727 }
1728
1729 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001730
Jeff Brown56194eb2011-03-02 19:23:13 -08001731 // Key down on external an keyboard should wake the device.
1732 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1733 // For internal keyboards, the key layout file should specify the policy flags for
1734 // each wake key individually.
1735 // TODO: Use the input device configuration to control this behavior more finely.
1736 if (down && getDevice()->isExternal()
1737 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1738 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1739 }
1740
Jeff Brown6328cdc2010-07-29 18:18:33 -07001741 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001742 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001743 }
1744
Jeff Brown05dc66a2011-03-02 14:41:58 -08001745 if (down && !isMetaKey(keyCode)) {
1746 getContext()->fadePointer();
1747 }
1748
Jeff Brownbe1aa822011-07-27 16:04:54 -07001749 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001750 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1751 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001752 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001753}
1754
Jeff Brownbe1aa822011-07-27 16:04:54 -07001755ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1756 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001757 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001758 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001759 return i;
1760 }
1761 }
1762 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001763}
1764
Jeff Brown6d0fec22010-07-23 21:28:06 -07001765int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1766 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1767}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001768
Jeff Brown6d0fec22010-07-23 21:28:06 -07001769int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1770 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1771}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001772
Jeff Brown6d0fec22010-07-23 21:28:06 -07001773bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1774 const int32_t* keyCodes, uint8_t* outFlags) {
1775 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1776}
1777
1778int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001779 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001780}
1781
Jeff Brownbe1aa822011-07-27 16:04:54 -07001782void KeyboardInputMapper::resetLedState() {
1783 initializeLedState(mCapsLockLedState, LED_CAPSL);
1784 initializeLedState(mNumLockLedState, LED_NUML);
1785 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001786
Jeff Brownbe1aa822011-07-27 16:04:54 -07001787 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001788}
1789
Jeff Brownbe1aa822011-07-27 16:04:54 -07001790void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001791 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1792 ledState.on = false;
1793}
1794
Jeff Brownbe1aa822011-07-27 16:04:54 -07001795void KeyboardInputMapper::updateLedState(bool reset) {
1796 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001797 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001798 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001799 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001800 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001801 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001802}
1803
Jeff Brownbe1aa822011-07-27 16:04:54 -07001804void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001805 int32_t led, int32_t modifier, bool reset) {
1806 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001807 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001808 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001809 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1810 ledState.on = desiredState;
1811 }
1812 }
1813}
1814
Jeff Brown6d0fec22010-07-23 21:28:06 -07001815
Jeff Brown83c09682010-12-23 17:50:18 -08001816// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001817
Jeff Brown83c09682010-12-23 17:50:18 -08001818CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001819 InputMapper(device) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001820 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001821}
1822
Jeff Brown83c09682010-12-23 17:50:18 -08001823CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001824}
1825
Jeff Brown83c09682010-12-23 17:50:18 -08001826uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001827 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001828}
1829
Jeff Brown83c09682010-12-23 17:50:18 -08001830void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001831 InputMapper::populateDeviceInfo(info);
1832
Jeff Brown83c09682010-12-23 17:50:18 -08001833 if (mParameters.mode == Parameters::MODE_POINTER) {
1834 float minX, minY, maxX, maxY;
1835 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001836 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1837 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001838 }
1839 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001840 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1841 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001842 }
Jeff Brownefd32662011-03-08 15:13:06 -08001843 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001844
Jeff Brown49754db2011-07-01 17:37:58 -07001845 if (mCursorMotionAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001846 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001847 }
Jeff Brown49754db2011-07-01 17:37:58 -07001848 if (mCursorMotionAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001849 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001850 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001851}
1852
Jeff Brown83c09682010-12-23 17:50:18 -08001853void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001854 dump.append(INDENT2 "Cursor Input Mapper:\n");
1855 dumpParameters(dump);
1856 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1857 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
1858 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1859 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1860 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
1861 toString(mCursorMotionAccumulator.haveRelativeVWheel()));
1862 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
1863 toString(mCursorMotionAccumulator.haveRelativeHWheel()));
1864 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1865 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
1866 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
1867 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
1868 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001869}
1870
Jeff Brown474dcb52011-06-14 20:22:50 -07001871void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1872 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001873
Jeff Brown474dcb52011-06-14 20:22:50 -07001874 if (!changes) { // first time only
Jeff Brown49754db2011-07-01 17:37:58 -07001875 mCursorMotionAccumulator.configure(getDevice());
1876
Jeff Brown474dcb52011-06-14 20:22:50 -07001877 // Configure basic parameters.
1878 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001879
Jeff Brown474dcb52011-06-14 20:22:50 -07001880 // Configure device mode.
1881 switch (mParameters.mode) {
1882 case Parameters::MODE_POINTER:
1883 mSource = AINPUT_SOURCE_MOUSE;
1884 mXPrecision = 1.0f;
1885 mYPrecision = 1.0f;
1886 mXScale = 1.0f;
1887 mYScale = 1.0f;
1888 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1889 break;
1890 case Parameters::MODE_NAVIGATION:
1891 mSource = AINPUT_SOURCE_TRACKBALL;
1892 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1893 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1894 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1895 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1896 break;
1897 }
1898
1899 mVWheelScale = 1.0f;
1900 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08001901 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001902
Jeff Brown474dcb52011-06-14 20:22:50 -07001903 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1904 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1905 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1906 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1907 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001908}
1909
Jeff Brown83c09682010-12-23 17:50:18 -08001910void CursorInputMapper::configureParameters() {
1911 mParameters.mode = Parameters::MODE_POINTER;
1912 String8 cursorModeString;
1913 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1914 if (cursorModeString == "navigation") {
1915 mParameters.mode = Parameters::MODE_NAVIGATION;
1916 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1917 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1918 }
1919 }
1920
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001921 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001922 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001923 mParameters.orientationAware);
1924
Jeff Brownbc68a592011-07-25 12:58:12 -07001925 mParameters.associatedDisplayId = -1;
1926 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
1927 mParameters.associatedDisplayId = 0;
1928 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001929}
1930
Jeff Brown83c09682010-12-23 17:50:18 -08001931void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001932 dump.append(INDENT3 "Parameters:\n");
1933 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1934 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001935
1936 switch (mParameters.mode) {
1937 case Parameters::MODE_POINTER:
1938 dump.append(INDENT4 "Mode: pointer\n");
1939 break;
1940 case Parameters::MODE_NAVIGATION:
1941 dump.append(INDENT4 "Mode: navigation\n");
1942 break;
1943 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001944 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001945 }
1946
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001947 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1948 toString(mParameters.orientationAware));
1949}
1950
Jeff Brownbe1aa822011-07-27 16:04:54 -07001951void CursorInputMapper::initialize() {
Jeff Brown49754db2011-07-01 17:37:58 -07001952 mCursorButtonAccumulator.clearButtons();
1953 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001954
Jeff Brownbe1aa822011-07-27 16:04:54 -07001955 mButtonState = 0;
1956 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001957}
1958
Jeff Brown83c09682010-12-23 17:50:18 -08001959void CursorInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001960 // Reset velocity.
1961 mPointerVelocityControl.reset();
1962 mWheelXVelocityControl.reset();
1963 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001964
Jeff Brownbe1aa822011-07-27 16:04:54 -07001965 // Synthesize button up event on reset.
1966 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
1967 mCursorButtonAccumulator.clearButtons();
1968 mCursorMotionAccumulator.clearRelativeAxes();
1969 sync(when);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001970
Jeff Brownbe1aa822011-07-27 16:04:54 -07001971 initialize();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001972
Jeff Brown6d0fec22010-07-23 21:28:06 -07001973 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001974}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001975
Jeff Brown83c09682010-12-23 17:50:18 -08001976void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07001977 mCursorButtonAccumulator.process(rawEvent);
1978 mCursorMotionAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08001979
Jeff Brown49754db2011-07-01 17:37:58 -07001980 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
1981 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001982 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001983}
1984
Jeff Brown83c09682010-12-23 17:50:18 -08001985void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001986 int32_t lastButtonState = mButtonState;
1987 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
1988 mButtonState = currentButtonState;
1989
1990 bool wasDown = isPointerDown(lastButtonState);
1991 bool down = isPointerDown(currentButtonState);
1992 bool downChanged;
1993 if (!wasDown && down) {
1994 mDownTime = when;
1995 downChanged = true;
1996 } else if (wasDown && !down) {
1997 downChanged = true;
1998 } else {
1999 downChanged = false;
2000 }
2001 nsecs_t downTime = mDownTime;
2002 bool buttonsChanged = currentButtonState != lastButtonState;
2003
2004 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2005 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2006 bool moved = deltaX != 0 || deltaY != 0;
2007
2008 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2009 && (deltaX != 0.0f || deltaY != 0.0f)) {
2010 // Rotate motion based on display orientation if needed.
2011 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
2012 int32_t orientation;
2013 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
2014 false /*external*/, NULL, NULL, & orientation)) {
2015 orientation = DISPLAY_ORIENTATION_0;
2016 }
2017
2018 rotateDelta(orientation, &deltaX, &deltaY);
2019 }
2020
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002021 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002022 pointerProperties.clear();
2023 pointerProperties.id = 0;
2024 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2025
Jeff Brown6328cdc2010-07-29 18:18:33 -07002026 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002027 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002028
Jeff Brownbe1aa822011-07-27 16:04:54 -07002029 float vscroll = mCursorMotionAccumulator.getRelativeVWheel();
2030 float hscroll = mCursorMotionAccumulator.getRelativeHWheel();
2031 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002032
Jeff Brownbe1aa822011-07-27 16:04:54 -07002033 mWheelYVelocityControl.move(when, NULL, &vscroll);
2034 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002035
Jeff Brownbe1aa822011-07-27 16:04:54 -07002036 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002037
Jeff Brownbe1aa822011-07-27 16:04:54 -07002038 if (mPointerController != NULL) {
2039 if (moved || scrolled || buttonsChanged) {
2040 mPointerController->setPresentation(
2041 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002042
Jeff Brownbe1aa822011-07-27 16:04:54 -07002043 if (moved) {
2044 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002045 }
2046
Jeff Brownbe1aa822011-07-27 16:04:54 -07002047 if (buttonsChanged) {
2048 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002049 }
Jeff Brownefd32662011-03-08 15:13:06 -08002050
Jeff Brownbe1aa822011-07-27 16:04:54 -07002051 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002052 }
2053
Jeff Brownbe1aa822011-07-27 16:04:54 -07002054 float x, y;
2055 mPointerController->getPosition(&x, &y);
2056 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2057 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2058 } else {
2059 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2060 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2061 }
2062
2063 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002064
Jeff Brown56194eb2011-03-02 19:23:13 -08002065 // Moving an external trackball or mouse should wake the device.
2066 // We don't do this for internal cursor devices to prevent them from waking up
2067 // the device in your pocket.
2068 // TODO: Use the input device configuration to control this behavior more finely.
2069 uint32_t policyFlags = 0;
2070 if (getDevice()->isExternal()) {
2071 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2072 }
2073
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002074 // Synthesize key down from buttons if needed.
2075 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2076 policyFlags, lastButtonState, currentButtonState);
2077
2078 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002079 if (downChanged || moved || scrolled || buttonsChanged) {
2080 int32_t metaState = mContext->getGlobalMetaState();
2081 int32_t motionEventAction;
2082 if (downChanged) {
2083 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2084 } else if (down || mPointerController == NULL) {
2085 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2086 } else {
2087 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2088 }
Jeff Brownb6997262010-10-08 22:31:17 -07002089
Jeff Brownbe1aa822011-07-27 16:04:54 -07002090 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2091 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002092 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002093 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002094
Jeff Brownbe1aa822011-07-27 16:04:54 -07002095 // Send hover move after UP to tell the application that the mouse is hovering now.
2096 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2097 && mPointerController != NULL) {
2098 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2099 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2100 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2101 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2102 getListener()->notifyMotion(&hoverArgs);
2103 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002104
Jeff Brownbe1aa822011-07-27 16:04:54 -07002105 // Send scroll events.
2106 if (scrolled) {
2107 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2108 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2109
2110 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2111 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2112 AMOTION_EVENT_EDGE_FLAG_NONE,
2113 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2114 getListener()->notifyMotion(&scrollArgs);
2115 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002116 }
Jeff Browna032cc02011-03-07 16:56:21 -08002117
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002118 // Synthesize key up from buttons if needed.
2119 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2120 policyFlags, lastButtonState, currentButtonState);
2121
Jeff Brown49754db2011-07-01 17:37:58 -07002122 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002123}
2124
Jeff Brown83c09682010-12-23 17:50:18 -08002125int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002126 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2127 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2128 } else {
2129 return AKEY_STATE_UNKNOWN;
2130 }
2131}
2132
Jeff Brown05dc66a2011-03-02 14:41:58 -08002133void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002134 if (mPointerController != NULL) {
2135 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2136 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002137}
2138
Jeff Brown6d0fec22010-07-23 21:28:06 -07002139
2140// --- TouchInputMapper ---
2141
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002142TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002143 InputMapper(device),
2144 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
2145 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002146}
2147
2148TouchInputMapper::~TouchInputMapper() {
2149}
2150
2151uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08002152 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002153}
2154
2155void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2156 InputMapper::populateDeviceInfo(info);
2157
Jeff Brownbe1aa822011-07-27 16:04:54 -07002158 // Ensure surface information is up to date so that orientation changes are
2159 // noticed immediately.
2160 if (!configureSurface()) {
2161 return;
2162 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002163
Jeff Brownbe1aa822011-07-27 16:04:54 -07002164 info->addMotionRange(mOrientedRanges.x);
2165 info->addMotionRange(mOrientedRanges.y);
2166
2167 if (mOrientedRanges.havePressure) {
2168 info->addMotionRange(mOrientedRanges.pressure);
2169 }
2170
2171 if (mOrientedRanges.haveSize) {
2172 info->addMotionRange(mOrientedRanges.size);
2173 }
2174
2175 if (mOrientedRanges.haveTouchSize) {
2176 info->addMotionRange(mOrientedRanges.touchMajor);
2177 info->addMotionRange(mOrientedRanges.touchMinor);
2178 }
2179
2180 if (mOrientedRanges.haveToolSize) {
2181 info->addMotionRange(mOrientedRanges.toolMajor);
2182 info->addMotionRange(mOrientedRanges.toolMinor);
2183 }
2184
2185 if (mOrientedRanges.haveOrientation) {
2186 info->addMotionRange(mOrientedRanges.orientation);
2187 }
2188
2189 if (mOrientedRanges.haveDistance) {
2190 info->addMotionRange(mOrientedRanges.distance);
2191 }
2192
2193 if (mPointerController != NULL) {
2194 float minX, minY, maxX, maxY;
2195 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
2196 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
2197 minX, maxX, 0.0f, 0.0f);
2198 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
2199 minY, maxY, 0.0f, 0.0f);
Jeff Brownefd32662011-03-08 15:13:06 -08002200 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002201 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
2202 0.0f, 1.0f, 0.0f, 0.0f);
2203 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002204}
2205
Jeff Brownef3d7e82010-09-30 14:33:04 -07002206void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002207 dump.append(INDENT2 "Touch Input Mapper:\n");
2208 dumpParameters(dump);
2209 dumpVirtualKeys(dump);
2210 dumpRawPointerAxes(dump);
2211 dumpCalibration(dump);
2212 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002213
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2215 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2216 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2217 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2218 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2219 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2221 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2222 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2223 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002224
Jeff Brownbe1aa822011-07-27 16:04:54 -07002225 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002226
Jeff Brownbe1aa822011-07-27 16:04:54 -07002227 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2228 mLastRawPointerData.pointerCount);
2229 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2230 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2231 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2232 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
2233 "orientation=%d, distance=%d, toolType=%d, isHovering=%s\n", i,
2234 pointer.id, pointer.x, pointer.y, pointer.pressure,
2235 pointer.touchMajor, pointer.touchMinor,
2236 pointer.toolMajor, pointer.toolMinor,
2237 pointer.orientation, pointer.distance,
2238 pointer.toolType, toString(pointer.isHovering));
2239 }
2240
2241 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2242 mLastCookedPointerData.pointerCount);
2243 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2244 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2245 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2246 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2247 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
2248 "orientation=%0.3f, distance=%0.3f, toolType=%d, isHovering=%s\n", i,
2249 pointerProperties.id,
2250 pointerCoords.getX(),
2251 pointerCoords.getY(),
2252 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2253 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2254 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2255 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2256 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2257 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
2258 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2259 pointerProperties.toolType,
2260 toString(mLastCookedPointerData.isHovering(i)));
2261 }
2262
2263 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2264 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2265 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
2266 mPointerGestureXMovementScale);
2267 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
2268 mPointerGestureYMovementScale);
2269 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
2270 mPointerGestureXZoomScale);
2271 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
2272 mPointerGestureYZoomScale);
2273 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2274 mPointerGestureMaxSwipeWidth);
2275 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002276}
2277
Jeff Brownbe1aa822011-07-27 16:04:54 -07002278void TouchInputMapper::initialize() {
2279 mCurrentRawPointerData.clear();
2280 mLastRawPointerData.clear();
2281 mCurrentCookedPointerData.clear();
2282 mLastCookedPointerData.clear();
2283 mCurrentButtonState = 0;
2284 mLastButtonState = 0;
2285 mSentHoverEnter = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002286 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002287
Jeff Brownbe1aa822011-07-27 16:04:54 -07002288 mCurrentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07002289
Jeff Brownbe1aa822011-07-27 16:04:54 -07002290 mOrientedRanges.havePressure = false;
2291 mOrientedRanges.haveSize = false;
2292 mOrientedRanges.haveTouchSize = false;
2293 mOrientedRanges.haveToolSize = false;
2294 mOrientedRanges.haveOrientation = false;
2295 mOrientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08002296
2297 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07002298}
2299
Jeff Brown474dcb52011-06-14 20:22:50 -07002300void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
2301 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002302
Jeff Brown474dcb52011-06-14 20:22:50 -07002303 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002304
Jeff Brown474dcb52011-06-14 20:22:50 -07002305 if (!changes) { // first time only
2306 // Configure basic parameters.
2307 configureParameters();
2308
2309 // Configure sources.
2310 switch (mParameters.deviceType) {
2311 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2312 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
2313 mPointerSource = 0;
2314 break;
2315 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2316 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2317 mPointerSource = 0;
2318 break;
2319 case Parameters::DEVICE_TYPE_POINTER:
2320 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2321 mPointerSource = AINPUT_SOURCE_MOUSE;
2322 break;
2323 default:
2324 LOG_ASSERT(false);
2325 }
2326
2327 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002328 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002329
2330 // Prepare input device calibration.
2331 parseCalibration();
2332 resolveCalibration();
2333
Jeff Brownbe1aa822011-07-27 16:04:54 -07002334 // Configure surface dimensions and orientation.
2335 configureSurface();
Jeff Brown83c09682010-12-23 17:50:18 -08002336 }
2337
Jeff Brown474dcb52011-06-14 20:22:50 -07002338 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2339 mPointerGesture.pointerVelocityControl.setParameters(
2340 mConfig.pointerVelocityControlParameters);
2341 }
Jeff Brown8d608662010-08-30 03:02:23 -07002342
Jeff Brown474dcb52011-06-14 20:22:50 -07002343 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
2344 // Reset the touch screen when pointer gesture enablement changes.
2345 reset();
2346 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002347}
2348
Jeff Brown8d608662010-08-30 03:02:23 -07002349void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002350 // Use the pointer presentation mode for devices that do not support distinct
2351 // multitouch. The spot-based presentation relies on being able to accurately
2352 // locate two or more fingers on the touch pad.
2353 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2354 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002355
Jeff Brown538881e2011-05-25 18:23:38 -07002356 String8 gestureModeString;
2357 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2358 gestureModeString)) {
2359 if (gestureModeString == "pointer") {
2360 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2361 } else if (gestureModeString == "spots") {
2362 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2363 } else if (gestureModeString != "default") {
2364 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2365 }
2366 }
2367
Jeff Brownace13b12011-03-09 17:39:48 -08002368 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2369 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2370 // The device is a cursor device with a touch pad attached.
2371 // By default don't use the touch pad to move the pointer.
2372 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002373 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2374 // The device is a pointing device like a track pad.
2375 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2376 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2377 // The device is a touch screen.
2378 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002379 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002380 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002381 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2382 }
2383
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002384 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002385 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2386 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002387 if (deviceTypeString == "touchScreen") {
2388 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002389 } else if (deviceTypeString == "touchPad") {
2390 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002391 } else if (deviceTypeString == "pointer") {
2392 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002393 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002394 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2395 }
2396 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002397
Jeff Brownefd32662011-03-08 15:13:06 -08002398 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002399 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2400 mParameters.orientationAware);
2401
Jeff Brownbc68a592011-07-25 12:58:12 -07002402 mParameters.associatedDisplayId = -1;
2403 mParameters.associatedDisplayIsExternal = false;
2404 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002405 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002406 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2407 mParameters.associatedDisplayIsExternal =
2408 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2409 && getDevice()->isExternal();
2410 mParameters.associatedDisplayId = 0;
2411 }
Jeff Brown8d608662010-08-30 03:02:23 -07002412}
2413
Jeff Brownef3d7e82010-09-30 14:33:04 -07002414void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002415 dump.append(INDENT3 "Parameters:\n");
2416
Jeff Brown538881e2011-05-25 18:23:38 -07002417 switch (mParameters.gestureMode) {
2418 case Parameters::GESTURE_MODE_POINTER:
2419 dump.append(INDENT4 "GestureMode: pointer\n");
2420 break;
2421 case Parameters::GESTURE_MODE_SPOTS:
2422 dump.append(INDENT4 "GestureMode: spots\n");
2423 break;
2424 default:
2425 assert(false);
2426 }
2427
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002428 switch (mParameters.deviceType) {
2429 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2430 dump.append(INDENT4 "DeviceType: touchScreen\n");
2431 break;
2432 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2433 dump.append(INDENT4 "DeviceType: touchPad\n");
2434 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002435 case Parameters::DEVICE_TYPE_POINTER:
2436 dump.append(INDENT4 "DeviceType: pointer\n");
2437 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002438 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002439 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002440 }
2441
2442 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2443 mParameters.associatedDisplayId);
2444 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2445 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002446}
2447
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448void TouchInputMapper::configureRawPointerAxes() {
2449 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002450}
2451
Jeff Brownbe1aa822011-07-27 16:04:54 -07002452void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2453 dump.append(INDENT3 "Raw Touch Axes:\n");
2454 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2455 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2456 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2457 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2458 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2459 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2460 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2461 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2462 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
2463 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2464 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002465}
2466
Jeff Brownbe1aa822011-07-27 16:04:54 -07002467bool TouchInputMapper::configureSurface() {
Jeff Brown9626b142011-03-03 02:09:54 -08002468 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002469 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002470 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2471 "The device will be inoperable.", getDeviceName().string());
2472 return false;
2473 }
2474
Jeff Brown6d0fec22010-07-23 21:28:06 -07002475 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002476 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002477 int32_t width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2478 int32_t height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002479
2480 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002481 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002482 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002483 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002484 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2485 &mAssociatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002486 return false;
2487 }
Jeff Brownefd32662011-03-08 15:13:06 -08002488
2489 // A touch screen inherits the dimensions of the display.
2490 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002491 width = mAssociatedDisplayWidth;
2492 height = mAssociatedDisplayHeight;
Jeff Brownefd32662011-03-08 15:13:06 -08002493 }
2494
2495 // The device inherits the orientation of the display if it is orientation aware.
2496 if (mParameters.orientationAware) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002497 orientation = mAssociatedDisplayOrientation;
Jeff Brownefd32662011-03-08 15:13:06 -08002498 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002499 }
2500
Jeff Brownace13b12011-03-09 17:39:48 -08002501 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2502 && mPointerController == NULL) {
2503 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2504 }
2505
Jeff Brownbe1aa822011-07-27 16:04:54 -07002506 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002507 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002508 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002509 }
2510
Jeff Brownbe1aa822011-07-27 16:04:54 -07002511 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002512 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002513 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002514 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002515
Jeff Brownbe1aa822011-07-27 16:04:54 -07002516 mSurfaceWidth = width;
2517 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002518
Jeff Brown8d608662010-08-30 03:02:23 -07002519 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002520 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2521 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2522 mXPrecision = 1.0f / mXScale;
2523 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002524
Jeff Brownbe1aa822011-07-27 16:04:54 -07002525 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2526 mOrientedRanges.x.source = mTouchSource;
2527 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2528 mOrientedRanges.y.source = mTouchSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002529
Jeff Brownbe1aa822011-07-27 16:04:54 -07002530 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002531
Jeff Brown8d608662010-08-30 03:02:23 -07002532 // Scale factor for terms that are not oriented in a particular axis.
2533 // If the pixels are square then xScale == yScale otherwise we fake it
2534 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002535 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002536
Jeff Brown8d608662010-08-30 03:02:23 -07002537 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002538 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002539
Jeff Browna1f89ce2011-08-11 00:05:01 -07002540 // Size factors.
2541 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2542 if (mRawPointerAxes.touchMajor.valid
2543 && mRawPointerAxes.touchMajor.maxValue != 0) {
2544 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2545 } else if (mRawPointerAxes.toolMajor.valid
2546 && mRawPointerAxes.toolMajor.maxValue != 0) {
2547 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2548 } else {
2549 mSizeScale = 0.0f;
2550 }
2551
Jeff Brownbe1aa822011-07-27 16:04:54 -07002552 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002553 mOrientedRanges.haveToolSize = true;
2554 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002555
Jeff Brownbe1aa822011-07-27 16:04:54 -07002556 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2557 mOrientedRanges.touchMajor.source = mTouchSource;
2558 mOrientedRanges.touchMajor.min = 0;
2559 mOrientedRanges.touchMajor.max = diagonalSize;
2560 mOrientedRanges.touchMajor.flat = 0;
2561 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002562
Jeff Brownbe1aa822011-07-27 16:04:54 -07002563 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2564 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002565
Jeff Brownbe1aa822011-07-27 16:04:54 -07002566 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2567 mOrientedRanges.toolMajor.source = mTouchSource;
2568 mOrientedRanges.toolMajor.min = 0;
2569 mOrientedRanges.toolMajor.max = diagonalSize;
2570 mOrientedRanges.toolMajor.flat = 0;
2571 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002572
Jeff Brownbe1aa822011-07-27 16:04:54 -07002573 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2574 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002575
2576 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2577 mOrientedRanges.size.source = mTouchSource;
2578 mOrientedRanges.size.min = 0;
2579 mOrientedRanges.size.max = 1.0;
2580 mOrientedRanges.size.flat = 0;
2581 mOrientedRanges.size.fuzz = 0;
2582 } else {
2583 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002584 }
2585
2586 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002587 mPressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002588 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002589 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2590 || mCalibration.pressureCalibration
2591 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2592 if (mCalibration.havePressureScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002593 mPressureScale = mCalibration.pressureScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002594 } else if (mRawPointerAxes.pressure.valid
2595 && mRawPointerAxes.pressure.maxValue != 0) {
2596 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002597 }
2598 }
2599
Jeff Brownbe1aa822011-07-27 16:04:54 -07002600 mOrientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002601
Jeff Brownbe1aa822011-07-27 16:04:54 -07002602 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2603 mOrientedRanges.pressure.source = mTouchSource;
2604 mOrientedRanges.pressure.min = 0;
2605 mOrientedRanges.pressure.max = 1.0;
2606 mOrientedRanges.pressure.flat = 0;
2607 mOrientedRanges.pressure.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002608 }
2609
Jeff Brown8d608662010-08-30 03:02:23 -07002610 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07002611 mOrientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002612 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002613 if (mCalibration.orientationCalibration
2614 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Browna1f89ce2011-08-11 00:05:01 -07002615 if (mRawPointerAxes.orientation.valid
2616 && mRawPointerAxes.orientation.maxValue != 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002617 mOrientationScale = float(M_PI_2) / mRawPointerAxes.orientation.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002618 }
2619 }
2620
Jeff Brownbe1aa822011-07-27 16:04:54 -07002621 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002622
Jeff Brownbe1aa822011-07-27 16:04:54 -07002623 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2624 mOrientedRanges.orientation.source = mTouchSource;
2625 mOrientedRanges.orientation.min = - M_PI_2;
2626 mOrientedRanges.orientation.max = M_PI_2;
2627 mOrientedRanges.orientation.flat = 0;
2628 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002629 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002630
2631 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002632 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002633 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2634 if (mCalibration.distanceCalibration
2635 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2636 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002637 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002638 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002639 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002640 }
2641 }
2642
Jeff Brownbe1aa822011-07-27 16:04:54 -07002643 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002644
Jeff Brownbe1aa822011-07-27 16:04:54 -07002645 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2646 mOrientedRanges.distance.source = mTouchSource;
2647 mOrientedRanges.distance.min =
2648 mRawPointerAxes.distance.minValue * mDistanceScale;
2649 mOrientedRanges.distance.max =
2650 mRawPointerAxes.distance.minValue * mDistanceScale;
2651 mOrientedRanges.distance.flat = 0;
2652 mOrientedRanges.distance.fuzz =
2653 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002654 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002655 }
2656
2657 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002658 // Compute oriented surface dimensions, precision, scales and ranges.
2659 // Note that the maximum value reported is an inclusive maximum value so it is one
2660 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002661 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002662 case DISPLAY_ORIENTATION_90:
2663 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002664 mOrientedSurfaceWidth = mSurfaceHeight;
2665 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002666
Jeff Brownbe1aa822011-07-27 16:04:54 -07002667 mOrientedXPrecision = mYPrecision;
2668 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002669
Jeff Brownbe1aa822011-07-27 16:04:54 -07002670 mOrientedRanges.x.min = 0;
2671 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2672 * mYScale;
2673 mOrientedRanges.x.flat = 0;
2674 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002675
Jeff Brownbe1aa822011-07-27 16:04:54 -07002676 mOrientedRanges.y.min = 0;
2677 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2678 * mXScale;
2679 mOrientedRanges.y.flat = 0;
2680 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002681 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002682
Jeff Brown6d0fec22010-07-23 21:28:06 -07002683 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002684 mOrientedSurfaceWidth = mSurfaceWidth;
2685 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002686
Jeff Brownbe1aa822011-07-27 16:04:54 -07002687 mOrientedXPrecision = mXPrecision;
2688 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002689
Jeff Brownbe1aa822011-07-27 16:04:54 -07002690 mOrientedRanges.x.min = 0;
2691 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2692 * mXScale;
2693 mOrientedRanges.x.flat = 0;
2694 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002695
Jeff Brownbe1aa822011-07-27 16:04:54 -07002696 mOrientedRanges.y.min = 0;
2697 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2698 * mYScale;
2699 mOrientedRanges.y.flat = 0;
2700 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002701 break;
2702 }
Jeff Brownace13b12011-03-09 17:39:48 -08002703
2704 // Compute pointer gesture detection parameters.
Jeff Brownace13b12011-03-09 17:39:48 -08002705 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002706 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2707 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002708 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002709 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2710 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002711
Jeff Brown2352b972011-04-12 22:39:53 -07002712 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002713 // given area relative to the diagonal size of the display when no acceleration
2714 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002715 // Assume that the touch pad has a square aspect ratio such that movements in
2716 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002717 mPointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002718 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002719 mPointerGestureYMovementScale = mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002720
2721 // Scale zooms to cover a smaller range of the display than movements do.
2722 // This value determines the area around the pointer that is affected by freeform
2723 // pointer gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002724 mPointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002725 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002726 mPointerGestureYZoomScale = mPointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002727
Jeff Brown2352b972011-04-12 22:39:53 -07002728 // Max width between pointers to detect a swipe gesture is more than some fraction
2729 // of the diagonal axis of the touch pad. Touches that are wider than this are
2730 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002731 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002732 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002733
2734 // Reset the current pointer gesture.
2735 mPointerGesture.reset();
2736
2737 // Remove any current spots.
2738 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2739 mPointerController->clearSpots();
2740 }
Jeff Brownace13b12011-03-09 17:39:48 -08002741 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002742 }
2743
2744 return true;
2745}
2746
Jeff Brownbe1aa822011-07-27 16:04:54 -07002747void TouchInputMapper::dumpSurface(String8& dump) {
2748 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2749 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2750 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002751}
2752
Jeff Brownbe1aa822011-07-27 16:04:54 -07002753void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002754 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002755 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002756
Jeff Brownbe1aa822011-07-27 16:04:54 -07002757 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002758
Jeff Brown6328cdc2010-07-29 18:18:33 -07002759 if (virtualKeyDefinitions.size() == 0) {
2760 return;
2761 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002762
Jeff Brownbe1aa822011-07-27 16:04:54 -07002763 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002764
Jeff Brownbe1aa822011-07-27 16:04:54 -07002765 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2766 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2767 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2768 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002769
2770 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002771 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002772 virtualKeyDefinitions[i];
2773
Jeff Brownbe1aa822011-07-27 16:04:54 -07002774 mVirtualKeys.add();
2775 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002776
2777 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2778 int32_t keyCode;
2779 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002780 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002781 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002782 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2783 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002784 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07002785 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002786 }
2787
Jeff Brown6328cdc2010-07-29 18:18:33 -07002788 virtualKey.keyCode = keyCode;
2789 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002790
Jeff Brown6328cdc2010-07-29 18:18:33 -07002791 // convert the key definition's display coordinates into touch coordinates for a hit box
2792 int32_t halfWidth = virtualKeyDefinition.width / 2;
2793 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002794
Jeff Brown6328cdc2010-07-29 18:18:33 -07002795 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002796 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002797 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002798 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002799 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002800 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002801 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002802 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002803 }
2804}
2805
Jeff Brownbe1aa822011-07-27 16:04:54 -07002806void TouchInputMapper::dumpVirtualKeys(String8& dump) {
2807 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002808 dump.append(INDENT3 "Virtual Keys:\n");
2809
Jeff Brownbe1aa822011-07-27 16:04:54 -07002810 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
2811 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002812 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2813 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2814 i, virtualKey.scanCode, virtualKey.keyCode,
2815 virtualKey.hitLeft, virtualKey.hitRight,
2816 virtualKey.hitTop, virtualKey.hitBottom);
2817 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002818 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002819}
2820
Jeff Brown8d608662010-08-30 03:02:23 -07002821void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002822 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002823 Calibration& out = mCalibration;
2824
Jeff Browna1f89ce2011-08-11 00:05:01 -07002825 // Size
2826 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2827 String8 sizeCalibrationString;
2828 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
2829 if (sizeCalibrationString == "none") {
2830 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2831 } else if (sizeCalibrationString == "geometric") {
2832 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
2833 } else if (sizeCalibrationString == "diameter") {
2834 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
2835 } else if (sizeCalibrationString == "area") {
2836 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
2837 } else if (sizeCalibrationString != "default") {
2838 LOGW("Invalid value for touch.size.calibration: '%s'",
2839 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002840 }
2841 }
2842
Jeff Browna1f89ce2011-08-11 00:05:01 -07002843 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
2844 out.sizeScale);
2845 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
2846 out.sizeBias);
2847 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
2848 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002849
2850 // Pressure
2851 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2852 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002853 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002854 if (pressureCalibrationString == "none") {
2855 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2856 } else if (pressureCalibrationString == "physical") {
2857 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2858 } else if (pressureCalibrationString == "amplitude") {
2859 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2860 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002861 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002862 pressureCalibrationString.string());
2863 }
2864 }
2865
Jeff Brown8d608662010-08-30 03:02:23 -07002866 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2867 out.pressureScale);
2868
Jeff Brown8d608662010-08-30 03:02:23 -07002869 // Orientation
2870 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2871 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002872 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002873 if (orientationCalibrationString == "none") {
2874 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2875 } else if (orientationCalibrationString == "interpolated") {
2876 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002877 } else if (orientationCalibrationString == "vector") {
2878 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002879 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002880 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002881 orientationCalibrationString.string());
2882 }
2883 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002884
2885 // Distance
2886 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2887 String8 distanceCalibrationString;
2888 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2889 if (distanceCalibrationString == "none") {
2890 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2891 } else if (distanceCalibrationString == "scaled") {
2892 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2893 } else if (distanceCalibrationString != "default") {
2894 LOGW("Invalid value for touch.distance.calibration: '%s'",
2895 distanceCalibrationString.string());
2896 }
2897 }
2898
2899 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2900 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002901}
2902
2903void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07002904 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07002905 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
2906 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
2907 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07002908 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07002909 } else {
2910 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2911 }
Jeff Brown8d608662010-08-30 03:02:23 -07002912
Jeff Browna1f89ce2011-08-11 00:05:01 -07002913 // Pressure
2914 if (mRawPointerAxes.pressure.valid) {
2915 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
2916 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2917 }
2918 } else {
2919 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002920 }
2921
2922 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07002923 if (mRawPointerAxes.orientation.valid) {
2924 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07002925 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07002926 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07002927 } else {
2928 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002929 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002930
2931 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07002932 if (mRawPointerAxes.distance.valid) {
2933 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002934 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002935 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07002936 } else {
2937 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002938 }
Jeff Brown8d608662010-08-30 03:02:23 -07002939}
2940
Jeff Brownef3d7e82010-09-30 14:33:04 -07002941void TouchInputMapper::dumpCalibration(String8& dump) {
2942 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002943
Jeff Browna1f89ce2011-08-11 00:05:01 -07002944 // Size
2945 switch (mCalibration.sizeCalibration) {
2946 case Calibration::SIZE_CALIBRATION_NONE:
2947 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002948 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002949 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
2950 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002951 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002952 case Calibration::SIZE_CALIBRATION_DIAMETER:
2953 dump.append(INDENT4 "touch.size.calibration: diameter\n");
2954 break;
2955 case Calibration::SIZE_CALIBRATION_AREA:
2956 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002957 break;
2958 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002959 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002960 }
2961
Jeff Browna1f89ce2011-08-11 00:05:01 -07002962 if (mCalibration.haveSizeScale) {
2963 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
2964 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002965 }
2966
Jeff Browna1f89ce2011-08-11 00:05:01 -07002967 if (mCalibration.haveSizeBias) {
2968 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
2969 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002970 }
2971
Jeff Browna1f89ce2011-08-11 00:05:01 -07002972 if (mCalibration.haveSizeIsSummed) {
2973 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
2974 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002975 }
2976
2977 // Pressure
2978 switch (mCalibration.pressureCalibration) {
2979 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002980 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002981 break;
2982 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002983 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002984 break;
2985 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002986 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002987 break;
2988 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002989 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002990 }
2991
Jeff Brown8d608662010-08-30 03:02:23 -07002992 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002993 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2994 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002995 }
2996
Jeff Brown8d608662010-08-30 03:02:23 -07002997 // Orientation
2998 switch (mCalibration.orientationCalibration) {
2999 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003000 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003001 break;
3002 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003003 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003004 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003005 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3006 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3007 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003008 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003009 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003010 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003011
3012 // Distance
3013 switch (mCalibration.distanceCalibration) {
3014 case Calibration::DISTANCE_CALIBRATION_NONE:
3015 dump.append(INDENT4 "touch.distance.calibration: none\n");
3016 break;
3017 case Calibration::DISTANCE_CALIBRATION_SCALED:
3018 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3019 break;
3020 default:
3021 LOG_ASSERT(false);
3022 }
3023
3024 if (mCalibration.haveDistanceScale) {
3025 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3026 mCalibration.distanceScale);
3027 }
Jeff Brown8d608662010-08-30 03:02:23 -07003028}
3029
Jeff Brown6d0fec22010-07-23 21:28:06 -07003030void TouchInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003031 // Synthesize touch up event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003032 // This will also take care of finishing virtual key processing if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003033 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
3034 mCurrentRawPointerData.clear();
3035 mCurrentButtonState = 0;
3036 syncTouch(when, true);
3037
3038 initialize();
3039
3040 if (mPointerController != NULL
3041 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3042 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3043 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003044 }
3045
Jeff Brown6328cdc2010-07-29 18:18:33 -07003046 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003047}
3048
3049void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07003050#if DEBUG_RAW_EVENTS
3051 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003052 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3053 mLastRawPointerData.pointerCount,
3054 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003055 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003056 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3057 "hovering ids 0x%08x -> 0x%08x",
3058 mLastRawPointerData.pointerCount,
3059 mCurrentRawPointerData.pointerCount,
3060 mLastRawPointerData.touchingIdBits.value,
3061 mCurrentRawPointerData.touchingIdBits.value,
3062 mLastRawPointerData.hoveringIdBits.value,
3063 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003064 }
3065#endif
3066
Jeff Brownbe1aa822011-07-27 16:04:54 -07003067 // Configure the surface now, if possible.
3068 if (!configureSurface()) {
3069 mLastRawPointerData.clear();
3070 mLastCookedPointerData.clear();
3071 mLastButtonState = 0;
3072 return;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003073 }
3074
Jeff Brownbe1aa822011-07-27 16:04:54 -07003075 // Preprocess pointer data.
3076 if (!havePointerIds) {
3077 assignPointerIds();
3078 }
3079
3080 // Handle policy on initial down or hover events.
Jeff Brown56194eb2011-03-02 19:23:13 -08003081 uint32_t policyFlags = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003082 if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08003083 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
3084 // If this is a touch screen, hide the pointer on an initial down.
3085 getContext()->fadePointer();
3086 }
Jeff Brown56194eb2011-03-02 19:23:13 -08003087
3088 // Initial downs on external touch devices should wake the device.
3089 // We don't do this for internal touch screens to prevent them from waking
3090 // up in your pocket.
3091 // TODO: Use the input device configuration to control this behavior more finely.
3092 if (getDevice()->isExternal()) {
3093 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3094 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08003095 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003096
Jeff Brownbe1aa822011-07-27 16:04:54 -07003097 // Synthesize key down from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003098 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003099 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003100
Jeff Brownbe1aa822011-07-27 16:04:54 -07003101 if (consumeRawTouches(when, policyFlags)) {
3102 mCurrentRawPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003103 }
3104
Jeff Brownbe1aa822011-07-27 16:04:54 -07003105 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
3106 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3107 }
3108
3109 cookPointerData();
3110 dispatchHoverExit(when, policyFlags);
3111 dispatchTouches(when, policyFlags);
3112 dispatchHoverEnterAndMove(when, policyFlags);
3113
3114 // Synthesize key up from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003115 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003116 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003117
Jeff Brown6328cdc2010-07-29 18:18:33 -07003118 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003119 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3120 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3121 mLastButtonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003122}
3123
Jeff Brown79ac9692011-04-19 21:20:10 -07003124void TouchInputMapper::timeoutExpired(nsecs_t when) {
3125 if (mPointerController != NULL) {
3126 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3127 }
3128}
3129
Jeff Brownbe1aa822011-07-27 16:04:54 -07003130bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3131 // Check for release of a virtual key.
3132 if (mCurrentVirtualKey.down) {
3133 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3134 // Pointer went up while virtual key was down.
3135 mCurrentVirtualKey.down = false;
3136 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003137#if DEBUG_VIRTUAL_KEYS
3138 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003139 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003140#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003141 dispatchVirtualKey(when, policyFlags,
3142 AKEY_EVENT_ACTION_UP,
3143 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003144 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003145 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003146 }
3147
Jeff Brownbe1aa822011-07-27 16:04:54 -07003148 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3149 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3150 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3151 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3152 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3153 // Pointer is still within the space of the virtual key.
3154 return true;
3155 }
3156 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003157
Jeff Brownbe1aa822011-07-27 16:04:54 -07003158 // Pointer left virtual key area or another pointer also went down.
3159 // Send key cancellation but do not consume the touch yet.
3160 // This is useful when the user swipes through from the virtual key area
3161 // into the main display surface.
3162 mCurrentVirtualKey.down = false;
3163 if (!mCurrentVirtualKey.ignored) {
3164#if DEBUG_VIRTUAL_KEYS
3165 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3166 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3167#endif
3168 dispatchVirtualKey(when, policyFlags,
3169 AKEY_EVENT_ACTION_UP,
3170 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3171 | AKEY_EVENT_FLAG_CANCELED);
3172 }
3173 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003174
Jeff Brownbe1aa822011-07-27 16:04:54 -07003175 if (mLastRawPointerData.touchingIdBits.isEmpty()
3176 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3177 // Pointer just went down. Check for virtual key press or off-screen touches.
3178 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3179 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3180 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3181 // If exactly one pointer went down, check for virtual key hit.
3182 // Otherwise we will drop the entire stroke.
3183 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3184 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3185 if (virtualKey) {
3186 mCurrentVirtualKey.down = true;
3187 mCurrentVirtualKey.downTime = when;
3188 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3189 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3190 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3191 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3192
3193 if (!mCurrentVirtualKey.ignored) {
3194#if DEBUG_VIRTUAL_KEYS
3195 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3196 mCurrentVirtualKey.keyCode,
3197 mCurrentVirtualKey.scanCode);
3198#endif
3199 dispatchVirtualKey(when, policyFlags,
3200 AKEY_EVENT_ACTION_DOWN,
3201 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3202 }
3203 }
3204 }
3205 return true;
3206 }
3207 }
3208
Jeff Brownfe508922011-01-18 15:10:10 -08003209 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003210 // most recent touch within the screen area. The idea is to filter out stray
3211 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003212 //
3213 // Problems we're trying to solve:
3214 //
3215 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3216 // virtual key area that is implemented by a separate touch panel and accidentally
3217 // triggers a virtual key.
3218 //
3219 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3220 // area and accidentally triggers a virtual key. This often happens when virtual keys
3221 // are layed out below the screen near to where the on screen keyboard's space bar
3222 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003223 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003224 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003225 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003226 return false;
3227}
3228
3229void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3230 int32_t keyEventAction, int32_t keyEventFlags) {
3231 int32_t keyCode = mCurrentVirtualKey.keyCode;
3232 int32_t scanCode = mCurrentVirtualKey.scanCode;
3233 nsecs_t downTime = mCurrentVirtualKey.downTime;
3234 int32_t metaState = mContext->getGlobalMetaState();
3235 policyFlags |= POLICY_FLAG_VIRTUAL;
3236
3237 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3238 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3239 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003240}
3241
Jeff Brown6d0fec22010-07-23 21:28:06 -07003242void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003243 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3244 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003245 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003246 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003247
3248 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003249 if (!currentIdBits.isEmpty()) {
3250 // No pointer id changes so this is a move event.
3251 // The listener takes care of batching moves so we don't have to deal with that here.
3252 dispatchMotion(when, policyFlags, mTouchSource,
3253 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3254 AMOTION_EVENT_EDGE_FLAG_NONE,
3255 mCurrentCookedPointerData.pointerProperties,
3256 mCurrentCookedPointerData.pointerCoords,
3257 mCurrentCookedPointerData.idToIndex,
3258 currentIdBits, -1,
3259 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3260 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003261 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003262 // There may be pointers going up and pointers going down and pointers moving
3263 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003264 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3265 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003266 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003267 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003268
Jeff Brownace13b12011-03-09 17:39:48 -08003269 // Update last coordinates of pointers that have moved so that we observe the new
3270 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003271 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003272 mCurrentCookedPointerData.pointerProperties,
3273 mCurrentCookedPointerData.pointerCoords,
3274 mCurrentCookedPointerData.idToIndex,
3275 mLastCookedPointerData.pointerProperties,
3276 mLastCookedPointerData.pointerCoords,
3277 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003278 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003279 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003280 moveNeeded = true;
3281 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003282
Jeff Brownace13b12011-03-09 17:39:48 -08003283 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003284 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003285 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003286
Jeff Brownace13b12011-03-09 17:39:48 -08003287 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003288 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003289 mLastCookedPointerData.pointerProperties,
3290 mLastCookedPointerData.pointerCoords,
3291 mLastCookedPointerData.idToIndex,
3292 dispatchedIdBits, upId,
3293 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003294 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003295 }
3296
Jeff Brownc3db8582010-10-20 15:33:38 -07003297 // Dispatch move events if any of the remaining pointers moved from their old locations.
3298 // Although applications receive new locations as part of individual pointer up
3299 // events, they do not generally handle them except when presented in a move event.
3300 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003301 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003302 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003303 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003304 mCurrentCookedPointerData.pointerProperties,
3305 mCurrentCookedPointerData.pointerCoords,
3306 mCurrentCookedPointerData.idToIndex,
3307 dispatchedIdBits, -1,
3308 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003309 }
3310
3311 // Dispatch pointer down events using the new pointer locations.
3312 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003313 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003314 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003315
Jeff Brownace13b12011-03-09 17:39:48 -08003316 if (dispatchedIdBits.count() == 1) {
3317 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003318 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003319 }
3320
Jeff Brownace13b12011-03-09 17:39:48 -08003321 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003322 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003323 mCurrentCookedPointerData.pointerProperties,
3324 mCurrentCookedPointerData.pointerCoords,
3325 mCurrentCookedPointerData.idToIndex,
3326 dispatchedIdBits, downId,
3327 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003328 }
3329 }
Jeff Brownace13b12011-03-09 17:39:48 -08003330}
3331
Jeff Brownbe1aa822011-07-27 16:04:54 -07003332void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3333 if (mSentHoverEnter &&
3334 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3335 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3336 int32_t metaState = getContext()->getGlobalMetaState();
3337 dispatchMotion(when, policyFlags, mTouchSource,
3338 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3339 mLastCookedPointerData.pointerProperties,
3340 mLastCookedPointerData.pointerCoords,
3341 mLastCookedPointerData.idToIndex,
3342 mLastCookedPointerData.hoveringIdBits, -1,
3343 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3344 mSentHoverEnter = false;
3345 }
3346}
Jeff Brownace13b12011-03-09 17:39:48 -08003347
Jeff Brownbe1aa822011-07-27 16:04:54 -07003348void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3349 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3350 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3351 int32_t metaState = getContext()->getGlobalMetaState();
3352 if (!mSentHoverEnter) {
3353 dispatchMotion(when, policyFlags, mTouchSource,
3354 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3355 mCurrentCookedPointerData.pointerProperties,
3356 mCurrentCookedPointerData.pointerCoords,
3357 mCurrentCookedPointerData.idToIndex,
3358 mCurrentCookedPointerData.hoveringIdBits, -1,
3359 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3360 mSentHoverEnter = true;
3361 }
Jeff Brownace13b12011-03-09 17:39:48 -08003362
Jeff Brownbe1aa822011-07-27 16:04:54 -07003363 dispatchMotion(when, policyFlags, mTouchSource,
3364 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3365 mCurrentCookedPointerData.pointerProperties,
3366 mCurrentCookedPointerData.pointerCoords,
3367 mCurrentCookedPointerData.idToIndex,
3368 mCurrentCookedPointerData.hoveringIdBits, -1,
3369 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3370 }
3371}
3372
3373void TouchInputMapper::cookPointerData() {
3374 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3375
3376 mCurrentCookedPointerData.clear();
3377 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3378 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3379 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3380
3381 // Walk through the the active pointers and map device coordinates onto
3382 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003383 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003384 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003385
Jeff Browna1f89ce2011-08-11 00:05:01 -07003386 // Size
3387 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3388 switch (mCalibration.sizeCalibration) {
3389 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3390 case Calibration::SIZE_CALIBRATION_DIAMETER:
3391 case Calibration::SIZE_CALIBRATION_AREA:
3392 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3393 touchMajor = in.touchMajor;
3394 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3395 toolMajor = in.toolMajor;
3396 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3397 size = mRawPointerAxes.touchMinor.valid
3398 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3399 } else if (mRawPointerAxes.touchMajor.valid) {
3400 toolMajor = touchMajor = in.touchMajor;
3401 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3402 ? in.touchMinor : in.touchMajor;
3403 size = mRawPointerAxes.touchMinor.valid
3404 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3405 } else if (mRawPointerAxes.toolMajor.valid) {
3406 touchMajor = toolMajor = in.toolMajor;
3407 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3408 ? in.toolMinor : in.toolMajor;
3409 size = mRawPointerAxes.toolMinor.valid
3410 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003411 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003412 LOG_ASSERT(false, "No touch or tool axes. "
3413 "Size calibration should have been resolved to NONE.");
3414 touchMajor = 0;
3415 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003416 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003417 toolMinor = 0;
3418 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003419 }
Jeff Brownace13b12011-03-09 17:39:48 -08003420
Jeff Browna1f89ce2011-08-11 00:05:01 -07003421 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3422 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3423 if (touchingCount > 1) {
3424 touchMajor /= touchingCount;
3425 touchMinor /= touchingCount;
3426 toolMajor /= touchingCount;
3427 toolMinor /= touchingCount;
3428 size /= touchingCount;
3429 }
3430 }
Jeff Brownace13b12011-03-09 17:39:48 -08003431
Jeff Browna1f89ce2011-08-11 00:05:01 -07003432 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3433 touchMajor *= mGeometricScale;
3434 touchMinor *= mGeometricScale;
3435 toolMajor *= mGeometricScale;
3436 toolMinor *= mGeometricScale;
3437 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3438 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003439 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003440 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3441 toolMinor = toolMajor;
3442 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3443 touchMinor = touchMajor;
3444 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003445 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003446
3447 mCalibration.applySizeScaleAndBias(&touchMajor);
3448 mCalibration.applySizeScaleAndBias(&touchMinor);
3449 mCalibration.applySizeScaleAndBias(&toolMajor);
3450 mCalibration.applySizeScaleAndBias(&toolMinor);
3451 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003452 break;
3453 default:
3454 touchMajor = 0;
3455 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003456 toolMajor = 0;
3457 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003458 size = 0;
3459 break;
3460 }
3461
Jeff Browna1f89ce2011-08-11 00:05:01 -07003462 // Pressure
3463 float pressure;
3464 switch (mCalibration.pressureCalibration) {
3465 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3466 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3467 pressure = in.pressure * mPressureScale;
3468 break;
3469 default:
3470 pressure = in.isHovering ? 0 : 1;
3471 break;
3472 }
3473
Jeff Brownace13b12011-03-09 17:39:48 -08003474 // Orientation
3475 float orientation;
3476 switch (mCalibration.orientationCalibration) {
3477 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003478 orientation = in.orientation * mOrientationScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003479 break;
3480 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3481 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3482 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3483 if (c1 != 0 || c2 != 0) {
3484 orientation = atan2f(c1, c2) * 0.5f;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003485 float confidence = hypotf(c1, c2);
3486 float scale = 1.0f + confidence / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003487 touchMajor *= scale;
3488 touchMinor /= scale;
3489 toolMajor *= scale;
3490 toolMinor /= scale;
3491 } else {
3492 orientation = 0;
3493 }
3494 break;
3495 }
3496 default:
3497 orientation = 0;
3498 }
3499
Jeff Brown80fd47c2011-05-24 01:07:44 -07003500 // Distance
3501 float distance;
3502 switch (mCalibration.distanceCalibration) {
3503 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003504 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003505 break;
3506 default:
3507 distance = 0;
3508 }
3509
Jeff Brownace13b12011-03-09 17:39:48 -08003510 // X and Y
3511 // Adjust coords for surface orientation.
3512 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003513 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003514 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003515 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3516 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003517 orientation -= M_PI_2;
3518 if (orientation < - M_PI_2) {
3519 orientation += M_PI;
3520 }
3521 break;
3522 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003523 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3524 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003525 break;
3526 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003527 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3528 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003529 orientation += M_PI_2;
3530 if (orientation > M_PI_2) {
3531 orientation -= M_PI;
3532 }
3533 break;
3534 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003535 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3536 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003537 break;
3538 }
3539
3540 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003541 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003542 out.clear();
3543 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3544 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3545 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3546 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3547 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3548 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3549 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3550 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3551 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003552 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003553
3554 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003555 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3556 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003557 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003558 properties.id = id;
3559 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003560
Jeff Brownbe1aa822011-07-27 16:04:54 -07003561 // Write id index.
3562 mCurrentCookedPointerData.idToIndex[id] = i;
3563 }
Jeff Brownace13b12011-03-09 17:39:48 -08003564}
3565
Jeff Brown79ac9692011-04-19 21:20:10 -07003566void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3567 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003568 // Update current gesture coordinates.
3569 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003570 bool sendEvents = preparePointerGestures(when,
3571 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3572 if (!sendEvents) {
3573 return;
3574 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003575 if (finishPreviousGesture) {
3576 cancelPreviousGesture = false;
3577 }
Jeff Brownace13b12011-03-09 17:39:48 -08003578
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003579 // Update the pointer presentation and spots.
3580 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3581 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3582 if (finishPreviousGesture || cancelPreviousGesture) {
3583 mPointerController->clearSpots();
3584 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003585 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3586 mPointerGesture.currentGestureIdToIndex,
3587 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003588 } else {
3589 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3590 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003591
Jeff Brown538881e2011-05-25 18:23:38 -07003592 // Show or hide the pointer if needed.
3593 switch (mPointerGesture.currentGestureMode) {
3594 case PointerGesture::NEUTRAL:
3595 case PointerGesture::QUIET:
3596 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3597 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3598 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3599 // Remind the user of where the pointer is after finishing a gesture with spots.
3600 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3601 }
3602 break;
3603 case PointerGesture::TAP:
3604 case PointerGesture::TAP_DRAG:
3605 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3606 case PointerGesture::HOVER:
3607 case PointerGesture::PRESS:
3608 // Unfade the pointer when the current gesture manipulates the
3609 // area directly under the pointer.
3610 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3611 break;
3612 case PointerGesture::SWIPE:
3613 case PointerGesture::FREEFORM:
3614 // Fade the pointer when the current gesture manipulates a different
3615 // area and there are spots to guide the user experience.
3616 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3617 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3618 } else {
3619 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3620 }
3621 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003622 }
3623
Jeff Brownace13b12011-03-09 17:39:48 -08003624 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003625 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003626 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003627
3628 // Update last coordinates of pointers that have moved so that we observe the new
3629 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003630 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3631 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3632 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003633 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003634 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3635 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3636 bool moveNeeded = false;
3637 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003638 && !mPointerGesture.lastGestureIdBits.isEmpty()
3639 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003640 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3641 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003642 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003643 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003644 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003645 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3646 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003647 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003648 moveNeeded = true;
3649 }
Jeff Brownace13b12011-03-09 17:39:48 -08003650 }
3651
3652 // Send motion events for all pointers that went up or were canceled.
3653 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3654 if (!dispatchedGestureIdBits.isEmpty()) {
3655 if (cancelPreviousGesture) {
3656 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003657 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3658 AMOTION_EVENT_EDGE_FLAG_NONE,
3659 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003660 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3661 dispatchedGestureIdBits, -1,
3662 0, 0, mPointerGesture.downTime);
3663
3664 dispatchedGestureIdBits.clear();
3665 } else {
3666 BitSet32 upGestureIdBits;
3667 if (finishPreviousGesture) {
3668 upGestureIdBits = dispatchedGestureIdBits;
3669 } else {
3670 upGestureIdBits.value = dispatchedGestureIdBits.value
3671 & ~mPointerGesture.currentGestureIdBits.value;
3672 }
3673 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003674 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003675
3676 dispatchMotion(when, policyFlags, mPointerSource,
3677 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003678 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3679 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003680 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3681 dispatchedGestureIdBits, id,
3682 0, 0, mPointerGesture.downTime);
3683
3684 dispatchedGestureIdBits.clearBit(id);
3685 }
3686 }
3687 }
3688
3689 // Send motion events for all pointers that moved.
3690 if (moveNeeded) {
3691 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003692 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3693 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003694 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3695 dispatchedGestureIdBits, -1,
3696 0, 0, mPointerGesture.downTime);
3697 }
3698
3699 // Send motion events for all pointers that went down.
3700 if (down) {
3701 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3702 & ~dispatchedGestureIdBits.value);
3703 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003704 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003705 dispatchedGestureIdBits.markBit(id);
3706
Jeff Brownace13b12011-03-09 17:39:48 -08003707 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003708 mPointerGesture.downTime = when;
3709 }
3710
3711 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003712 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003713 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003714 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3715 dispatchedGestureIdBits, id,
3716 0, 0, mPointerGesture.downTime);
3717 }
3718 }
3719
Jeff Brownace13b12011-03-09 17:39:48 -08003720 // Send motion events for hover.
3721 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3722 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003723 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3724 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3725 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003726 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3727 mPointerGesture.currentGestureIdBits, -1,
3728 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003729 } else if (dispatchedGestureIdBits.isEmpty()
3730 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3731 // Synthesize a hover move event after all pointers go up to indicate that
3732 // the pointer is hovering again even if the user is not currently touching
3733 // the touch pad. This ensures that a view will receive a fresh hover enter
3734 // event after a tap.
3735 float x, y;
3736 mPointerController->getPosition(&x, &y);
3737
3738 PointerProperties pointerProperties;
3739 pointerProperties.clear();
3740 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07003741 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07003742
3743 PointerCoords pointerCoords;
3744 pointerCoords.clear();
3745 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3746 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3747
Jeff Brownbe1aa822011-07-27 16:04:54 -07003748 NotifyMotionArgs args(when, getDeviceId(), mPointerSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07003749 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3750 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3751 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003752 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08003753 }
3754
3755 // Update state.
3756 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3757 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003758 mPointerGesture.lastGestureIdBits.clear();
3759 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003760 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3761 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003762 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003763 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003764 mPointerGesture.lastGestureProperties[index].copyFrom(
3765 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003766 mPointerGesture.lastGestureCoords[index].copyFrom(
3767 mPointerGesture.currentGestureCoords[index]);
3768 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003769 }
3770 }
3771}
3772
Jeff Brown79ac9692011-04-19 21:20:10 -07003773bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3774 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003775 *outCancelPreviousGesture = false;
3776 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003777
Jeff Brown79ac9692011-04-19 21:20:10 -07003778 // Handle TAP timeout.
3779 if (isTimeout) {
3780#if DEBUG_GESTURES
3781 LOGD("Gestures: Processing timeout");
3782#endif
3783
3784 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003785 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003786 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003787 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003788 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003789 } else {
3790 // The tap is finished.
3791#if DEBUG_GESTURES
3792 LOGD("Gestures: TAP finished");
3793#endif
3794 *outFinishPreviousGesture = true;
3795
3796 mPointerGesture.activeGestureId = -1;
3797 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3798 mPointerGesture.currentGestureIdBits.clear();
3799
Jeff Brown19c97d462011-06-01 12:33:19 -07003800 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07003801 return true;
3802 }
3803 }
3804
3805 // We did not handle this timeout.
3806 return false;
3807 }
3808
Jeff Brownace13b12011-03-09 17:39:48 -08003809 // Update the velocity tracker.
3810 {
3811 VelocityTracker::Position positions[MAX_POINTERS];
3812 uint32_t count = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003813 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); count++) {
3814 uint32_t id = idBits.clearFirstMarkedBit();
3815 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3816 positions[count].x = pointer.x * mPointerGestureXMovementScale;
3817 positions[count].y = pointer.y * mPointerGestureYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003818 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003819 mPointerGesture.velocityTracker.addMovement(when,
3820 mCurrentRawPointerData.touchingIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08003821 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003822
Jeff Brownace13b12011-03-09 17:39:48 -08003823 // Pick a new active touch id if needed.
3824 // Choose an arbitrary pointer that just went down, if there is one.
3825 // Otherwise choose an arbitrary remaining pointer.
3826 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003827 // We keep the same active touch id for as long as possible.
3828 bool activeTouchChanged = false;
3829 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3830 int32_t activeTouchId = lastActiveTouchId;
3831 if (activeTouchId < 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003832 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07003833 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003834 activeTouchId = mPointerGesture.activeTouchId =
3835 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07003836 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003837 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003838 } else if (!mCurrentRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07003839 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003840 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3841 activeTouchId = mPointerGesture.activeTouchId =
3842 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07003843 } else {
3844 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003845 }
3846 }
3847
Jeff Brownbe1aa822011-07-27 16:04:54 -07003848 uint32_t currentTouchingPointerCount = mCurrentRawPointerData.touchingIdBits.count();
3849 uint32_t lastTouchingPointerCount = mLastRawPointerData.touchingIdBits.count();
3850
Jeff Brownace13b12011-03-09 17:39:48 -08003851 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003852 bool isQuietTime = false;
3853 if (activeTouchId < 0) {
3854 mPointerGesture.resetQuietTime();
3855 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07003856 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07003857 if (!isQuietTime) {
3858 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3859 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3860 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003861 && currentTouchingPointerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07003862 // Enter quiet time when exiting swipe or freeform state.
3863 // This is to prevent accidentally entering the hover state and flinging the
3864 // pointer when finishing a swipe and there is still one pointer left onscreen.
3865 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003866 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brownbe1aa822011-07-27 16:04:54 -07003867 && currentTouchingPointerCount >= 2
3868 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07003869 // Enter quiet time when releasing the button and there are still two or more
3870 // fingers down. This may indicate that one finger was used to press the button
3871 // but it has not gone up yet.
3872 isQuietTime = true;
3873 }
3874 if (isQuietTime) {
3875 mPointerGesture.quietTime = when;
3876 }
Jeff Brownace13b12011-03-09 17:39:48 -08003877 }
3878 }
3879
3880 // Switch states based on button and pointer state.
3881 if (isQuietTime) {
3882 // Case 1: Quiet time. (QUIET)
3883#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003884 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003885 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003886#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003887 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
3888 *outFinishPreviousGesture = true;
3889 }
Jeff Brownace13b12011-03-09 17:39:48 -08003890
3891 mPointerGesture.activeGestureId = -1;
3892 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003893 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003894
Jeff Brown19c97d462011-06-01 12:33:19 -07003895 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003896 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003897 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003898 // The pointer follows the active touch point.
3899 // Emit DOWN, MOVE, UP events at the pointer location.
3900 //
3901 // Only the active touch matters; other fingers are ignored. This policy helps
3902 // to handle the case where the user places a second finger on the touch pad
3903 // to apply the necessary force to depress an integrated button below the surface.
3904 // We don't want the second finger to be delivered to applications.
3905 //
3906 // For this to work well, we need to make sure to track the pointer that is really
3907 // active. If the user first puts one finger down to click then adds another
3908 // finger to drag then the active pointer should switch to the finger that is
3909 // being dragged.
3910#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003911 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003912 "currentTouchingPointerCount=%d", activeTouchId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08003913#endif
3914 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003915 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003916 *outFinishPreviousGesture = true;
3917 mPointerGesture.activeGestureId = 0;
3918 }
3919
3920 // Switch pointers if needed.
3921 // Find the fastest pointer and follow it.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003922 if (activeTouchId >= 0 && currentTouchingPointerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07003923 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07003924 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003925 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3926 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07003927 float vx, vy;
3928 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3929 float speed = hypotf(vx, vy);
3930 if (speed > bestSpeed) {
3931 bestId = id;
3932 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08003933 }
Jeff Brown8d608662010-08-30 03:02:23 -07003934 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003935 }
3936 if (bestId >= 0 && bestId != activeTouchId) {
3937 mPointerGesture.activeTouchId = activeTouchId = bestId;
3938 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003939#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07003940 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3941 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08003942#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003943 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003944 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003945
Jeff Brownbe1aa822011-07-27 16:04:54 -07003946 if (activeTouchId >= 0 && mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
3947 const RawPointerData::Pointer& currentPointer =
3948 mCurrentRawPointerData.pointerForId(activeTouchId);
3949 const RawPointerData::Pointer& lastPointer =
3950 mLastRawPointerData.pointerForId(activeTouchId);
3951 float deltaX = (currentPointer.x - lastPointer.x) * mPointerGestureXMovementScale;
3952 float deltaY = (currentPointer.y - lastPointer.y) * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003953
Jeff Brownbe1aa822011-07-27 16:04:54 -07003954 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003955 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3956
3957 // Move the pointer using a relative motion.
3958 // When using spots, the click will occur at the position of the anchor
3959 // spot and all other spots will move there.
3960 mPointerController->move(deltaX, deltaY);
3961 } else {
3962 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003963 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003964
Jeff Brownace13b12011-03-09 17:39:48 -08003965 float x, y;
3966 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003967
Jeff Brown79ac9692011-04-19 21:20:10 -07003968 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003969 mPointerGesture.currentGestureIdBits.clear();
3970 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3971 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003972 mPointerGesture.currentGestureProperties[0].clear();
3973 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07003974 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003975 mPointerGesture.currentGestureCoords[0].clear();
3976 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3977 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3978 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003979 } else if (currentTouchingPointerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08003980 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003981 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
3982 *outFinishPreviousGesture = true;
3983 }
Jeff Brownace13b12011-03-09 17:39:48 -08003984
Jeff Brown79ac9692011-04-19 21:20:10 -07003985 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003986 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08003987 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003988 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3989 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003990 && lastTouchingPointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003991 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08003992 float x, y;
3993 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003994 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3995 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08003996#if DEBUG_GESTURES
3997 LOGD("Gestures: TAP");
3998#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003999
4000 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004001 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004002 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004003
Jeff Brownace13b12011-03-09 17:39:48 -08004004 mPointerGesture.activeGestureId = 0;
4005 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004006 mPointerGesture.currentGestureIdBits.clear();
4007 mPointerGesture.currentGestureIdBits.markBit(
4008 mPointerGesture.activeGestureId);
4009 mPointerGesture.currentGestureIdToIndex[
4010 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004011 mPointerGesture.currentGestureProperties[0].clear();
4012 mPointerGesture.currentGestureProperties[0].id =
4013 mPointerGesture.activeGestureId;
4014 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004015 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004016 mPointerGesture.currentGestureCoords[0].clear();
4017 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004018 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004019 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004020 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004021 mPointerGesture.currentGestureCoords[0].setAxisValue(
4022 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004023
Jeff Brownace13b12011-03-09 17:39:48 -08004024 tapped = true;
4025 } else {
4026#if DEBUG_GESTURES
4027 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004028 x - mPointerGesture.tapX,
4029 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004030#endif
4031 }
4032 } else {
4033#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004034 LOGD("Gestures: Not a TAP, %0.3fms since down",
4035 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004036#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004037 }
Jeff Brownace13b12011-03-09 17:39:48 -08004038 }
Jeff Brown2352b972011-04-12 22:39:53 -07004039
Jeff Brown19c97d462011-06-01 12:33:19 -07004040 mPointerGesture.pointerVelocityControl.reset();
4041
Jeff Brownace13b12011-03-09 17:39:48 -08004042 if (!tapped) {
4043#if DEBUG_GESTURES
4044 LOGD("Gestures: NEUTRAL");
4045#endif
4046 mPointerGesture.activeGestureId = -1;
4047 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004048 mPointerGesture.currentGestureIdBits.clear();
4049 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004050 } else if (currentTouchingPointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004051 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004052 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004053 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4054 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004055 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004056
Jeff Brown79ac9692011-04-19 21:20:10 -07004057 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4058 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004059 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004060 float x, y;
4061 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004062 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4063 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004064 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4065 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004066#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004067 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4068 x - mPointerGesture.tapX,
4069 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004070#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004071 }
4072 } else {
4073#if DEBUG_GESTURES
4074 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4075 (when - mPointerGesture.tapUpTime) * 0.000001f);
4076#endif
4077 }
4078 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4079 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4080 }
Jeff Brownace13b12011-03-09 17:39:48 -08004081
Jeff Brownbe1aa822011-07-27 16:04:54 -07004082 if (mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
4083 const RawPointerData::Pointer& currentPointer =
4084 mCurrentRawPointerData.pointerForId(activeTouchId);
4085 const RawPointerData::Pointer& lastPointer =
4086 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004087 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004088 * mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004089 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004090 * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004091
Jeff Brownbe1aa822011-07-27 16:04:54 -07004092 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004093 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4094
Jeff Brown2352b972011-04-12 22:39:53 -07004095 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004096 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004097 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004098 } else {
4099 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004100 }
4101
Jeff Brown79ac9692011-04-19 21:20:10 -07004102 bool down;
4103 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4104#if DEBUG_GESTURES
4105 LOGD("Gestures: TAP_DRAG");
4106#endif
4107 down = true;
4108 } else {
4109#if DEBUG_GESTURES
4110 LOGD("Gestures: HOVER");
4111#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004112 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4113 *outFinishPreviousGesture = true;
4114 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004115 mPointerGesture.activeGestureId = 0;
4116 down = false;
4117 }
Jeff Brownace13b12011-03-09 17:39:48 -08004118
4119 float x, y;
4120 mPointerController->getPosition(&x, &y);
4121
Jeff Brownace13b12011-03-09 17:39:48 -08004122 mPointerGesture.currentGestureIdBits.clear();
4123 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4124 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004125 mPointerGesture.currentGestureProperties[0].clear();
4126 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4127 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004128 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004129 mPointerGesture.currentGestureCoords[0].clear();
4130 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4131 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004132 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4133 down ? 1.0f : 0.0f);
4134
Jeff Brownbe1aa822011-07-27 16:04:54 -07004135 if (lastTouchingPointerCount == 0 && currentTouchingPointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004136 mPointerGesture.resetTap();
4137 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004138 mPointerGesture.tapX = x;
4139 mPointerGesture.tapY = y;
4140 }
Jeff Brownace13b12011-03-09 17:39:48 -08004141 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004142 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4143 // We need to provide feedback for each finger that goes down so we cannot wait
4144 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004145 //
Jeff Brown2352b972011-04-12 22:39:53 -07004146 // The ambiguous case is deciding what to do when there are two fingers down but they
4147 // have not moved enough to determine whether they are part of a drag or part of a
4148 // freeform gesture, or just a press or long-press at the pointer location.
4149 //
4150 // When there are two fingers we start with the PRESS hypothesis and we generate a
4151 // down at the pointer location.
4152 //
4153 // When the two fingers move enough or when additional fingers are added, we make
4154 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004155 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004156
Jeff Brown214eaf42011-05-26 19:17:02 -07004157 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004158 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004159 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004160 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4161 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004162 *outFinishPreviousGesture = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004163 } else if (!settled && currentTouchingPointerCount > lastTouchingPointerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004164 // Additional pointers have gone down but not yet settled.
4165 // Reset the gesture.
4166#if DEBUG_GESTURES
4167 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004168 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004169 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004170 * 0.000001f);
4171#endif
4172 *outCancelPreviousGesture = true;
4173 } else {
4174 // Continue previous gesture.
4175 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4176 }
4177
4178 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004179 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4180 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004181 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004182 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004183
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004184 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004185#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004186 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4187 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004188 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004189 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004190#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004191 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4192 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004193 &mPointerGesture.referenceTouchY);
4194 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4195 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004196 }
Jeff Brownace13b12011-03-09 17:39:48 -08004197
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004198 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004199 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits.value
4200 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4201 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004202 mPointerGesture.referenceDeltas[id].dx = 0;
4203 mPointerGesture.referenceDeltas[id].dy = 0;
4204 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004205 mPointerGesture.referenceIdBits = mCurrentRawPointerData.touchingIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004206
4207 // Add delta for all fingers and calculate a common movement delta.
4208 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004209 BitSet32 commonIdBits(mLastRawPointerData.touchingIdBits.value
4210 & mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004211 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4212 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004213 uint32_t id = idBits.clearFirstMarkedBit();
4214 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4215 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004216 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4217 delta.dx += cpd.x - lpd.x;
4218 delta.dy += cpd.y - lpd.y;
4219
4220 if (first) {
4221 commonDeltaX = delta.dx;
4222 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004223 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004224 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4225 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4226 }
4227 }
Jeff Brownace13b12011-03-09 17:39:48 -08004228
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004229 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4230 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4231 float dist[MAX_POINTER_ID + 1];
4232 int32_t distOverThreshold = 0;
4233 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004234 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004235 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbe1aa822011-07-27 16:04:54 -07004236 dist[id] = hypotf(delta.dx * mPointerGestureXZoomScale,
4237 delta.dy * mPointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004238 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004239 distOverThreshold += 1;
4240 }
4241 }
4242
4243 // Only transition when at least two pointers have moved further than
4244 // the minimum distance threshold.
4245 if (distOverThreshold >= 2) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004246 if (currentTouchingPointerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004247 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004248#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004249 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004250 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004251#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004252 *outCancelPreviousGesture = true;
4253 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4254 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004255 // There are exactly two pointers.
4256 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4257 uint32_t id1 = idBits.clearFirstMarkedBit();
4258 uint32_t id2 = idBits.firstMarkedBit();
4259 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4260 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4261 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4262 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4263 // There are two pointers but they are too far apart for a SWIPE,
4264 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004265#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004266 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4267 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004268#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004269 *outCancelPreviousGesture = true;
4270 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4271 } else {
4272 // There are two pointers. Wait for both pointers to start moving
4273 // before deciding whether this is a SWIPE or FREEFORM gesture.
4274 float dist1 = dist[id1];
4275 float dist2 = dist[id2];
4276 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4277 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4278 // Calculate the dot product of the displacement vectors.
4279 // When the vectors are oriented in approximately the same direction,
4280 // the angle betweeen them is near zero and the cosine of the angle
4281 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4282 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4283 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
4284 float dx1 = delta1.dx * mPointerGestureXZoomScale;
4285 float dy1 = delta1.dy * mPointerGestureYZoomScale;
4286 float dx2 = delta2.dx * mPointerGestureXZoomScale;
4287 float dy2 = delta2.dy * mPointerGestureYZoomScale;
4288 float dot = dx1 * dx2 + dy1 * dy2;
4289 float cosine = dot / (dist1 * dist2); // denominator always > 0
4290 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4291 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004292#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004293 LOGD("Gestures: PRESS transitioned to SWIPE, "
4294 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4295 "cosine %0.3f >= %0.3f",
4296 dist1, mConfig.pointerGestureMultitouchMinDistance,
4297 dist2, mConfig.pointerGestureMultitouchMinDistance,
4298 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004299#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004300 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4301 } else {
4302 // Pointers are moving in different directions. Switch to FREEFORM.
4303#if DEBUG_GESTURES
4304 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4305 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4306 "cosine %0.3f < %0.3f",
4307 dist1, mConfig.pointerGestureMultitouchMinDistance,
4308 dist2, mConfig.pointerGestureMultitouchMinDistance,
4309 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4310#endif
4311 *outCancelPreviousGesture = true;
4312 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4313 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004314 }
Jeff Brownace13b12011-03-09 17:39:48 -08004315 }
4316 }
Jeff Brownace13b12011-03-09 17:39:48 -08004317 }
4318 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004319 // Switch from SWIPE to FREEFORM if additional pointers go down.
4320 // Cancel previous gesture.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004321 if (currentTouchingPointerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004322#if DEBUG_GESTURES
4323 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004324 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004325#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004326 *outCancelPreviousGesture = true;
4327 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004328 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004329 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004330
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004331 // Move the reference points based on the overall group motion of the fingers
4332 // except in PRESS mode while waiting for a transition to occur.
4333 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4334 && (commonDeltaX || commonDeltaY)) {
4335 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004336 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004337 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004338 delta.dx = 0;
4339 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004340 }
4341
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004342 mPointerGesture.referenceTouchX += commonDeltaX;
4343 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004344
Jeff Brownbe1aa822011-07-27 16:04:54 -07004345 commonDeltaX *= mPointerGestureXMovementScale;
4346 commonDeltaY *= mPointerGestureYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004347
Jeff Brownbe1aa822011-07-27 16:04:54 -07004348 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004349 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004350
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004351 mPointerGesture.referenceGestureX += commonDeltaX;
4352 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004353 }
4354
4355 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004356 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4357 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4358 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004359#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004360 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004361 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004362 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004363#endif
4364 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4365
4366 mPointerGesture.currentGestureIdBits.clear();
4367 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4368 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004369 mPointerGesture.currentGestureProperties[0].clear();
4370 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4371 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004372 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004373 mPointerGesture.currentGestureCoords[0].clear();
4374 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4375 mPointerGesture.referenceGestureX);
4376 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4377 mPointerGesture.referenceGestureY);
4378 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004379 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4380 // FREEFORM mode.
4381#if DEBUG_GESTURES
4382 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4383 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004384 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004385#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004386 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004387
Jeff Brownace13b12011-03-09 17:39:48 -08004388 mPointerGesture.currentGestureIdBits.clear();
4389
4390 BitSet32 mappedTouchIdBits;
4391 BitSet32 usedGestureIdBits;
4392 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4393 // Initially, assign the active gesture id to the active touch point
4394 // if there is one. No other touch id bits are mapped yet.
4395 if (!*outCancelPreviousGesture) {
4396 mappedTouchIdBits.markBit(activeTouchId);
4397 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4398 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4399 mPointerGesture.activeGestureId;
4400 } else {
4401 mPointerGesture.activeGestureId = -1;
4402 }
4403 } else {
4404 // Otherwise, assume we mapped all touches from the previous frame.
4405 // Reuse all mappings that are still applicable.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004406 mappedTouchIdBits.value = mLastRawPointerData.touchingIdBits.value
4407 & mCurrentRawPointerData.touchingIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004408 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4409
4410 // Check whether we need to choose a new active gesture id because the
4411 // current went went up.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004412 for (BitSet32 upTouchIdBits(mLastRawPointerData.touchingIdBits.value
4413 & ~mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004414 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004415 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004416 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4417 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4418 mPointerGesture.activeGestureId = -1;
4419 break;
4420 }
4421 }
4422 }
4423
4424#if DEBUG_GESTURES
4425 LOGD("Gestures: FREEFORM follow up "
4426 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4427 "activeGestureId=%d",
4428 mappedTouchIdBits.value, usedGestureIdBits.value,
4429 mPointerGesture.activeGestureId);
4430#endif
4431
Jeff Brownbe1aa822011-07-27 16:04:54 -07004432 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4433 for (uint32_t i = 0; i < currentTouchingPointerCount; i++) {
4434 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004435 uint32_t gestureId;
4436 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004437 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004438 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4439#if DEBUG_GESTURES
4440 LOGD("Gestures: FREEFORM "
4441 "new mapping for touch id %d -> gesture id %d",
4442 touchId, gestureId);
4443#endif
4444 } else {
4445 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4446#if DEBUG_GESTURES
4447 LOGD("Gestures: FREEFORM "
4448 "existing mapping for touch id %d -> gesture id %d",
4449 touchId, gestureId);
4450#endif
4451 }
4452 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4453 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4454
Jeff Brownbe1aa822011-07-27 16:04:54 -07004455 const RawPointerData::Pointer& pointer =
4456 mCurrentRawPointerData.pointerForId(touchId);
4457 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
4458 * mPointerGestureXZoomScale;
4459 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
4460 * mPointerGestureYZoomScale;
4461 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004462
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004463 mPointerGesture.currentGestureProperties[i].clear();
4464 mPointerGesture.currentGestureProperties[i].id = gestureId;
4465 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004466 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004467 mPointerGesture.currentGestureCoords[i].clear();
4468 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004469 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004470 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004471 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004472 mPointerGesture.currentGestureCoords[i].setAxisValue(
4473 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4474 }
4475
4476 if (mPointerGesture.activeGestureId < 0) {
4477 mPointerGesture.activeGestureId =
4478 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4479#if DEBUG_GESTURES
4480 LOGD("Gestures: FREEFORM new "
4481 "activeGestureId=%d", mPointerGesture.activeGestureId);
4482#endif
4483 }
Jeff Brown2352b972011-04-12 22:39:53 -07004484 }
Jeff Brownace13b12011-03-09 17:39:48 -08004485 }
4486
Jeff Brownbe1aa822011-07-27 16:04:54 -07004487 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004488
Jeff Brownace13b12011-03-09 17:39:48 -08004489#if DEBUG_GESTURES
4490 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004491 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4492 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004493 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004494 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4495 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004496 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004497 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004498 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004499 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004500 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004501 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4502 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4503 id, index, properties.toolType,
4504 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004505 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4506 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4507 }
4508 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004509 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004510 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004511 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004512 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004513 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4514 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4515 id, index, properties.toolType,
4516 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004517 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4518 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4519 }
4520#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004521 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004522}
4523
4524void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004525 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4526 const PointerProperties* properties, const PointerCoords* coords,
4527 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004528 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4529 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004530 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004531 uint32_t pointerCount = 0;
4532 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004533 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004534 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004535 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004536 pointerCoords[pointerCount].copyFrom(coords[index]);
4537
4538 if (changedId >= 0 && id == uint32_t(changedId)) {
4539 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4540 }
4541
4542 pointerCount += 1;
4543 }
4544
Jeff Brownb6110c22011-04-01 16:15:13 -07004545 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004546
4547 if (changedId >= 0 && pointerCount == 1) {
4548 // Replace initial down and final up action.
4549 // We can compare the action without masking off the changed pointer index
4550 // because we know the index is 0.
4551 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4552 action = AMOTION_EVENT_ACTION_DOWN;
4553 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4554 action = AMOTION_EVENT_ACTION_UP;
4555 } else {
4556 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004557 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004558 }
4559 }
4560
Jeff Brownbe1aa822011-07-27 16:04:54 -07004561 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004562 action, flags, metaState, buttonState, edgeFlags,
4563 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004564 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004565}
4566
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004567bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004568 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004569 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4570 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004571 bool changed = false;
4572 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004573 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004574 uint32_t inIndex = inIdToIndex[id];
4575 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004576
4577 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004578 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004579 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004580 PointerCoords& curOutCoords = outCoords[outIndex];
4581
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004582 if (curInProperties != curOutProperties) {
4583 curOutProperties.copyFrom(curInProperties);
4584 changed = true;
4585 }
4586
Jeff Brownace13b12011-03-09 17:39:48 -08004587 if (curInCoords != curOutCoords) {
4588 curOutCoords.copyFrom(curInCoords);
4589 changed = true;
4590 }
4591 }
4592 return changed;
4593}
4594
4595void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004596 if (mPointerController != NULL) {
4597 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4598 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004599}
4600
Jeff Brownbe1aa822011-07-27 16:04:54 -07004601bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
4602 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
4603 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004604}
4605
Jeff Brownbe1aa822011-07-27 16:04:54 -07004606const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07004607 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004608 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07004609 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004610 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004611
4612#if DEBUG_VIRTUAL_KEYS
4613 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4614 "left=%d, top=%d, right=%d, bottom=%d",
4615 x, y,
4616 virtualKey.keyCode, virtualKey.scanCode,
4617 virtualKey.hitLeft, virtualKey.hitTop,
4618 virtualKey.hitRight, virtualKey.hitBottom);
4619#endif
4620
4621 if (virtualKey.isHit(x, y)) {
4622 return & virtualKey;
4623 }
4624 }
4625
4626 return NULL;
4627}
4628
Jeff Brownbe1aa822011-07-27 16:04:54 -07004629void TouchInputMapper::assignPointerIds() {
4630 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4631 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
4632
4633 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004634
4635 if (currentPointerCount == 0) {
4636 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004637 return;
4638 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004639
Jeff Brownbe1aa822011-07-27 16:04:54 -07004640 if (lastPointerCount == 0) {
4641 // All pointers are new.
4642 for (uint32_t i = 0; i < currentPointerCount; i++) {
4643 uint32_t id = i;
4644 mCurrentRawPointerData.pointers[i].id = id;
4645 mCurrentRawPointerData.idToIndex[id] = i;
4646 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
4647 }
4648 return;
4649 }
4650
4651 if (currentPointerCount == 1 && lastPointerCount == 1
4652 && mCurrentRawPointerData.pointers[0].toolType
4653 == mLastRawPointerData.pointers[0].toolType) {
4654 // Only one pointer and no change in count so it must have the same id as before.
4655 uint32_t id = mLastRawPointerData.pointers[0].id;
4656 mCurrentRawPointerData.pointers[0].id = id;
4657 mCurrentRawPointerData.idToIndex[id] = 0;
4658 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
4659 return;
4660 }
4661
4662 // General case.
4663 // We build a heap of squared euclidean distances between current and last pointers
4664 // associated with the current and last pointer indices. Then, we find the best
4665 // match (by distance) for each current pointer.
4666 // The pointers must have the same tool type but it is possible for them to
4667 // transition from hovering to touching or vice-versa while retaining the same id.
4668 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4669
4670 uint32_t heapSize = 0;
4671 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4672 currentPointerIndex++) {
4673 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4674 lastPointerIndex++) {
4675 const RawPointerData::Pointer& currentPointer =
4676 mCurrentRawPointerData.pointers[currentPointerIndex];
4677 const RawPointerData::Pointer& lastPointer =
4678 mLastRawPointerData.pointers[lastPointerIndex];
4679 if (currentPointer.toolType == lastPointer.toolType) {
4680 int64_t deltaX = currentPointer.x - lastPointer.x;
4681 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004682
4683 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4684
4685 // Insert new element into the heap (sift up).
4686 heap[heapSize].currentPointerIndex = currentPointerIndex;
4687 heap[heapSize].lastPointerIndex = lastPointerIndex;
4688 heap[heapSize].distance = distance;
4689 heapSize += 1;
4690 }
4691 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004692 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004693
Jeff Brownbe1aa822011-07-27 16:04:54 -07004694 // Heapify
4695 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4696 startIndex -= 1;
4697 for (uint32_t parentIndex = startIndex; ;) {
4698 uint32_t childIndex = parentIndex * 2 + 1;
4699 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004700 break;
4701 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004702
4703 if (childIndex + 1 < heapSize
4704 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4705 childIndex += 1;
4706 }
4707
4708 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4709 break;
4710 }
4711
4712 swap(heap[parentIndex], heap[childIndex]);
4713 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004714 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004715 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004716
4717#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07004718 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
4719 for (size_t i = 0; i < heapSize; i++) {
4720 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4721 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4722 heap[i].distance);
4723 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004724#endif
4725
Jeff Brownbe1aa822011-07-27 16:04:54 -07004726 // Pull matches out by increasing order of distance.
4727 // To avoid reassigning pointers that have already been matched, the loop keeps track
4728 // of which last and current pointers have been matched using the matchedXXXBits variables.
4729 // It also tracks the used pointer id bits.
4730 BitSet32 matchedLastBits(0);
4731 BitSet32 matchedCurrentBits(0);
4732 BitSet32 usedIdBits(0);
4733 bool first = true;
4734 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
4735 while (heapSize > 0) {
4736 if (first) {
4737 // The first time through the loop, we just consume the root element of
4738 // the heap (the one with smallest distance).
4739 first = false;
4740 } else {
4741 // Previous iterations consumed the root element of the heap.
4742 // Pop root element off of the heap (sift down).
4743 heap[0] = heap[heapSize];
4744 for (uint32_t parentIndex = 0; ;) {
4745 uint32_t childIndex = parentIndex * 2 + 1;
4746 if (childIndex >= heapSize) {
4747 break;
4748 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004749
Jeff Brownbe1aa822011-07-27 16:04:54 -07004750 if (childIndex + 1 < heapSize
4751 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4752 childIndex += 1;
4753 }
4754
4755 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4756 break;
4757 }
4758
4759 swap(heap[parentIndex], heap[childIndex]);
4760 parentIndex = childIndex;
4761 }
4762
4763#if DEBUG_POINTER_ASSIGNMENT
4764 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
4765 for (size_t i = 0; i < heapSize; i++) {
4766 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4767 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4768 heap[i].distance);
4769 }
4770#endif
4771 }
4772
4773 heapSize -= 1;
4774
4775 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4776 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4777
4778 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4779 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4780
4781 matchedCurrentBits.markBit(currentPointerIndex);
4782 matchedLastBits.markBit(lastPointerIndex);
4783
4784 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
4785 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
4786 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
4787 mCurrentRawPointerData.markIdBit(id,
4788 mCurrentRawPointerData.isHovering(currentPointerIndex));
4789 usedIdBits.markBit(id);
4790
4791#if DEBUG_POINTER_ASSIGNMENT
4792 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4793 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4794#endif
4795 break;
4796 }
4797 }
4798
4799 // Assign fresh ids to pointers that were not matched in the process.
4800 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
4801 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
4802 uint32_t id = usedIdBits.markFirstUnmarkedBit();
4803
4804 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
4805 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
4806 mCurrentRawPointerData.markIdBit(id,
4807 mCurrentRawPointerData.isHovering(currentPointerIndex));
4808
4809#if DEBUG_POINTER_ASSIGNMENT
4810 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
4811 currentPointerIndex, id);
4812#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07004813 }
4814}
4815
Jeff Brown6d0fec22010-07-23 21:28:06 -07004816int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004817 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4818 return AKEY_STATE_VIRTUAL;
4819 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004820
Jeff Brownbe1aa822011-07-27 16:04:54 -07004821 size_t numVirtualKeys = mVirtualKeys.size();
4822 for (size_t i = 0; i < numVirtualKeys; i++) {
4823 const VirtualKey& virtualKey = mVirtualKeys[i];
4824 if (virtualKey.keyCode == keyCode) {
4825 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004826 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004827 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004828
4829 return AKEY_STATE_UNKNOWN;
4830}
4831
4832int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004833 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4834 return AKEY_STATE_VIRTUAL;
4835 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004836
Jeff Brownbe1aa822011-07-27 16:04:54 -07004837 size_t numVirtualKeys = mVirtualKeys.size();
4838 for (size_t i = 0; i < numVirtualKeys; i++) {
4839 const VirtualKey& virtualKey = mVirtualKeys[i];
4840 if (virtualKey.scanCode == scanCode) {
4841 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004842 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004843 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004844
4845 return AKEY_STATE_UNKNOWN;
4846}
4847
4848bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4849 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004850 size_t numVirtualKeys = mVirtualKeys.size();
4851 for (size_t i = 0; i < numVirtualKeys; i++) {
4852 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004853
Jeff Brownbe1aa822011-07-27 16:04:54 -07004854 for (size_t i = 0; i < numCodes; i++) {
4855 if (virtualKey.keyCode == keyCodes[i]) {
4856 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004857 }
4858 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004859 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004860
4861 return true;
4862}
4863
4864
4865// --- SingleTouchInputMapper ---
4866
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004867SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
4868 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004869 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004870}
4871
4872SingleTouchInputMapper::~SingleTouchInputMapper() {
4873}
4874
Jeff Brown80fd47c2011-05-24 01:07:44 -07004875void SingleTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07004876 mCursorButtonAccumulator.clearButtons();
4877 mTouchButtonAccumulator.clearButtons();
4878 mSingleTouchMotionAccumulator.clearAbsoluteAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004879}
4880
4881void SingleTouchInputMapper::reset() {
4882 TouchInputMapper::reset();
4883
Jeff Brown80fd47c2011-05-24 01:07:44 -07004884 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004885 }
4886
4887void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07004888 mCursorButtonAccumulator.process(rawEvent);
4889 mTouchButtonAccumulator.process(rawEvent);
4890 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004891
Jeff Brown49754db2011-07-01 17:37:58 -07004892 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
4893 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004894 }
4895}
4896
4897void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004898 mCurrentRawPointerData.clear();
4899 mCurrentButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004900
Jeff Brownd87c6d52011-08-10 14:55:59 -07004901 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004902 mCurrentRawPointerData.pointerCount = 1;
4903 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004904
Jeff Brownbe1aa822011-07-27 16:04:54 -07004905 bool isHovering = mTouchButtonAccumulator.isHovering()
4906 || mSingleTouchMotionAccumulator.getAbsoluteDistance() > 0;
4907 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07004908
Jeff Brownbe1aa822011-07-27 16:04:54 -07004909 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07004910 outPointer.id = 0;
4911 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
4912 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
4913 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
4914 outPointer.touchMajor = 0;
4915 outPointer.touchMinor = 0;
4916 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
4917 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
4918 outPointer.orientation = 0;
4919 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
4920 outPointer.toolType = mTouchButtonAccumulator.getToolType();
4921 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
4922 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
4923 }
4924 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004925 }
4926
Jeff Brownd87c6d52011-08-10 14:55:59 -07004927 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
4928 | mCursorButtonAccumulator.getButtonState();
4929
Jeff Brown6d0fec22010-07-23 21:28:06 -07004930 syncTouch(when, true);
4931}
4932
Jeff Brownbe1aa822011-07-27 16:04:54 -07004933void SingleTouchInputMapper::configureRawPointerAxes() {
4934 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004935
Jeff Brown49754db2011-07-01 17:37:58 -07004936 mTouchButtonAccumulator.configure(getDevice());
4937
Jeff Brownbe1aa822011-07-27 16:04:54 -07004938 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
4939 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
4940 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
4941 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
4942 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004943}
4944
4945
4946// --- MultiTouchInputMapper ---
4947
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004948MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07004949 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004950}
4951
4952MultiTouchInputMapper::~MultiTouchInputMapper() {
4953}
4954
Jeff Brown80fd47c2011-05-24 01:07:44 -07004955void MultiTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07004956 mCursorButtonAccumulator.clearButtons();
4957 mTouchButtonAccumulator.clearButtons();
Jeff Brown6894a292011-07-01 17:59:27 -07004958 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07004959
Jeff Brown49754db2011-07-01 17:37:58 -07004960 if (mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
Jeff Brown2717eff2011-06-30 23:53:07 -07004961 // Query the driver for the current slot index and use it as the initial slot
4962 // before we start reading events from the device. It is possible that the
4963 // current slot index will not be the same as it was when the first event was
4964 // written into the evdev buffer, which means the input mapper could start
4965 // out of sync with the initial state of the events in the evdev buffer.
4966 // In the extremely unlikely case that this happens, the data from
4967 // two slots will be confused until the next ABS_MT_SLOT event is received.
4968 // This can cause the touch point to "jump", but at least there will be
4969 // no stuck touches.
Jeff Brown49754db2011-07-01 17:37:58 -07004970 int32_t initialSlot;
Jeff Brown2717eff2011-06-30 23:53:07 -07004971 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
Jeff Brown49754db2011-07-01 17:37:58 -07004972 &initialSlot);
Jeff Brown2717eff2011-06-30 23:53:07 -07004973 if (status) {
4974 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown49754db2011-07-01 17:37:58 -07004975 initialSlot = -1;
Jeff Brown2717eff2011-06-30 23:53:07 -07004976 }
Jeff Brown49754db2011-07-01 17:37:58 -07004977 mMultiTouchMotionAccumulator.clearSlots(initialSlot);
4978 } else {
4979 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown2717eff2011-06-30 23:53:07 -07004980 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004981}
4982
4983void MultiTouchInputMapper::reset() {
4984 TouchInputMapper::reset();
4985
Jeff Brown80fd47c2011-05-24 01:07:44 -07004986 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004987}
4988
4989void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07004990 mCursorButtonAccumulator.process(rawEvent);
4991 mTouchButtonAccumulator.process(rawEvent);
4992 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08004993
Jeff Brown49754db2011-07-01 17:37:58 -07004994 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
4995 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004996 }
4997}
4998
4999void MultiTouchInputMapper::sync(nsecs_t when) {
Jeff Brown49754db2011-07-01 17:37:58 -07005000 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005001 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005002 bool havePointerIds = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005003 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005004
Jeff Brownbe1aa822011-07-27 16:04:54 -07005005 mCurrentRawPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005006
Jeff Brown80fd47c2011-05-24 01:07:44 -07005007 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005008 const MultiTouchMotionAccumulator::Slot* inSlot =
5009 mMultiTouchMotionAccumulator.getSlot(inIndex);
5010 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005011 continue;
5012 }
5013
Jeff Brown80fd47c2011-05-24 01:07:44 -07005014 if (outCount >= MAX_POINTERS) {
5015#if DEBUG_POINTERS
5016 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5017 "ignoring the rest.",
5018 getDeviceName().string(), MAX_POINTERS);
5019#endif
5020 break; // too many fingers!
5021 }
5022
Jeff Brownbe1aa822011-07-27 16:04:54 -07005023 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005024 outPointer.x = inSlot->getX();
5025 outPointer.y = inSlot->getY();
5026 outPointer.pressure = inSlot->getPressure();
5027 outPointer.touchMajor = inSlot->getTouchMajor();
5028 outPointer.touchMinor = inSlot->getTouchMinor();
5029 outPointer.toolMajor = inSlot->getToolMajor();
5030 outPointer.toolMinor = inSlot->getToolMinor();
5031 outPointer.orientation = inSlot->getOrientation();
5032 outPointer.distance = inSlot->getDistance();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005033
Jeff Brown49754db2011-07-01 17:37:58 -07005034 outPointer.toolType = inSlot->getToolType();
5035 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5036 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5037 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5038 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5039 }
Jeff Brown8d608662010-08-30 03:02:23 -07005040 }
5041
Jeff Brownbe1aa822011-07-27 16:04:54 -07005042 bool isHovering = mTouchButtonAccumulator.isHovering()
Jeff Brown49754db2011-07-01 17:37:58 -07005043 || inSlot->getDistance() > 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005044 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005045
Jeff Brown8d608662010-08-30 03:02:23 -07005046 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005047 if (havePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005048 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005049 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005050 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005051 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005052 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005053 if (mPointerTrackingIdMap[n] == trackingId) {
5054 id = n;
5055 }
5056 }
5057
5058 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005059 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005060 mPointerTrackingIdMap[id] = trackingId;
5061 }
5062 }
5063 if (id < 0) {
5064 havePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005065 mCurrentRawPointerData.clearIdBits();
5066 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005067 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005068 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005069 mCurrentRawPointerData.idToIndex[id] = outCount;
5070 mCurrentRawPointerData.markIdBit(id, isHovering);
5071 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005072 }
5073 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005074
Jeff Brown6d0fec22010-07-23 21:28:06 -07005075 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005076 }
5077
Jeff Brownbe1aa822011-07-27 16:04:54 -07005078 mCurrentRawPointerData.pointerCount = outCount;
5079 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
5080 | mCursorButtonAccumulator.getButtonState();
Jeff Brownace13b12011-03-09 17:39:48 -08005081
Jeff Brownbe1aa822011-07-27 16:04:54 -07005082 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005083
Jeff Brown6d0fec22010-07-23 21:28:06 -07005084 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005085
Jeff Brown49754db2011-07-01 17:37:58 -07005086 if (!mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
5087 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown441a9c22011-06-02 18:22:25 -07005088 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005089}
5090
Jeff Brownbe1aa822011-07-27 16:04:54 -07005091void MultiTouchInputMapper::configureRawPointerAxes() {
5092 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005093
Jeff Brown49754db2011-07-01 17:37:58 -07005094 mTouchButtonAccumulator.configure(getDevice());
5095
Jeff Brownbe1aa822011-07-27 16:04:54 -07005096 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5097 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5098 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5099 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5100 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5101 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5102 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5103 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5104 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5105 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5106 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005107
Jeff Brownbe1aa822011-07-27 16:04:54 -07005108 if (mRawPointerAxes.trackingId.valid
5109 && mRawPointerAxes.slot.valid
5110 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5111 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005112 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005113 LOGW("MultiTouch Device %s reported %d slots but the framework "
5114 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005115 getDeviceName().string(), slotCount, MAX_SLOTS);
5116 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005117 }
Jeff Brown49754db2011-07-01 17:37:58 -07005118 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005119 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005120 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005121 }
5122
Jeff Brown2717eff2011-06-30 23:53:07 -07005123 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005124}
5125
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005126
Jeff Browncb1404e2011-01-15 18:14:15 -08005127// --- JoystickInputMapper ---
5128
5129JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5130 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005131}
5132
5133JoystickInputMapper::~JoystickInputMapper() {
5134}
5135
5136uint32_t JoystickInputMapper::getSources() {
5137 return AINPUT_SOURCE_JOYSTICK;
5138}
5139
5140void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5141 InputMapper::populateDeviceInfo(info);
5142
Jeff Brown6f2fba42011-02-19 01:08:02 -08005143 for (size_t i = 0; i < mAxes.size(); i++) {
5144 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005145 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5146 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005147 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005148 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5149 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005150 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005151 }
5152}
5153
5154void JoystickInputMapper::dump(String8& dump) {
5155 dump.append(INDENT2 "Joystick Input Mapper:\n");
5156
Jeff Brown6f2fba42011-02-19 01:08:02 -08005157 dump.append(INDENT3 "Axes:\n");
5158 size_t numAxes = mAxes.size();
5159 for (size_t i = 0; i < numAxes; i++) {
5160 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005161 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005162 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005163 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005164 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005165 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005166 }
Jeff Brown85297452011-03-04 13:07:49 -08005167 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5168 label = getAxisLabel(axis.axisInfo.highAxis);
5169 if (label) {
5170 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5171 } else {
5172 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5173 axis.axisInfo.splitValue);
5174 }
5175 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5176 dump.append(" (invert)");
5177 }
5178
5179 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5180 axis.min, axis.max, axis.flat, axis.fuzz);
5181 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5182 "highScale=%0.5f, highOffset=%0.5f\n",
5183 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005184 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5185 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005186 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005187 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005188 }
5189}
5190
Jeff Brown474dcb52011-06-14 20:22:50 -07005191void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5192 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005193
Jeff Brown474dcb52011-06-14 20:22:50 -07005194 if (!changes) { // first time only
5195 // Collect all axes.
5196 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5197 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005198 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005199 if (rawAxisInfo.valid) {
5200 // Map axis.
5201 AxisInfo axisInfo;
5202 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5203 if (!explicitlyMapped) {
5204 // Axis is not explicitly mapped, will choose a generic axis later.
5205 axisInfo.mode = AxisInfo::MODE_NORMAL;
5206 axisInfo.axis = -1;
5207 }
5208
5209 // Apply flat override.
5210 int32_t rawFlat = axisInfo.flatOverride < 0
5211 ? rawAxisInfo.flat : axisInfo.flatOverride;
5212
5213 // Calculate scaling factors and limits.
5214 Axis axis;
5215 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5216 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5217 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5218 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5219 scale, 0.0f, highScale, 0.0f,
5220 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5221 } else if (isCenteredAxis(axisInfo.axis)) {
5222 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5223 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5224 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5225 scale, offset, scale, offset,
5226 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5227 } else {
5228 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5229 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5230 scale, 0.0f, scale, 0.0f,
5231 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5232 }
5233
5234 // To eliminate noise while the joystick is at rest, filter out small variations
5235 // in axis values up front.
5236 axis.filter = axis.flat * 0.25f;
5237
5238 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005239 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005240 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005241
Jeff Brown474dcb52011-06-14 20:22:50 -07005242 // If there are too many axes, start dropping them.
5243 // Prefer to keep explicitly mapped axes.
5244 if (mAxes.size() > PointerCoords::MAX_AXES) {
5245 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5246 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5247 pruneAxes(true);
5248 pruneAxes(false);
5249 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005250
Jeff Brown474dcb52011-06-14 20:22:50 -07005251 // Assign generic axis ids to remaining axes.
5252 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5253 size_t numAxes = mAxes.size();
5254 for (size_t i = 0; i < numAxes; i++) {
5255 Axis& axis = mAxes.editValueAt(i);
5256 if (axis.axisInfo.axis < 0) {
5257 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5258 && haveAxis(nextGenericAxisId)) {
5259 nextGenericAxisId += 1;
5260 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005261
Jeff Brown474dcb52011-06-14 20:22:50 -07005262 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5263 axis.axisInfo.axis = nextGenericAxisId;
5264 nextGenericAxisId += 1;
5265 } else {
5266 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5267 "have already been assigned to other axes.",
5268 getDeviceName().string(), mAxes.keyAt(i));
5269 mAxes.removeItemsAt(i--);
5270 numAxes -= 1;
5271 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005272 }
5273 }
5274 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005275}
5276
Jeff Brown85297452011-03-04 13:07:49 -08005277bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005278 size_t numAxes = mAxes.size();
5279 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005280 const Axis& axis = mAxes.valueAt(i);
5281 if (axis.axisInfo.axis == axisId
5282 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5283 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005284 return true;
5285 }
5286 }
5287 return false;
5288}
Jeff Browncb1404e2011-01-15 18:14:15 -08005289
Jeff Brown6f2fba42011-02-19 01:08:02 -08005290void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5291 size_t i = mAxes.size();
5292 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5293 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5294 continue;
5295 }
5296 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5297 getDeviceName().string(), mAxes.keyAt(i));
5298 mAxes.removeItemsAt(i);
5299 }
5300}
5301
5302bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5303 switch (axis) {
5304 case AMOTION_EVENT_AXIS_X:
5305 case AMOTION_EVENT_AXIS_Y:
5306 case AMOTION_EVENT_AXIS_Z:
5307 case AMOTION_EVENT_AXIS_RX:
5308 case AMOTION_EVENT_AXIS_RY:
5309 case AMOTION_EVENT_AXIS_RZ:
5310 case AMOTION_EVENT_AXIS_HAT_X:
5311 case AMOTION_EVENT_AXIS_HAT_Y:
5312 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005313 case AMOTION_EVENT_AXIS_RUDDER:
5314 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005315 return true;
5316 default:
5317 return false;
5318 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005319}
5320
5321void JoystickInputMapper::reset() {
5322 // Recenter all axes.
5323 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005324
Jeff Brown6f2fba42011-02-19 01:08:02 -08005325 size_t numAxes = mAxes.size();
5326 for (size_t i = 0; i < numAxes; i++) {
5327 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005328 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005329 }
5330
5331 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005332
5333 InputMapper::reset();
5334}
5335
5336void JoystickInputMapper::process(const RawEvent* rawEvent) {
5337 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005338 case EV_ABS: {
5339 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5340 if (index >= 0) {
5341 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005342 float newValue, highNewValue;
5343 switch (axis.axisInfo.mode) {
5344 case AxisInfo::MODE_INVERT:
5345 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5346 * axis.scale + axis.offset;
5347 highNewValue = 0.0f;
5348 break;
5349 case AxisInfo::MODE_SPLIT:
5350 if (rawEvent->value < axis.axisInfo.splitValue) {
5351 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5352 * axis.scale + axis.offset;
5353 highNewValue = 0.0f;
5354 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5355 newValue = 0.0f;
5356 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5357 * axis.highScale + axis.highOffset;
5358 } else {
5359 newValue = 0.0f;
5360 highNewValue = 0.0f;
5361 }
5362 break;
5363 default:
5364 newValue = rawEvent->value * axis.scale + axis.offset;
5365 highNewValue = 0.0f;
5366 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005367 }
Jeff Brown85297452011-03-04 13:07:49 -08005368 axis.newValue = newValue;
5369 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005370 }
5371 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005372 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005373
5374 case EV_SYN:
5375 switch (rawEvent->scanCode) {
5376 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005377 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005378 break;
5379 }
5380 break;
5381 }
5382}
5383
Jeff Brown6f2fba42011-02-19 01:08:02 -08005384void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005385 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005386 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005387 }
5388
5389 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005390 int32_t buttonState = 0;
5391
5392 PointerProperties pointerProperties;
5393 pointerProperties.clear();
5394 pointerProperties.id = 0;
5395 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005396
Jeff Brown6f2fba42011-02-19 01:08:02 -08005397 PointerCoords pointerCoords;
5398 pointerCoords.clear();
5399
5400 size_t numAxes = mAxes.size();
5401 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005402 const Axis& axis = mAxes.valueAt(i);
5403 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5404 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5405 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5406 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005407 }
5408
Jeff Brown56194eb2011-03-02 19:23:13 -08005409 // Moving a joystick axis should not wake the devide because joysticks can
5410 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5411 // button will likely wake the device.
5412 // TODO: Use the input device configuration to control this behavior more finely.
5413 uint32_t policyFlags = 0;
5414
Jeff Brownbe1aa822011-07-27 16:04:54 -07005415 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005416 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5417 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005418 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005419}
5420
Jeff Brown85297452011-03-04 13:07:49 -08005421bool JoystickInputMapper::filterAxes(bool force) {
5422 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005423 size_t numAxes = mAxes.size();
5424 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005425 Axis& axis = mAxes.editValueAt(i);
5426 if (force || hasValueChangedSignificantly(axis.filter,
5427 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5428 axis.currentValue = axis.newValue;
5429 atLeastOneSignificantChange = true;
5430 }
5431 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5432 if (force || hasValueChangedSignificantly(axis.filter,
5433 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5434 axis.highCurrentValue = axis.highNewValue;
5435 atLeastOneSignificantChange = true;
5436 }
5437 }
5438 }
5439 return atLeastOneSignificantChange;
5440}
5441
5442bool JoystickInputMapper::hasValueChangedSignificantly(
5443 float filter, float newValue, float currentValue, float min, float max) {
5444 if (newValue != currentValue) {
5445 // Filter out small changes in value unless the value is converging on the axis
5446 // bounds or center point. This is intended to reduce the amount of information
5447 // sent to applications by particularly noisy joysticks (such as PS3).
5448 if (fabs(newValue - currentValue) > filter
5449 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5450 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5451 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5452 return true;
5453 }
5454 }
5455 return false;
5456}
5457
5458bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5459 float filter, float newValue, float currentValue, float thresholdValue) {
5460 float newDistance = fabs(newValue - thresholdValue);
5461 if (newDistance < filter) {
5462 float oldDistance = fabs(currentValue - thresholdValue);
5463 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005464 return true;
5465 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005466 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005467 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005468}
5469
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005470} // namespace android