blob: 2035a4b56ff37e5832991bdae7a7b086d83894b0 [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++) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001338 mSlots[i].clear();
Jeff Brown49754db2011-07-01 17:37:58 -07001339 }
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) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001399 // The slot is no longer in use but it retains its previous contents,
1400 // which may be reused for subsequent touches.
1401 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001402 } else {
1403 slot->mInUse = true;
1404 slot->mAbsMTTrackingId = rawEvent->value;
1405 }
1406 break;
1407 case ABS_MT_PRESSURE:
1408 slot->mInUse = true;
1409 slot->mAbsMTPressure = rawEvent->value;
1410 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001411 case ABS_MT_DISTANCE:
1412 slot->mInUse = true;
1413 slot->mAbsMTDistance = rawEvent->value;
1414 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001415 case ABS_MT_TOOL_TYPE:
1416 slot->mInUse = true;
1417 slot->mAbsMTToolType = rawEvent->value;
1418 slot->mHaveAbsMTToolType = true;
1419 break;
1420 }
1421 }
1422 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1423 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1424 mCurrentSlot += 1;
1425 }
1426}
1427
1428
1429// --- MultiTouchMotionAccumulator::Slot ---
1430
1431MultiTouchMotionAccumulator::Slot::Slot() {
1432 clear();
1433}
1434
Jeff Brown49754db2011-07-01 17:37:58 -07001435void MultiTouchMotionAccumulator::Slot::clear() {
1436 mInUse = false;
1437 mHaveAbsMTTouchMinor = false;
1438 mHaveAbsMTWidthMinor = false;
1439 mHaveAbsMTToolType = false;
1440 mAbsMTPositionX = 0;
1441 mAbsMTPositionY = 0;
1442 mAbsMTTouchMajor = 0;
1443 mAbsMTTouchMinor = 0;
1444 mAbsMTWidthMajor = 0;
1445 mAbsMTWidthMinor = 0;
1446 mAbsMTOrientation = 0;
1447 mAbsMTTrackingId = -1;
1448 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001449 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001450 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001451}
1452
1453int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1454 if (mHaveAbsMTToolType) {
1455 switch (mAbsMTToolType) {
1456 case MT_TOOL_FINGER:
1457 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1458 case MT_TOOL_PEN:
1459 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1460 }
1461 }
1462 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1463}
1464
1465
Jeff Brown6d0fec22010-07-23 21:28:06 -07001466// --- InputMapper ---
1467
1468InputMapper::InputMapper(InputDevice* device) :
1469 mDevice(device), mContext(device->getContext()) {
1470}
1471
1472InputMapper::~InputMapper() {
1473}
1474
1475void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1476 info->addSource(getSources());
1477}
1478
Jeff Brownef3d7e82010-09-30 14:33:04 -07001479void InputMapper::dump(String8& dump) {
1480}
1481
Jeff Brown474dcb52011-06-14 20:22:50 -07001482void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001483}
1484
1485void InputMapper::reset() {
1486}
1487
Jeff Brownaa3855d2011-03-17 01:34:19 -07001488void InputMapper::timeoutExpired(nsecs_t when) {
1489}
1490
Jeff Brown6d0fec22010-07-23 21:28:06 -07001491int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1492 return AKEY_STATE_UNKNOWN;
1493}
1494
1495int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1496 return AKEY_STATE_UNKNOWN;
1497}
1498
1499int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1500 return AKEY_STATE_UNKNOWN;
1501}
1502
1503bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1504 const int32_t* keyCodes, uint8_t* outFlags) {
1505 return false;
1506}
1507
1508int32_t InputMapper::getMetaState() {
1509 return 0;
1510}
1511
Jeff Brown05dc66a2011-03-02 14:41:58 -08001512void InputMapper::fadePointer() {
1513}
1514
Jeff Brownbe1aa822011-07-27 16:04:54 -07001515status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1516 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1517}
1518
Jeff Browncb1404e2011-01-15 18:14:15 -08001519void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1520 const RawAbsoluteAxisInfo& axis, const char* name) {
1521 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001522 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1523 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001524 } else {
1525 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1526 }
1527}
1528
Jeff Brown6d0fec22010-07-23 21:28:06 -07001529
1530// --- SwitchInputMapper ---
1531
1532SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1533 InputMapper(device) {
1534}
1535
1536SwitchInputMapper::~SwitchInputMapper() {
1537}
1538
1539uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001540 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001541}
1542
1543void SwitchInputMapper::process(const RawEvent* rawEvent) {
1544 switch (rawEvent->type) {
1545 case EV_SW:
1546 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1547 break;
1548 }
1549}
1550
1551void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001552 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1553 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001554}
1555
1556int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1557 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1558}
1559
1560
1561// --- KeyboardInputMapper ---
1562
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001563KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001564 uint32_t source, int32_t keyboardType) :
1565 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001566 mKeyboardType(keyboardType) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001567 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001568}
1569
1570KeyboardInputMapper::~KeyboardInputMapper() {
1571}
1572
Jeff Brownbe1aa822011-07-27 16:04:54 -07001573void KeyboardInputMapper::initialize() {
1574 mMetaState = AMETA_NONE;
1575 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001576}
1577
1578uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001579 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001580}
1581
1582void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1583 InputMapper::populateDeviceInfo(info);
1584
1585 info->setKeyboardType(mKeyboardType);
1586}
1587
Jeff Brownef3d7e82010-09-30 14:33:04 -07001588void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001589 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1590 dumpParameters(dump);
1591 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1592 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1593 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1594 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001595}
1596
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001597
Jeff Brown474dcb52011-06-14 20:22:50 -07001598void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1599 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001600
Jeff Brown474dcb52011-06-14 20:22:50 -07001601 if (!changes) { // first time only
1602 // Configure basic parameters.
1603 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001604
Jeff Brown474dcb52011-06-14 20:22:50 -07001605 // Reset LEDs.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001606 resetLedState();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001607 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001608}
1609
1610void KeyboardInputMapper::configureParameters() {
1611 mParameters.orientationAware = false;
1612 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1613 mParameters.orientationAware);
1614
Jeff Brownbc68a592011-07-25 12:58:12 -07001615 mParameters.associatedDisplayId = -1;
1616 if (mParameters.orientationAware) {
1617 mParameters.associatedDisplayId = 0;
1618 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001619}
1620
1621void KeyboardInputMapper::dumpParameters(String8& dump) {
1622 dump.append(INDENT3 "Parameters:\n");
1623 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1624 mParameters.associatedDisplayId);
1625 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1626 toString(mParameters.orientationAware));
1627}
1628
Jeff Brown6d0fec22010-07-23 21:28:06 -07001629void KeyboardInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001630 // Synthesize key up event on reset if keys are currently down.
1631 while (!mKeyDowns.isEmpty()) {
1632 const KeyDown& keyDown = mKeyDowns.top();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001633 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001634 processKey(when, false, keyDown.keyCode, keyDown.scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001635 }
1636
Jeff Brownbe1aa822011-07-27 16:04:54 -07001637 initialize();
1638 resetLedState();
1639
Jeff Brown6d0fec22010-07-23 21:28:06 -07001640 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001641 getContext()->updateGlobalMetaState();
1642}
1643
1644void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1645 switch (rawEvent->type) {
1646 case EV_KEY: {
1647 int32_t scanCode = rawEvent->scanCode;
1648 if (isKeyboardOrGamepadKey(scanCode)) {
1649 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1650 rawEvent->flags);
1651 }
1652 break;
1653 }
1654 }
1655}
1656
1657bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1658 return scanCode < BTN_MOUSE
1659 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001660 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001661 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001662}
1663
Jeff Brown6328cdc2010-07-29 18:18:33 -07001664void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1665 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001666
Jeff Brownbe1aa822011-07-27 16:04:54 -07001667 if (down) {
1668 // Rotate key codes according to orientation if needed.
1669 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1670 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1671 int32_t orientation;
1672 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1673 false /*external*/, NULL, NULL, & orientation)) {
1674 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001675 }
1676
Jeff Brownbe1aa822011-07-27 16:04:54 -07001677 keyCode = rotateKeyCode(keyCode, orientation);
1678 }
Jeff Brownfe508922011-01-18 15:10:10 -08001679
Jeff Brownbe1aa822011-07-27 16:04:54 -07001680 // Add key down.
1681 ssize_t keyDownIndex = findKeyDown(scanCode);
1682 if (keyDownIndex >= 0) {
1683 // key repeat, be sure to use same keycode as before in case of rotation
1684 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001685 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001686 // key down
1687 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1688 && mContext->shouldDropVirtualKey(when,
1689 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001690 return;
1691 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001692
1693 mKeyDowns.push();
1694 KeyDown& keyDown = mKeyDowns.editTop();
1695 keyDown.keyCode = keyCode;
1696 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001697 }
1698
Jeff Brownbe1aa822011-07-27 16:04:54 -07001699 mDownTime = when;
1700 } else {
1701 // Remove key down.
1702 ssize_t keyDownIndex = findKeyDown(scanCode);
1703 if (keyDownIndex >= 0) {
1704 // key up, be sure to use same keycode as before in case of rotation
1705 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1706 mKeyDowns.removeAt(size_t(keyDownIndex));
1707 } else {
1708 // key was not actually down
1709 LOGI("Dropping key up from device %s because the key was not down. "
1710 "keyCode=%d, scanCode=%d",
1711 getDeviceName().string(), keyCode, scanCode);
1712 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001713 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001714 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001715
Jeff Brownbe1aa822011-07-27 16:04:54 -07001716 bool metaStateChanged = false;
1717 int32_t oldMetaState = mMetaState;
1718 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1719 if (oldMetaState != newMetaState) {
1720 mMetaState = newMetaState;
1721 metaStateChanged = true;
1722 updateLedState(false);
1723 }
1724
1725 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001726
Jeff Brown56194eb2011-03-02 19:23:13 -08001727 // Key down on external an keyboard should wake the device.
1728 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1729 // For internal keyboards, the key layout file should specify the policy flags for
1730 // each wake key individually.
1731 // TODO: Use the input device configuration to control this behavior more finely.
1732 if (down && getDevice()->isExternal()
1733 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1734 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1735 }
1736
Jeff Brown6328cdc2010-07-29 18:18:33 -07001737 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001738 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001739 }
1740
Jeff Brown05dc66a2011-03-02 14:41:58 -08001741 if (down && !isMetaKey(keyCode)) {
1742 getContext()->fadePointer();
1743 }
1744
Jeff Brownbe1aa822011-07-27 16:04:54 -07001745 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001746 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1747 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001748 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001749}
1750
Jeff Brownbe1aa822011-07-27 16:04:54 -07001751ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1752 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001753 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001754 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001755 return i;
1756 }
1757 }
1758 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001759}
1760
Jeff Brown6d0fec22010-07-23 21:28:06 -07001761int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1762 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1763}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001764
Jeff Brown6d0fec22010-07-23 21:28:06 -07001765int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1766 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1767}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001768
Jeff Brown6d0fec22010-07-23 21:28:06 -07001769bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1770 const int32_t* keyCodes, uint8_t* outFlags) {
1771 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1772}
1773
1774int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001775 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001776}
1777
Jeff Brownbe1aa822011-07-27 16:04:54 -07001778void KeyboardInputMapper::resetLedState() {
1779 initializeLedState(mCapsLockLedState, LED_CAPSL);
1780 initializeLedState(mNumLockLedState, LED_NUML);
1781 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001782
Jeff Brownbe1aa822011-07-27 16:04:54 -07001783 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001784}
1785
Jeff Brownbe1aa822011-07-27 16:04:54 -07001786void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001787 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1788 ledState.on = false;
1789}
1790
Jeff Brownbe1aa822011-07-27 16:04:54 -07001791void KeyboardInputMapper::updateLedState(bool reset) {
1792 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001793 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001794 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001795 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001796 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001797 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001798}
1799
Jeff Brownbe1aa822011-07-27 16:04:54 -07001800void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001801 int32_t led, int32_t modifier, bool reset) {
1802 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001803 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001804 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001805 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1806 ledState.on = desiredState;
1807 }
1808 }
1809}
1810
Jeff Brown6d0fec22010-07-23 21:28:06 -07001811
Jeff Brown83c09682010-12-23 17:50:18 -08001812// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001813
Jeff Brown83c09682010-12-23 17:50:18 -08001814CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001815 InputMapper(device) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001816 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001817}
1818
Jeff Brown83c09682010-12-23 17:50:18 -08001819CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001820}
1821
Jeff Brown83c09682010-12-23 17:50:18 -08001822uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001823 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001824}
1825
Jeff Brown83c09682010-12-23 17:50:18 -08001826void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001827 InputMapper::populateDeviceInfo(info);
1828
Jeff Brown83c09682010-12-23 17:50:18 -08001829 if (mParameters.mode == Parameters::MODE_POINTER) {
1830 float minX, minY, maxX, maxY;
1831 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001832 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1833 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001834 }
1835 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001836 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1837 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001838 }
Jeff Brownefd32662011-03-08 15:13:06 -08001839 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001840
Jeff Brown49754db2011-07-01 17:37:58 -07001841 if (mCursorMotionAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001842 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001843 }
Jeff Brown49754db2011-07-01 17:37:58 -07001844 if (mCursorMotionAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001845 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001846 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001847}
1848
Jeff Brown83c09682010-12-23 17:50:18 -08001849void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001850 dump.append(INDENT2 "Cursor Input Mapper:\n");
1851 dumpParameters(dump);
1852 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1853 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
1854 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1855 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1856 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
1857 toString(mCursorMotionAccumulator.haveRelativeVWheel()));
1858 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
1859 toString(mCursorMotionAccumulator.haveRelativeHWheel()));
1860 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1861 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
1862 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
1863 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
1864 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001865}
1866
Jeff Brown474dcb52011-06-14 20:22:50 -07001867void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1868 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001869
Jeff Brown474dcb52011-06-14 20:22:50 -07001870 if (!changes) { // first time only
Jeff Brown49754db2011-07-01 17:37:58 -07001871 mCursorMotionAccumulator.configure(getDevice());
1872
Jeff Brown474dcb52011-06-14 20:22:50 -07001873 // Configure basic parameters.
1874 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001875
Jeff Brown474dcb52011-06-14 20:22:50 -07001876 // Configure device mode.
1877 switch (mParameters.mode) {
1878 case Parameters::MODE_POINTER:
1879 mSource = AINPUT_SOURCE_MOUSE;
1880 mXPrecision = 1.0f;
1881 mYPrecision = 1.0f;
1882 mXScale = 1.0f;
1883 mYScale = 1.0f;
1884 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1885 break;
1886 case Parameters::MODE_NAVIGATION:
1887 mSource = AINPUT_SOURCE_TRACKBALL;
1888 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1889 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1890 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1891 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1892 break;
1893 }
1894
1895 mVWheelScale = 1.0f;
1896 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08001897 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001898
Jeff Brown474dcb52011-06-14 20:22:50 -07001899 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1900 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1901 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1902 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1903 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001904}
1905
Jeff Brown83c09682010-12-23 17:50:18 -08001906void CursorInputMapper::configureParameters() {
1907 mParameters.mode = Parameters::MODE_POINTER;
1908 String8 cursorModeString;
1909 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1910 if (cursorModeString == "navigation") {
1911 mParameters.mode = Parameters::MODE_NAVIGATION;
1912 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1913 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1914 }
1915 }
1916
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001917 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001918 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001919 mParameters.orientationAware);
1920
Jeff Brownbc68a592011-07-25 12:58:12 -07001921 mParameters.associatedDisplayId = -1;
1922 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
1923 mParameters.associatedDisplayId = 0;
1924 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001925}
1926
Jeff Brown83c09682010-12-23 17:50:18 -08001927void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001928 dump.append(INDENT3 "Parameters:\n");
1929 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1930 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001931
1932 switch (mParameters.mode) {
1933 case Parameters::MODE_POINTER:
1934 dump.append(INDENT4 "Mode: pointer\n");
1935 break;
1936 case Parameters::MODE_NAVIGATION:
1937 dump.append(INDENT4 "Mode: navigation\n");
1938 break;
1939 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001940 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001941 }
1942
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001943 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1944 toString(mParameters.orientationAware));
1945}
1946
Jeff Brownbe1aa822011-07-27 16:04:54 -07001947void CursorInputMapper::initialize() {
Jeff Brown49754db2011-07-01 17:37:58 -07001948 mCursorButtonAccumulator.clearButtons();
1949 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001950
Jeff Brownbe1aa822011-07-27 16:04:54 -07001951 mButtonState = 0;
1952 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001953}
1954
Jeff Brown83c09682010-12-23 17:50:18 -08001955void CursorInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001956 // Reset velocity.
1957 mPointerVelocityControl.reset();
1958 mWheelXVelocityControl.reset();
1959 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001960
Jeff Brownbe1aa822011-07-27 16:04:54 -07001961 // Synthesize button up event on reset.
1962 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
1963 mCursorButtonAccumulator.clearButtons();
1964 mCursorMotionAccumulator.clearRelativeAxes();
1965 sync(when);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001966
Jeff Brownbe1aa822011-07-27 16:04:54 -07001967 initialize();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001968
Jeff Brown6d0fec22010-07-23 21:28:06 -07001969 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001971
Jeff Brown83c09682010-12-23 17:50:18 -08001972void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07001973 mCursorButtonAccumulator.process(rawEvent);
1974 mCursorMotionAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08001975
Jeff Brown49754db2011-07-01 17:37:58 -07001976 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
1977 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001978 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001979}
1980
Jeff Brown83c09682010-12-23 17:50:18 -08001981void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001982 int32_t lastButtonState = mButtonState;
1983 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
1984 mButtonState = currentButtonState;
1985
1986 bool wasDown = isPointerDown(lastButtonState);
1987 bool down = isPointerDown(currentButtonState);
1988 bool downChanged;
1989 if (!wasDown && down) {
1990 mDownTime = when;
1991 downChanged = true;
1992 } else if (wasDown && !down) {
1993 downChanged = true;
1994 } else {
1995 downChanged = false;
1996 }
1997 nsecs_t downTime = mDownTime;
1998 bool buttonsChanged = currentButtonState != lastButtonState;
1999
2000 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2001 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2002 bool moved = deltaX != 0 || deltaY != 0;
2003
2004 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2005 && (deltaX != 0.0f || deltaY != 0.0f)) {
2006 // Rotate motion based on display orientation if needed.
2007 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
2008 int32_t orientation;
2009 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
2010 false /*external*/, NULL, NULL, & orientation)) {
2011 orientation = DISPLAY_ORIENTATION_0;
2012 }
2013
2014 rotateDelta(orientation, &deltaX, &deltaY);
2015 }
2016
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002017 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002018 pointerProperties.clear();
2019 pointerProperties.id = 0;
2020 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2021
Jeff Brown6328cdc2010-07-29 18:18:33 -07002022 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002023 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002024
Jeff Brownbe1aa822011-07-27 16:04:54 -07002025 float vscroll = mCursorMotionAccumulator.getRelativeVWheel();
2026 float hscroll = mCursorMotionAccumulator.getRelativeHWheel();
2027 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002028
Jeff Brownbe1aa822011-07-27 16:04:54 -07002029 mWheelYVelocityControl.move(when, NULL, &vscroll);
2030 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002031
Jeff Brownbe1aa822011-07-27 16:04:54 -07002032 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002033
Jeff Brownbe1aa822011-07-27 16:04:54 -07002034 if (mPointerController != NULL) {
2035 if (moved || scrolled || buttonsChanged) {
2036 mPointerController->setPresentation(
2037 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002038
Jeff Brownbe1aa822011-07-27 16:04:54 -07002039 if (moved) {
2040 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002041 }
2042
Jeff Brownbe1aa822011-07-27 16:04:54 -07002043 if (buttonsChanged) {
2044 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002045 }
Jeff Brownefd32662011-03-08 15:13:06 -08002046
Jeff Brownbe1aa822011-07-27 16:04:54 -07002047 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002048 }
2049
Jeff Brownbe1aa822011-07-27 16:04:54 -07002050 float x, y;
2051 mPointerController->getPosition(&x, &y);
2052 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2053 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2054 } else {
2055 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2056 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2057 }
2058
2059 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002060
Jeff Brown56194eb2011-03-02 19:23:13 -08002061 // Moving an external trackball or mouse should wake the device.
2062 // We don't do this for internal cursor devices to prevent them from waking up
2063 // the device in your pocket.
2064 // TODO: Use the input device configuration to control this behavior more finely.
2065 uint32_t policyFlags = 0;
2066 if (getDevice()->isExternal()) {
2067 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2068 }
2069
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002070 // Synthesize key down from buttons if needed.
2071 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2072 policyFlags, lastButtonState, currentButtonState);
2073
2074 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002075 if (downChanged || moved || scrolled || buttonsChanged) {
2076 int32_t metaState = mContext->getGlobalMetaState();
2077 int32_t motionEventAction;
2078 if (downChanged) {
2079 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2080 } else if (down || mPointerController == NULL) {
2081 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2082 } else {
2083 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2084 }
Jeff Brownb6997262010-10-08 22:31:17 -07002085
Jeff Brownbe1aa822011-07-27 16:04:54 -07002086 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2087 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002088 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002089 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002090
Jeff Brownbe1aa822011-07-27 16:04:54 -07002091 // Send hover move after UP to tell the application that the mouse is hovering now.
2092 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2093 && mPointerController != NULL) {
2094 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2095 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2096 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2097 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2098 getListener()->notifyMotion(&hoverArgs);
2099 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002100
Jeff Brownbe1aa822011-07-27 16:04:54 -07002101 // Send scroll events.
2102 if (scrolled) {
2103 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2104 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2105
2106 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2107 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2108 AMOTION_EVENT_EDGE_FLAG_NONE,
2109 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2110 getListener()->notifyMotion(&scrollArgs);
2111 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002112 }
Jeff Browna032cc02011-03-07 16:56:21 -08002113
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002114 // Synthesize key up from buttons if needed.
2115 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2116 policyFlags, lastButtonState, currentButtonState);
2117
Jeff Brown49754db2011-07-01 17:37:58 -07002118 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002119}
2120
Jeff Brown83c09682010-12-23 17:50:18 -08002121int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002122 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2123 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2124 } else {
2125 return AKEY_STATE_UNKNOWN;
2126 }
2127}
2128
Jeff Brown05dc66a2011-03-02 14:41:58 -08002129void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002130 if (mPointerController != NULL) {
2131 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2132 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002133}
2134
Jeff Brown6d0fec22010-07-23 21:28:06 -07002135
2136// --- TouchInputMapper ---
2137
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002138TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002139 InputMapper(device),
2140 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
2141 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002142}
2143
2144TouchInputMapper::~TouchInputMapper() {
2145}
2146
2147uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08002148 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002149}
2150
2151void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2152 InputMapper::populateDeviceInfo(info);
2153
Jeff Brownbe1aa822011-07-27 16:04:54 -07002154 // Ensure surface information is up to date so that orientation changes are
2155 // noticed immediately.
2156 if (!configureSurface()) {
2157 return;
2158 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002159
Jeff Brownbe1aa822011-07-27 16:04:54 -07002160 info->addMotionRange(mOrientedRanges.x);
2161 info->addMotionRange(mOrientedRanges.y);
2162
2163 if (mOrientedRanges.havePressure) {
2164 info->addMotionRange(mOrientedRanges.pressure);
2165 }
2166
2167 if (mOrientedRanges.haveSize) {
2168 info->addMotionRange(mOrientedRanges.size);
2169 }
2170
2171 if (mOrientedRanges.haveTouchSize) {
2172 info->addMotionRange(mOrientedRanges.touchMajor);
2173 info->addMotionRange(mOrientedRanges.touchMinor);
2174 }
2175
2176 if (mOrientedRanges.haveToolSize) {
2177 info->addMotionRange(mOrientedRanges.toolMajor);
2178 info->addMotionRange(mOrientedRanges.toolMinor);
2179 }
2180
2181 if (mOrientedRanges.haveOrientation) {
2182 info->addMotionRange(mOrientedRanges.orientation);
2183 }
2184
2185 if (mOrientedRanges.haveDistance) {
2186 info->addMotionRange(mOrientedRanges.distance);
2187 }
2188
2189 if (mPointerController != NULL) {
2190 float minX, minY, maxX, maxY;
2191 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
2192 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
2193 minX, maxX, 0.0f, 0.0f);
2194 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
2195 minY, maxY, 0.0f, 0.0f);
Jeff Brownefd32662011-03-08 15:13:06 -08002196 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002197 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
2198 0.0f, 1.0f, 0.0f, 0.0f);
2199 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002200}
2201
Jeff Brownef3d7e82010-09-30 14:33:04 -07002202void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002203 dump.append(INDENT2 "Touch Input Mapper:\n");
2204 dumpParameters(dump);
2205 dumpVirtualKeys(dump);
2206 dumpRawPointerAxes(dump);
2207 dumpCalibration(dump);
2208 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002209
Jeff Brownbe1aa822011-07-27 16:04:54 -07002210 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2211 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2212 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2213 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2214 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2215 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2217 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2218 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2219 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002220
Jeff Brownbe1aa822011-07-27 16:04:54 -07002221 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002222
Jeff Brownbe1aa822011-07-27 16:04:54 -07002223 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2224 mLastRawPointerData.pointerCount);
2225 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2226 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2227 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2228 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
2229 "orientation=%d, distance=%d, toolType=%d, isHovering=%s\n", i,
2230 pointer.id, pointer.x, pointer.y, pointer.pressure,
2231 pointer.touchMajor, pointer.touchMinor,
2232 pointer.toolMajor, pointer.toolMinor,
2233 pointer.orientation, pointer.distance,
2234 pointer.toolType, toString(pointer.isHovering));
2235 }
2236
2237 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2238 mLastCookedPointerData.pointerCount);
2239 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2240 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2241 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2242 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2243 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
2244 "orientation=%0.3f, distance=%0.3f, toolType=%d, isHovering=%s\n", i,
2245 pointerProperties.id,
2246 pointerCoords.getX(),
2247 pointerCoords.getY(),
2248 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2249 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2250 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2251 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2252 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2253 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
2254 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2255 pointerProperties.toolType,
2256 toString(mLastCookedPointerData.isHovering(i)));
2257 }
2258
2259 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2260 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2261 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
2262 mPointerGestureXMovementScale);
2263 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
2264 mPointerGestureYMovementScale);
2265 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
2266 mPointerGestureXZoomScale);
2267 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
2268 mPointerGestureYZoomScale);
2269 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2270 mPointerGestureMaxSwipeWidth);
2271 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002272}
2273
Jeff Brownbe1aa822011-07-27 16:04:54 -07002274void TouchInputMapper::initialize() {
2275 mCurrentRawPointerData.clear();
2276 mLastRawPointerData.clear();
2277 mCurrentCookedPointerData.clear();
2278 mLastCookedPointerData.clear();
2279 mCurrentButtonState = 0;
2280 mLastButtonState = 0;
2281 mSentHoverEnter = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002282 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002283
Jeff Brownbe1aa822011-07-27 16:04:54 -07002284 mCurrentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07002285
Jeff Brownbe1aa822011-07-27 16:04:54 -07002286 mOrientedRanges.havePressure = false;
2287 mOrientedRanges.haveSize = false;
2288 mOrientedRanges.haveTouchSize = false;
2289 mOrientedRanges.haveToolSize = false;
2290 mOrientedRanges.haveOrientation = false;
2291 mOrientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08002292
2293 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07002294}
2295
Jeff Brown474dcb52011-06-14 20:22:50 -07002296void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
2297 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002298
Jeff Brown474dcb52011-06-14 20:22:50 -07002299 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002300
Jeff Brown474dcb52011-06-14 20:22:50 -07002301 if (!changes) { // first time only
2302 // Configure basic parameters.
2303 configureParameters();
2304
2305 // Configure sources.
2306 switch (mParameters.deviceType) {
2307 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2308 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
2309 mPointerSource = 0;
2310 break;
2311 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2312 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2313 mPointerSource = 0;
2314 break;
2315 case Parameters::DEVICE_TYPE_POINTER:
2316 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2317 mPointerSource = AINPUT_SOURCE_MOUSE;
2318 break;
2319 default:
2320 LOG_ASSERT(false);
2321 }
2322
2323 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002324 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002325
2326 // Prepare input device calibration.
2327 parseCalibration();
2328 resolveCalibration();
2329
Jeff Brownbe1aa822011-07-27 16:04:54 -07002330 // Configure surface dimensions and orientation.
2331 configureSurface();
Jeff Brown83c09682010-12-23 17:50:18 -08002332 }
2333
Jeff Brown474dcb52011-06-14 20:22:50 -07002334 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2335 mPointerGesture.pointerVelocityControl.setParameters(
2336 mConfig.pointerVelocityControlParameters);
2337 }
Jeff Brown8d608662010-08-30 03:02:23 -07002338
Jeff Brown474dcb52011-06-14 20:22:50 -07002339 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
2340 // Reset the touch screen when pointer gesture enablement changes.
2341 reset();
2342 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002343}
2344
Jeff Brown8d608662010-08-30 03:02:23 -07002345void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002346 // Use the pointer presentation mode for devices that do not support distinct
2347 // multitouch. The spot-based presentation relies on being able to accurately
2348 // locate two or more fingers on the touch pad.
2349 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2350 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002351
Jeff Brown538881e2011-05-25 18:23:38 -07002352 String8 gestureModeString;
2353 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2354 gestureModeString)) {
2355 if (gestureModeString == "pointer") {
2356 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2357 } else if (gestureModeString == "spots") {
2358 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2359 } else if (gestureModeString != "default") {
2360 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2361 }
2362 }
2363
Jeff Brownace13b12011-03-09 17:39:48 -08002364 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2365 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2366 // The device is a cursor device with a touch pad attached.
2367 // By default don't use the touch pad to move the pointer.
2368 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002369 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2370 // The device is a pointing device like a track pad.
2371 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2372 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2373 // The device is a touch screen.
2374 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002375 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002376 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002377 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2378 }
2379
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002380 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002381 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2382 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002383 if (deviceTypeString == "touchScreen") {
2384 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002385 } else if (deviceTypeString == "touchPad") {
2386 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002387 } else if (deviceTypeString == "pointer") {
2388 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002389 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002390 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2391 }
2392 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002393
Jeff Brownefd32662011-03-08 15:13:06 -08002394 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002395 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2396 mParameters.orientationAware);
2397
Jeff Brownbc68a592011-07-25 12:58:12 -07002398 mParameters.associatedDisplayId = -1;
2399 mParameters.associatedDisplayIsExternal = false;
2400 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002401 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002402 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2403 mParameters.associatedDisplayIsExternal =
2404 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2405 && getDevice()->isExternal();
2406 mParameters.associatedDisplayId = 0;
2407 }
Jeff Brown8d608662010-08-30 03:02:23 -07002408}
2409
Jeff Brownef3d7e82010-09-30 14:33:04 -07002410void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002411 dump.append(INDENT3 "Parameters:\n");
2412
Jeff Brown538881e2011-05-25 18:23:38 -07002413 switch (mParameters.gestureMode) {
2414 case Parameters::GESTURE_MODE_POINTER:
2415 dump.append(INDENT4 "GestureMode: pointer\n");
2416 break;
2417 case Parameters::GESTURE_MODE_SPOTS:
2418 dump.append(INDENT4 "GestureMode: spots\n");
2419 break;
2420 default:
2421 assert(false);
2422 }
2423
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002424 switch (mParameters.deviceType) {
2425 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2426 dump.append(INDENT4 "DeviceType: touchScreen\n");
2427 break;
2428 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2429 dump.append(INDENT4 "DeviceType: touchPad\n");
2430 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002431 case Parameters::DEVICE_TYPE_POINTER:
2432 dump.append(INDENT4 "DeviceType: pointer\n");
2433 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002434 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002435 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002436 }
2437
2438 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2439 mParameters.associatedDisplayId);
2440 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2441 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002442}
2443
Jeff Brownbe1aa822011-07-27 16:04:54 -07002444void TouchInputMapper::configureRawPointerAxes() {
2445 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002446}
2447
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2449 dump.append(INDENT3 "Raw Touch Axes:\n");
2450 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2451 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2452 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2453 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2454 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2455 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2456 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2457 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2458 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
2459 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2460 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002461}
2462
Jeff Brownbe1aa822011-07-27 16:04:54 -07002463bool TouchInputMapper::configureSurface() {
Jeff Brown9626b142011-03-03 02:09:54 -08002464 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002465 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002466 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2467 "The device will be inoperable.", getDeviceName().string());
2468 return false;
2469 }
2470
Jeff Brown6d0fec22010-07-23 21:28:06 -07002471 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002472 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002473 int32_t width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2474 int32_t height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002475
2476 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002477 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002478 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002479 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002480 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2481 &mAssociatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002482 return false;
2483 }
Jeff Brownefd32662011-03-08 15:13:06 -08002484
2485 // A touch screen inherits the dimensions of the display.
2486 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002487 width = mAssociatedDisplayWidth;
2488 height = mAssociatedDisplayHeight;
Jeff Brownefd32662011-03-08 15:13:06 -08002489 }
2490
2491 // The device inherits the orientation of the display if it is orientation aware.
2492 if (mParameters.orientationAware) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002493 orientation = mAssociatedDisplayOrientation;
Jeff Brownefd32662011-03-08 15:13:06 -08002494 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002495 }
2496
Jeff Brownace13b12011-03-09 17:39:48 -08002497 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2498 && mPointerController == NULL) {
2499 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2500 }
2501
Jeff Brownbe1aa822011-07-27 16:04:54 -07002502 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002503 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002504 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002505 }
2506
Jeff Brownbe1aa822011-07-27 16:04:54 -07002507 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002508 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002509 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002510 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002511
Jeff Brownbe1aa822011-07-27 16:04:54 -07002512 mSurfaceWidth = width;
2513 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002514
Jeff Brown8d608662010-08-30 03:02:23 -07002515 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002516 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2517 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2518 mXPrecision = 1.0f / mXScale;
2519 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002520
Jeff Brownbe1aa822011-07-27 16:04:54 -07002521 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2522 mOrientedRanges.x.source = mTouchSource;
2523 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2524 mOrientedRanges.y.source = mTouchSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002525
Jeff Brownbe1aa822011-07-27 16:04:54 -07002526 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002527
Jeff Brown8d608662010-08-30 03:02:23 -07002528 // Scale factor for terms that are not oriented in a particular axis.
2529 // If the pixels are square then xScale == yScale otherwise we fake it
2530 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002531 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002532
Jeff Brown8d608662010-08-30 03:02:23 -07002533 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002534 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002535
Jeff Browna1f89ce2011-08-11 00:05:01 -07002536 // Size factors.
2537 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2538 if (mRawPointerAxes.touchMajor.valid
2539 && mRawPointerAxes.touchMajor.maxValue != 0) {
2540 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2541 } else if (mRawPointerAxes.toolMajor.valid
2542 && mRawPointerAxes.toolMajor.maxValue != 0) {
2543 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2544 } else {
2545 mSizeScale = 0.0f;
2546 }
2547
Jeff Brownbe1aa822011-07-27 16:04:54 -07002548 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002549 mOrientedRanges.haveToolSize = true;
2550 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002551
Jeff Brownbe1aa822011-07-27 16:04:54 -07002552 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2553 mOrientedRanges.touchMajor.source = mTouchSource;
2554 mOrientedRanges.touchMajor.min = 0;
2555 mOrientedRanges.touchMajor.max = diagonalSize;
2556 mOrientedRanges.touchMajor.flat = 0;
2557 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002558
Jeff Brownbe1aa822011-07-27 16:04:54 -07002559 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2560 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002561
Jeff Brownbe1aa822011-07-27 16:04:54 -07002562 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2563 mOrientedRanges.toolMajor.source = mTouchSource;
2564 mOrientedRanges.toolMajor.min = 0;
2565 mOrientedRanges.toolMajor.max = diagonalSize;
2566 mOrientedRanges.toolMajor.flat = 0;
2567 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002568
Jeff Brownbe1aa822011-07-27 16:04:54 -07002569 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2570 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002571
2572 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2573 mOrientedRanges.size.source = mTouchSource;
2574 mOrientedRanges.size.min = 0;
2575 mOrientedRanges.size.max = 1.0;
2576 mOrientedRanges.size.flat = 0;
2577 mOrientedRanges.size.fuzz = 0;
2578 } else {
2579 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002580 }
2581
2582 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002583 mPressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002584 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002585 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2586 || mCalibration.pressureCalibration
2587 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2588 if (mCalibration.havePressureScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002589 mPressureScale = mCalibration.pressureScale;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002590 } else if (mRawPointerAxes.pressure.valid
2591 && mRawPointerAxes.pressure.maxValue != 0) {
2592 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002593 }
2594 }
2595
Jeff Brownbe1aa822011-07-27 16:04:54 -07002596 mOrientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002597
Jeff Brownbe1aa822011-07-27 16:04:54 -07002598 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2599 mOrientedRanges.pressure.source = mTouchSource;
2600 mOrientedRanges.pressure.min = 0;
2601 mOrientedRanges.pressure.max = 1.0;
2602 mOrientedRanges.pressure.flat = 0;
2603 mOrientedRanges.pressure.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002604 }
2605
Jeff Brown8d608662010-08-30 03:02:23 -07002606 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07002607 mOrientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002608 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002609 if (mCalibration.orientationCalibration
2610 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Browna1f89ce2011-08-11 00:05:01 -07002611 if (mRawPointerAxes.orientation.valid
2612 && mRawPointerAxes.orientation.maxValue != 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002613 mOrientationScale = float(M_PI_2) / mRawPointerAxes.orientation.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002614 }
2615 }
2616
Jeff Brownbe1aa822011-07-27 16:04:54 -07002617 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002618
Jeff Brownbe1aa822011-07-27 16:04:54 -07002619 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2620 mOrientedRanges.orientation.source = mTouchSource;
2621 mOrientedRanges.orientation.min = - M_PI_2;
2622 mOrientedRanges.orientation.max = M_PI_2;
2623 mOrientedRanges.orientation.flat = 0;
2624 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002625 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002626
2627 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002628 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002629 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2630 if (mCalibration.distanceCalibration
2631 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2632 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002633 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002634 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002635 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002636 }
2637 }
2638
Jeff Brownbe1aa822011-07-27 16:04:54 -07002639 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002640
Jeff Brownbe1aa822011-07-27 16:04:54 -07002641 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2642 mOrientedRanges.distance.source = mTouchSource;
2643 mOrientedRanges.distance.min =
2644 mRawPointerAxes.distance.minValue * mDistanceScale;
2645 mOrientedRanges.distance.max =
2646 mRawPointerAxes.distance.minValue * mDistanceScale;
2647 mOrientedRanges.distance.flat = 0;
2648 mOrientedRanges.distance.fuzz =
2649 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002650 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002651 }
2652
2653 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002654 // Compute oriented surface dimensions, precision, scales and ranges.
2655 // Note that the maximum value reported is an inclusive maximum value so it is one
2656 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002657 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002658 case DISPLAY_ORIENTATION_90:
2659 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002660 mOrientedSurfaceWidth = mSurfaceHeight;
2661 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002662
Jeff Brownbe1aa822011-07-27 16:04:54 -07002663 mOrientedXPrecision = mYPrecision;
2664 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002665
Jeff Brownbe1aa822011-07-27 16:04:54 -07002666 mOrientedRanges.x.min = 0;
2667 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2668 * mYScale;
2669 mOrientedRanges.x.flat = 0;
2670 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002671
Jeff Brownbe1aa822011-07-27 16:04:54 -07002672 mOrientedRanges.y.min = 0;
2673 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2674 * mXScale;
2675 mOrientedRanges.y.flat = 0;
2676 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002677 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002678
Jeff Brown6d0fec22010-07-23 21:28:06 -07002679 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002680 mOrientedSurfaceWidth = mSurfaceWidth;
2681 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002682
Jeff Brownbe1aa822011-07-27 16:04:54 -07002683 mOrientedXPrecision = mXPrecision;
2684 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002685
Jeff Brownbe1aa822011-07-27 16:04:54 -07002686 mOrientedRanges.x.min = 0;
2687 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2688 * mXScale;
2689 mOrientedRanges.x.flat = 0;
2690 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002691
Jeff Brownbe1aa822011-07-27 16:04:54 -07002692 mOrientedRanges.y.min = 0;
2693 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2694 * mYScale;
2695 mOrientedRanges.y.flat = 0;
2696 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002697 break;
2698 }
Jeff Brownace13b12011-03-09 17:39:48 -08002699
2700 // Compute pointer gesture detection parameters.
Jeff Brownace13b12011-03-09 17:39:48 -08002701 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002702 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2703 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002704 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002705 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2706 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002707
Jeff Brown2352b972011-04-12 22:39:53 -07002708 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002709 // given area relative to the diagonal size of the display when no acceleration
2710 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002711 // Assume that the touch pad has a square aspect ratio such that movements in
2712 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002713 mPointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002714 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002715 mPointerGestureYMovementScale = mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002716
2717 // Scale zooms to cover a smaller range of the display than movements do.
2718 // This value determines the area around the pointer that is affected by freeform
2719 // pointer gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002720 mPointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002721 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002722 mPointerGestureYZoomScale = mPointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002723
Jeff Brown2352b972011-04-12 22:39:53 -07002724 // Max width between pointers to detect a swipe gesture is more than some fraction
2725 // of the diagonal axis of the touch pad. Touches that are wider than this are
2726 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002727 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002728 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002729
2730 // Reset the current pointer gesture.
2731 mPointerGesture.reset();
2732
2733 // Remove any current spots.
2734 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2735 mPointerController->clearSpots();
2736 }
Jeff Brownace13b12011-03-09 17:39:48 -08002737 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002738 }
2739
2740 return true;
2741}
2742
Jeff Brownbe1aa822011-07-27 16:04:54 -07002743void TouchInputMapper::dumpSurface(String8& dump) {
2744 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2745 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2746 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002747}
2748
Jeff Brownbe1aa822011-07-27 16:04:54 -07002749void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002750 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002751 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002752
Jeff Brownbe1aa822011-07-27 16:04:54 -07002753 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002754
Jeff Brown6328cdc2010-07-29 18:18:33 -07002755 if (virtualKeyDefinitions.size() == 0) {
2756 return;
2757 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002758
Jeff Brownbe1aa822011-07-27 16:04:54 -07002759 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002760
Jeff Brownbe1aa822011-07-27 16:04:54 -07002761 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2762 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2763 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2764 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002765
2766 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002767 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002768 virtualKeyDefinitions[i];
2769
Jeff Brownbe1aa822011-07-27 16:04:54 -07002770 mVirtualKeys.add();
2771 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002772
2773 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2774 int32_t keyCode;
2775 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002776 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002777 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002778 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2779 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002780 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07002781 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002782 }
2783
Jeff Brown6328cdc2010-07-29 18:18:33 -07002784 virtualKey.keyCode = keyCode;
2785 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002786
Jeff Brown6328cdc2010-07-29 18:18:33 -07002787 // convert the key definition's display coordinates into touch coordinates for a hit box
2788 int32_t halfWidth = virtualKeyDefinition.width / 2;
2789 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002790
Jeff Brown6328cdc2010-07-29 18:18:33 -07002791 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002792 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002793 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002794 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002795 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002796 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002797 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002798 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002799 }
2800}
2801
Jeff Brownbe1aa822011-07-27 16:04:54 -07002802void TouchInputMapper::dumpVirtualKeys(String8& dump) {
2803 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002804 dump.append(INDENT3 "Virtual Keys:\n");
2805
Jeff Brownbe1aa822011-07-27 16:04:54 -07002806 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
2807 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002808 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2809 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2810 i, virtualKey.scanCode, virtualKey.keyCode,
2811 virtualKey.hitLeft, virtualKey.hitRight,
2812 virtualKey.hitTop, virtualKey.hitBottom);
2813 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002814 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002815}
2816
Jeff Brown8d608662010-08-30 03:02:23 -07002817void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002818 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002819 Calibration& out = mCalibration;
2820
Jeff Browna1f89ce2011-08-11 00:05:01 -07002821 // Size
2822 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2823 String8 sizeCalibrationString;
2824 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
2825 if (sizeCalibrationString == "none") {
2826 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2827 } else if (sizeCalibrationString == "geometric") {
2828 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
2829 } else if (sizeCalibrationString == "diameter") {
2830 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
2831 } else if (sizeCalibrationString == "area") {
2832 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
2833 } else if (sizeCalibrationString != "default") {
2834 LOGW("Invalid value for touch.size.calibration: '%s'",
2835 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002836 }
2837 }
2838
Jeff Browna1f89ce2011-08-11 00:05:01 -07002839 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
2840 out.sizeScale);
2841 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
2842 out.sizeBias);
2843 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
2844 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002845
2846 // Pressure
2847 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2848 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002849 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002850 if (pressureCalibrationString == "none") {
2851 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2852 } else if (pressureCalibrationString == "physical") {
2853 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2854 } else if (pressureCalibrationString == "amplitude") {
2855 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2856 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002857 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002858 pressureCalibrationString.string());
2859 }
2860 }
2861
Jeff Brown8d608662010-08-30 03:02:23 -07002862 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2863 out.pressureScale);
2864
Jeff Brown8d608662010-08-30 03:02:23 -07002865 // Orientation
2866 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2867 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002868 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002869 if (orientationCalibrationString == "none") {
2870 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2871 } else if (orientationCalibrationString == "interpolated") {
2872 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002873 } else if (orientationCalibrationString == "vector") {
2874 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002875 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002876 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002877 orientationCalibrationString.string());
2878 }
2879 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002880
2881 // Distance
2882 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2883 String8 distanceCalibrationString;
2884 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2885 if (distanceCalibrationString == "none") {
2886 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2887 } else if (distanceCalibrationString == "scaled") {
2888 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2889 } else if (distanceCalibrationString != "default") {
2890 LOGW("Invalid value for touch.distance.calibration: '%s'",
2891 distanceCalibrationString.string());
2892 }
2893 }
2894
2895 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2896 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002897}
2898
2899void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07002900 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07002901 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
2902 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
2903 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07002904 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07002905 } else {
2906 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2907 }
Jeff Brown8d608662010-08-30 03:02:23 -07002908
Jeff Browna1f89ce2011-08-11 00:05:01 -07002909 // Pressure
2910 if (mRawPointerAxes.pressure.valid) {
2911 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
2912 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2913 }
2914 } else {
2915 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002916 }
2917
2918 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07002919 if (mRawPointerAxes.orientation.valid) {
2920 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07002921 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07002922 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07002923 } else {
2924 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002925 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002926
2927 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07002928 if (mRawPointerAxes.distance.valid) {
2929 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002930 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002931 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07002932 } else {
2933 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002934 }
Jeff Brown8d608662010-08-30 03:02:23 -07002935}
2936
Jeff Brownef3d7e82010-09-30 14:33:04 -07002937void TouchInputMapper::dumpCalibration(String8& dump) {
2938 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002939
Jeff Browna1f89ce2011-08-11 00:05:01 -07002940 // Size
2941 switch (mCalibration.sizeCalibration) {
2942 case Calibration::SIZE_CALIBRATION_NONE:
2943 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002944 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002945 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
2946 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002947 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002948 case Calibration::SIZE_CALIBRATION_DIAMETER:
2949 dump.append(INDENT4 "touch.size.calibration: diameter\n");
2950 break;
2951 case Calibration::SIZE_CALIBRATION_AREA:
2952 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002953 break;
2954 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002955 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002956 }
2957
Jeff Browna1f89ce2011-08-11 00:05:01 -07002958 if (mCalibration.haveSizeScale) {
2959 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
2960 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002961 }
2962
Jeff Browna1f89ce2011-08-11 00:05:01 -07002963 if (mCalibration.haveSizeBias) {
2964 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
2965 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002966 }
2967
Jeff Browna1f89ce2011-08-11 00:05:01 -07002968 if (mCalibration.haveSizeIsSummed) {
2969 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
2970 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002971 }
2972
2973 // Pressure
2974 switch (mCalibration.pressureCalibration) {
2975 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002976 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002977 break;
2978 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002979 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002980 break;
2981 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002982 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002983 break;
2984 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002985 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002986 }
2987
Jeff Brown8d608662010-08-30 03:02:23 -07002988 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002989 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2990 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002991 }
2992
Jeff Brown8d608662010-08-30 03:02:23 -07002993 // Orientation
2994 switch (mCalibration.orientationCalibration) {
2995 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002996 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002997 break;
2998 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002999 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003000 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003001 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3002 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3003 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003004 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003005 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003006 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003007
3008 // Distance
3009 switch (mCalibration.distanceCalibration) {
3010 case Calibration::DISTANCE_CALIBRATION_NONE:
3011 dump.append(INDENT4 "touch.distance.calibration: none\n");
3012 break;
3013 case Calibration::DISTANCE_CALIBRATION_SCALED:
3014 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3015 break;
3016 default:
3017 LOG_ASSERT(false);
3018 }
3019
3020 if (mCalibration.haveDistanceScale) {
3021 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3022 mCalibration.distanceScale);
3023 }
Jeff Brown8d608662010-08-30 03:02:23 -07003024}
3025
Jeff Brown6d0fec22010-07-23 21:28:06 -07003026void TouchInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003027 // Synthesize touch up event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003028 // This will also take care of finishing virtual key processing if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003029 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
3030 mCurrentRawPointerData.clear();
3031 mCurrentButtonState = 0;
3032 syncTouch(when, true);
3033
3034 initialize();
3035
3036 if (mPointerController != NULL
3037 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3038 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3039 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003040 }
3041
Jeff Brown6328cdc2010-07-29 18:18:33 -07003042 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003043}
3044
3045void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07003046#if DEBUG_RAW_EVENTS
3047 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003048 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3049 mLastRawPointerData.pointerCount,
3050 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003051 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003052 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3053 "hovering ids 0x%08x -> 0x%08x",
3054 mLastRawPointerData.pointerCount,
3055 mCurrentRawPointerData.pointerCount,
3056 mLastRawPointerData.touchingIdBits.value,
3057 mCurrentRawPointerData.touchingIdBits.value,
3058 mLastRawPointerData.hoveringIdBits.value,
3059 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003060 }
3061#endif
3062
Jeff Brownbe1aa822011-07-27 16:04:54 -07003063 // Configure the surface now, if possible.
3064 if (!configureSurface()) {
3065 mLastRawPointerData.clear();
3066 mLastCookedPointerData.clear();
3067 mLastButtonState = 0;
3068 return;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003069 }
3070
Jeff Brownbe1aa822011-07-27 16:04:54 -07003071 // Preprocess pointer data.
3072 if (!havePointerIds) {
3073 assignPointerIds();
3074 }
3075
3076 // Handle policy on initial down or hover events.
Jeff Brown56194eb2011-03-02 19:23:13 -08003077 uint32_t policyFlags = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003078 if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08003079 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
3080 // If this is a touch screen, hide the pointer on an initial down.
3081 getContext()->fadePointer();
3082 }
Jeff Brown56194eb2011-03-02 19:23:13 -08003083
3084 // Initial downs on external touch devices should wake the device.
3085 // We don't do this for internal touch screens to prevent them from waking
3086 // up in your pocket.
3087 // TODO: Use the input device configuration to control this behavior more finely.
3088 if (getDevice()->isExternal()) {
3089 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3090 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08003091 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003092
Jeff Brownbe1aa822011-07-27 16:04:54 -07003093 // Synthesize key down from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003094 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003095 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003096
Jeff Brownbe1aa822011-07-27 16:04:54 -07003097 if (consumeRawTouches(when, policyFlags)) {
3098 mCurrentRawPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003099 }
3100
Jeff Brownbe1aa822011-07-27 16:04:54 -07003101 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
3102 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3103 }
3104
3105 cookPointerData();
3106 dispatchHoverExit(when, policyFlags);
3107 dispatchTouches(when, policyFlags);
3108 dispatchHoverEnterAndMove(when, policyFlags);
3109
3110 // Synthesize key up from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003111 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003112 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003113
Jeff Brown6328cdc2010-07-29 18:18:33 -07003114 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003115 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3116 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3117 mLastButtonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003118}
3119
Jeff Brown79ac9692011-04-19 21:20:10 -07003120void TouchInputMapper::timeoutExpired(nsecs_t when) {
3121 if (mPointerController != NULL) {
3122 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3123 }
3124}
3125
Jeff Brownbe1aa822011-07-27 16:04:54 -07003126bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3127 // Check for release of a virtual key.
3128 if (mCurrentVirtualKey.down) {
3129 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3130 // Pointer went up while virtual key was down.
3131 mCurrentVirtualKey.down = false;
3132 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003133#if DEBUG_VIRTUAL_KEYS
3134 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003135 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003136#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003137 dispatchVirtualKey(when, policyFlags,
3138 AKEY_EVENT_ACTION_UP,
3139 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003140 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003141 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003142 }
3143
Jeff Brownbe1aa822011-07-27 16:04:54 -07003144 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3145 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3146 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3147 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3148 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3149 // Pointer is still within the space of the virtual key.
3150 return true;
3151 }
3152 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003153
Jeff Brownbe1aa822011-07-27 16:04:54 -07003154 // Pointer left virtual key area or another pointer also went down.
3155 // Send key cancellation but do not consume the touch yet.
3156 // This is useful when the user swipes through from the virtual key area
3157 // into the main display surface.
3158 mCurrentVirtualKey.down = false;
3159 if (!mCurrentVirtualKey.ignored) {
3160#if DEBUG_VIRTUAL_KEYS
3161 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3162 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3163#endif
3164 dispatchVirtualKey(when, policyFlags,
3165 AKEY_EVENT_ACTION_UP,
3166 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3167 | AKEY_EVENT_FLAG_CANCELED);
3168 }
3169 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003170
Jeff Brownbe1aa822011-07-27 16:04:54 -07003171 if (mLastRawPointerData.touchingIdBits.isEmpty()
3172 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3173 // Pointer just went down. Check for virtual key press or off-screen touches.
3174 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3175 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3176 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3177 // If exactly one pointer went down, check for virtual key hit.
3178 // Otherwise we will drop the entire stroke.
3179 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3180 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3181 if (virtualKey) {
3182 mCurrentVirtualKey.down = true;
3183 mCurrentVirtualKey.downTime = when;
3184 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3185 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3186 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3187 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3188
3189 if (!mCurrentVirtualKey.ignored) {
3190#if DEBUG_VIRTUAL_KEYS
3191 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3192 mCurrentVirtualKey.keyCode,
3193 mCurrentVirtualKey.scanCode);
3194#endif
3195 dispatchVirtualKey(when, policyFlags,
3196 AKEY_EVENT_ACTION_DOWN,
3197 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3198 }
3199 }
3200 }
3201 return true;
3202 }
3203 }
3204
Jeff Brownfe508922011-01-18 15:10:10 -08003205 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003206 // most recent touch within the screen area. The idea is to filter out stray
3207 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003208 //
3209 // Problems we're trying to solve:
3210 //
3211 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3212 // virtual key area that is implemented by a separate touch panel and accidentally
3213 // triggers a virtual key.
3214 //
3215 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3216 // area and accidentally triggers a virtual key. This often happens when virtual keys
3217 // are layed out below the screen near to where the on screen keyboard's space bar
3218 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003219 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003220 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003221 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003222 return false;
3223}
3224
3225void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3226 int32_t keyEventAction, int32_t keyEventFlags) {
3227 int32_t keyCode = mCurrentVirtualKey.keyCode;
3228 int32_t scanCode = mCurrentVirtualKey.scanCode;
3229 nsecs_t downTime = mCurrentVirtualKey.downTime;
3230 int32_t metaState = mContext->getGlobalMetaState();
3231 policyFlags |= POLICY_FLAG_VIRTUAL;
3232
3233 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3234 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3235 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003236}
3237
Jeff Brown6d0fec22010-07-23 21:28:06 -07003238void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003239 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3240 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003241 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003242 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003243
3244 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003245 if (!currentIdBits.isEmpty()) {
3246 // No pointer id changes so this is a move event.
3247 // The listener takes care of batching moves so we don't have to deal with that here.
3248 dispatchMotion(when, policyFlags, mTouchSource,
3249 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3250 AMOTION_EVENT_EDGE_FLAG_NONE,
3251 mCurrentCookedPointerData.pointerProperties,
3252 mCurrentCookedPointerData.pointerCoords,
3253 mCurrentCookedPointerData.idToIndex,
3254 currentIdBits, -1,
3255 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3256 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003257 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003258 // There may be pointers going up and pointers going down and pointers moving
3259 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003260 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3261 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003262 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003263 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003264
Jeff Brownace13b12011-03-09 17:39:48 -08003265 // Update last coordinates of pointers that have moved so that we observe the new
3266 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003267 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003268 mCurrentCookedPointerData.pointerProperties,
3269 mCurrentCookedPointerData.pointerCoords,
3270 mCurrentCookedPointerData.idToIndex,
3271 mLastCookedPointerData.pointerProperties,
3272 mLastCookedPointerData.pointerCoords,
3273 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003274 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003275 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003276 moveNeeded = true;
3277 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003278
Jeff Brownace13b12011-03-09 17:39:48 -08003279 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003280 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003281 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003282
Jeff Brownace13b12011-03-09 17:39:48 -08003283 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003284 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003285 mLastCookedPointerData.pointerProperties,
3286 mLastCookedPointerData.pointerCoords,
3287 mLastCookedPointerData.idToIndex,
3288 dispatchedIdBits, upId,
3289 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003290 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003291 }
3292
Jeff Brownc3db8582010-10-20 15:33:38 -07003293 // Dispatch move events if any of the remaining pointers moved from their old locations.
3294 // Although applications receive new locations as part of individual pointer up
3295 // events, they do not generally handle them except when presented in a move event.
3296 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003297 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003298 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003299 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003300 mCurrentCookedPointerData.pointerProperties,
3301 mCurrentCookedPointerData.pointerCoords,
3302 mCurrentCookedPointerData.idToIndex,
3303 dispatchedIdBits, -1,
3304 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003305 }
3306
3307 // Dispatch pointer down events using the new pointer locations.
3308 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003309 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003310 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003311
Jeff Brownace13b12011-03-09 17:39:48 -08003312 if (dispatchedIdBits.count() == 1) {
3313 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003314 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003315 }
3316
Jeff Brownace13b12011-03-09 17:39:48 -08003317 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003318 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003319 mCurrentCookedPointerData.pointerProperties,
3320 mCurrentCookedPointerData.pointerCoords,
3321 mCurrentCookedPointerData.idToIndex,
3322 dispatchedIdBits, downId,
3323 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003324 }
3325 }
Jeff Brownace13b12011-03-09 17:39:48 -08003326}
3327
Jeff Brownbe1aa822011-07-27 16:04:54 -07003328void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3329 if (mSentHoverEnter &&
3330 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3331 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3332 int32_t metaState = getContext()->getGlobalMetaState();
3333 dispatchMotion(when, policyFlags, mTouchSource,
3334 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3335 mLastCookedPointerData.pointerProperties,
3336 mLastCookedPointerData.pointerCoords,
3337 mLastCookedPointerData.idToIndex,
3338 mLastCookedPointerData.hoveringIdBits, -1,
3339 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3340 mSentHoverEnter = false;
3341 }
3342}
Jeff Brownace13b12011-03-09 17:39:48 -08003343
Jeff Brownbe1aa822011-07-27 16:04:54 -07003344void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3345 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3346 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3347 int32_t metaState = getContext()->getGlobalMetaState();
3348 if (!mSentHoverEnter) {
3349 dispatchMotion(when, policyFlags, mTouchSource,
3350 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3351 mCurrentCookedPointerData.pointerProperties,
3352 mCurrentCookedPointerData.pointerCoords,
3353 mCurrentCookedPointerData.idToIndex,
3354 mCurrentCookedPointerData.hoveringIdBits, -1,
3355 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3356 mSentHoverEnter = true;
3357 }
Jeff Brownace13b12011-03-09 17:39:48 -08003358
Jeff Brownbe1aa822011-07-27 16:04:54 -07003359 dispatchMotion(when, policyFlags, mTouchSource,
3360 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3361 mCurrentCookedPointerData.pointerProperties,
3362 mCurrentCookedPointerData.pointerCoords,
3363 mCurrentCookedPointerData.idToIndex,
3364 mCurrentCookedPointerData.hoveringIdBits, -1,
3365 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3366 }
3367}
3368
3369void TouchInputMapper::cookPointerData() {
3370 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3371
3372 mCurrentCookedPointerData.clear();
3373 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3374 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3375 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3376
3377 // Walk through the the active pointers and map device coordinates onto
3378 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003379 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003380 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003381
Jeff Browna1f89ce2011-08-11 00:05:01 -07003382 // Size
3383 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3384 switch (mCalibration.sizeCalibration) {
3385 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3386 case Calibration::SIZE_CALIBRATION_DIAMETER:
3387 case Calibration::SIZE_CALIBRATION_AREA:
3388 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3389 touchMajor = in.touchMajor;
3390 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3391 toolMajor = in.toolMajor;
3392 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3393 size = mRawPointerAxes.touchMinor.valid
3394 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3395 } else if (mRawPointerAxes.touchMajor.valid) {
3396 toolMajor = touchMajor = in.touchMajor;
3397 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3398 ? in.touchMinor : in.touchMajor;
3399 size = mRawPointerAxes.touchMinor.valid
3400 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3401 } else if (mRawPointerAxes.toolMajor.valid) {
3402 touchMajor = toolMajor = in.toolMajor;
3403 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3404 ? in.toolMinor : in.toolMajor;
3405 size = mRawPointerAxes.toolMinor.valid
3406 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003407 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003408 LOG_ASSERT(false, "No touch or tool axes. "
3409 "Size calibration should have been resolved to NONE.");
3410 touchMajor = 0;
3411 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003412 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003413 toolMinor = 0;
3414 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003415 }
Jeff Brownace13b12011-03-09 17:39:48 -08003416
Jeff Browna1f89ce2011-08-11 00:05:01 -07003417 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3418 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3419 if (touchingCount > 1) {
3420 touchMajor /= touchingCount;
3421 touchMinor /= touchingCount;
3422 toolMajor /= touchingCount;
3423 toolMinor /= touchingCount;
3424 size /= touchingCount;
3425 }
3426 }
Jeff Brownace13b12011-03-09 17:39:48 -08003427
Jeff Browna1f89ce2011-08-11 00:05:01 -07003428 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3429 touchMajor *= mGeometricScale;
3430 touchMinor *= mGeometricScale;
3431 toolMajor *= mGeometricScale;
3432 toolMinor *= mGeometricScale;
3433 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3434 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003435 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003436 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3437 toolMinor = toolMajor;
3438 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3439 touchMinor = touchMajor;
3440 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003441 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003442
3443 mCalibration.applySizeScaleAndBias(&touchMajor);
3444 mCalibration.applySizeScaleAndBias(&touchMinor);
3445 mCalibration.applySizeScaleAndBias(&toolMajor);
3446 mCalibration.applySizeScaleAndBias(&toolMinor);
3447 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003448 break;
3449 default:
3450 touchMajor = 0;
3451 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003452 toolMajor = 0;
3453 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003454 size = 0;
3455 break;
3456 }
3457
Jeff Browna1f89ce2011-08-11 00:05:01 -07003458 // Pressure
3459 float pressure;
3460 switch (mCalibration.pressureCalibration) {
3461 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3462 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3463 pressure = in.pressure * mPressureScale;
3464 break;
3465 default:
3466 pressure = in.isHovering ? 0 : 1;
3467 break;
3468 }
3469
Jeff Brownace13b12011-03-09 17:39:48 -08003470 // Orientation
3471 float orientation;
3472 switch (mCalibration.orientationCalibration) {
3473 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003474 orientation = in.orientation * mOrientationScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003475 break;
3476 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3477 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3478 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3479 if (c1 != 0 || c2 != 0) {
3480 orientation = atan2f(c1, c2) * 0.5f;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003481 float confidence = hypotf(c1, c2);
3482 float scale = 1.0f + confidence / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003483 touchMajor *= scale;
3484 touchMinor /= scale;
3485 toolMajor *= scale;
3486 toolMinor /= scale;
3487 } else {
3488 orientation = 0;
3489 }
3490 break;
3491 }
3492 default:
3493 orientation = 0;
3494 }
3495
Jeff Brown80fd47c2011-05-24 01:07:44 -07003496 // Distance
3497 float distance;
3498 switch (mCalibration.distanceCalibration) {
3499 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003500 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003501 break;
3502 default:
3503 distance = 0;
3504 }
3505
Jeff Brownace13b12011-03-09 17:39:48 -08003506 // X and Y
3507 // Adjust coords for surface orientation.
3508 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003509 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003510 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003511 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3512 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003513 orientation -= M_PI_2;
3514 if (orientation < - M_PI_2) {
3515 orientation += M_PI;
3516 }
3517 break;
3518 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003519 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3520 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003521 break;
3522 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003523 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3524 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003525 orientation += M_PI_2;
3526 if (orientation > M_PI_2) {
3527 orientation -= M_PI;
3528 }
3529 break;
3530 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003531 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3532 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003533 break;
3534 }
3535
3536 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003537 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003538 out.clear();
3539 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3540 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3541 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3542 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3543 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3544 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3545 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3546 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3547 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003548 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003549
3550 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003551 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3552 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003553 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003554 properties.id = id;
3555 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003556
Jeff Brownbe1aa822011-07-27 16:04:54 -07003557 // Write id index.
3558 mCurrentCookedPointerData.idToIndex[id] = i;
3559 }
Jeff Brownace13b12011-03-09 17:39:48 -08003560}
3561
Jeff Brown79ac9692011-04-19 21:20:10 -07003562void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3563 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003564 // Update current gesture coordinates.
3565 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003566 bool sendEvents = preparePointerGestures(when,
3567 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3568 if (!sendEvents) {
3569 return;
3570 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003571 if (finishPreviousGesture) {
3572 cancelPreviousGesture = false;
3573 }
Jeff Brownace13b12011-03-09 17:39:48 -08003574
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003575 // Update the pointer presentation and spots.
3576 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3577 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3578 if (finishPreviousGesture || cancelPreviousGesture) {
3579 mPointerController->clearSpots();
3580 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003581 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3582 mPointerGesture.currentGestureIdToIndex,
3583 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003584 } else {
3585 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3586 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003587
Jeff Brown538881e2011-05-25 18:23:38 -07003588 // Show or hide the pointer if needed.
3589 switch (mPointerGesture.currentGestureMode) {
3590 case PointerGesture::NEUTRAL:
3591 case PointerGesture::QUIET:
3592 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3593 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3594 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3595 // Remind the user of where the pointer is after finishing a gesture with spots.
3596 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3597 }
3598 break;
3599 case PointerGesture::TAP:
3600 case PointerGesture::TAP_DRAG:
3601 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3602 case PointerGesture::HOVER:
3603 case PointerGesture::PRESS:
3604 // Unfade the pointer when the current gesture manipulates the
3605 // area directly under the pointer.
3606 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3607 break;
3608 case PointerGesture::SWIPE:
3609 case PointerGesture::FREEFORM:
3610 // Fade the pointer when the current gesture manipulates a different
3611 // area and there are spots to guide the user experience.
3612 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3613 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3614 } else {
3615 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3616 }
3617 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003618 }
3619
Jeff Brownace13b12011-03-09 17:39:48 -08003620 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003621 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003622 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003623
3624 // Update last coordinates of pointers that have moved so that we observe the new
3625 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003626 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3627 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3628 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003629 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003630 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3631 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3632 bool moveNeeded = false;
3633 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003634 && !mPointerGesture.lastGestureIdBits.isEmpty()
3635 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003636 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3637 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003638 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003639 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003640 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003641 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3642 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003643 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003644 moveNeeded = true;
3645 }
Jeff Brownace13b12011-03-09 17:39:48 -08003646 }
3647
3648 // Send motion events for all pointers that went up or were canceled.
3649 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3650 if (!dispatchedGestureIdBits.isEmpty()) {
3651 if (cancelPreviousGesture) {
3652 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003653 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3654 AMOTION_EVENT_EDGE_FLAG_NONE,
3655 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003656 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3657 dispatchedGestureIdBits, -1,
3658 0, 0, mPointerGesture.downTime);
3659
3660 dispatchedGestureIdBits.clear();
3661 } else {
3662 BitSet32 upGestureIdBits;
3663 if (finishPreviousGesture) {
3664 upGestureIdBits = dispatchedGestureIdBits;
3665 } else {
3666 upGestureIdBits.value = dispatchedGestureIdBits.value
3667 & ~mPointerGesture.currentGestureIdBits.value;
3668 }
3669 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003670 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003671
3672 dispatchMotion(when, policyFlags, mPointerSource,
3673 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003674 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3675 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003676 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3677 dispatchedGestureIdBits, id,
3678 0, 0, mPointerGesture.downTime);
3679
3680 dispatchedGestureIdBits.clearBit(id);
3681 }
3682 }
3683 }
3684
3685 // Send motion events for all pointers that moved.
3686 if (moveNeeded) {
3687 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003688 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3689 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003690 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3691 dispatchedGestureIdBits, -1,
3692 0, 0, mPointerGesture.downTime);
3693 }
3694
3695 // Send motion events for all pointers that went down.
3696 if (down) {
3697 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3698 & ~dispatchedGestureIdBits.value);
3699 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003700 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003701 dispatchedGestureIdBits.markBit(id);
3702
Jeff Brownace13b12011-03-09 17:39:48 -08003703 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003704 mPointerGesture.downTime = when;
3705 }
3706
3707 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003708 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003709 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003710 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3711 dispatchedGestureIdBits, id,
3712 0, 0, mPointerGesture.downTime);
3713 }
3714 }
3715
Jeff Brownace13b12011-03-09 17:39:48 -08003716 // Send motion events for hover.
3717 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3718 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003719 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3720 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3721 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003722 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3723 mPointerGesture.currentGestureIdBits, -1,
3724 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003725 } else if (dispatchedGestureIdBits.isEmpty()
3726 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3727 // Synthesize a hover move event after all pointers go up to indicate that
3728 // the pointer is hovering again even if the user is not currently touching
3729 // the touch pad. This ensures that a view will receive a fresh hover enter
3730 // event after a tap.
3731 float x, y;
3732 mPointerController->getPosition(&x, &y);
3733
3734 PointerProperties pointerProperties;
3735 pointerProperties.clear();
3736 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07003737 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07003738
3739 PointerCoords pointerCoords;
3740 pointerCoords.clear();
3741 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3742 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3743
Jeff Brownbe1aa822011-07-27 16:04:54 -07003744 NotifyMotionArgs args(when, getDeviceId(), mPointerSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07003745 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3746 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3747 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003748 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08003749 }
3750
3751 // Update state.
3752 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3753 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003754 mPointerGesture.lastGestureIdBits.clear();
3755 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003756 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3757 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003758 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003759 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003760 mPointerGesture.lastGestureProperties[index].copyFrom(
3761 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003762 mPointerGesture.lastGestureCoords[index].copyFrom(
3763 mPointerGesture.currentGestureCoords[index]);
3764 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003765 }
3766 }
3767}
3768
Jeff Brown79ac9692011-04-19 21:20:10 -07003769bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3770 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003771 *outCancelPreviousGesture = false;
3772 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003773
Jeff Brown79ac9692011-04-19 21:20:10 -07003774 // Handle TAP timeout.
3775 if (isTimeout) {
3776#if DEBUG_GESTURES
3777 LOGD("Gestures: Processing timeout");
3778#endif
3779
3780 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003781 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003782 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003783 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003784 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003785 } else {
3786 // The tap is finished.
3787#if DEBUG_GESTURES
3788 LOGD("Gestures: TAP finished");
3789#endif
3790 *outFinishPreviousGesture = true;
3791
3792 mPointerGesture.activeGestureId = -1;
3793 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3794 mPointerGesture.currentGestureIdBits.clear();
3795
Jeff Brown19c97d462011-06-01 12:33:19 -07003796 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07003797 return true;
3798 }
3799 }
3800
3801 // We did not handle this timeout.
3802 return false;
3803 }
3804
Jeff Brownace13b12011-03-09 17:39:48 -08003805 // Update the velocity tracker.
3806 {
3807 VelocityTracker::Position positions[MAX_POINTERS];
3808 uint32_t count = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003809 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); count++) {
3810 uint32_t id = idBits.clearFirstMarkedBit();
3811 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3812 positions[count].x = pointer.x * mPointerGestureXMovementScale;
3813 positions[count].y = pointer.y * mPointerGestureYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003814 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003815 mPointerGesture.velocityTracker.addMovement(when,
3816 mCurrentRawPointerData.touchingIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08003817 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003818
Jeff Brownace13b12011-03-09 17:39:48 -08003819 // Pick a new active touch id if needed.
3820 // Choose an arbitrary pointer that just went down, if there is one.
3821 // Otherwise choose an arbitrary remaining pointer.
3822 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003823 // We keep the same active touch id for as long as possible.
3824 bool activeTouchChanged = false;
3825 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3826 int32_t activeTouchId = lastActiveTouchId;
3827 if (activeTouchId < 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003828 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07003829 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003830 activeTouchId = mPointerGesture.activeTouchId =
3831 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07003832 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003833 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003834 } else if (!mCurrentRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07003835 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003836 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3837 activeTouchId = mPointerGesture.activeTouchId =
3838 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07003839 } else {
3840 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003841 }
3842 }
3843
Jeff Brownbe1aa822011-07-27 16:04:54 -07003844 uint32_t currentTouchingPointerCount = mCurrentRawPointerData.touchingIdBits.count();
3845 uint32_t lastTouchingPointerCount = mLastRawPointerData.touchingIdBits.count();
3846
Jeff Brownace13b12011-03-09 17:39:48 -08003847 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003848 bool isQuietTime = false;
3849 if (activeTouchId < 0) {
3850 mPointerGesture.resetQuietTime();
3851 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07003852 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07003853 if (!isQuietTime) {
3854 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3855 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3856 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003857 && currentTouchingPointerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07003858 // Enter quiet time when exiting swipe or freeform state.
3859 // This is to prevent accidentally entering the hover state and flinging the
3860 // pointer when finishing a swipe and there is still one pointer left onscreen.
3861 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003862 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brownbe1aa822011-07-27 16:04:54 -07003863 && currentTouchingPointerCount >= 2
3864 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07003865 // Enter quiet time when releasing the button and there are still two or more
3866 // fingers down. This may indicate that one finger was used to press the button
3867 // but it has not gone up yet.
3868 isQuietTime = true;
3869 }
3870 if (isQuietTime) {
3871 mPointerGesture.quietTime = when;
3872 }
Jeff Brownace13b12011-03-09 17:39:48 -08003873 }
3874 }
3875
3876 // Switch states based on button and pointer state.
3877 if (isQuietTime) {
3878 // Case 1: Quiet time. (QUIET)
3879#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003880 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003881 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003882#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003883 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
3884 *outFinishPreviousGesture = true;
3885 }
Jeff Brownace13b12011-03-09 17:39:48 -08003886
3887 mPointerGesture.activeGestureId = -1;
3888 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003889 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003890
Jeff Brown19c97d462011-06-01 12:33:19 -07003891 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003892 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003893 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003894 // The pointer follows the active touch point.
3895 // Emit DOWN, MOVE, UP events at the pointer location.
3896 //
3897 // Only the active touch matters; other fingers are ignored. This policy helps
3898 // to handle the case where the user places a second finger on the touch pad
3899 // to apply the necessary force to depress an integrated button below the surface.
3900 // We don't want the second finger to be delivered to applications.
3901 //
3902 // For this to work well, we need to make sure to track the pointer that is really
3903 // active. If the user first puts one finger down to click then adds another
3904 // finger to drag then the active pointer should switch to the finger that is
3905 // being dragged.
3906#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003907 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003908 "currentTouchingPointerCount=%d", activeTouchId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08003909#endif
3910 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003911 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003912 *outFinishPreviousGesture = true;
3913 mPointerGesture.activeGestureId = 0;
3914 }
3915
3916 // Switch pointers if needed.
3917 // Find the fastest pointer and follow it.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003918 if (activeTouchId >= 0 && currentTouchingPointerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07003919 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07003920 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003921 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3922 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07003923 float vx, vy;
3924 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3925 float speed = hypotf(vx, vy);
3926 if (speed > bestSpeed) {
3927 bestId = id;
3928 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08003929 }
Jeff Brown8d608662010-08-30 03:02:23 -07003930 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003931 }
3932 if (bestId >= 0 && bestId != activeTouchId) {
3933 mPointerGesture.activeTouchId = activeTouchId = bestId;
3934 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003935#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07003936 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3937 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08003938#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003939 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003940 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003941
Jeff Brownbe1aa822011-07-27 16:04:54 -07003942 if (activeTouchId >= 0 && mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
3943 const RawPointerData::Pointer& currentPointer =
3944 mCurrentRawPointerData.pointerForId(activeTouchId);
3945 const RawPointerData::Pointer& lastPointer =
3946 mLastRawPointerData.pointerForId(activeTouchId);
3947 float deltaX = (currentPointer.x - lastPointer.x) * mPointerGestureXMovementScale;
3948 float deltaY = (currentPointer.y - lastPointer.y) * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003949
Jeff Brownbe1aa822011-07-27 16:04:54 -07003950 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003951 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3952
3953 // Move the pointer using a relative motion.
3954 // When using spots, the click will occur at the position of the anchor
3955 // spot and all other spots will move there.
3956 mPointerController->move(deltaX, deltaY);
3957 } else {
3958 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003959 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003960
Jeff Brownace13b12011-03-09 17:39:48 -08003961 float x, y;
3962 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003963
Jeff Brown79ac9692011-04-19 21:20:10 -07003964 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003965 mPointerGesture.currentGestureIdBits.clear();
3966 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3967 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003968 mPointerGesture.currentGestureProperties[0].clear();
3969 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07003970 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003971 mPointerGesture.currentGestureCoords[0].clear();
3972 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3973 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3974 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003975 } else if (currentTouchingPointerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08003976 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003977 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
3978 *outFinishPreviousGesture = true;
3979 }
Jeff Brownace13b12011-03-09 17:39:48 -08003980
Jeff Brown79ac9692011-04-19 21:20:10 -07003981 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003982 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08003983 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003984 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3985 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003986 && lastTouchingPointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003987 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08003988 float x, y;
3989 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003990 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3991 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08003992#if DEBUG_GESTURES
3993 LOGD("Gestures: TAP");
3994#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003995
3996 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07003997 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07003998 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003999
Jeff Brownace13b12011-03-09 17:39:48 -08004000 mPointerGesture.activeGestureId = 0;
4001 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004002 mPointerGesture.currentGestureIdBits.clear();
4003 mPointerGesture.currentGestureIdBits.markBit(
4004 mPointerGesture.activeGestureId);
4005 mPointerGesture.currentGestureIdToIndex[
4006 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004007 mPointerGesture.currentGestureProperties[0].clear();
4008 mPointerGesture.currentGestureProperties[0].id =
4009 mPointerGesture.activeGestureId;
4010 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004011 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004012 mPointerGesture.currentGestureCoords[0].clear();
4013 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004014 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004015 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004016 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004017 mPointerGesture.currentGestureCoords[0].setAxisValue(
4018 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004019
Jeff Brownace13b12011-03-09 17:39:48 -08004020 tapped = true;
4021 } else {
4022#if DEBUG_GESTURES
4023 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004024 x - mPointerGesture.tapX,
4025 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004026#endif
4027 }
4028 } else {
4029#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004030 LOGD("Gestures: Not a TAP, %0.3fms since down",
4031 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004032#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004033 }
Jeff Brownace13b12011-03-09 17:39:48 -08004034 }
Jeff Brown2352b972011-04-12 22:39:53 -07004035
Jeff Brown19c97d462011-06-01 12:33:19 -07004036 mPointerGesture.pointerVelocityControl.reset();
4037
Jeff Brownace13b12011-03-09 17:39:48 -08004038 if (!tapped) {
4039#if DEBUG_GESTURES
4040 LOGD("Gestures: NEUTRAL");
4041#endif
4042 mPointerGesture.activeGestureId = -1;
4043 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004044 mPointerGesture.currentGestureIdBits.clear();
4045 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004046 } else if (currentTouchingPointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004047 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004048 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004049 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4050 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004051 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004052
Jeff Brown79ac9692011-04-19 21:20:10 -07004053 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4054 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004055 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004056 float x, y;
4057 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004058 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4059 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004060 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4061 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004062#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004063 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4064 x - mPointerGesture.tapX,
4065 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004066#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004067 }
4068 } else {
4069#if DEBUG_GESTURES
4070 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4071 (when - mPointerGesture.tapUpTime) * 0.000001f);
4072#endif
4073 }
4074 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4075 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4076 }
Jeff Brownace13b12011-03-09 17:39:48 -08004077
Jeff Brownbe1aa822011-07-27 16:04:54 -07004078 if (mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
4079 const RawPointerData::Pointer& currentPointer =
4080 mCurrentRawPointerData.pointerForId(activeTouchId);
4081 const RawPointerData::Pointer& lastPointer =
4082 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004083 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004084 * mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004085 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004086 * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004087
Jeff Brownbe1aa822011-07-27 16:04:54 -07004088 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004089 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4090
Jeff Brown2352b972011-04-12 22:39:53 -07004091 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004092 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004093 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004094 } else {
4095 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004096 }
4097
Jeff Brown79ac9692011-04-19 21:20:10 -07004098 bool down;
4099 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4100#if DEBUG_GESTURES
4101 LOGD("Gestures: TAP_DRAG");
4102#endif
4103 down = true;
4104 } else {
4105#if DEBUG_GESTURES
4106 LOGD("Gestures: HOVER");
4107#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004108 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4109 *outFinishPreviousGesture = true;
4110 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004111 mPointerGesture.activeGestureId = 0;
4112 down = false;
4113 }
Jeff Brownace13b12011-03-09 17:39:48 -08004114
4115 float x, y;
4116 mPointerController->getPosition(&x, &y);
4117
Jeff Brownace13b12011-03-09 17:39:48 -08004118 mPointerGesture.currentGestureIdBits.clear();
4119 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4120 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004121 mPointerGesture.currentGestureProperties[0].clear();
4122 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4123 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004124 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004125 mPointerGesture.currentGestureCoords[0].clear();
4126 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4127 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004128 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4129 down ? 1.0f : 0.0f);
4130
Jeff Brownbe1aa822011-07-27 16:04:54 -07004131 if (lastTouchingPointerCount == 0 && currentTouchingPointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004132 mPointerGesture.resetTap();
4133 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004134 mPointerGesture.tapX = x;
4135 mPointerGesture.tapY = y;
4136 }
Jeff Brownace13b12011-03-09 17:39:48 -08004137 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004138 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4139 // We need to provide feedback for each finger that goes down so we cannot wait
4140 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004141 //
Jeff Brown2352b972011-04-12 22:39:53 -07004142 // The ambiguous case is deciding what to do when there are two fingers down but they
4143 // have not moved enough to determine whether they are part of a drag or part of a
4144 // freeform gesture, or just a press or long-press at the pointer location.
4145 //
4146 // When there are two fingers we start with the PRESS hypothesis and we generate a
4147 // down at the pointer location.
4148 //
4149 // When the two fingers move enough or when additional fingers are added, we make
4150 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004151 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004152
Jeff Brown214eaf42011-05-26 19:17:02 -07004153 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004154 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004155 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004156 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4157 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004158 *outFinishPreviousGesture = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004159 } else if (!settled && currentTouchingPointerCount > lastTouchingPointerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004160 // Additional pointers have gone down but not yet settled.
4161 // Reset the gesture.
4162#if DEBUG_GESTURES
4163 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004164 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004165 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004166 * 0.000001f);
4167#endif
4168 *outCancelPreviousGesture = true;
4169 } else {
4170 // Continue previous gesture.
4171 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4172 }
4173
4174 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004175 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4176 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004177 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004178 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004179
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004180 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004181#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004182 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4183 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004184 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004185 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004186#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004187 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4188 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004189 &mPointerGesture.referenceTouchY);
4190 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4191 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004192 }
Jeff Brownace13b12011-03-09 17:39:48 -08004193
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004194 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004195 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits.value
4196 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4197 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004198 mPointerGesture.referenceDeltas[id].dx = 0;
4199 mPointerGesture.referenceDeltas[id].dy = 0;
4200 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004201 mPointerGesture.referenceIdBits = mCurrentRawPointerData.touchingIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004202
4203 // Add delta for all fingers and calculate a common movement delta.
4204 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004205 BitSet32 commonIdBits(mLastRawPointerData.touchingIdBits.value
4206 & mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004207 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4208 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004209 uint32_t id = idBits.clearFirstMarkedBit();
4210 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4211 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004212 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4213 delta.dx += cpd.x - lpd.x;
4214 delta.dy += cpd.y - lpd.y;
4215
4216 if (first) {
4217 commonDeltaX = delta.dx;
4218 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004219 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004220 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4221 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4222 }
4223 }
Jeff Brownace13b12011-03-09 17:39:48 -08004224
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004225 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4226 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4227 float dist[MAX_POINTER_ID + 1];
4228 int32_t distOverThreshold = 0;
4229 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004230 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004231 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbe1aa822011-07-27 16:04:54 -07004232 dist[id] = hypotf(delta.dx * mPointerGestureXZoomScale,
4233 delta.dy * mPointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004234 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004235 distOverThreshold += 1;
4236 }
4237 }
4238
4239 // Only transition when at least two pointers have moved further than
4240 // the minimum distance threshold.
4241 if (distOverThreshold >= 2) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004242 if (currentTouchingPointerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004243 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004244#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004245 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004246 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004247#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004248 *outCancelPreviousGesture = true;
4249 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4250 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004251 // There are exactly two pointers.
4252 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4253 uint32_t id1 = idBits.clearFirstMarkedBit();
4254 uint32_t id2 = idBits.firstMarkedBit();
4255 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4256 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4257 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4258 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4259 // There are two pointers but they are too far apart for a SWIPE,
4260 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004261#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004262 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4263 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004264#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004265 *outCancelPreviousGesture = true;
4266 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4267 } else {
4268 // There are two pointers. Wait for both pointers to start moving
4269 // before deciding whether this is a SWIPE or FREEFORM gesture.
4270 float dist1 = dist[id1];
4271 float dist2 = dist[id2];
4272 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4273 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4274 // Calculate the dot product of the displacement vectors.
4275 // When the vectors are oriented in approximately the same direction,
4276 // the angle betweeen them is near zero and the cosine of the angle
4277 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4278 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4279 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
4280 float dx1 = delta1.dx * mPointerGestureXZoomScale;
4281 float dy1 = delta1.dy * mPointerGestureYZoomScale;
4282 float dx2 = delta2.dx * mPointerGestureXZoomScale;
4283 float dy2 = delta2.dy * mPointerGestureYZoomScale;
4284 float dot = dx1 * dx2 + dy1 * dy2;
4285 float cosine = dot / (dist1 * dist2); // denominator always > 0
4286 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4287 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004288#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004289 LOGD("Gestures: PRESS transitioned to SWIPE, "
4290 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4291 "cosine %0.3f >= %0.3f",
4292 dist1, mConfig.pointerGestureMultitouchMinDistance,
4293 dist2, mConfig.pointerGestureMultitouchMinDistance,
4294 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004295#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004296 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4297 } else {
4298 // Pointers are moving in different directions. Switch to FREEFORM.
4299#if DEBUG_GESTURES
4300 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4301 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4302 "cosine %0.3f < %0.3f",
4303 dist1, mConfig.pointerGestureMultitouchMinDistance,
4304 dist2, mConfig.pointerGestureMultitouchMinDistance,
4305 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4306#endif
4307 *outCancelPreviousGesture = true;
4308 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4309 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004310 }
Jeff Brownace13b12011-03-09 17:39:48 -08004311 }
4312 }
Jeff Brownace13b12011-03-09 17:39:48 -08004313 }
4314 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004315 // Switch from SWIPE to FREEFORM if additional pointers go down.
4316 // Cancel previous gesture.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004317 if (currentTouchingPointerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004318#if DEBUG_GESTURES
4319 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004320 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004321#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004322 *outCancelPreviousGesture = true;
4323 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004324 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004325 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004326
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004327 // Move the reference points based on the overall group motion of the fingers
4328 // except in PRESS mode while waiting for a transition to occur.
4329 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4330 && (commonDeltaX || commonDeltaY)) {
4331 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004332 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004333 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004334 delta.dx = 0;
4335 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004336 }
4337
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004338 mPointerGesture.referenceTouchX += commonDeltaX;
4339 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004340
Jeff Brownbe1aa822011-07-27 16:04:54 -07004341 commonDeltaX *= mPointerGestureXMovementScale;
4342 commonDeltaY *= mPointerGestureYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004343
Jeff Brownbe1aa822011-07-27 16:04:54 -07004344 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004345 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004346
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004347 mPointerGesture.referenceGestureX += commonDeltaX;
4348 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004349 }
4350
4351 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004352 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4353 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4354 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004355#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004356 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004357 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004358 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004359#endif
4360 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4361
4362 mPointerGesture.currentGestureIdBits.clear();
4363 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4364 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004365 mPointerGesture.currentGestureProperties[0].clear();
4366 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4367 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004368 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004369 mPointerGesture.currentGestureCoords[0].clear();
4370 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4371 mPointerGesture.referenceGestureX);
4372 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4373 mPointerGesture.referenceGestureY);
4374 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004375 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4376 // FREEFORM mode.
4377#if DEBUG_GESTURES
4378 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4379 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004380 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004381#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004382 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004383
Jeff Brownace13b12011-03-09 17:39:48 -08004384 mPointerGesture.currentGestureIdBits.clear();
4385
4386 BitSet32 mappedTouchIdBits;
4387 BitSet32 usedGestureIdBits;
4388 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4389 // Initially, assign the active gesture id to the active touch point
4390 // if there is one. No other touch id bits are mapped yet.
4391 if (!*outCancelPreviousGesture) {
4392 mappedTouchIdBits.markBit(activeTouchId);
4393 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4394 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4395 mPointerGesture.activeGestureId;
4396 } else {
4397 mPointerGesture.activeGestureId = -1;
4398 }
4399 } else {
4400 // Otherwise, assume we mapped all touches from the previous frame.
4401 // Reuse all mappings that are still applicable.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004402 mappedTouchIdBits.value = mLastRawPointerData.touchingIdBits.value
4403 & mCurrentRawPointerData.touchingIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004404 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4405
4406 // Check whether we need to choose a new active gesture id because the
4407 // current went went up.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004408 for (BitSet32 upTouchIdBits(mLastRawPointerData.touchingIdBits.value
4409 & ~mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004410 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004411 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004412 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4413 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4414 mPointerGesture.activeGestureId = -1;
4415 break;
4416 }
4417 }
4418 }
4419
4420#if DEBUG_GESTURES
4421 LOGD("Gestures: FREEFORM follow up "
4422 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4423 "activeGestureId=%d",
4424 mappedTouchIdBits.value, usedGestureIdBits.value,
4425 mPointerGesture.activeGestureId);
4426#endif
4427
Jeff Brownbe1aa822011-07-27 16:04:54 -07004428 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4429 for (uint32_t i = 0; i < currentTouchingPointerCount; i++) {
4430 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004431 uint32_t gestureId;
4432 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004433 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004434 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4435#if DEBUG_GESTURES
4436 LOGD("Gestures: FREEFORM "
4437 "new mapping for touch id %d -> gesture id %d",
4438 touchId, gestureId);
4439#endif
4440 } else {
4441 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4442#if DEBUG_GESTURES
4443 LOGD("Gestures: FREEFORM "
4444 "existing mapping for touch id %d -> gesture id %d",
4445 touchId, gestureId);
4446#endif
4447 }
4448 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4449 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4450
Jeff Brownbe1aa822011-07-27 16:04:54 -07004451 const RawPointerData::Pointer& pointer =
4452 mCurrentRawPointerData.pointerForId(touchId);
4453 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
4454 * mPointerGestureXZoomScale;
4455 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
4456 * mPointerGestureYZoomScale;
4457 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004458
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004459 mPointerGesture.currentGestureProperties[i].clear();
4460 mPointerGesture.currentGestureProperties[i].id = gestureId;
4461 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004462 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004463 mPointerGesture.currentGestureCoords[i].clear();
4464 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004465 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004466 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004467 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004468 mPointerGesture.currentGestureCoords[i].setAxisValue(
4469 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4470 }
4471
4472 if (mPointerGesture.activeGestureId < 0) {
4473 mPointerGesture.activeGestureId =
4474 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4475#if DEBUG_GESTURES
4476 LOGD("Gestures: FREEFORM new "
4477 "activeGestureId=%d", mPointerGesture.activeGestureId);
4478#endif
4479 }
Jeff Brown2352b972011-04-12 22:39:53 -07004480 }
Jeff Brownace13b12011-03-09 17:39:48 -08004481 }
4482
Jeff Brownbe1aa822011-07-27 16:04:54 -07004483 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004484
Jeff Brownace13b12011-03-09 17:39:48 -08004485#if DEBUG_GESTURES
4486 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004487 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4488 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004489 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004490 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4491 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004492 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004493 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004494 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004495 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004496 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004497 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4498 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4499 id, index, properties.toolType,
4500 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004501 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4502 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4503 }
4504 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004505 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004506 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004507 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004508 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004509 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4510 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4511 id, index, properties.toolType,
4512 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004513 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4514 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4515 }
4516#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004517 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004518}
4519
4520void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004521 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4522 const PointerProperties* properties, const PointerCoords* coords,
4523 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004524 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4525 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004526 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004527 uint32_t pointerCount = 0;
4528 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004529 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004530 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004531 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004532 pointerCoords[pointerCount].copyFrom(coords[index]);
4533
4534 if (changedId >= 0 && id == uint32_t(changedId)) {
4535 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4536 }
4537
4538 pointerCount += 1;
4539 }
4540
Jeff Brownb6110c22011-04-01 16:15:13 -07004541 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004542
4543 if (changedId >= 0 && pointerCount == 1) {
4544 // Replace initial down and final up action.
4545 // We can compare the action without masking off the changed pointer index
4546 // because we know the index is 0.
4547 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4548 action = AMOTION_EVENT_ACTION_DOWN;
4549 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4550 action = AMOTION_EVENT_ACTION_UP;
4551 } else {
4552 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004553 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004554 }
4555 }
4556
Jeff Brownbe1aa822011-07-27 16:04:54 -07004557 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004558 action, flags, metaState, buttonState, edgeFlags,
4559 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004560 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004561}
4562
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004563bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004564 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004565 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4566 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004567 bool changed = false;
4568 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004569 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004570 uint32_t inIndex = inIdToIndex[id];
4571 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004572
4573 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004574 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004575 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004576 PointerCoords& curOutCoords = outCoords[outIndex];
4577
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004578 if (curInProperties != curOutProperties) {
4579 curOutProperties.copyFrom(curInProperties);
4580 changed = true;
4581 }
4582
Jeff Brownace13b12011-03-09 17:39:48 -08004583 if (curInCoords != curOutCoords) {
4584 curOutCoords.copyFrom(curInCoords);
4585 changed = true;
4586 }
4587 }
4588 return changed;
4589}
4590
4591void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004592 if (mPointerController != NULL) {
4593 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4594 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004595}
4596
Jeff Brownbe1aa822011-07-27 16:04:54 -07004597bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
4598 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
4599 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004600}
4601
Jeff Brownbe1aa822011-07-27 16:04:54 -07004602const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07004603 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004604 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07004605 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004606 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004607
4608#if DEBUG_VIRTUAL_KEYS
4609 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4610 "left=%d, top=%d, right=%d, bottom=%d",
4611 x, y,
4612 virtualKey.keyCode, virtualKey.scanCode,
4613 virtualKey.hitLeft, virtualKey.hitTop,
4614 virtualKey.hitRight, virtualKey.hitBottom);
4615#endif
4616
4617 if (virtualKey.isHit(x, y)) {
4618 return & virtualKey;
4619 }
4620 }
4621
4622 return NULL;
4623}
4624
Jeff Brownbe1aa822011-07-27 16:04:54 -07004625void TouchInputMapper::assignPointerIds() {
4626 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4627 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
4628
4629 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004630
4631 if (currentPointerCount == 0) {
4632 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004633 return;
4634 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004635
Jeff Brownbe1aa822011-07-27 16:04:54 -07004636 if (lastPointerCount == 0) {
4637 // All pointers are new.
4638 for (uint32_t i = 0; i < currentPointerCount; i++) {
4639 uint32_t id = i;
4640 mCurrentRawPointerData.pointers[i].id = id;
4641 mCurrentRawPointerData.idToIndex[id] = i;
4642 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
4643 }
4644 return;
4645 }
4646
4647 if (currentPointerCount == 1 && lastPointerCount == 1
4648 && mCurrentRawPointerData.pointers[0].toolType
4649 == mLastRawPointerData.pointers[0].toolType) {
4650 // Only one pointer and no change in count so it must have the same id as before.
4651 uint32_t id = mLastRawPointerData.pointers[0].id;
4652 mCurrentRawPointerData.pointers[0].id = id;
4653 mCurrentRawPointerData.idToIndex[id] = 0;
4654 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
4655 return;
4656 }
4657
4658 // General case.
4659 // We build a heap of squared euclidean distances between current and last pointers
4660 // associated with the current and last pointer indices. Then, we find the best
4661 // match (by distance) for each current pointer.
4662 // The pointers must have the same tool type but it is possible for them to
4663 // transition from hovering to touching or vice-versa while retaining the same id.
4664 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4665
4666 uint32_t heapSize = 0;
4667 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4668 currentPointerIndex++) {
4669 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4670 lastPointerIndex++) {
4671 const RawPointerData::Pointer& currentPointer =
4672 mCurrentRawPointerData.pointers[currentPointerIndex];
4673 const RawPointerData::Pointer& lastPointer =
4674 mLastRawPointerData.pointers[lastPointerIndex];
4675 if (currentPointer.toolType == lastPointer.toolType) {
4676 int64_t deltaX = currentPointer.x - lastPointer.x;
4677 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004678
4679 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4680
4681 // Insert new element into the heap (sift up).
4682 heap[heapSize].currentPointerIndex = currentPointerIndex;
4683 heap[heapSize].lastPointerIndex = lastPointerIndex;
4684 heap[heapSize].distance = distance;
4685 heapSize += 1;
4686 }
4687 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004688 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004689
Jeff Brownbe1aa822011-07-27 16:04:54 -07004690 // Heapify
4691 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4692 startIndex -= 1;
4693 for (uint32_t parentIndex = startIndex; ;) {
4694 uint32_t childIndex = parentIndex * 2 + 1;
4695 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004696 break;
4697 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004698
4699 if (childIndex + 1 < heapSize
4700 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4701 childIndex += 1;
4702 }
4703
4704 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4705 break;
4706 }
4707
4708 swap(heap[parentIndex], heap[childIndex]);
4709 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004710 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004711 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004712
4713#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07004714 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
4715 for (size_t i = 0; i < heapSize; i++) {
4716 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4717 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4718 heap[i].distance);
4719 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004720#endif
4721
Jeff Brownbe1aa822011-07-27 16:04:54 -07004722 // Pull matches out by increasing order of distance.
4723 // To avoid reassigning pointers that have already been matched, the loop keeps track
4724 // of which last and current pointers have been matched using the matchedXXXBits variables.
4725 // It also tracks the used pointer id bits.
4726 BitSet32 matchedLastBits(0);
4727 BitSet32 matchedCurrentBits(0);
4728 BitSet32 usedIdBits(0);
4729 bool first = true;
4730 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
4731 while (heapSize > 0) {
4732 if (first) {
4733 // The first time through the loop, we just consume the root element of
4734 // the heap (the one with smallest distance).
4735 first = false;
4736 } else {
4737 // Previous iterations consumed the root element of the heap.
4738 // Pop root element off of the heap (sift down).
4739 heap[0] = heap[heapSize];
4740 for (uint32_t parentIndex = 0; ;) {
4741 uint32_t childIndex = parentIndex * 2 + 1;
4742 if (childIndex >= heapSize) {
4743 break;
4744 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004745
Jeff Brownbe1aa822011-07-27 16:04:54 -07004746 if (childIndex + 1 < heapSize
4747 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4748 childIndex += 1;
4749 }
4750
4751 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4752 break;
4753 }
4754
4755 swap(heap[parentIndex], heap[childIndex]);
4756 parentIndex = childIndex;
4757 }
4758
4759#if DEBUG_POINTER_ASSIGNMENT
4760 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
4761 for (size_t i = 0; i < heapSize; i++) {
4762 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4763 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4764 heap[i].distance);
4765 }
4766#endif
4767 }
4768
4769 heapSize -= 1;
4770
4771 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4772 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4773
4774 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4775 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4776
4777 matchedCurrentBits.markBit(currentPointerIndex);
4778 matchedLastBits.markBit(lastPointerIndex);
4779
4780 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
4781 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
4782 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
4783 mCurrentRawPointerData.markIdBit(id,
4784 mCurrentRawPointerData.isHovering(currentPointerIndex));
4785 usedIdBits.markBit(id);
4786
4787#if DEBUG_POINTER_ASSIGNMENT
4788 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4789 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4790#endif
4791 break;
4792 }
4793 }
4794
4795 // Assign fresh ids to pointers that were not matched in the process.
4796 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
4797 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
4798 uint32_t id = usedIdBits.markFirstUnmarkedBit();
4799
4800 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
4801 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
4802 mCurrentRawPointerData.markIdBit(id,
4803 mCurrentRawPointerData.isHovering(currentPointerIndex));
4804
4805#if DEBUG_POINTER_ASSIGNMENT
4806 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
4807 currentPointerIndex, id);
4808#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07004809 }
4810}
4811
Jeff Brown6d0fec22010-07-23 21:28:06 -07004812int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004813 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
4814 return AKEY_STATE_VIRTUAL;
4815 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004816
Jeff Brownbe1aa822011-07-27 16:04:54 -07004817 size_t numVirtualKeys = mVirtualKeys.size();
4818 for (size_t i = 0; i < numVirtualKeys; i++) {
4819 const VirtualKey& virtualKey = mVirtualKeys[i];
4820 if (virtualKey.keyCode == keyCode) {
4821 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004822 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004823 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004824
4825 return AKEY_STATE_UNKNOWN;
4826}
4827
4828int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004829 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
4830 return AKEY_STATE_VIRTUAL;
4831 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004832
Jeff Brownbe1aa822011-07-27 16:04:54 -07004833 size_t numVirtualKeys = mVirtualKeys.size();
4834 for (size_t i = 0; i < numVirtualKeys; i++) {
4835 const VirtualKey& virtualKey = mVirtualKeys[i];
4836 if (virtualKey.scanCode == scanCode) {
4837 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004838 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004839 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004840
4841 return AKEY_STATE_UNKNOWN;
4842}
4843
4844bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4845 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004846 size_t numVirtualKeys = mVirtualKeys.size();
4847 for (size_t i = 0; i < numVirtualKeys; i++) {
4848 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004849
Jeff Brownbe1aa822011-07-27 16:04:54 -07004850 for (size_t i = 0; i < numCodes; i++) {
4851 if (virtualKey.keyCode == keyCodes[i]) {
4852 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004853 }
4854 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004855 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004856
4857 return true;
4858}
4859
4860
4861// --- SingleTouchInputMapper ---
4862
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004863SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
4864 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004865 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004866}
4867
4868SingleTouchInputMapper::~SingleTouchInputMapper() {
4869}
4870
Jeff Brown80fd47c2011-05-24 01:07:44 -07004871void SingleTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07004872 mCursorButtonAccumulator.clearButtons();
4873 mTouchButtonAccumulator.clearButtons();
4874 mSingleTouchMotionAccumulator.clearAbsoluteAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004875}
4876
4877void SingleTouchInputMapper::reset() {
4878 TouchInputMapper::reset();
4879
Jeff Brown80fd47c2011-05-24 01:07:44 -07004880 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004881 }
4882
4883void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07004884 mCursorButtonAccumulator.process(rawEvent);
4885 mTouchButtonAccumulator.process(rawEvent);
4886 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004887
Jeff Brown49754db2011-07-01 17:37:58 -07004888 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
4889 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004890 }
4891}
4892
4893void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004894 mCurrentRawPointerData.clear();
4895 mCurrentButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004896
Jeff Brownd87c6d52011-08-10 14:55:59 -07004897 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004898 mCurrentRawPointerData.pointerCount = 1;
4899 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004900
Jeff Brownbe1aa822011-07-27 16:04:54 -07004901 bool isHovering = mTouchButtonAccumulator.isHovering()
4902 || mSingleTouchMotionAccumulator.getAbsoluteDistance() > 0;
4903 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07004904
Jeff Brownbe1aa822011-07-27 16:04:54 -07004905 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07004906 outPointer.id = 0;
4907 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
4908 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
4909 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
4910 outPointer.touchMajor = 0;
4911 outPointer.touchMinor = 0;
4912 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
4913 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
4914 outPointer.orientation = 0;
4915 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
4916 outPointer.toolType = mTouchButtonAccumulator.getToolType();
4917 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
4918 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
4919 }
4920 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004921 }
4922
Jeff Brownd87c6d52011-08-10 14:55:59 -07004923 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
4924 | mCursorButtonAccumulator.getButtonState();
4925
Jeff Brown6d0fec22010-07-23 21:28:06 -07004926 syncTouch(when, true);
4927}
4928
Jeff Brownbe1aa822011-07-27 16:04:54 -07004929void SingleTouchInputMapper::configureRawPointerAxes() {
4930 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004931
Jeff Brown49754db2011-07-01 17:37:58 -07004932 mTouchButtonAccumulator.configure(getDevice());
4933
Jeff Brownbe1aa822011-07-27 16:04:54 -07004934 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
4935 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
4936 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
4937 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
4938 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004939}
4940
4941
4942// --- MultiTouchInputMapper ---
4943
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004944MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07004945 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004946}
4947
4948MultiTouchInputMapper::~MultiTouchInputMapper() {
4949}
4950
Jeff Brown80fd47c2011-05-24 01:07:44 -07004951void MultiTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07004952 mCursorButtonAccumulator.clearButtons();
4953 mTouchButtonAccumulator.clearButtons();
Jeff Brown6894a292011-07-01 17:59:27 -07004954 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07004955
Jeff Brown49754db2011-07-01 17:37:58 -07004956 if (mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
Jeff Brown2717eff2011-06-30 23:53:07 -07004957 // Query the driver for the current slot index and use it as the initial slot
4958 // before we start reading events from the device. It is possible that the
4959 // current slot index will not be the same as it was when the first event was
4960 // written into the evdev buffer, which means the input mapper could start
4961 // out of sync with the initial state of the events in the evdev buffer.
4962 // In the extremely unlikely case that this happens, the data from
4963 // two slots will be confused until the next ABS_MT_SLOT event is received.
4964 // This can cause the touch point to "jump", but at least there will be
4965 // no stuck touches.
Jeff Brown49754db2011-07-01 17:37:58 -07004966 int32_t initialSlot;
Jeff Brown2717eff2011-06-30 23:53:07 -07004967 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
Jeff Brown49754db2011-07-01 17:37:58 -07004968 &initialSlot);
Jeff Brown2717eff2011-06-30 23:53:07 -07004969 if (status) {
4970 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown49754db2011-07-01 17:37:58 -07004971 initialSlot = -1;
Jeff Brown2717eff2011-06-30 23:53:07 -07004972 }
Jeff Brown49754db2011-07-01 17:37:58 -07004973 mMultiTouchMotionAccumulator.clearSlots(initialSlot);
4974 } else {
4975 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown2717eff2011-06-30 23:53:07 -07004976 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004977}
4978
4979void MultiTouchInputMapper::reset() {
4980 TouchInputMapper::reset();
4981
Jeff Brown80fd47c2011-05-24 01:07:44 -07004982 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004983}
4984
4985void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07004986 mCursorButtonAccumulator.process(rawEvent);
4987 mTouchButtonAccumulator.process(rawEvent);
4988 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08004989
Jeff Brown49754db2011-07-01 17:37:58 -07004990 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
4991 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004992 }
4993}
4994
4995void MultiTouchInputMapper::sync(nsecs_t when) {
Jeff Brown49754db2011-07-01 17:37:58 -07004996 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07004997 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004998 bool havePointerIds = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004999 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005000
Jeff Brownbe1aa822011-07-27 16:04:54 -07005001 mCurrentRawPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005002
Jeff Brown80fd47c2011-05-24 01:07:44 -07005003 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005004 const MultiTouchMotionAccumulator::Slot* inSlot =
5005 mMultiTouchMotionAccumulator.getSlot(inIndex);
5006 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005007 continue;
5008 }
5009
Jeff Brown80fd47c2011-05-24 01:07:44 -07005010 if (outCount >= MAX_POINTERS) {
5011#if DEBUG_POINTERS
5012 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5013 "ignoring the rest.",
5014 getDeviceName().string(), MAX_POINTERS);
5015#endif
5016 break; // too many fingers!
5017 }
5018
Jeff Brownbe1aa822011-07-27 16:04:54 -07005019 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005020 outPointer.x = inSlot->getX();
5021 outPointer.y = inSlot->getY();
5022 outPointer.pressure = inSlot->getPressure();
5023 outPointer.touchMajor = inSlot->getTouchMajor();
5024 outPointer.touchMinor = inSlot->getTouchMinor();
5025 outPointer.toolMajor = inSlot->getToolMajor();
5026 outPointer.toolMinor = inSlot->getToolMinor();
5027 outPointer.orientation = inSlot->getOrientation();
5028 outPointer.distance = inSlot->getDistance();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005029
Jeff Brown49754db2011-07-01 17:37:58 -07005030 outPointer.toolType = inSlot->getToolType();
5031 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5032 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5033 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5034 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5035 }
Jeff Brown8d608662010-08-30 03:02:23 -07005036 }
5037
Jeff Brownbe1aa822011-07-27 16:04:54 -07005038 bool isHovering = mTouchButtonAccumulator.isHovering()
Jeff Brown49754db2011-07-01 17:37:58 -07005039 || inSlot->getDistance() > 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005040 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005041
Jeff Brown8d608662010-08-30 03:02:23 -07005042 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005043 if (havePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005044 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005045 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005046 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005047 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005048 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005049 if (mPointerTrackingIdMap[n] == trackingId) {
5050 id = n;
5051 }
5052 }
5053
5054 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005055 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005056 mPointerTrackingIdMap[id] = trackingId;
5057 }
5058 }
5059 if (id < 0) {
5060 havePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005061 mCurrentRawPointerData.clearIdBits();
5062 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005063 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005064 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005065 mCurrentRawPointerData.idToIndex[id] = outCount;
5066 mCurrentRawPointerData.markIdBit(id, isHovering);
5067 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005068 }
5069 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005070
Jeff Brown6d0fec22010-07-23 21:28:06 -07005071 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005072 }
5073
Jeff Brownbe1aa822011-07-27 16:04:54 -07005074 mCurrentRawPointerData.pointerCount = outCount;
5075 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
5076 | mCursorButtonAccumulator.getButtonState();
Jeff Brownace13b12011-03-09 17:39:48 -08005077
Jeff Brownbe1aa822011-07-27 16:04:54 -07005078 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005079
Jeff Brown6d0fec22010-07-23 21:28:06 -07005080 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005081
Jeff Brown49754db2011-07-01 17:37:58 -07005082 if (!mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
5083 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown441a9c22011-06-02 18:22:25 -07005084 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005085}
5086
Jeff Brownbe1aa822011-07-27 16:04:54 -07005087void MultiTouchInputMapper::configureRawPointerAxes() {
5088 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005089
Jeff Brown49754db2011-07-01 17:37:58 -07005090 mTouchButtonAccumulator.configure(getDevice());
5091
Jeff Brownbe1aa822011-07-27 16:04:54 -07005092 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5093 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5094 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5095 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5096 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5097 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5098 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5099 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5100 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5101 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5102 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005103
Jeff Brownbe1aa822011-07-27 16:04:54 -07005104 if (mRawPointerAxes.trackingId.valid
5105 && mRawPointerAxes.slot.valid
5106 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5107 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005108 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005109 LOGW("MultiTouch Device %s reported %d slots but the framework "
5110 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005111 getDeviceName().string(), slotCount, MAX_SLOTS);
5112 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005113 }
Jeff Brown49754db2011-07-01 17:37:58 -07005114 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005115 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005116 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005117 }
5118
Jeff Brown2717eff2011-06-30 23:53:07 -07005119 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005120}
5121
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005122
Jeff Browncb1404e2011-01-15 18:14:15 -08005123// --- JoystickInputMapper ---
5124
5125JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5126 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005127}
5128
5129JoystickInputMapper::~JoystickInputMapper() {
5130}
5131
5132uint32_t JoystickInputMapper::getSources() {
5133 return AINPUT_SOURCE_JOYSTICK;
5134}
5135
5136void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5137 InputMapper::populateDeviceInfo(info);
5138
Jeff Brown6f2fba42011-02-19 01:08:02 -08005139 for (size_t i = 0; i < mAxes.size(); i++) {
5140 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005141 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5142 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005143 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005144 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5145 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005146 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005147 }
5148}
5149
5150void JoystickInputMapper::dump(String8& dump) {
5151 dump.append(INDENT2 "Joystick Input Mapper:\n");
5152
Jeff Brown6f2fba42011-02-19 01:08:02 -08005153 dump.append(INDENT3 "Axes:\n");
5154 size_t numAxes = mAxes.size();
5155 for (size_t i = 0; i < numAxes; i++) {
5156 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005157 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005158 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005159 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005160 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005161 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005162 }
Jeff Brown85297452011-03-04 13:07:49 -08005163 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5164 label = getAxisLabel(axis.axisInfo.highAxis);
5165 if (label) {
5166 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5167 } else {
5168 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5169 axis.axisInfo.splitValue);
5170 }
5171 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5172 dump.append(" (invert)");
5173 }
5174
5175 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5176 axis.min, axis.max, axis.flat, axis.fuzz);
5177 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5178 "highScale=%0.5f, highOffset=%0.5f\n",
5179 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005180 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5181 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005182 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005183 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005184 }
5185}
5186
Jeff Brown474dcb52011-06-14 20:22:50 -07005187void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5188 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005189
Jeff Brown474dcb52011-06-14 20:22:50 -07005190 if (!changes) { // first time only
5191 // Collect all axes.
5192 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5193 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005194 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005195 if (rawAxisInfo.valid) {
5196 // Map axis.
5197 AxisInfo axisInfo;
5198 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5199 if (!explicitlyMapped) {
5200 // Axis is not explicitly mapped, will choose a generic axis later.
5201 axisInfo.mode = AxisInfo::MODE_NORMAL;
5202 axisInfo.axis = -1;
5203 }
5204
5205 // Apply flat override.
5206 int32_t rawFlat = axisInfo.flatOverride < 0
5207 ? rawAxisInfo.flat : axisInfo.flatOverride;
5208
5209 // Calculate scaling factors and limits.
5210 Axis axis;
5211 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5212 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5213 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5214 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5215 scale, 0.0f, highScale, 0.0f,
5216 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5217 } else if (isCenteredAxis(axisInfo.axis)) {
5218 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5219 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5220 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5221 scale, offset, scale, offset,
5222 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5223 } else {
5224 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5225 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5226 scale, 0.0f, scale, 0.0f,
5227 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5228 }
5229
5230 // To eliminate noise while the joystick is at rest, filter out small variations
5231 // in axis values up front.
5232 axis.filter = axis.flat * 0.25f;
5233
5234 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005235 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005236 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005237
Jeff Brown474dcb52011-06-14 20:22:50 -07005238 // If there are too many axes, start dropping them.
5239 // Prefer to keep explicitly mapped axes.
5240 if (mAxes.size() > PointerCoords::MAX_AXES) {
5241 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5242 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5243 pruneAxes(true);
5244 pruneAxes(false);
5245 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005246
Jeff Brown474dcb52011-06-14 20:22:50 -07005247 // Assign generic axis ids to remaining axes.
5248 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5249 size_t numAxes = mAxes.size();
5250 for (size_t i = 0; i < numAxes; i++) {
5251 Axis& axis = mAxes.editValueAt(i);
5252 if (axis.axisInfo.axis < 0) {
5253 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5254 && haveAxis(nextGenericAxisId)) {
5255 nextGenericAxisId += 1;
5256 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005257
Jeff Brown474dcb52011-06-14 20:22:50 -07005258 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5259 axis.axisInfo.axis = nextGenericAxisId;
5260 nextGenericAxisId += 1;
5261 } else {
5262 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5263 "have already been assigned to other axes.",
5264 getDeviceName().string(), mAxes.keyAt(i));
5265 mAxes.removeItemsAt(i--);
5266 numAxes -= 1;
5267 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005268 }
5269 }
5270 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005271}
5272
Jeff Brown85297452011-03-04 13:07:49 -08005273bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005274 size_t numAxes = mAxes.size();
5275 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005276 const Axis& axis = mAxes.valueAt(i);
5277 if (axis.axisInfo.axis == axisId
5278 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5279 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005280 return true;
5281 }
5282 }
5283 return false;
5284}
Jeff Browncb1404e2011-01-15 18:14:15 -08005285
Jeff Brown6f2fba42011-02-19 01:08:02 -08005286void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5287 size_t i = mAxes.size();
5288 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5289 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5290 continue;
5291 }
5292 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5293 getDeviceName().string(), mAxes.keyAt(i));
5294 mAxes.removeItemsAt(i);
5295 }
5296}
5297
5298bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5299 switch (axis) {
5300 case AMOTION_EVENT_AXIS_X:
5301 case AMOTION_EVENT_AXIS_Y:
5302 case AMOTION_EVENT_AXIS_Z:
5303 case AMOTION_EVENT_AXIS_RX:
5304 case AMOTION_EVENT_AXIS_RY:
5305 case AMOTION_EVENT_AXIS_RZ:
5306 case AMOTION_EVENT_AXIS_HAT_X:
5307 case AMOTION_EVENT_AXIS_HAT_Y:
5308 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005309 case AMOTION_EVENT_AXIS_RUDDER:
5310 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005311 return true;
5312 default:
5313 return false;
5314 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005315}
5316
5317void JoystickInputMapper::reset() {
5318 // Recenter all axes.
5319 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005320
Jeff Brown6f2fba42011-02-19 01:08:02 -08005321 size_t numAxes = mAxes.size();
5322 for (size_t i = 0; i < numAxes; i++) {
5323 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005324 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005325 }
5326
5327 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005328
5329 InputMapper::reset();
5330}
5331
5332void JoystickInputMapper::process(const RawEvent* rawEvent) {
5333 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005334 case EV_ABS: {
5335 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5336 if (index >= 0) {
5337 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005338 float newValue, highNewValue;
5339 switch (axis.axisInfo.mode) {
5340 case AxisInfo::MODE_INVERT:
5341 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5342 * axis.scale + axis.offset;
5343 highNewValue = 0.0f;
5344 break;
5345 case AxisInfo::MODE_SPLIT:
5346 if (rawEvent->value < axis.axisInfo.splitValue) {
5347 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5348 * axis.scale + axis.offset;
5349 highNewValue = 0.0f;
5350 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5351 newValue = 0.0f;
5352 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5353 * axis.highScale + axis.highOffset;
5354 } else {
5355 newValue = 0.0f;
5356 highNewValue = 0.0f;
5357 }
5358 break;
5359 default:
5360 newValue = rawEvent->value * axis.scale + axis.offset;
5361 highNewValue = 0.0f;
5362 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005363 }
Jeff Brown85297452011-03-04 13:07:49 -08005364 axis.newValue = newValue;
5365 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005366 }
5367 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005368 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005369
5370 case EV_SYN:
5371 switch (rawEvent->scanCode) {
5372 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005373 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005374 break;
5375 }
5376 break;
5377 }
5378}
5379
Jeff Brown6f2fba42011-02-19 01:08:02 -08005380void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005381 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005382 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005383 }
5384
5385 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005386 int32_t buttonState = 0;
5387
5388 PointerProperties pointerProperties;
5389 pointerProperties.clear();
5390 pointerProperties.id = 0;
5391 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005392
Jeff Brown6f2fba42011-02-19 01:08:02 -08005393 PointerCoords pointerCoords;
5394 pointerCoords.clear();
5395
5396 size_t numAxes = mAxes.size();
5397 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005398 const Axis& axis = mAxes.valueAt(i);
5399 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5400 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5401 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5402 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005403 }
5404
Jeff Brown56194eb2011-03-02 19:23:13 -08005405 // Moving a joystick axis should not wake the devide because joysticks can
5406 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5407 // button will likely wake the device.
5408 // TODO: Use the input device configuration to control this behavior more finely.
5409 uint32_t policyFlags = 0;
5410
Jeff Brownbe1aa822011-07-27 16:04:54 -07005411 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005412 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5413 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005414 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005415}
5416
Jeff Brown85297452011-03-04 13:07:49 -08005417bool JoystickInputMapper::filterAxes(bool force) {
5418 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005419 size_t numAxes = mAxes.size();
5420 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005421 Axis& axis = mAxes.editValueAt(i);
5422 if (force || hasValueChangedSignificantly(axis.filter,
5423 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5424 axis.currentValue = axis.newValue;
5425 atLeastOneSignificantChange = true;
5426 }
5427 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5428 if (force || hasValueChangedSignificantly(axis.filter,
5429 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5430 axis.highCurrentValue = axis.highNewValue;
5431 atLeastOneSignificantChange = true;
5432 }
5433 }
5434 }
5435 return atLeastOneSignificantChange;
5436}
5437
5438bool JoystickInputMapper::hasValueChangedSignificantly(
5439 float filter, float newValue, float currentValue, float min, float max) {
5440 if (newValue != currentValue) {
5441 // Filter out small changes in value unless the value is converging on the axis
5442 // bounds or center point. This is intended to reduce the amount of information
5443 // sent to applications by particularly noisy joysticks (such as PS3).
5444 if (fabs(newValue - currentValue) > filter
5445 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5446 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5447 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5448 return true;
5449 }
5450 }
5451 return false;
5452}
5453
5454bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5455 float filter, float newValue, float currentValue, float thresholdValue) {
5456 float newDistance = fabs(newValue - thresholdValue);
5457 if (newDistance < filter) {
5458 float oldDistance = fabs(currentValue - thresholdValue);
5459 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005460 return true;
5461 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005462 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005463 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005464}
5465
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005466} // namespace android