blob: ea9da3914699c996720240e99846687f18366da9 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac02010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac02010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac02010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac02010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown6d0fec22010-07-23 21:28:06 -0700128static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
129 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
130}
131
Jeff Brownefd32662011-03-08 15:13:06 -0800132static uint32_t getButtonStateForScanCode(int32_t scanCode) {
133 // Currently all buttons are mapped to the primary button.
134 switch (scanCode) {
135 case BTN_LEFT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700136 return AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800137 case BTN_RIGHT:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700138 case BTN_STYLUS:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700139 return AMOTION_EVENT_BUTTON_SECONDARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800140 case BTN_MIDDLE:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700141 case BTN_STYLUS2:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700142 return AMOTION_EVENT_BUTTON_TERTIARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800143 case BTN_SIDE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700144 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800145 case BTN_FORWARD:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700146 case BTN_EXTRA:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700147 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800148 case BTN_BACK:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700149 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800150 case BTN_TASK:
Jeff Brownefd32662011-03-08 15:13:06 -0800151 default:
152 return 0;
153 }
154}
155
156// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700157// button states. This determines whether the event is reported as a touch event.
158static bool isPointerDown(int32_t buttonState) {
159 return buttonState &
160 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700161 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800162}
163
Jeff Brown2352b972011-04-12 22:39:53 -0700164static float calculateCommonVector(float a, float b) {
165 if (a > 0 && b > 0) {
166 return a < b ? a : b;
167 } else if (a < 0 && b < 0) {
168 return a > b ? a : b;
169 } else {
170 return 0;
171 }
172}
173
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700174static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
175 nsecs_t when, int32_t deviceId, uint32_t source,
176 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
177 int32_t buttonState, int32_t keyCode) {
178 if (
179 (action == AKEY_EVENT_ACTION_DOWN
180 && !(lastButtonState & buttonState)
181 && (currentButtonState & buttonState))
182 || (action == AKEY_EVENT_ACTION_UP
183 && (lastButtonState & buttonState)
184 && !(currentButtonState & buttonState))) {
185 context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags,
186 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
187 }
188}
189
190static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
191 nsecs_t when, int32_t deviceId, uint32_t source,
192 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
193 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
194 lastButtonState, currentButtonState,
195 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
196 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
197 lastButtonState, currentButtonState,
198 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
199}
200
Jeff Brown46b9ac02010-04-22 18:58:52 -0700201
Jeff Brown46b9ac02010-04-22 18:58:52 -0700202// --- InputReader ---
203
204InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700205 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700206 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700207 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700208 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700209 mConfigurationChangesToRefresh(0) {
210 refreshConfiguration(0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700211 updateGlobalMetaState();
212 updateInputConfiguration();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700213}
214
215InputReader::~InputReader() {
216 for (size_t i = 0; i < mDevices.size(); i++) {
217 delete mDevices.valueAt(i);
218 }
219}
220
221void InputReader::loopOnce() {
Jeff Brown474dcb52011-06-14 20:22:50 -0700222 uint32_t changes;
223 { // acquire lock
224 AutoMutex _l(mStateLock);
225
226 changes = mConfigurationChangesToRefresh;
227 mConfigurationChangesToRefresh = 0;
228 } // release lock
229
230 if (changes) {
231 refreshConfiguration(changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700232 }
233
Jeff Brownaa3855d2011-03-17 01:34:19 -0700234 int32_t timeoutMillis = -1;
235 if (mNextTimeout != LLONG_MAX) {
236 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
237 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
238 }
239
Jeff Brownb7198742011-03-18 18:14:26 -0700240 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
241 if (count) {
242 processEvents(mEventBuffer, count);
243 }
244 if (!count || timeoutMillis == 0) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700245 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
246#if DEBUG_RAW_EVENTS
247 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
248#endif
249 mNextTimeout = LLONG_MAX;
250 timeoutExpired(now);
251 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700252}
253
Jeff Brownb7198742011-03-18 18:14:26 -0700254void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
255 for (const RawEvent* rawEvent = rawEvents; count;) {
256 int32_t type = rawEvent->type;
257 size_t batchSize = 1;
258 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
259 int32_t deviceId = rawEvent->deviceId;
260 while (batchSize < count) {
261 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
262 || rawEvent[batchSize].deviceId != deviceId) {
263 break;
264 }
265 batchSize += 1;
266 }
267#if DEBUG_RAW_EVENTS
268 LOGD("BatchSize: %d Count: %d", batchSize, count);
269#endif
270 processEventsForDevice(deviceId, rawEvent, batchSize);
271 } else {
272 switch (rawEvent->type) {
273 case EventHubInterface::DEVICE_ADDED:
274 addDevice(rawEvent->deviceId);
275 break;
276 case EventHubInterface::DEVICE_REMOVED:
277 removeDevice(rawEvent->deviceId);
278 break;
279 case EventHubInterface::FINISHED_DEVICE_SCAN:
280 handleConfigurationChanged(rawEvent->when);
281 break;
282 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700283 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700284 break;
285 }
286 }
287 count -= batchSize;
288 rawEvent += batchSize;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700289 }
290}
291
Jeff Brown7342bb92010-10-01 18:55:43 -0700292void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700293 String8 name = mEventHub->getDeviceName(deviceId);
294 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
295
296 InputDevice* device = createDevice(deviceId, name, classes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700297 device->configure(&mConfig, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700298
Jeff Brown8d608662010-08-30 03:02:23 -0700299 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800300 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700301 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800302 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700303 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700304 }
305
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306 bool added = false;
307 { // acquire device registry writer lock
308 RWLock::AutoWLock _wl(mDeviceRegistryLock);
309
310 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
311 if (deviceIndex < 0) {
312 mDevices.add(deviceId, device);
313 added = true;
314 }
315 } // release device registry writer lock
316
317 if (! added) {
318 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
319 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700320 return;
321 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700322}
323
Jeff Brown7342bb92010-10-01 18:55:43 -0700324void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700325 bool removed = false;
326 InputDevice* device = NULL;
327 { // acquire device registry writer lock
328 RWLock::AutoWLock _wl(mDeviceRegistryLock);
329
330 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
331 if (deviceIndex >= 0) {
332 device = mDevices.valueAt(deviceIndex);
333 mDevices.removeItemsAt(deviceIndex, 1);
334 removed = true;
335 }
336 } // release device registry writer lock
337
338 if (! removed) {
339 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700340 return;
341 }
342
Jeff Brown6d0fec22010-07-23 21:28:06 -0700343 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800344 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700345 device->getId(), device->getName().string());
346 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800347 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700348 device->getId(), device->getName().string(), device->getSources());
349 }
350
Jeff Brown8d608662010-08-30 03:02:23 -0700351 device->reset();
352
Jeff Brown6d0fec22010-07-23 21:28:06 -0700353 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700354}
355
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
357 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700358
Jeff Brown56194eb2011-03-02 19:23:13 -0800359 // External devices.
360 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
361 device->setExternal(true);
362 }
363
Jeff Brown6d0fec22010-07-23 21:28:06 -0700364 // Switch-like devices.
365 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
366 device->addMapper(new SwitchInputMapper(device));
367 }
368
369 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800370 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700371 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
372 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800373 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374 }
375 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
376 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
377 }
378 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800379 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700380 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800381 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800382 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800383 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384
Jeff Brownefd32662011-03-08 15:13:06 -0800385 if (keyboardSource != 0) {
386 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387 }
388
Jeff Brown83c09682010-12-23 17:50:18 -0800389 // Cursor-like devices.
390 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
391 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392 }
393
Jeff Brown58a2da82011-01-25 16:02:22 -0800394 // Touchscreens and touchpad devices.
395 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800396 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800397 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800398 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700399 }
400
Jeff Browncb1404e2011-01-15 18:14:15 -0800401 // Joystick-like devices.
402 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
403 device->addMapper(new JoystickInputMapper(device));
404 }
405
Jeff Brown6d0fec22010-07-23 21:28:06 -0700406 return device;
407}
408
Jeff Brownb7198742011-03-18 18:14:26 -0700409void InputReader::processEventsForDevice(int32_t deviceId,
410 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700411 { // acquire device registry reader lock
412 RWLock::AutoRLock _rl(mDeviceRegistryLock);
413
414 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
415 if (deviceIndex < 0) {
416 LOGW("Discarding event for unknown deviceId %d.", deviceId);
417 return;
418 }
419
420 InputDevice* device = mDevices.valueAt(deviceIndex);
421 if (device->isIgnored()) {
422 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
423 return;
424 }
425
Jeff Brownb7198742011-03-18 18:14:26 -0700426 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700427 } // release device registry reader lock
428}
429
Jeff Brownaa3855d2011-03-17 01:34:19 -0700430void InputReader::timeoutExpired(nsecs_t when) {
431 { // acquire device registry reader lock
432 RWLock::AutoRLock _rl(mDeviceRegistryLock);
433
434 for (size_t i = 0; i < mDevices.size(); i++) {
435 InputDevice* device = mDevices.valueAt(i);
436 if (!device->isIgnored()) {
437 device->timeoutExpired(when);
438 }
439 }
440 } // release device registry reader lock
441}
442
Jeff Brownc3db8582010-10-20 15:33:38 -0700443void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444 // Reset global meta state because it depends on the list of all configured devices.
445 updateGlobalMetaState();
446
447 // Update input configuration.
448 updateInputConfiguration();
449
450 // Enqueue configuration changed.
451 mDispatcher->notifyConfigurationChanged(when);
452}
453
Jeff Brown474dcb52011-06-14 20:22:50 -0700454void InputReader::refreshConfiguration(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700455 mPolicy->getReaderConfiguration(&mConfig);
456 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
457
Jeff Brown474dcb52011-06-14 20:22:50 -0700458 if (changes) {
459 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
460
461 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
462 mEventHub->requestReopenDevices();
463 } else {
464 { // acquire device registry reader lock
465 RWLock::AutoRLock _rl(mDeviceRegistryLock);
466
467 for (size_t i = 0; i < mDevices.size(); i++) {
468 InputDevice* device = mDevices.valueAt(i);
469 device->configure(&mConfig, changes);
470 }
471 } // release device registry reader lock
472 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700473 }
474}
475
476void InputReader::updateGlobalMetaState() {
477 { // acquire state lock
478 AutoMutex _l(mStateLock);
479
480 mGlobalMetaState = 0;
481
482 { // acquire device registry reader lock
483 RWLock::AutoRLock _rl(mDeviceRegistryLock);
484
485 for (size_t i = 0; i < mDevices.size(); i++) {
486 InputDevice* device = mDevices.valueAt(i);
487 mGlobalMetaState |= device->getMetaState();
488 }
489 } // release device registry reader lock
490 } // release state lock
491}
492
493int32_t InputReader::getGlobalMetaState() {
494 { // acquire state lock
495 AutoMutex _l(mStateLock);
496
497 return mGlobalMetaState;
498 } // release state lock
499}
500
501void InputReader::updateInputConfiguration() {
502 { // acquire state lock
503 AutoMutex _l(mStateLock);
504
505 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
506 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
507 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
508 { // acquire device registry reader lock
509 RWLock::AutoRLock _rl(mDeviceRegistryLock);
510
511 InputDeviceInfo deviceInfo;
512 for (size_t i = 0; i < mDevices.size(); i++) {
513 InputDevice* device = mDevices.valueAt(i);
514 device->getDeviceInfo(& deviceInfo);
515 uint32_t sources = deviceInfo.getSources();
516
517 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
518 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
519 }
520 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
521 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
522 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
523 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
524 }
525 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
526 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700527 }
528 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700529 } // release device registry reader lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700530
Jeff Brown6d0fec22010-07-23 21:28:06 -0700531 mInputConfiguration.touchScreen = touchScreenConfig;
532 mInputConfiguration.keyboard = keyboardConfig;
533 mInputConfiguration.navigation = navigationConfig;
534 } // release state lock
535}
536
Jeff Brownfe508922011-01-18 15:10:10 -0800537void InputReader::disableVirtualKeysUntil(nsecs_t time) {
538 mDisableVirtualKeysTimeout = time;
539}
540
541bool InputReader::shouldDropVirtualKey(nsecs_t now,
542 InputDevice* device, int32_t keyCode, int32_t scanCode) {
543 if (now < mDisableVirtualKeysTimeout) {
544 LOGI("Dropping virtual key from device %s because virtual keys are "
545 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
546 device->getName().string(),
547 (mDisableVirtualKeysTimeout - now) * 0.000001,
548 keyCode, scanCode);
549 return true;
550 } else {
551 return false;
552 }
553}
554
Jeff Brown05dc66a2011-03-02 14:41:58 -0800555void InputReader::fadePointer() {
556 { // acquire device registry reader lock
557 RWLock::AutoRLock _rl(mDeviceRegistryLock);
558
559 for (size_t i = 0; i < mDevices.size(); i++) {
560 InputDevice* device = mDevices.valueAt(i);
561 device->fadePointer();
562 }
563 } // release device registry reader lock
564}
565
Jeff Brownaa3855d2011-03-17 01:34:19 -0700566void InputReader::requestTimeoutAtTime(nsecs_t when) {
567 if (when < mNextTimeout) {
568 mNextTimeout = when;
569 }
570}
571
Jeff Brown6d0fec22010-07-23 21:28:06 -0700572void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
573 { // acquire state lock
574 AutoMutex _l(mStateLock);
575
576 *outConfiguration = mInputConfiguration;
577 } // release state lock
578}
579
580status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
581 { // acquire device registry reader lock
582 RWLock::AutoRLock _rl(mDeviceRegistryLock);
583
584 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
585 if (deviceIndex < 0) {
586 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700587 }
588
Jeff Brown6d0fec22010-07-23 21:28:06 -0700589 InputDevice* device = mDevices.valueAt(deviceIndex);
590 if (device->isIgnored()) {
591 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700592 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700593
594 device->getDeviceInfo(outDeviceInfo);
595 return OK;
596 } // release device registy reader lock
597}
598
599void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
600 outDeviceIds.clear();
601
602 { // acquire device registry reader lock
603 RWLock::AutoRLock _rl(mDeviceRegistryLock);
604
605 size_t numDevices = mDevices.size();
606 for (size_t i = 0; i < numDevices; i++) {
607 InputDevice* device = mDevices.valueAt(i);
608 if (! device->isIgnored()) {
609 outDeviceIds.add(device->getId());
610 }
611 }
612 } // release device registy reader lock
613}
614
615int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
616 int32_t keyCode) {
617 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
618}
619
620int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
621 int32_t scanCode) {
622 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
623}
624
625int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
626 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
627}
628
629int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
630 GetStateFunc getStateFunc) {
631 { // acquire device registry reader lock
632 RWLock::AutoRLock _rl(mDeviceRegistryLock);
633
634 int32_t result = AKEY_STATE_UNKNOWN;
635 if (deviceId >= 0) {
636 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
637 if (deviceIndex >= 0) {
638 InputDevice* device = mDevices.valueAt(deviceIndex);
639 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
640 result = (device->*getStateFunc)(sourceMask, code);
641 }
642 }
643 } 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->*getStateFunc)(sourceMask, code);
649 if (result >= AKEY_STATE_DOWN) {
650 return result;
651 }
652 }
653 }
654 }
655 return result;
656 } // release device registy reader lock
657}
658
659bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
660 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
661 memset(outFlags, 0, numCodes);
662 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
663}
664
665bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
666 const int32_t* keyCodes, uint8_t* outFlags) {
667 { // acquire device registry reader lock
668 RWLock::AutoRLock _rl(mDeviceRegistryLock);
669 bool result = false;
670 if (deviceId >= 0) {
671 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
672 if (deviceIndex >= 0) {
673 InputDevice* device = mDevices.valueAt(deviceIndex);
674 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
675 result = device->markSupportedKeyCodes(sourceMask,
676 numCodes, keyCodes, outFlags);
677 }
678 }
679 } else {
680 size_t numDevices = mDevices.size();
681 for (size_t i = 0; i < numDevices; i++) {
682 InputDevice* device = mDevices.valueAt(i);
683 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
684 result |= device->markSupportedKeyCodes(sourceMask,
685 numCodes, keyCodes, outFlags);
686 }
687 }
688 }
689 return result;
690 } // release device registy reader lock
691}
692
Jeff Brown474dcb52011-06-14 20:22:50 -0700693void InputReader::requestRefreshConfiguration(uint32_t changes) {
694 if (changes) {
695 bool needWake;
696 { // acquire lock
697 AutoMutex _l(mStateLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700698
Jeff Brown474dcb52011-06-14 20:22:50 -0700699 needWake = !mConfigurationChangesToRefresh;
700 mConfigurationChangesToRefresh |= changes;
701 } // release lock
702
703 if (needWake) {
704 mEventHub->wake();
705 }
706 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700707}
708
Jeff Brownb88102f2010-09-08 11:49:43 -0700709void InputReader::dump(String8& dump) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700710 mEventHub->dump(dump);
711 dump.append("\n");
712
713 dump.append("Input Reader State:\n");
714
Jeff Brownef3d7e82010-09-30 14:33:04 -0700715 { // acquire device registry reader lock
716 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700717
Jeff Brownef3d7e82010-09-30 14:33:04 -0700718 for (size_t i = 0; i < mDevices.size(); i++) {
719 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700720 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700721 } // release device registy reader lock
Jeff Brown214eaf42011-05-26 19:17:02 -0700722
723 dump.append(INDENT "Configuration:\n");
724 dump.append(INDENT2 "ExcludedDeviceNames: [");
725 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
726 if (i != 0) {
727 dump.append(", ");
728 }
729 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
730 }
731 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700732 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
733 mConfig.virtualKeyQuietTime * 0.000001f);
734
Jeff Brown19c97d462011-06-01 12:33:19 -0700735 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
736 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
737 mConfig.pointerVelocityControlParameters.scale,
738 mConfig.pointerVelocityControlParameters.lowThreshold,
739 mConfig.pointerVelocityControlParameters.highThreshold,
740 mConfig.pointerVelocityControlParameters.acceleration);
741
742 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
743 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
744 mConfig.wheelVelocityControlParameters.scale,
745 mConfig.wheelVelocityControlParameters.lowThreshold,
746 mConfig.wheelVelocityControlParameters.highThreshold,
747 mConfig.wheelVelocityControlParameters.acceleration);
748
Jeff Brown214eaf42011-05-26 19:17:02 -0700749 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700750 dump.appendFormat(INDENT3 "Enabled: %s\n",
751 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700752 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
753 mConfig.pointerGestureQuietInterval * 0.000001f);
754 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
755 mConfig.pointerGestureDragMinSwitchSpeed);
756 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
757 mConfig.pointerGestureTapInterval * 0.000001f);
758 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
759 mConfig.pointerGestureTapDragInterval * 0.000001f);
760 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
761 mConfig.pointerGestureTapSlop);
762 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
763 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700764 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
765 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700766 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
767 mConfig.pointerGestureSwipeTransitionAngleCosine);
768 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
769 mConfig.pointerGestureSwipeMaxWidthRatio);
770 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
771 mConfig.pointerGestureMovementSpeedRatio);
772 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
773 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700774}
775
Jeff Brown6d0fec22010-07-23 21:28:06 -0700776
777// --- InputReaderThread ---
778
779InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
780 Thread(/*canCallJava*/ true), mReader(reader) {
781}
782
783InputReaderThread::~InputReaderThread() {
784}
785
786bool InputReaderThread::threadLoop() {
787 mReader->loopOnce();
788 return true;
789}
790
791
792// --- InputDevice ---
793
794InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700795 mContext(context), mId(id), mName(name), mSources(0),
796 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700797}
798
799InputDevice::~InputDevice() {
800 size_t numMappers = mMappers.size();
801 for (size_t i = 0; i < numMappers; i++) {
802 delete mMappers[i];
803 }
804 mMappers.clear();
805}
806
Jeff Brownef3d7e82010-09-30 14:33:04 -0700807void InputDevice::dump(String8& dump) {
808 InputDeviceInfo deviceInfo;
809 getDeviceInfo(& deviceInfo);
810
Jeff Brown90655042010-12-02 13:50:46 -0800811 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700812 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800813 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700814 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
815 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800816
Jeff Brownefd32662011-03-08 15:13:06 -0800817 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800818 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700819 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800820 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800821 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
822 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800823 char name[32];
824 if (label) {
825 strncpy(name, label, sizeof(name));
826 name[sizeof(name) - 1] = '\0';
827 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800828 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800829 }
Jeff Brownefd32662011-03-08 15:13:06 -0800830 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
831 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
832 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800833 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700834 }
835
836 size_t numMappers = mMappers.size();
837 for (size_t i = 0; i < numMappers; i++) {
838 InputMapper* mapper = mMappers[i];
839 mapper->dump(dump);
840 }
841}
842
Jeff Brown6d0fec22010-07-23 21:28:06 -0700843void InputDevice::addMapper(InputMapper* mapper) {
844 mMappers.add(mapper);
845}
846
Jeff Brown474dcb52011-06-14 20:22:50 -0700847void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700848 mSources = 0;
849
Jeff Brown474dcb52011-06-14 20:22:50 -0700850 if (!isIgnored()) {
851 if (!changes) { // first time only
852 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
853 }
854
855 size_t numMappers = mMappers.size();
856 for (size_t i = 0; i < numMappers; i++) {
857 InputMapper* mapper = mMappers[i];
858 mapper->configure(config, changes);
859 mSources |= mapper->getSources();
860 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700861 }
862}
863
Jeff Brown6d0fec22010-07-23 21:28:06 -0700864void InputDevice::reset() {
865 size_t numMappers = mMappers.size();
866 for (size_t i = 0; i < numMappers; i++) {
867 InputMapper* mapper = mMappers[i];
868 mapper->reset();
869 }
870}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700871
Jeff Brownb7198742011-03-18 18:14:26 -0700872void InputDevice::process(const RawEvent* rawEvents, size_t count) {
873 // Process all of the events in order for each mapper.
874 // We cannot simply ask each mapper to process them in bulk because mappers may
875 // have side-effects that must be interleaved. For example, joystick movement events and
876 // gamepad button presses are handled by different mappers but they should be dispatched
877 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700878 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700879 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
880#if DEBUG_RAW_EVENTS
881 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700882 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700883 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
884 rawEvent->value, rawEvent->flags);
885#endif
886
Jeff Brown80fd47c2011-05-24 01:07:44 -0700887 if (mDropUntilNextSync) {
888 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
889 mDropUntilNextSync = false;
890#if DEBUG_RAW_EVENTS
891 LOGD("Recovered from input event buffer overrun.");
892#endif
893 } else {
894#if DEBUG_RAW_EVENTS
895 LOGD("Dropped input event while waiting for next input sync.");
896#endif
897 }
898 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
899 LOGI("Detected input event buffer overrun for device %s.", mName.string());
900 mDropUntilNextSync = true;
901 reset();
902 } else {
903 for (size_t i = 0; i < numMappers; i++) {
904 InputMapper* mapper = mMappers[i];
905 mapper->process(rawEvent);
906 }
Jeff Brownb7198742011-03-18 18:14:26 -0700907 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700908 }
909}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700910
Jeff Brownaa3855d2011-03-17 01:34:19 -0700911void InputDevice::timeoutExpired(nsecs_t when) {
912 size_t numMappers = mMappers.size();
913 for (size_t i = 0; i < numMappers; i++) {
914 InputMapper* mapper = mMappers[i];
915 mapper->timeoutExpired(when);
916 }
917}
918
Jeff Brown6d0fec22010-07-23 21:28:06 -0700919void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
920 outDeviceInfo->initialize(mId, mName);
921
922 size_t numMappers = mMappers.size();
923 for (size_t i = 0; i < numMappers; i++) {
924 InputMapper* mapper = mMappers[i];
925 mapper->populateDeviceInfo(outDeviceInfo);
926 }
927}
928
929int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
930 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
931}
932
933int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
934 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
935}
936
937int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
938 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
939}
940
941int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
942 int32_t result = AKEY_STATE_UNKNOWN;
943 size_t numMappers = mMappers.size();
944 for (size_t i = 0; i < numMappers; i++) {
945 InputMapper* mapper = mMappers[i];
946 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
947 result = (mapper->*getStateFunc)(sourceMask, code);
948 if (result >= AKEY_STATE_DOWN) {
949 return result;
950 }
951 }
952 }
953 return result;
954}
955
956bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
957 const int32_t* keyCodes, uint8_t* outFlags) {
958 bool result = false;
959 size_t numMappers = mMappers.size();
960 for (size_t i = 0; i < numMappers; i++) {
961 InputMapper* mapper = mMappers[i];
962 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
963 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
964 }
965 }
966 return result;
967}
968
969int32_t InputDevice::getMetaState() {
970 int32_t result = 0;
971 size_t numMappers = mMappers.size();
972 for (size_t i = 0; i < numMappers; i++) {
973 InputMapper* mapper = mMappers[i];
974 result |= mapper->getMetaState();
975 }
976 return result;
977}
978
Jeff Brown05dc66a2011-03-02 14:41:58 -0800979void InputDevice::fadePointer() {
980 size_t numMappers = mMappers.size();
981 for (size_t i = 0; i < numMappers; i++) {
982 InputMapper* mapper = mMappers[i];
983 mapper->fadePointer();
984 }
985}
986
Jeff Brown6d0fec22010-07-23 21:28:06 -0700987
988// --- InputMapper ---
989
990InputMapper::InputMapper(InputDevice* device) :
991 mDevice(device), mContext(device->getContext()) {
992}
993
994InputMapper::~InputMapper() {
995}
996
997void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
998 info->addSource(getSources());
999}
1000
Jeff Brownef3d7e82010-09-30 14:33:04 -07001001void InputMapper::dump(String8& dump) {
1002}
1003
Jeff Brown474dcb52011-06-14 20:22:50 -07001004void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005}
1006
1007void InputMapper::reset() {
1008}
1009
Jeff Brownaa3855d2011-03-17 01:34:19 -07001010void InputMapper::timeoutExpired(nsecs_t when) {
1011}
1012
Jeff Brown6d0fec22010-07-23 21:28:06 -07001013int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1014 return AKEY_STATE_UNKNOWN;
1015}
1016
1017int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1018 return AKEY_STATE_UNKNOWN;
1019}
1020
1021int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1022 return AKEY_STATE_UNKNOWN;
1023}
1024
1025bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1026 const int32_t* keyCodes, uint8_t* outFlags) {
1027 return false;
1028}
1029
1030int32_t InputMapper::getMetaState() {
1031 return 0;
1032}
1033
Jeff Brown05dc66a2011-03-02 14:41:58 -08001034void InputMapper::fadePointer() {
1035}
1036
Jeff Browncb1404e2011-01-15 18:14:15 -08001037void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1038 const RawAbsoluteAxisInfo& axis, const char* name) {
1039 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001040 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1041 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001042 } else {
1043 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1044 }
1045}
1046
Jeff Brown6d0fec22010-07-23 21:28:06 -07001047
1048// --- SwitchInputMapper ---
1049
1050SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1051 InputMapper(device) {
1052}
1053
1054SwitchInputMapper::~SwitchInputMapper() {
1055}
1056
1057uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001058 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001059}
1060
1061void SwitchInputMapper::process(const RawEvent* rawEvent) {
1062 switch (rawEvent->type) {
1063 case EV_SW:
1064 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1065 break;
1066 }
1067}
1068
1069void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001070 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001071}
1072
1073int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1074 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1075}
1076
1077
1078// --- KeyboardInputMapper ---
1079
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001080KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001081 uint32_t source, int32_t keyboardType) :
1082 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001083 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001084 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001085}
1086
1087KeyboardInputMapper::~KeyboardInputMapper() {
1088}
1089
Jeff Brown6328cdc2010-07-29 18:18:33 -07001090void KeyboardInputMapper::initializeLocked() {
1091 mLocked.metaState = AMETA_NONE;
1092 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001093}
1094
1095uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001096 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001097}
1098
1099void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1100 InputMapper::populateDeviceInfo(info);
1101
1102 info->setKeyboardType(mKeyboardType);
1103}
1104
Jeff Brownef3d7e82010-09-30 14:33:04 -07001105void KeyboardInputMapper::dump(String8& dump) {
1106 { // acquire lock
1107 AutoMutex _l(mLock);
1108 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001109 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001110 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1111 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1112 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1113 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1114 } // release lock
1115}
1116
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001117
Jeff Brown474dcb52011-06-14 20:22:50 -07001118void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1119 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001120
Jeff Brown474dcb52011-06-14 20:22:50 -07001121 if (!changes) { // first time only
1122 // Configure basic parameters.
1123 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001124
Jeff Brown474dcb52011-06-14 20:22:50 -07001125 // Reset LEDs.
1126 {
1127 AutoMutex _l(mLock);
1128 resetLedStateLocked();
1129 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001130 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001131}
1132
1133void KeyboardInputMapper::configureParameters() {
1134 mParameters.orientationAware = false;
1135 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1136 mParameters.orientationAware);
1137
1138 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1139}
1140
1141void KeyboardInputMapper::dumpParameters(String8& dump) {
1142 dump.append(INDENT3 "Parameters:\n");
1143 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1144 mParameters.associatedDisplayId);
1145 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1146 toString(mParameters.orientationAware));
1147}
1148
Jeff Brown6d0fec22010-07-23 21:28:06 -07001149void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001150 for (;;) {
1151 int32_t keyCode, scanCode;
1152 { // acquire lock
1153 AutoMutex _l(mLock);
1154
1155 // Synthesize key up event on reset if keys are currently down.
1156 if (mLocked.keyDowns.isEmpty()) {
1157 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001158 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001159 break; // done
1160 }
1161
1162 const KeyDown& keyDown = mLocked.keyDowns.top();
1163 keyCode = keyDown.keyCode;
1164 scanCode = keyDown.scanCode;
1165 } // release lock
1166
Jeff Brown6d0fec22010-07-23 21:28:06 -07001167 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001168 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001169 }
1170
1171 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001172 getContext()->updateGlobalMetaState();
1173}
1174
1175void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1176 switch (rawEvent->type) {
1177 case EV_KEY: {
1178 int32_t scanCode = rawEvent->scanCode;
1179 if (isKeyboardOrGamepadKey(scanCode)) {
1180 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1181 rawEvent->flags);
1182 }
1183 break;
1184 }
1185 }
1186}
1187
1188bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1189 return scanCode < BTN_MOUSE
1190 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001191 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001192 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001193}
1194
Jeff Brown6328cdc2010-07-29 18:18:33 -07001195void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1196 int32_t scanCode, uint32_t policyFlags) {
1197 int32_t newMetaState;
1198 nsecs_t downTime;
1199 bool metaStateChanged = false;
1200
1201 { // acquire lock
1202 AutoMutex _l(mLock);
1203
1204 if (down) {
1205 // Rotate key codes according to orientation if needed.
1206 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001207 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001208 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001209 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1210 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001211 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001212 }
1213
1214 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001215 }
1216
Jeff Brown6328cdc2010-07-29 18:18:33 -07001217 // Add key down.
1218 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1219 if (keyDownIndex >= 0) {
1220 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001221 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001222 } else {
1223 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001224 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1225 && mContext->shouldDropVirtualKey(when,
1226 getDevice(), keyCode, scanCode)) {
1227 return;
1228 }
1229
Jeff Brown6328cdc2010-07-29 18:18:33 -07001230 mLocked.keyDowns.push();
1231 KeyDown& keyDown = mLocked.keyDowns.editTop();
1232 keyDown.keyCode = keyCode;
1233 keyDown.scanCode = scanCode;
1234 }
1235
1236 mLocked.downTime = when;
1237 } else {
1238 // Remove key down.
1239 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1240 if (keyDownIndex >= 0) {
1241 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001242 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001243 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1244 } else {
1245 // key was not actually down
1246 LOGI("Dropping key up from device %s because the key was not down. "
1247 "keyCode=%d, scanCode=%d",
1248 getDeviceName().string(), keyCode, scanCode);
1249 return;
1250 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001251 }
1252
Jeff Brown6328cdc2010-07-29 18:18:33 -07001253 int32_t oldMetaState = mLocked.metaState;
1254 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1255 if (oldMetaState != newMetaState) {
1256 mLocked.metaState = newMetaState;
1257 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001258 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001259 }
Jeff Brownfd035822010-06-30 16:10:35 -07001260
Jeff Brown6328cdc2010-07-29 18:18:33 -07001261 downTime = mLocked.downTime;
1262 } // release lock
1263
Jeff Brown56194eb2011-03-02 19:23:13 -08001264 // Key down on external an keyboard should wake the device.
1265 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1266 // For internal keyboards, the key layout file should specify the policy flags for
1267 // each wake key individually.
1268 // TODO: Use the input device configuration to control this behavior more finely.
1269 if (down && getDevice()->isExternal()
1270 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1271 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1272 }
1273
Jeff Brown6328cdc2010-07-29 18:18:33 -07001274 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001275 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001276 }
1277
Jeff Brown05dc66a2011-03-02 14:41:58 -08001278 if (down && !isMetaKey(keyCode)) {
1279 getContext()->fadePointer();
1280 }
1281
Jeff Brownefd32662011-03-08 15:13:06 -08001282 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001283 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1284 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001285}
1286
Jeff Brown6328cdc2010-07-29 18:18:33 -07001287ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1288 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001289 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001290 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001291 return i;
1292 }
1293 }
1294 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001295}
1296
Jeff Brown6d0fec22010-07-23 21:28:06 -07001297int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1298 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1299}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001300
Jeff Brown6d0fec22010-07-23 21:28:06 -07001301int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1302 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1303}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001304
Jeff Brown6d0fec22010-07-23 21:28:06 -07001305bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1306 const int32_t* keyCodes, uint8_t* outFlags) {
1307 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1308}
1309
1310int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001311 { // acquire lock
1312 AutoMutex _l(mLock);
1313 return mLocked.metaState;
1314 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001315}
1316
Jeff Brown49ed71d2010-12-06 17:13:33 -08001317void KeyboardInputMapper::resetLedStateLocked() {
1318 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1319 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1320 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1321
1322 updateLedStateLocked(true);
1323}
1324
1325void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1326 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1327 ledState.on = false;
1328}
1329
Jeff Brown497a92c2010-09-12 17:55:08 -07001330void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1331 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001332 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001333 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001334 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001335 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001336 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001337}
1338
1339void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1340 int32_t led, int32_t modifier, bool reset) {
1341 if (ledState.avail) {
1342 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001343 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001344 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1345 ledState.on = desiredState;
1346 }
1347 }
1348}
1349
Jeff Brown6d0fec22010-07-23 21:28:06 -07001350
Jeff Brown83c09682010-12-23 17:50:18 -08001351// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001352
Jeff Brown83c09682010-12-23 17:50:18 -08001353CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001354 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001355 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001356}
1357
Jeff Brown83c09682010-12-23 17:50:18 -08001358CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001359}
1360
Jeff Brown83c09682010-12-23 17:50:18 -08001361uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001362 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001363}
1364
Jeff Brown83c09682010-12-23 17:50:18 -08001365void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001366 InputMapper::populateDeviceInfo(info);
1367
Jeff Brown83c09682010-12-23 17:50:18 -08001368 if (mParameters.mode == Parameters::MODE_POINTER) {
1369 float minX, minY, maxX, maxY;
1370 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001371 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1372 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001373 }
1374 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001375 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1376 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001377 }
Jeff Brownefd32662011-03-08 15:13:06 -08001378 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001379
1380 if (mHaveVWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001381 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001382 }
1383 if (mHaveHWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001384 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001385 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001386}
1387
Jeff Brown83c09682010-12-23 17:50:18 -08001388void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001389 { // acquire lock
1390 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001391 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001392 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001393 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1394 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001395 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1396 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001397 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1398 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1399 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1400 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001401 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1402 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001403 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1404 } // release lock
1405}
1406
Jeff Brown474dcb52011-06-14 20:22:50 -07001407void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1408 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001409
Jeff Brown474dcb52011-06-14 20:22:50 -07001410 if (!changes) { // first time only
1411 // Configure basic parameters.
1412 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001413
Jeff Brown474dcb52011-06-14 20:22:50 -07001414 // Configure device mode.
1415 switch (mParameters.mode) {
1416 case Parameters::MODE_POINTER:
1417 mSource = AINPUT_SOURCE_MOUSE;
1418 mXPrecision = 1.0f;
1419 mYPrecision = 1.0f;
1420 mXScale = 1.0f;
1421 mYScale = 1.0f;
1422 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1423 break;
1424 case Parameters::MODE_NAVIGATION:
1425 mSource = AINPUT_SOURCE_TRACKBALL;
1426 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1427 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1428 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1429 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1430 break;
1431 }
1432
1433 mVWheelScale = 1.0f;
1434 mHWheelScale = 1.0f;
1435
1436 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1437 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown83c09682010-12-23 17:50:18 -08001438 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001439
Jeff Brown474dcb52011-06-14 20:22:50 -07001440 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1441 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1442 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1443 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1444 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001445}
1446
Jeff Brown83c09682010-12-23 17:50:18 -08001447void CursorInputMapper::configureParameters() {
1448 mParameters.mode = Parameters::MODE_POINTER;
1449 String8 cursorModeString;
1450 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1451 if (cursorModeString == "navigation") {
1452 mParameters.mode = Parameters::MODE_NAVIGATION;
1453 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1454 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1455 }
1456 }
1457
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001458 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001459 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001460 mParameters.orientationAware);
1461
Jeff Brown83c09682010-12-23 17:50:18 -08001462 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1463 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001464}
1465
Jeff Brown83c09682010-12-23 17:50:18 -08001466void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001467 dump.append(INDENT3 "Parameters:\n");
1468 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1469 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001470
1471 switch (mParameters.mode) {
1472 case Parameters::MODE_POINTER:
1473 dump.append(INDENT4 "Mode: pointer\n");
1474 break;
1475 case Parameters::MODE_NAVIGATION:
1476 dump.append(INDENT4 "Mode: navigation\n");
1477 break;
1478 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001479 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001480 }
1481
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001482 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1483 toString(mParameters.orientationAware));
1484}
1485
Jeff Brown83c09682010-12-23 17:50:18 -08001486void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001487 mAccumulator.clear();
1488
Jeff Brownefd32662011-03-08 15:13:06 -08001489 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001490 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001491}
1492
Jeff Brown83c09682010-12-23 17:50:18 -08001493void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001494 for (;;) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001495 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001496 { // acquire lock
1497 AutoMutex _l(mLock);
1498
Jeff Brownefd32662011-03-08 15:13:06 -08001499 buttonState = mLocked.buttonState;
1500 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001501 initializeLocked();
1502 break; // done
1503 }
1504 } // release lock
1505
Jeff Brown19c97d462011-06-01 12:33:19 -07001506 // Reset velocity.
1507 mPointerVelocityControl.reset();
1508 mWheelXVelocityControl.reset();
1509 mWheelYVelocityControl.reset();
1510
Jeff Brown83c09682010-12-23 17:50:18 -08001511 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001512 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownefd32662011-03-08 15:13:06 -08001513 mAccumulator.clear();
1514 mAccumulator.buttonDown = 0;
1515 mAccumulator.buttonUp = buttonState;
1516 mAccumulator.fields = Accumulator::FIELD_BUTTONS;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001517 sync(when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001518 }
1519
Jeff Brown6d0fec22010-07-23 21:28:06 -07001520 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001521}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001522
Jeff Brown83c09682010-12-23 17:50:18 -08001523void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001524 switch (rawEvent->type) {
Jeff Brownefd32662011-03-08 15:13:06 -08001525 case EV_KEY: {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001526 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownefd32662011-03-08 15:13:06 -08001527 if (buttonState) {
1528 if (rawEvent->value) {
1529 mAccumulator.buttonDown = buttonState;
1530 mAccumulator.buttonUp = 0;
1531 } else {
1532 mAccumulator.buttonDown = 0;
1533 mAccumulator.buttonUp = buttonState;
1534 }
1535 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
1536
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001537 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1538 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001539 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001540 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001541 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001542 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001543 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001544
Jeff Brown6d0fec22010-07-23 21:28:06 -07001545 case EV_REL:
1546 switch (rawEvent->scanCode) {
1547 case REL_X:
1548 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1549 mAccumulator.relX = rawEvent->value;
1550 break;
1551 case REL_Y:
1552 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1553 mAccumulator.relY = rawEvent->value;
1554 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001555 case REL_WHEEL:
1556 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1557 mAccumulator.relWheel = rawEvent->value;
1558 break;
1559 case REL_HWHEEL:
1560 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1561 mAccumulator.relHWheel = rawEvent->value;
1562 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001563 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001564 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001565
Jeff Brown6d0fec22010-07-23 21:28:06 -07001566 case EV_SYN:
1567 switch (rawEvent->scanCode) {
1568 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001569 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001570 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001571 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001572 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001573 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001574}
1575
Jeff Brown83c09682010-12-23 17:50:18 -08001576void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001577 uint32_t fields = mAccumulator.fields;
1578 if (fields == 0) {
1579 return; // no new state changes, so nothing to do
1580 }
1581
Jeff Brown9626b142011-03-03 02:09:54 -08001582 int32_t motionEventAction;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001583 int32_t lastButtonState, currentButtonState;
1584 PointerProperties pointerProperties;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001585 PointerCoords pointerCoords;
1586 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001587 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001588 { // acquire lock
1589 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001590
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001591 lastButtonState = mLocked.buttonState;
1592
Jeff Brownefd32662011-03-08 15:13:06 -08001593 bool down, downChanged;
1594 bool wasDown = isPointerDown(mLocked.buttonState);
1595 bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS;
1596 if (buttonsChanged) {
1597 mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown)
1598 & ~mAccumulator.buttonUp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001599
Jeff Brownefd32662011-03-08 15:13:06 -08001600 down = isPointerDown(mLocked.buttonState);
1601
1602 if (!wasDown && down) {
1603 mLocked.downTime = when;
1604 downChanged = true;
1605 } else if (wasDown && !down) {
1606 downChanged = true;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001607 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001608 downChanged = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001609 }
Jeff Brownefd32662011-03-08 15:13:06 -08001610 } else {
1611 down = wasDown;
1612 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001613 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001614
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001615 currentButtonState = mLocked.buttonState;
1616
Jeff Brown6328cdc2010-07-29 18:18:33 -07001617 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001618 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1619 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001620
Jeff Brown6328cdc2010-07-29 18:18:33 -07001621 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001622 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1623 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001624 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001625 } else {
1626 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001627 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001628
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001629 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001630 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001631 // Rotate motion based on display orientation if needed.
1632 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1633 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001634 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1635 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001636 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001637 }
1638
1639 float temp;
1640 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001641 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001642 temp = deltaX;
1643 deltaX = deltaY;
1644 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001645 break;
1646
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001647 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001648 deltaX = -deltaX;
1649 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001650 break;
1651
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001652 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001653 temp = deltaX;
1654 deltaX = -deltaY;
1655 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001656 break;
1657 }
1658 }
Jeff Brown83c09682010-12-23 17:50:18 -08001659
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001660 pointerProperties.clear();
1661 pointerProperties.id = 0;
1662 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
1663
1664 pointerCoords.clear();
1665
Jeff Brown2352b972011-04-12 22:39:53 -07001666 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
1667 vscroll = mAccumulator.relWheel;
1668 } else {
1669 vscroll = 0;
1670 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001671 mWheelYVelocityControl.move(when, NULL, &vscroll);
1672
Jeff Brown2352b972011-04-12 22:39:53 -07001673 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
1674 hscroll = mAccumulator.relHWheel;
1675 } else {
1676 hscroll = 0;
1677 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001678 mWheelXVelocityControl.move(when, &hscroll, NULL);
1679
1680 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown2352b972011-04-12 22:39:53 -07001681
Jeff Brown83c09682010-12-23 17:50:18 -08001682 if (mPointerController != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -07001683 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
1684 || buttonsChanged) {
1685 mPointerController->setPresentation(
1686 PointerControllerInterface::PRESENTATION_POINTER);
1687
1688 if (deltaX != 0 || deltaY != 0) {
1689 mPointerController->move(deltaX, deltaY);
1690 }
1691
1692 if (buttonsChanged) {
1693 mPointerController->setButtonState(mLocked.buttonState);
1694 }
1695
Jeff Brown538881e2011-05-25 18:23:38 -07001696 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08001697 }
Jeff Brownefd32662011-03-08 15:13:06 -08001698
Jeff Brown91c69ab2011-02-14 17:03:18 -08001699 float x, y;
1700 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001701 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1702 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83c09682010-12-23 17:50:18 -08001703 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001704 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1705 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001706 }
1707
Jeff Brownefd32662011-03-08 15:13:06 -08001708 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001709 } // release lock
1710
Jeff Brown56194eb2011-03-02 19:23:13 -08001711 // Moving an external trackball or mouse should wake the device.
1712 // We don't do this for internal cursor devices to prevent them from waking up
1713 // the device in your pocket.
1714 // TODO: Use the input device configuration to control this behavior more finely.
1715 uint32_t policyFlags = 0;
1716 if (getDevice()->isExternal()) {
1717 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1718 }
1719
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001720 // Synthesize key down from buttons if needed.
1721 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
1722 policyFlags, lastButtonState, currentButtonState);
1723
1724 // Send motion event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001725 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownefd32662011-03-08 15:13:06 -08001726 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Browna6111372011-07-14 21:48:23 -07001727 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001728 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownb6997262010-10-08 22:31:17 -07001729
Jeff Browna032cc02011-03-07 16:56:21 -08001730 // Send hover move after UP to tell the application that the mouse is hovering now.
1731 if (motionEventAction == AMOTION_EVENT_ACTION_UP
1732 && mPointerController != NULL) {
1733 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001734 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
1735 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1736 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Browna032cc02011-03-07 16:56:21 -08001737 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001738
Jeff Browna032cc02011-03-07 16:56:21 -08001739 // Send scroll events.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001740 if (vscroll != 0 || hscroll != 0) {
1741 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1742 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1743
Jeff Brownefd32662011-03-08 15:13:06 -08001744 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001745 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
1746 AMOTION_EVENT_EDGE_FLAG_NONE,
1747 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001748 }
Jeff Browna032cc02011-03-07 16:56:21 -08001749
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001750 // Synthesize key up from buttons if needed.
1751 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
1752 policyFlags, lastButtonState, currentButtonState);
1753
Jeff Browna032cc02011-03-07 16:56:21 -08001754 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001755}
1756
Jeff Brown83c09682010-12-23 17:50:18 -08001757int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001758 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1759 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1760 } else {
1761 return AKEY_STATE_UNKNOWN;
1762 }
1763}
1764
Jeff Brown05dc66a2011-03-02 14:41:58 -08001765void CursorInputMapper::fadePointer() {
1766 { // acquire lock
1767 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08001768 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07001769 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownefd32662011-03-08 15:13:06 -08001770 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001771 } // release lock
1772}
1773
Jeff Brown6d0fec22010-07-23 21:28:06 -07001774
1775// --- TouchInputMapper ---
1776
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001777TouchInputMapper::TouchInputMapper(InputDevice* device) :
1778 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001779 mLocked.surfaceOrientation = -1;
1780 mLocked.surfaceWidth = -1;
1781 mLocked.surfaceHeight = -1;
1782
1783 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001784}
1785
1786TouchInputMapper::~TouchInputMapper() {
1787}
1788
1789uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08001790 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001791}
1792
1793void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1794 InputMapper::populateDeviceInfo(info);
1795
Jeff Brown6328cdc2010-07-29 18:18:33 -07001796 { // acquire lock
1797 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001798
Jeff Brown6328cdc2010-07-29 18:18:33 -07001799 // Ensure surface information is up to date so that orientation changes are
1800 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08001801 if (!configureSurfaceLocked()) {
1802 return;
1803 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001804
Jeff Brownefd32662011-03-08 15:13:06 -08001805 info->addMotionRange(mLocked.orientedRanges.x);
1806 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001807
1808 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08001809 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07001810 }
1811
1812 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001813 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07001814 }
1815
Jeff Brownc6d282b2010-10-14 21:42:15 -07001816 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001817 info->addMotionRange(mLocked.orientedRanges.touchMajor);
1818 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001819 }
1820
Jeff Brownc6d282b2010-10-14 21:42:15 -07001821 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001822 info->addMotionRange(mLocked.orientedRanges.toolMajor);
1823 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001824 }
1825
1826 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08001827 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07001828 }
Jeff Brownace13b12011-03-09 17:39:48 -08001829
Jeff Brown80fd47c2011-05-24 01:07:44 -07001830 if (mLocked.orientedRanges.haveDistance) {
1831 info->addMotionRange(mLocked.orientedRanges.distance);
1832 }
1833
Jeff Brownace13b12011-03-09 17:39:48 -08001834 if (mPointerController != NULL) {
1835 float minX, minY, maxX, maxY;
1836 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1837 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
1838 minX, maxX, 0.0f, 0.0f);
1839 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
1840 minY, maxY, 0.0f, 0.0f);
1841 }
1842 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
1843 0.0f, 1.0f, 0.0f, 0.0f);
1844 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001845 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001846}
1847
Jeff Brownef3d7e82010-09-30 14:33:04 -07001848void TouchInputMapper::dump(String8& dump) {
1849 { // acquire lock
1850 AutoMutex _l(mLock);
1851 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001852 dumpParameters(dump);
1853 dumpVirtualKeysLocked(dump);
1854 dumpRawAxes(dump);
1855 dumpCalibration(dump);
1856 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08001857
Jeff Brown511ee5f2010-10-18 13:32:20 -07001858 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001859 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1860 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1861 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1862 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1863 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1864 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1865 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1866 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1867 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1868 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1869 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001870 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001871 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mLocked.distanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001872
1873 dump.appendFormat(INDENT3 "Last Touch:\n");
Jeff Brownace13b12011-03-09 17:39:48 -08001874 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
Jeff Brownaba321a2011-06-28 20:34:40 -07001875 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
1876 for (uint32_t i = 0; i < mLastTouch.pointerCount; i++) {
1877 const PointerData& pointer = mLastTouch.pointers[i];
1878 dump.appendFormat(INDENT5 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
1879 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
1880 "orientation=%d, distance=%d, isStylus=%s\n", i,
1881 pointer.id, pointer.x, pointer.y, pointer.pressure,
1882 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor, pointer.toolMinor,
1883 pointer.orientation, pointer.distance, toString(pointer.isStylus));
1884 }
Jeff Brownace13b12011-03-09 17:39:48 -08001885
1886 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
1887 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
1888 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
1889 mLocked.pointerGestureXMovementScale);
1890 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
1891 mLocked.pointerGestureYMovementScale);
1892 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
1893 mLocked.pointerGestureXZoomScale);
1894 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
1895 mLocked.pointerGestureYZoomScale);
Jeff Brown2352b972011-04-12 22:39:53 -07001896 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
1897 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brownace13b12011-03-09 17:39:48 -08001898 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07001899 } // release lock
1900}
1901
Jeff Brown6328cdc2010-07-29 18:18:33 -07001902void TouchInputMapper::initializeLocked() {
1903 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001904 mLastTouch.clear();
1905 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001906
Jeff Brown6328cdc2010-07-29 18:18:33 -07001907 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001908
1909 mLocked.orientedRanges.havePressure = false;
1910 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001911 mLocked.orientedRanges.haveTouchSize = false;
1912 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001913 mLocked.orientedRanges.haveOrientation = false;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001914 mLocked.orientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08001915
1916 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07001917}
1918
Jeff Brown474dcb52011-06-14 20:22:50 -07001919void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1920 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001921
Jeff Brown474dcb52011-06-14 20:22:50 -07001922 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001923
Jeff Brown474dcb52011-06-14 20:22:50 -07001924 if (!changes) { // first time only
1925 // Configure basic parameters.
1926 configureParameters();
1927
1928 // Configure sources.
1929 switch (mParameters.deviceType) {
1930 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1931 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
1932 mPointerSource = 0;
1933 break;
1934 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1935 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1936 mPointerSource = 0;
1937 break;
1938 case Parameters::DEVICE_TYPE_POINTER:
1939 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1940 mPointerSource = AINPUT_SOURCE_MOUSE;
1941 break;
1942 default:
1943 LOG_ASSERT(false);
1944 }
1945
1946 // Configure absolute axis information.
1947 configureRawAxes();
1948
1949 // Prepare input device calibration.
1950 parseCalibration();
1951 resolveCalibration();
1952
1953 { // acquire lock
1954 AutoMutex _l(mLock);
1955
1956 // Configure surface dimensions and orientation.
1957 configureSurfaceLocked();
1958 } // release lock
Jeff Brown83c09682010-12-23 17:50:18 -08001959 }
1960
Jeff Brown474dcb52011-06-14 20:22:50 -07001961 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1962 mPointerGesture.pointerVelocityControl.setParameters(
1963 mConfig.pointerVelocityControlParameters);
1964 }
Jeff Brown8d608662010-08-30 03:02:23 -07001965
Jeff Brown474dcb52011-06-14 20:22:50 -07001966 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
1967 // Reset the touch screen when pointer gesture enablement changes.
1968 reset();
1969 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970}
1971
Jeff Brown8d608662010-08-30 03:02:23 -07001972void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07001973 // Use the pointer presentation mode for devices that do not support distinct
1974 // multitouch. The spot-based presentation relies on being able to accurately
1975 // locate two or more fingers on the touch pad.
1976 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
1977 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07001978
Jeff Brown538881e2011-05-25 18:23:38 -07001979 String8 gestureModeString;
1980 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
1981 gestureModeString)) {
1982 if (gestureModeString == "pointer") {
1983 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
1984 } else if (gestureModeString == "spots") {
1985 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1986 } else if (gestureModeString != "default") {
1987 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
1988 }
1989 }
1990
Jeff Brownace13b12011-03-09 17:39:48 -08001991 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
1992 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
1993 // The device is a cursor device with a touch pad attached.
1994 // By default don't use the touch pad to move the pointer.
1995 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001996 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
1997 // The device is a pointing device like a track pad.
1998 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
1999 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2000 // The device is a touch screen.
2001 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002002 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002003 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002004 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2005 }
2006
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002007 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002008 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2009 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002010 if (deviceTypeString == "touchScreen") {
2011 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002012 } else if (deviceTypeString == "touchPad") {
2013 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002014 } else if (deviceTypeString == "pointer") {
2015 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002016 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002017 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2018 }
2019 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002020
Jeff Brownefd32662011-03-08 15:13:06 -08002021 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002022 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2023 mParameters.orientationAware);
2024
Jeff Brownefd32662011-03-08 15:13:06 -08002025 mParameters.associatedDisplayId = mParameters.orientationAware
2026 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownace13b12011-03-09 17:39:48 -08002027 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08002028 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07002029}
2030
Jeff Brownef3d7e82010-09-30 14:33:04 -07002031void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002032 dump.append(INDENT3 "Parameters:\n");
2033
Jeff Brown538881e2011-05-25 18:23:38 -07002034 switch (mParameters.gestureMode) {
2035 case Parameters::GESTURE_MODE_POINTER:
2036 dump.append(INDENT4 "GestureMode: pointer\n");
2037 break;
2038 case Parameters::GESTURE_MODE_SPOTS:
2039 dump.append(INDENT4 "GestureMode: spots\n");
2040 break;
2041 default:
2042 assert(false);
2043 }
2044
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002045 switch (mParameters.deviceType) {
2046 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2047 dump.append(INDENT4 "DeviceType: touchScreen\n");
2048 break;
2049 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2050 dump.append(INDENT4 "DeviceType: touchPad\n");
2051 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002052 case Parameters::DEVICE_TYPE_POINTER:
2053 dump.append(INDENT4 "DeviceType: pointer\n");
2054 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002055 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002056 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002057 }
2058
2059 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2060 mParameters.associatedDisplayId);
2061 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2062 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002063}
2064
Jeff Brown8d608662010-08-30 03:02:23 -07002065void TouchInputMapper::configureRawAxes() {
2066 mRawAxes.x.clear();
2067 mRawAxes.y.clear();
2068 mRawAxes.pressure.clear();
2069 mRawAxes.touchMajor.clear();
2070 mRawAxes.touchMinor.clear();
2071 mRawAxes.toolMajor.clear();
2072 mRawAxes.toolMinor.clear();
2073 mRawAxes.orientation.clear();
Jeff Brown80fd47c2011-05-24 01:07:44 -07002074 mRawAxes.distance.clear();
2075 mRawAxes.trackingId.clear();
2076 mRawAxes.slot.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002077}
2078
Jeff Brownef3d7e82010-09-30 14:33:04 -07002079void TouchInputMapper::dumpRawAxes(String8& dump) {
2080 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002081 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2082 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2083 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2084 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2085 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2086 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2087 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2088 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown80fd47c2011-05-24 01:07:44 -07002089 dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance");
2090 dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId");
2091 dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002092}
2093
Jeff Brown6328cdc2010-07-29 18:18:33 -07002094bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002095 // Ensure we have valid X and Y axes.
2096 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2097 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2098 "The device will be inoperable.", getDeviceName().string());
2099 return false;
2100 }
2101
Jeff Brown6d0fec22010-07-23 21:28:06 -07002102 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002103 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002104 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2105 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002106
2107 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002108 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002109 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002110 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2111 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002112 return false;
2113 }
Jeff Brownefd32662011-03-08 15:13:06 -08002114
2115 // A touch screen inherits the dimensions of the display.
2116 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2117 width = mLocked.associatedDisplayWidth;
2118 height = mLocked.associatedDisplayHeight;
2119 }
2120
2121 // The device inherits the orientation of the display if it is orientation aware.
2122 if (mParameters.orientationAware) {
2123 orientation = mLocked.associatedDisplayOrientation;
2124 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002125 }
2126
Jeff Brownace13b12011-03-09 17:39:48 -08002127 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2128 && mPointerController == NULL) {
2129 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2130 }
2131
Jeff Brown6328cdc2010-07-29 18:18:33 -07002132 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002133 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002134 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002135 }
2136
Jeff Brown6328cdc2010-07-29 18:18:33 -07002137 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002138 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002139 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002140 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002141
Jeff Brown6328cdc2010-07-29 18:18:33 -07002142 mLocked.surfaceWidth = width;
2143 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002144
Jeff Brown8d608662010-08-30 03:02:23 -07002145 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002146 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2147 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2148 mLocked.xPrecision = 1.0f / mLocked.xScale;
2149 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002150
Jeff Brownefd32662011-03-08 15:13:06 -08002151 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2152 mLocked.orientedRanges.x.source = mTouchSource;
2153 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2154 mLocked.orientedRanges.y.source = mTouchSource;
2155
Jeff Brown9626b142011-03-03 02:09:54 -08002156 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002157
Jeff Brown8d608662010-08-30 03:02:23 -07002158 // Scale factor for terms that are not oriented in a particular axis.
2159 // If the pixels are square then xScale == yScale otherwise we fake it
2160 // by choosing an average.
2161 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002162
Jeff Brown8d608662010-08-30 03:02:23 -07002163 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002164 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002165
Jeff Brown8d608662010-08-30 03:02:23 -07002166 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002167 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2168 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002169
2170 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2171 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002172 mLocked.orientedRanges.touchMajor.min = 0;
2173 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2174 mLocked.orientedRanges.touchMajor.flat = 0;
2175 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002176
Jeff Brown8d608662010-08-30 03:02:23 -07002177 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002178 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002179 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002180
Jeff Brown8d608662010-08-30 03:02:23 -07002181 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002182 mLocked.toolSizeLinearScale = 0;
2183 mLocked.toolSizeLinearBias = 0;
2184 mLocked.toolSizeAreaScale = 0;
2185 mLocked.toolSizeAreaBias = 0;
2186 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2187 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2188 if (mCalibration.haveToolSizeLinearScale) {
2189 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002190 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002191 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002192 / mRawAxes.toolMajor.maxValue;
2193 }
2194
Jeff Brownc6d282b2010-10-14 21:42:15 -07002195 if (mCalibration.haveToolSizeLinearBias) {
2196 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2197 }
2198 } else if (mCalibration.toolSizeCalibration ==
2199 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2200 if (mCalibration.haveToolSizeLinearScale) {
2201 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2202 } else {
2203 mLocked.toolSizeLinearScale = min(width, height);
2204 }
2205
2206 if (mCalibration.haveToolSizeLinearBias) {
2207 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2208 }
2209
2210 if (mCalibration.haveToolSizeAreaScale) {
2211 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2212 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2213 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2214 }
2215
2216 if (mCalibration.haveToolSizeAreaBias) {
2217 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002218 }
2219 }
2220
Jeff Brownc6d282b2010-10-14 21:42:15 -07002221 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002222
2223 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2224 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002225 mLocked.orientedRanges.toolMajor.min = 0;
2226 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2227 mLocked.orientedRanges.toolMajor.flat = 0;
2228 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002229
Jeff Brown8d608662010-08-30 03:02:23 -07002230 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002231 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002232 }
2233
2234 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002235 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002236 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2237 RawAbsoluteAxisInfo rawPressureAxis;
2238 switch (mCalibration.pressureSource) {
2239 case Calibration::PRESSURE_SOURCE_PRESSURE:
2240 rawPressureAxis = mRawAxes.pressure;
2241 break;
2242 case Calibration::PRESSURE_SOURCE_TOUCH:
2243 rawPressureAxis = mRawAxes.touchMajor;
2244 break;
2245 default:
2246 rawPressureAxis.clear();
2247 }
2248
Jeff Brown8d608662010-08-30 03:02:23 -07002249 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2250 || mCalibration.pressureCalibration
2251 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2252 if (mCalibration.havePressureScale) {
2253 mLocked.pressureScale = mCalibration.pressureScale;
2254 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2255 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2256 }
2257 }
2258
2259 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002260
2261 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2262 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002263 mLocked.orientedRanges.pressure.min = 0;
2264 mLocked.orientedRanges.pressure.max = 1.0;
2265 mLocked.orientedRanges.pressure.flat = 0;
2266 mLocked.orientedRanges.pressure.fuzz = 0;
2267 }
2268
2269 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002270 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002271 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002272 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2273 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2274 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2275 }
2276 }
2277
2278 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002279
2280 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2281 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002282 mLocked.orientedRanges.size.min = 0;
2283 mLocked.orientedRanges.size.max = 1.0;
2284 mLocked.orientedRanges.size.flat = 0;
2285 mLocked.orientedRanges.size.fuzz = 0;
2286 }
2287
2288 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002289 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002290 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002291 if (mCalibration.orientationCalibration
2292 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2293 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2294 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2295 }
2296 }
2297
Jeff Brown80fd47c2011-05-24 01:07:44 -07002298 mLocked.orientedRanges.haveOrientation = true;
2299
Jeff Brownefd32662011-03-08 15:13:06 -08002300 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2301 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002302 mLocked.orientedRanges.orientation.min = - M_PI_2;
2303 mLocked.orientedRanges.orientation.max = M_PI_2;
2304 mLocked.orientedRanges.orientation.flat = 0;
2305 mLocked.orientedRanges.orientation.fuzz = 0;
2306 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002307
2308 // Distance
2309 mLocked.distanceScale = 0;
2310 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2311 if (mCalibration.distanceCalibration
2312 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2313 if (mCalibration.haveDistanceScale) {
2314 mLocked.distanceScale = mCalibration.distanceScale;
2315 } else {
2316 mLocked.distanceScale = 1.0f;
2317 }
2318 }
2319
2320 mLocked.orientedRanges.haveDistance = true;
2321
2322 mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2323 mLocked.orientedRanges.distance.source = mTouchSource;
2324 mLocked.orientedRanges.distance.min =
2325 mRawAxes.distance.minValue * mLocked.distanceScale;
2326 mLocked.orientedRanges.distance.max =
2327 mRawAxes.distance.minValue * mLocked.distanceScale;
2328 mLocked.orientedRanges.distance.flat = 0;
2329 mLocked.orientedRanges.distance.fuzz =
2330 mRawAxes.distance.fuzz * mLocked.distanceScale;
2331 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002332 }
2333
2334 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002335 // Compute oriented surface dimensions, precision, scales and ranges.
2336 // Note that the maximum value reported is an inclusive maximum value so it is one
2337 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002338 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002339 case DISPLAY_ORIENTATION_90:
2340 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002341 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2342 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002343
Jeff Brown6328cdc2010-07-29 18:18:33 -07002344 mLocked.orientedXPrecision = mLocked.yPrecision;
2345 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002346
2347 mLocked.orientedRanges.x.min = 0;
2348 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2349 * mLocked.yScale;
2350 mLocked.orientedRanges.x.flat = 0;
2351 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2352
2353 mLocked.orientedRanges.y.min = 0;
2354 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2355 * mLocked.xScale;
2356 mLocked.orientedRanges.y.flat = 0;
2357 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002358 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002359
Jeff Brown6d0fec22010-07-23 21:28:06 -07002360 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002361 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2362 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002363
Jeff Brown6328cdc2010-07-29 18:18:33 -07002364 mLocked.orientedXPrecision = mLocked.xPrecision;
2365 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002366
2367 mLocked.orientedRanges.x.min = 0;
2368 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2369 * mLocked.xScale;
2370 mLocked.orientedRanges.x.flat = 0;
2371 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2372
2373 mLocked.orientedRanges.y.min = 0;
2374 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2375 * mLocked.yScale;
2376 mLocked.orientedRanges.y.flat = 0;
2377 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002378 break;
2379 }
Jeff Brownace13b12011-03-09 17:39:48 -08002380
2381 // Compute pointer gesture detection parameters.
2382 // TODO: These factors should not be hardcoded.
2383 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2384 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2385 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002386 float rawDiagonal = hypotf(rawWidth, rawHeight);
2387 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2388 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002389
Jeff Brown2352b972011-04-12 22:39:53 -07002390 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002391 // given area relative to the diagonal size of the display when no acceleration
2392 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002393 // Assume that the touch pad has a square aspect ratio such that movements in
2394 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown474dcb52011-06-14 20:22:50 -07002395 mLocked.pointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002396 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002397 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2398
2399 // Scale zooms to cover a smaller range of the display than movements do.
2400 // This value determines the area around the pointer that is affected by freeform
2401 // pointer gestures.
Jeff Brown474dcb52011-06-14 20:22:50 -07002402 mLocked.pointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002403 * displayDiagonal / rawDiagonal;
2404 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002405
Jeff Brown2352b972011-04-12 22:39:53 -07002406 // Max width between pointers to detect a swipe gesture is more than some fraction
2407 // of the diagonal axis of the touch pad. Touches that are wider than this are
2408 // translated into freeform gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002409 mLocked.pointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002410 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002411
2412 // Reset the current pointer gesture.
2413 mPointerGesture.reset();
2414
2415 // Remove any current spots.
2416 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2417 mPointerController->clearSpots();
2418 }
Jeff Brownace13b12011-03-09 17:39:48 -08002419 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002420 }
2421
2422 return true;
2423}
2424
Jeff Brownef3d7e82010-09-30 14:33:04 -07002425void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2426 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2427 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2428 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002429}
2430
Jeff Brown6328cdc2010-07-29 18:18:33 -07002431void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002432 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002433 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002434
Jeff Brown6328cdc2010-07-29 18:18:33 -07002435 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002436
Jeff Brown6328cdc2010-07-29 18:18:33 -07002437 if (virtualKeyDefinitions.size() == 0) {
2438 return;
2439 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002440
Jeff Brown6328cdc2010-07-29 18:18:33 -07002441 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2442
Jeff Brown8d608662010-08-30 03:02:23 -07002443 int32_t touchScreenLeft = mRawAxes.x.minValue;
2444 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002445 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2446 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002447
2448 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002449 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002450 virtualKeyDefinitions[i];
2451
2452 mLocked.virtualKeys.add();
2453 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2454
2455 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2456 int32_t keyCode;
2457 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002458 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002459 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002460 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2461 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002462 mLocked.virtualKeys.pop(); // drop the key
2463 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002464 }
2465
Jeff Brown6328cdc2010-07-29 18:18:33 -07002466 virtualKey.keyCode = keyCode;
2467 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002468
Jeff Brown6328cdc2010-07-29 18:18:33 -07002469 // convert the key definition's display coordinates into touch coordinates for a hit box
2470 int32_t halfWidth = virtualKeyDefinition.width / 2;
2471 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002472
Jeff Brown6328cdc2010-07-29 18:18:33 -07002473 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2474 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2475 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2476 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2477 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2478 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2479 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2480 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002481 }
2482}
2483
2484void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2485 if (!mLocked.virtualKeys.isEmpty()) {
2486 dump.append(INDENT3 "Virtual Keys:\n");
2487
2488 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2489 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2490 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2491 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2492 i, virtualKey.scanCode, virtualKey.keyCode,
2493 virtualKey.hitLeft, virtualKey.hitRight,
2494 virtualKey.hitTop, virtualKey.hitBottom);
2495 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002496 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002497}
2498
Jeff Brown8d608662010-08-30 03:02:23 -07002499void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002500 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002501 Calibration& out = mCalibration;
2502
Jeff Brownc6d282b2010-10-14 21:42:15 -07002503 // Touch Size
2504 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2505 String8 touchSizeCalibrationString;
2506 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2507 if (touchSizeCalibrationString == "none") {
2508 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2509 } else if (touchSizeCalibrationString == "geometric") {
2510 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2511 } else if (touchSizeCalibrationString == "pressure") {
2512 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2513 } else if (touchSizeCalibrationString != "default") {
2514 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2515 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002516 }
2517 }
2518
Jeff Brownc6d282b2010-10-14 21:42:15 -07002519 // Tool Size
2520 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2521 String8 toolSizeCalibrationString;
2522 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2523 if (toolSizeCalibrationString == "none") {
2524 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2525 } else if (toolSizeCalibrationString == "geometric") {
2526 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2527 } else if (toolSizeCalibrationString == "linear") {
2528 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2529 } else if (toolSizeCalibrationString == "area") {
2530 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2531 } else if (toolSizeCalibrationString != "default") {
2532 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2533 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002534 }
2535 }
2536
Jeff Brownc6d282b2010-10-14 21:42:15 -07002537 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2538 out.toolSizeLinearScale);
2539 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2540 out.toolSizeLinearBias);
2541 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2542 out.toolSizeAreaScale);
2543 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2544 out.toolSizeAreaBias);
2545 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2546 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002547
2548 // Pressure
2549 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2550 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002551 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002552 if (pressureCalibrationString == "none") {
2553 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2554 } else if (pressureCalibrationString == "physical") {
2555 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2556 } else if (pressureCalibrationString == "amplitude") {
2557 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2558 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002559 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002560 pressureCalibrationString.string());
2561 }
2562 }
2563
2564 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2565 String8 pressureSourceString;
2566 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2567 if (pressureSourceString == "pressure") {
2568 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2569 } else if (pressureSourceString == "touch") {
2570 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2571 } else if (pressureSourceString != "default") {
2572 LOGW("Invalid value for touch.pressure.source: '%s'",
2573 pressureSourceString.string());
2574 }
2575 }
2576
2577 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2578 out.pressureScale);
2579
2580 // Size
2581 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2582 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002583 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002584 if (sizeCalibrationString == "none") {
2585 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2586 } else if (sizeCalibrationString == "normalized") {
2587 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2588 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002589 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002590 sizeCalibrationString.string());
2591 }
2592 }
2593
2594 // Orientation
2595 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2596 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002597 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002598 if (orientationCalibrationString == "none") {
2599 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2600 } else if (orientationCalibrationString == "interpolated") {
2601 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002602 } else if (orientationCalibrationString == "vector") {
2603 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002604 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002605 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002606 orientationCalibrationString.string());
2607 }
2608 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002609
2610 // Distance
2611 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2612 String8 distanceCalibrationString;
2613 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2614 if (distanceCalibrationString == "none") {
2615 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2616 } else if (distanceCalibrationString == "scaled") {
2617 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2618 } else if (distanceCalibrationString != "default") {
2619 LOGW("Invalid value for touch.distance.calibration: '%s'",
2620 distanceCalibrationString.string());
2621 }
2622 }
2623
2624 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2625 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002626}
2627
2628void TouchInputMapper::resolveCalibration() {
2629 // Pressure
2630 switch (mCalibration.pressureSource) {
2631 case Calibration::PRESSURE_SOURCE_DEFAULT:
2632 if (mRawAxes.pressure.valid) {
2633 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2634 } else if (mRawAxes.touchMajor.valid) {
2635 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2636 }
2637 break;
2638
2639 case Calibration::PRESSURE_SOURCE_PRESSURE:
2640 if (! mRawAxes.pressure.valid) {
2641 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2642 "the pressure axis is not available.");
2643 }
2644 break;
2645
2646 case Calibration::PRESSURE_SOURCE_TOUCH:
2647 if (! mRawAxes.touchMajor.valid) {
2648 LOGW("Calibration property touch.pressure.source is 'touch' but "
2649 "the touchMajor axis is not available.");
2650 }
2651 break;
2652
2653 default:
2654 break;
2655 }
2656
2657 switch (mCalibration.pressureCalibration) {
2658 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2659 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2660 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2661 } else {
2662 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2663 }
2664 break;
2665
2666 default:
2667 break;
2668 }
2669
Jeff Brownc6d282b2010-10-14 21:42:15 -07002670 // Tool Size
2671 switch (mCalibration.toolSizeCalibration) {
2672 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002673 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002674 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002675 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002676 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002677 }
2678 break;
2679
2680 default:
2681 break;
2682 }
2683
Jeff Brownc6d282b2010-10-14 21:42:15 -07002684 // Touch Size
2685 switch (mCalibration.touchSizeCalibration) {
2686 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002687 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002688 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2689 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002690 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002691 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002692 }
2693 break;
2694
2695 default:
2696 break;
2697 }
2698
2699 // Size
2700 switch (mCalibration.sizeCalibration) {
2701 case Calibration::SIZE_CALIBRATION_DEFAULT:
2702 if (mRawAxes.toolMajor.valid) {
2703 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2704 } else {
2705 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2706 }
2707 break;
2708
2709 default:
2710 break;
2711 }
2712
2713 // Orientation
2714 switch (mCalibration.orientationCalibration) {
2715 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2716 if (mRawAxes.orientation.valid) {
2717 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2718 } else {
2719 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2720 }
2721 break;
2722
2723 default:
2724 break;
2725 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002726
2727 // Distance
2728 switch (mCalibration.distanceCalibration) {
2729 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
2730 if (mRawAxes.distance.valid) {
2731 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2732 } else {
2733 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2734 }
2735 break;
2736
2737 default:
2738 break;
2739 }
Jeff Brown8d608662010-08-30 03:02:23 -07002740}
2741
Jeff Brownef3d7e82010-09-30 14:33:04 -07002742void TouchInputMapper::dumpCalibration(String8& dump) {
2743 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002744
Jeff Brownc6d282b2010-10-14 21:42:15 -07002745 // Touch Size
2746 switch (mCalibration.touchSizeCalibration) {
2747 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2748 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002749 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002750 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2751 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002752 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002753 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2754 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002755 break;
2756 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002757 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002758 }
2759
Jeff Brownc6d282b2010-10-14 21:42:15 -07002760 // Tool Size
2761 switch (mCalibration.toolSizeCalibration) {
2762 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2763 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002764 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002765 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2766 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002767 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002768 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2769 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2770 break;
2771 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2772 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002773 break;
2774 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002775 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002776 }
2777
Jeff Brownc6d282b2010-10-14 21:42:15 -07002778 if (mCalibration.haveToolSizeLinearScale) {
2779 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2780 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002781 }
2782
Jeff Brownc6d282b2010-10-14 21:42:15 -07002783 if (mCalibration.haveToolSizeLinearBias) {
2784 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2785 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002786 }
2787
Jeff Brownc6d282b2010-10-14 21:42:15 -07002788 if (mCalibration.haveToolSizeAreaScale) {
2789 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2790 mCalibration.toolSizeAreaScale);
2791 }
2792
2793 if (mCalibration.haveToolSizeAreaBias) {
2794 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2795 mCalibration.toolSizeAreaBias);
2796 }
2797
2798 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002799 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002800 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002801 }
2802
2803 // Pressure
2804 switch (mCalibration.pressureCalibration) {
2805 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002806 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002807 break;
2808 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002809 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002810 break;
2811 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002812 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002813 break;
2814 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002815 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002816 }
2817
2818 switch (mCalibration.pressureSource) {
2819 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002820 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002821 break;
2822 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002823 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002824 break;
2825 case Calibration::PRESSURE_SOURCE_DEFAULT:
2826 break;
2827 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002828 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002829 }
2830
2831 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002832 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2833 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002834 }
2835
2836 // Size
2837 switch (mCalibration.sizeCalibration) {
2838 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002839 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002840 break;
2841 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002842 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002843 break;
2844 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002845 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002846 }
2847
2848 // Orientation
2849 switch (mCalibration.orientationCalibration) {
2850 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002851 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002852 break;
2853 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002854 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002855 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002856 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2857 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2858 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002859 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002860 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002861 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002862
2863 // Distance
2864 switch (mCalibration.distanceCalibration) {
2865 case Calibration::DISTANCE_CALIBRATION_NONE:
2866 dump.append(INDENT4 "touch.distance.calibration: none\n");
2867 break;
2868 case Calibration::DISTANCE_CALIBRATION_SCALED:
2869 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
2870 break;
2871 default:
2872 LOG_ASSERT(false);
2873 }
2874
2875 if (mCalibration.haveDistanceScale) {
2876 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
2877 mCalibration.distanceScale);
2878 }
Jeff Brown8d608662010-08-30 03:02:23 -07002879}
2880
Jeff Brown6d0fec22010-07-23 21:28:06 -07002881void TouchInputMapper::reset() {
2882 // Synthesize touch up event if touch is currently down.
2883 // This will also take care of finishing virtual key processing if needed.
2884 if (mLastTouch.pointerCount != 0) {
2885 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2886 mCurrentTouch.clear();
2887 syncTouch(when, true);
2888 }
2889
Jeff Brown6328cdc2010-07-29 18:18:33 -07002890 { // acquire lock
2891 AutoMutex _l(mLock);
2892 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07002893
2894 if (mPointerController != NULL
2895 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown474dcb52011-06-14 20:22:50 -07002896 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brown2352b972011-04-12 22:39:53 -07002897 mPointerController->clearSpots();
2898 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002899 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002900
Jeff Brown6328cdc2010-07-29 18:18:33 -07002901 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002902}
2903
2904void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07002905#if DEBUG_RAW_EVENTS
2906 if (!havePointerIds) {
2907 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2908 } else {
2909 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2910 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2911 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2912 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2913 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2914 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2915 }
2916#endif
2917
Jeff Brown6328cdc2010-07-29 18:18:33 -07002918 // Preprocess pointer data.
Jeff Brownaa3855d2011-03-17 01:34:19 -07002919 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002920 calculatePointerIds();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002921 }
2922
Jeff Brown56194eb2011-03-02 19:23:13 -08002923 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002924 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002925 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2926 // If this is a touch screen, hide the pointer on an initial down.
2927 getContext()->fadePointer();
2928 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002929
2930 // Initial downs on external touch devices should wake the device.
2931 // We don't do this for internal touch screens to prevent them from waking
2932 // up in your pocket.
2933 // TODO: Use the input device configuration to control this behavior more finely.
2934 if (getDevice()->isExternal()) {
2935 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2936 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002937 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002938
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002939 // Synthesize key down from buttons if needed.
2940 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
2941 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2942
2943 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07002944 TouchResult touchResult;
2945 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2946 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2947 // Drop spurious syncs.
2948 touchResult = DROP_STROKE;
2949 } else {
2950 // Process touches and virtual keys.
2951 touchResult = consumeOffScreenTouches(when, policyFlags);
2952 if (touchResult == DISPATCH_TOUCH) {
2953 suppressSwipeOntoVirtualKeys(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002954 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
Jeff Brown79ac9692011-04-19 21:20:10 -07002955 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
2956 }
2957 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08002958 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002959 }
2960
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002961 // Synthesize key up from buttons if needed.
2962 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
2963 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2964
Jeff Brown6328cdc2010-07-29 18:18:33 -07002965 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08002966 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002967 if (touchResult == DROP_STROKE) {
2968 mLastTouch.clear();
Jeff Browna4d1bc52011-07-01 19:23:40 -07002969 mLastTouch.buttonState = mCurrentTouch.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002970 } else {
Jeff Browna4d1bc52011-07-01 19:23:40 -07002971 mLastTouch.copyFrom(mCurrentTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002972 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002973}
2974
Jeff Brown79ac9692011-04-19 21:20:10 -07002975void TouchInputMapper::timeoutExpired(nsecs_t when) {
2976 if (mPointerController != NULL) {
2977 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
2978 }
2979}
2980
Jeff Brown6d0fec22010-07-23 21:28:06 -07002981TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2982 nsecs_t when, uint32_t policyFlags) {
2983 int32_t keyEventAction, keyEventFlags;
2984 int32_t keyCode, scanCode, downTime;
2985 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002986
Jeff Brown6328cdc2010-07-29 18:18:33 -07002987 { // acquire lock
2988 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002989
Jeff Brown6328cdc2010-07-29 18:18:33 -07002990 // Update surface size and orientation, including virtual key positions.
2991 if (! configureSurfaceLocked()) {
2992 return DROP_STROKE;
2993 }
2994
2995 // Check for virtual key press.
2996 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002997 if (mCurrentTouch.pointerCount == 0) {
2998 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002999 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003000#if DEBUG_VIRTUAL_KEYS
3001 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003002 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003003#endif
3004 keyEventAction = AKEY_EVENT_ACTION_UP;
3005 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3006 touchResult = SKIP_TOUCH;
3007 goto DispatchVirtualKey;
3008 }
3009
3010 if (mCurrentTouch.pointerCount == 1) {
3011 int32_t x = mCurrentTouch.pointers[0].x;
3012 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003013 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
3014 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003015 // Pointer is still within the space of the virtual key.
3016 return SKIP_TOUCH;
3017 }
3018 }
3019
3020 // Pointer left virtual key area or another pointer also went down.
3021 // Send key cancellation and drop the stroke so subsequent motions will be
3022 // considered fresh downs. This is useful when the user swipes away from the
3023 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003024 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003025#if DEBUG_VIRTUAL_KEYS
3026 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003027 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003028#endif
3029 keyEventAction = AKEY_EVENT_ACTION_UP;
3030 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3031 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07003032
3033 // Check whether the pointer moved inside the display area where we should
3034 // start a new stroke.
3035 int32_t x = mCurrentTouch.pointers[0].x;
3036 int32_t y = mCurrentTouch.pointers[0].y;
3037 if (isPointInsideSurfaceLocked(x, y)) {
3038 mLastTouch.clear();
3039 touchResult = DISPATCH_TOUCH;
3040 } else {
3041 touchResult = DROP_STROKE;
3042 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003043 } else {
3044 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
3045 // Pointer just went down. Handle off-screen touches, if needed.
3046 int32_t x = mCurrentTouch.pointers[0].x;
3047 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003048 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003049 // If exactly one pointer went down, check for virtual key hit.
3050 // Otherwise we will drop the entire stroke.
3051 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003052 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003053 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08003054 if (mContext->shouldDropVirtualKey(when, getDevice(),
3055 virtualKey->keyCode, virtualKey->scanCode)) {
3056 return DROP_STROKE;
3057 }
3058
Jeff Brown6328cdc2010-07-29 18:18:33 -07003059 mLocked.currentVirtualKey.down = true;
3060 mLocked.currentVirtualKey.downTime = when;
3061 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
3062 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003063#if DEBUG_VIRTUAL_KEYS
3064 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003065 mLocked.currentVirtualKey.keyCode,
3066 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003067#endif
3068 keyEventAction = AKEY_EVENT_ACTION_DOWN;
3069 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
3070 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3071 touchResult = SKIP_TOUCH;
3072 goto DispatchVirtualKey;
3073 }
3074 }
3075 return DROP_STROKE;
3076 }
3077 }
3078 return DISPATCH_TOUCH;
3079 }
3080
3081 DispatchVirtualKey:
3082 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003083 keyCode = mLocked.currentVirtualKey.keyCode;
3084 scanCode = mLocked.currentVirtualKey.scanCode;
3085 downTime = mLocked.currentVirtualKey.downTime;
3086 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003087
3088 // Dispatch virtual key.
3089 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07003090 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07003091 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3092 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3093 return touchResult;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003094}
3095
Jeff Brownefd32662011-03-08 15:13:06 -08003096void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08003097 // Disable all virtual key touches that happen within a short time interval of the
3098 // most recent touch. The idea is to filter out stray virtual key presses when
3099 // interacting with the touch screen.
3100 //
3101 // Problems we're trying to solve:
3102 //
3103 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3104 // virtual key area that is implemented by a separate touch panel and accidentally
3105 // triggers a virtual key.
3106 //
3107 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3108 // area and accidentally triggers a virtual key. This often happens when virtual keys
3109 // are layed out below the screen near to where the on screen keyboard's space bar
3110 // is displayed.
Jeff Brown474dcb52011-06-14 20:22:50 -07003111 if (mConfig.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
3112 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003113 }
3114}
3115
Jeff Brown6d0fec22010-07-23 21:28:06 -07003116void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3117 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3118 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003119 if (currentPointerCount == 0 && lastPointerCount == 0) {
3120 return; // nothing to do!
3121 }
3122
Jeff Brownace13b12011-03-09 17:39:48 -08003123 // Update current touch coordinates.
Jeff Brownace13b12011-03-09 17:39:48 -08003124 float xPrecision, yPrecision;
Jeff Browna6111372011-07-14 21:48:23 -07003125 prepareTouches(&xPrecision, &yPrecision);
Jeff Brownace13b12011-03-09 17:39:48 -08003126
3127 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003128 BitSet32 currentIdBits = mCurrentTouch.idBits;
3129 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003130 int32_t metaState = getContext()->getGlobalMetaState();
3131 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003132
3133 if (currentIdBits == lastIdBits) {
3134 // No pointer id changes so this is a move event.
3135 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003136 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003137 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3138 AMOTION_EVENT_EDGE_FLAG_NONE,
3139 mCurrentTouchProperties, mCurrentTouchCoords,
3140 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003141 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003142 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003143 // There may be pointers going up and pointers going down and pointers moving
3144 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003145 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3146 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003147 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003148 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003149
Jeff Brownace13b12011-03-09 17:39:48 -08003150 // Update last coordinates of pointers that have moved so that we observe the new
3151 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003152 bool moveNeeded = updateMovedPointers(
3153 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3154 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003155 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003156 if (buttonState != mLastTouch.buttonState) {
3157 moveNeeded = true;
3158 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003159
Jeff Brownace13b12011-03-09 17:39:48 -08003160 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003161 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003162 uint32_t upId = upIdBits.firstMarkedBit();
3163 upIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003164
Jeff Brownace13b12011-03-09 17:39:48 -08003165 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003166 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3167 mLastTouchProperties, mLastTouchCoords,
3168 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003169 xPrecision, yPrecision, mDownTime);
3170 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003171 }
3172
Jeff Brownc3db8582010-10-20 15:33:38 -07003173 // Dispatch move events if any of the remaining pointers moved from their old locations.
3174 // Although applications receive new locations as part of individual pointer up
3175 // events, they do not generally handle them except when presented in a move event.
3176 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003177 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003178 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003179 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3180 mCurrentTouchProperties, mCurrentTouchCoords,
3181 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003182 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003183 }
3184
3185 // Dispatch pointer down events using the new pointer locations.
3186 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003187 uint32_t downId = downIdBits.firstMarkedBit();
3188 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003189 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003190
Jeff Brownace13b12011-03-09 17:39:48 -08003191 if (dispatchedIdBits.count() == 1) {
3192 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003193 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003194 }
3195
Jeff Brownace13b12011-03-09 17:39:48 -08003196 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003197 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003198 mCurrentTouchProperties, mCurrentTouchCoords,
3199 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003200 xPrecision, yPrecision, mDownTime);
3201 }
3202 }
3203
3204 // Update state for next time.
3205 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003206 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003207 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3208 }
3209}
3210
Jeff Browna6111372011-07-14 21:48:23 -07003211void TouchInputMapper::prepareTouches(float* outXPrecision, float* outYPrecision) {
Jeff Brownace13b12011-03-09 17:39:48 -08003212 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3213 uint32_t lastPointerCount = mLastTouch.pointerCount;
3214
3215 AutoMutex _l(mLock);
3216
3217 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3218 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3219 for (uint32_t i = 0; i < currentPointerCount; i++) {
3220 const PointerData& in = mCurrentTouch.pointers[i];
3221
3222 // ToolMajor and ToolMinor
3223 float toolMajor, toolMinor;
3224 switch (mCalibration.toolSizeCalibration) {
3225 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3226 toolMajor = in.toolMajor * mLocked.geometricScale;
3227 if (mRawAxes.toolMinor.valid) {
3228 toolMinor = in.toolMinor * mLocked.geometricScale;
3229 } else {
3230 toolMinor = toolMajor;
3231 }
3232 break;
3233 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3234 toolMajor = in.toolMajor != 0
3235 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3236 : 0;
3237 if (mRawAxes.toolMinor.valid) {
3238 toolMinor = in.toolMinor != 0
3239 ? in.toolMinor * mLocked.toolSizeLinearScale
3240 + mLocked.toolSizeLinearBias
3241 : 0;
3242 } else {
3243 toolMinor = toolMajor;
3244 }
3245 break;
3246 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3247 if (in.toolMajor != 0) {
3248 float diameter = sqrtf(in.toolMajor
3249 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3250 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3251 } else {
3252 toolMajor = 0;
3253 }
3254 toolMinor = toolMajor;
3255 break;
3256 default:
3257 toolMajor = 0;
3258 toolMinor = 0;
3259 break;
3260 }
3261
3262 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3263 toolMajor /= currentPointerCount;
3264 toolMinor /= currentPointerCount;
3265 }
3266
3267 // Pressure
3268 float rawPressure;
3269 switch (mCalibration.pressureSource) {
3270 case Calibration::PRESSURE_SOURCE_PRESSURE:
3271 rawPressure = in.pressure;
3272 break;
3273 case Calibration::PRESSURE_SOURCE_TOUCH:
3274 rawPressure = in.touchMajor;
3275 break;
3276 default:
3277 rawPressure = 0;
3278 }
3279
3280 float pressure;
3281 switch (mCalibration.pressureCalibration) {
3282 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3283 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3284 pressure = rawPressure * mLocked.pressureScale;
3285 break;
3286 default:
3287 pressure = 1;
3288 break;
3289 }
3290
3291 // TouchMajor and TouchMinor
3292 float touchMajor, touchMinor;
3293 switch (mCalibration.touchSizeCalibration) {
3294 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3295 touchMajor = in.touchMajor * mLocked.geometricScale;
3296 if (mRawAxes.touchMinor.valid) {
3297 touchMinor = in.touchMinor * mLocked.geometricScale;
3298 } else {
3299 touchMinor = touchMajor;
3300 }
3301 break;
3302 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3303 touchMajor = toolMajor * pressure;
3304 touchMinor = toolMinor * pressure;
3305 break;
3306 default:
3307 touchMajor = 0;
3308 touchMinor = 0;
3309 break;
3310 }
3311
3312 if (touchMajor > toolMajor) {
3313 touchMajor = toolMajor;
3314 }
3315 if (touchMinor > toolMinor) {
3316 touchMinor = toolMinor;
3317 }
3318
3319 // Size
3320 float size;
3321 switch (mCalibration.sizeCalibration) {
3322 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3323 float rawSize = mRawAxes.toolMinor.valid
3324 ? avg(in.toolMajor, in.toolMinor)
3325 : in.toolMajor;
3326 size = rawSize * mLocked.sizeScale;
3327 break;
3328 }
3329 default:
3330 size = 0;
3331 break;
3332 }
3333
3334 // Orientation
3335 float orientation;
3336 switch (mCalibration.orientationCalibration) {
3337 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3338 orientation = in.orientation * mLocked.orientationScale;
3339 break;
3340 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3341 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3342 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3343 if (c1 != 0 || c2 != 0) {
3344 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003345 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003346 touchMajor *= scale;
3347 touchMinor /= scale;
3348 toolMajor *= scale;
3349 toolMinor /= scale;
3350 } else {
3351 orientation = 0;
3352 }
3353 break;
3354 }
3355 default:
3356 orientation = 0;
3357 }
3358
Jeff Brown80fd47c2011-05-24 01:07:44 -07003359 // Distance
3360 float distance;
3361 switch (mCalibration.distanceCalibration) {
3362 case Calibration::DISTANCE_CALIBRATION_SCALED:
3363 distance = in.distance * mLocked.distanceScale;
3364 break;
3365 default:
3366 distance = 0;
3367 }
3368
Jeff Brownace13b12011-03-09 17:39:48 -08003369 // X and Y
3370 // Adjust coords for surface orientation.
3371 float x, y;
3372 switch (mLocked.surfaceOrientation) {
3373 case DISPLAY_ORIENTATION_90:
3374 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3375 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3376 orientation -= M_PI_2;
3377 if (orientation < - M_PI_2) {
3378 orientation += M_PI;
3379 }
3380 break;
3381 case DISPLAY_ORIENTATION_180:
3382 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3383 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3384 break;
3385 case DISPLAY_ORIENTATION_270:
3386 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3387 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3388 orientation += M_PI_2;
3389 if (orientation > M_PI_2) {
3390 orientation -= M_PI;
3391 }
3392 break;
3393 default:
3394 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3395 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3396 break;
3397 }
3398
3399 // Write output coords.
3400 PointerCoords& out = mCurrentTouchCoords[i];
3401 out.clear();
3402 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3403 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3404 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3405 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3406 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3407 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3408 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3409 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3410 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003411 if (distance != 0) {
3412 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
3413 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003414
3415 // Write output properties.
3416 PointerProperties& properties = mCurrentTouchProperties[i];
3417 properties.clear();
3418 properties.id = mCurrentTouch.pointers[i].id;
3419 properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus);
Jeff Brownace13b12011-03-09 17:39:48 -08003420 }
3421
Jeff Brownace13b12011-03-09 17:39:48 -08003422 *outXPrecision = mLocked.orientedXPrecision;
3423 *outYPrecision = mLocked.orientedYPrecision;
3424}
3425
Jeff Brown79ac9692011-04-19 21:20:10 -07003426void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3427 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003428 // Update current gesture coordinates.
3429 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003430 bool sendEvents = preparePointerGestures(when,
3431 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3432 if (!sendEvents) {
3433 return;
3434 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003435 if (finishPreviousGesture) {
3436 cancelPreviousGesture = false;
3437 }
Jeff Brownace13b12011-03-09 17:39:48 -08003438
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003439 // Update the pointer presentation and spots.
3440 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3441 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3442 if (finishPreviousGesture || cancelPreviousGesture) {
3443 mPointerController->clearSpots();
3444 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003445 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3446 mPointerGesture.currentGestureIdToIndex,
3447 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003448 } else {
3449 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3450 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003451
Jeff Brown538881e2011-05-25 18:23:38 -07003452 // Show or hide the pointer if needed.
3453 switch (mPointerGesture.currentGestureMode) {
3454 case PointerGesture::NEUTRAL:
3455 case PointerGesture::QUIET:
3456 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3457 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3458 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3459 // Remind the user of where the pointer is after finishing a gesture with spots.
3460 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3461 }
3462 break;
3463 case PointerGesture::TAP:
3464 case PointerGesture::TAP_DRAG:
3465 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3466 case PointerGesture::HOVER:
3467 case PointerGesture::PRESS:
3468 // Unfade the pointer when the current gesture manipulates the
3469 // area directly under the pointer.
3470 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3471 break;
3472 case PointerGesture::SWIPE:
3473 case PointerGesture::FREEFORM:
3474 // Fade the pointer when the current gesture manipulates a different
3475 // area and there are spots to guide the user experience.
3476 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3477 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3478 } else {
3479 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3480 }
3481 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003482 }
3483
Jeff Brownace13b12011-03-09 17:39:48 -08003484 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003485 int32_t metaState = getContext()->getGlobalMetaState();
3486 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003487
3488 // Update last coordinates of pointers that have moved so that we observe the new
3489 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003490 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3491 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3492 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003493 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003494 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3495 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3496 bool moveNeeded = false;
3497 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003498 && !mPointerGesture.lastGestureIdBits.isEmpty()
3499 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003500 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3501 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003502 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003503 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003504 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003505 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3506 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003507 if (buttonState != mLastTouch.buttonState) {
3508 moveNeeded = true;
3509 }
Jeff Brownace13b12011-03-09 17:39:48 -08003510 }
3511
3512 // Send motion events for all pointers that went up or were canceled.
3513 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3514 if (!dispatchedGestureIdBits.isEmpty()) {
3515 if (cancelPreviousGesture) {
3516 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003517 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3518 AMOTION_EVENT_EDGE_FLAG_NONE,
3519 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003520 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3521 dispatchedGestureIdBits, -1,
3522 0, 0, mPointerGesture.downTime);
3523
3524 dispatchedGestureIdBits.clear();
3525 } else {
3526 BitSet32 upGestureIdBits;
3527 if (finishPreviousGesture) {
3528 upGestureIdBits = dispatchedGestureIdBits;
3529 } else {
3530 upGestureIdBits.value = dispatchedGestureIdBits.value
3531 & ~mPointerGesture.currentGestureIdBits.value;
3532 }
3533 while (!upGestureIdBits.isEmpty()) {
3534 uint32_t id = upGestureIdBits.firstMarkedBit();
3535 upGestureIdBits.clearBit(id);
3536
3537 dispatchMotion(when, policyFlags, mPointerSource,
3538 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003539 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3540 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003541 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3542 dispatchedGestureIdBits, id,
3543 0, 0, mPointerGesture.downTime);
3544
3545 dispatchedGestureIdBits.clearBit(id);
3546 }
3547 }
3548 }
3549
3550 // Send motion events for all pointers that moved.
3551 if (moveNeeded) {
3552 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003553 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3554 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003555 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3556 dispatchedGestureIdBits, -1,
3557 0, 0, mPointerGesture.downTime);
3558 }
3559
3560 // Send motion events for all pointers that went down.
3561 if (down) {
3562 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3563 & ~dispatchedGestureIdBits.value);
3564 while (!downGestureIdBits.isEmpty()) {
3565 uint32_t id = downGestureIdBits.firstMarkedBit();
3566 downGestureIdBits.clearBit(id);
3567 dispatchedGestureIdBits.markBit(id);
3568
Jeff Brownace13b12011-03-09 17:39:48 -08003569 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003570 mPointerGesture.downTime = when;
3571 }
3572
3573 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003574 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003575 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003576 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3577 dispatchedGestureIdBits, id,
3578 0, 0, mPointerGesture.downTime);
3579 }
3580 }
3581
Jeff Brownace13b12011-03-09 17:39:48 -08003582 // Send motion events for hover.
3583 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3584 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003585 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3586 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3587 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003588 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3589 mPointerGesture.currentGestureIdBits, -1,
3590 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003591 } else if (dispatchedGestureIdBits.isEmpty()
3592 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3593 // Synthesize a hover move event after all pointers go up to indicate that
3594 // the pointer is hovering again even if the user is not currently touching
3595 // the touch pad. This ensures that a view will receive a fresh hover enter
3596 // event after a tap.
3597 float x, y;
3598 mPointerController->getPosition(&x, &y);
3599
3600 PointerProperties pointerProperties;
3601 pointerProperties.clear();
3602 pointerProperties.id = 0;
3603 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
3604
3605 PointerCoords pointerCoords;
3606 pointerCoords.clear();
3607 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3608 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3609
3610 getDispatcher()->notifyMotion(when, getDeviceId(), mPointerSource, policyFlags,
3611 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3612 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3613 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003614 }
3615
3616 // Update state.
3617 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3618 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003619 mPointerGesture.lastGestureIdBits.clear();
3620 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003621 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3622 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3623 uint32_t id = idBits.firstMarkedBit();
3624 idBits.clearBit(id);
3625 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003626 mPointerGesture.lastGestureProperties[index].copyFrom(
3627 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003628 mPointerGesture.lastGestureCoords[index].copyFrom(
3629 mPointerGesture.currentGestureCoords[index]);
3630 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003631 }
3632 }
3633}
3634
Jeff Brown79ac9692011-04-19 21:20:10 -07003635bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3636 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003637 *outCancelPreviousGesture = false;
3638 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003639
Jeff Brownace13b12011-03-09 17:39:48 -08003640 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003641
Jeff Brown79ac9692011-04-19 21:20:10 -07003642 // Handle TAP timeout.
3643 if (isTimeout) {
3644#if DEBUG_GESTURES
3645 LOGD("Gestures: Processing timeout");
3646#endif
3647
3648 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003649 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003650 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003651 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003652 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003653 } else {
3654 // The tap is finished.
3655#if DEBUG_GESTURES
3656 LOGD("Gestures: TAP finished");
3657#endif
3658 *outFinishPreviousGesture = true;
3659
3660 mPointerGesture.activeGestureId = -1;
3661 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3662 mPointerGesture.currentGestureIdBits.clear();
3663
Jeff Brown19c97d462011-06-01 12:33:19 -07003664 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07003665 return true;
3666 }
3667 }
3668
3669 // We did not handle this timeout.
3670 return false;
3671 }
3672
Jeff Brownace13b12011-03-09 17:39:48 -08003673 // Update the velocity tracker.
3674 {
3675 VelocityTracker::Position positions[MAX_POINTERS];
3676 uint32_t count = 0;
3677 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003678 uint32_t id = idBits.firstMarkedBit();
3679 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003680 uint32_t index = mCurrentTouch.idToIndex[id];
3681 positions[count].x = mCurrentTouch.pointers[index].x
3682 * mLocked.pointerGestureXMovementScale;
3683 positions[count].y = mCurrentTouch.pointers[index].y
3684 * mLocked.pointerGestureYMovementScale;
3685 }
3686 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3687 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003688
Jeff Brownace13b12011-03-09 17:39:48 -08003689 // Pick a new active touch id if needed.
3690 // Choose an arbitrary pointer that just went down, if there is one.
3691 // Otherwise choose an arbitrary remaining pointer.
3692 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003693 // We keep the same active touch id for as long as possible.
3694 bool activeTouchChanged = false;
3695 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3696 int32_t activeTouchId = lastActiveTouchId;
3697 if (activeTouchId < 0) {
3698 if (!mCurrentTouch.idBits.isEmpty()) {
3699 activeTouchChanged = true;
3700 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3701 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003702 }
Jeff Brown2352b972011-04-12 22:39:53 -07003703 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3704 activeTouchChanged = true;
3705 if (!mCurrentTouch.idBits.isEmpty()) {
3706 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3707 } else {
3708 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003709 }
3710 }
3711
3712 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003713 bool isQuietTime = false;
3714 if (activeTouchId < 0) {
3715 mPointerGesture.resetQuietTime();
3716 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07003717 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07003718 if (!isQuietTime) {
3719 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3720 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3721 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3722 && mCurrentTouch.pointerCount < 2) {
3723 // Enter quiet time when exiting swipe or freeform state.
3724 // This is to prevent accidentally entering the hover state and flinging the
3725 // pointer when finishing a swipe and there is still one pointer left onscreen.
3726 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003727 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003728 && mCurrentTouch.pointerCount >= 2
3729 && !isPointerDown(mCurrentTouch.buttonState)) {
3730 // Enter quiet time when releasing the button and there are still two or more
3731 // fingers down. This may indicate that one finger was used to press the button
3732 // but it has not gone up yet.
3733 isQuietTime = true;
3734 }
3735 if (isQuietTime) {
3736 mPointerGesture.quietTime = when;
3737 }
Jeff Brownace13b12011-03-09 17:39:48 -08003738 }
3739 }
3740
3741 // Switch states based on button and pointer state.
3742 if (isQuietTime) {
3743 // Case 1: Quiet time. (QUIET)
3744#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003745 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003746 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003747#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003748 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
3749 *outFinishPreviousGesture = true;
3750 }
Jeff Brownace13b12011-03-09 17:39:48 -08003751
3752 mPointerGesture.activeGestureId = -1;
3753 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003754 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003755
Jeff Brown19c97d462011-06-01 12:33:19 -07003756 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08003757 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003758 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003759 // The pointer follows the active touch point.
3760 // Emit DOWN, MOVE, UP events at the pointer location.
3761 //
3762 // Only the active touch matters; other fingers are ignored. This policy helps
3763 // to handle the case where the user places a second finger on the touch pad
3764 // to apply the necessary force to depress an integrated button below the surface.
3765 // We don't want the second finger to be delivered to applications.
3766 //
3767 // For this to work well, we need to make sure to track the pointer that is really
3768 // active. If the user first puts one finger down to click then adds another
3769 // finger to drag then the active pointer should switch to the finger that is
3770 // being dragged.
3771#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003772 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08003773 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3774#endif
3775 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003776 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003777 *outFinishPreviousGesture = true;
3778 mPointerGesture.activeGestureId = 0;
3779 }
3780
3781 // Switch pointers if needed.
3782 // Find the fastest pointer and follow it.
Jeff Brown19c97d462011-06-01 12:33:19 -07003783 if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) {
3784 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07003785 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown19c97d462011-06-01 12:33:19 -07003786 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3787 uint32_t id = mCurrentTouch.pointers[i].id;
3788 float vx, vy;
3789 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3790 float speed = hypotf(vx, vy);
3791 if (speed > bestSpeed) {
3792 bestId = id;
3793 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08003794 }
Jeff Brown8d608662010-08-30 03:02:23 -07003795 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003796 }
3797 if (bestId >= 0 && bestId != activeTouchId) {
3798 mPointerGesture.activeTouchId = activeTouchId = bestId;
3799 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003800#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07003801 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3802 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08003803#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003804 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003805 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003806
Jeff Brown19c97d462011-06-01 12:33:19 -07003807 if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) {
3808 const PointerData& currentPointer =
3809 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3810 const PointerData& lastPointer =
3811 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3812 float deltaX = (currentPointer.x - lastPointer.x)
3813 * mLocked.pointerGestureXMovementScale;
3814 float deltaY = (currentPointer.y - lastPointer.y)
3815 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003816
Jeff Brown19c97d462011-06-01 12:33:19 -07003817 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3818
3819 // Move the pointer using a relative motion.
3820 // When using spots, the click will occur at the position of the anchor
3821 // spot and all other spots will move there.
3822 mPointerController->move(deltaX, deltaY);
3823 } else {
3824 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003825 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003826
Jeff Brownace13b12011-03-09 17:39:48 -08003827 float x, y;
3828 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003829
Jeff Brown79ac9692011-04-19 21:20:10 -07003830 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003831 mPointerGesture.currentGestureIdBits.clear();
3832 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3833 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003834 mPointerGesture.currentGestureProperties[0].clear();
3835 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3836 mPointerGesture.currentGestureProperties[0].toolType =
3837 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003838 mPointerGesture.currentGestureCoords[0].clear();
3839 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3840 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3841 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3842 } else if (mCurrentTouch.pointerCount == 0) {
3843 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003844 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
3845 *outFinishPreviousGesture = true;
3846 }
Jeff Brownace13b12011-03-09 17:39:48 -08003847
Jeff Brown79ac9692011-04-19 21:20:10 -07003848 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003849 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08003850 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003851 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3852 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07003853 && mLastTouch.pointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003854 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08003855 float x, y;
3856 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003857 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3858 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08003859#if DEBUG_GESTURES
3860 LOGD("Gestures: TAP");
3861#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003862
3863 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07003864 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07003865 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003866
Jeff Brownace13b12011-03-09 17:39:48 -08003867 mPointerGesture.activeGestureId = 0;
3868 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08003869 mPointerGesture.currentGestureIdBits.clear();
3870 mPointerGesture.currentGestureIdBits.markBit(
3871 mPointerGesture.activeGestureId);
3872 mPointerGesture.currentGestureIdToIndex[
3873 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003874 mPointerGesture.currentGestureProperties[0].clear();
3875 mPointerGesture.currentGestureProperties[0].id =
3876 mPointerGesture.activeGestureId;
3877 mPointerGesture.currentGestureProperties[0].toolType =
3878 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003879 mPointerGesture.currentGestureCoords[0].clear();
3880 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003881 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08003882 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003883 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003884 mPointerGesture.currentGestureCoords[0].setAxisValue(
3885 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003886
Jeff Brownace13b12011-03-09 17:39:48 -08003887 tapped = true;
3888 } else {
3889#if DEBUG_GESTURES
3890 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07003891 x - mPointerGesture.tapX,
3892 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003893#endif
3894 }
3895 } else {
3896#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003897 LOGD("Gestures: Not a TAP, %0.3fms since down",
3898 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003899#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003900 }
Jeff Brownace13b12011-03-09 17:39:48 -08003901 }
Jeff Brown2352b972011-04-12 22:39:53 -07003902
Jeff Brown19c97d462011-06-01 12:33:19 -07003903 mPointerGesture.pointerVelocityControl.reset();
3904
Jeff Brownace13b12011-03-09 17:39:48 -08003905 if (!tapped) {
3906#if DEBUG_GESTURES
3907 LOGD("Gestures: NEUTRAL");
3908#endif
3909 mPointerGesture.activeGestureId = -1;
3910 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08003911 mPointerGesture.currentGestureIdBits.clear();
3912 }
3913 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003914 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003915 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07003916 // When in HOVER, emit HOVER_MOVE events at the pointer location.
3917 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07003918 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08003919
Jeff Brown79ac9692011-04-19 21:20:10 -07003920 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
3921 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003922 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003923 float x, y;
3924 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003925 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3926 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003927 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3928 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003929#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003930 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3931 x - mPointerGesture.tapX,
3932 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003933#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003934 }
3935 } else {
3936#if DEBUG_GESTURES
3937 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3938 (when - mPointerGesture.tapUpTime) * 0.000001f);
3939#endif
3940 }
3941 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
3942 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3943 }
Jeff Brownace13b12011-03-09 17:39:48 -08003944
3945 if (mLastTouch.idBits.hasBit(activeTouchId)) {
3946 const PointerData& currentPointer =
3947 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3948 const PointerData& lastPointer =
3949 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3950 float deltaX = (currentPointer.x - lastPointer.x)
3951 * mLocked.pointerGestureXMovementScale;
3952 float deltaY = (currentPointer.y - lastPointer.y)
3953 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003954
Jeff Brown19c97d462011-06-01 12:33:19 -07003955 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3956
Jeff Brown2352b972011-04-12 22:39:53 -07003957 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07003958 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08003959 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003960 } else {
3961 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08003962 }
3963
Jeff Brown79ac9692011-04-19 21:20:10 -07003964 bool down;
3965 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
3966#if DEBUG_GESTURES
3967 LOGD("Gestures: TAP_DRAG");
3968#endif
3969 down = true;
3970 } else {
3971#if DEBUG_GESTURES
3972 LOGD("Gestures: HOVER");
3973#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003974 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
3975 *outFinishPreviousGesture = true;
3976 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003977 mPointerGesture.activeGestureId = 0;
3978 down = false;
3979 }
Jeff Brownace13b12011-03-09 17:39:48 -08003980
3981 float x, y;
3982 mPointerController->getPosition(&x, &y);
3983
Jeff Brownace13b12011-03-09 17:39:48 -08003984 mPointerGesture.currentGestureIdBits.clear();
3985 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3986 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003987 mPointerGesture.currentGestureProperties[0].clear();
3988 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3989 mPointerGesture.currentGestureProperties[0].toolType =
3990 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003991 mPointerGesture.currentGestureCoords[0].clear();
3992 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3993 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07003994 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3995 down ? 1.0f : 0.0f);
3996
Jeff Brownace13b12011-03-09 17:39:48 -08003997 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003998 mPointerGesture.resetTap();
3999 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004000 mPointerGesture.tapX = x;
4001 mPointerGesture.tapY = y;
4002 }
Jeff Brownace13b12011-03-09 17:39:48 -08004003 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004004 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4005 // We need to provide feedback for each finger that goes down so we cannot wait
4006 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004007 //
Jeff Brown2352b972011-04-12 22:39:53 -07004008 // The ambiguous case is deciding what to do when there are two fingers down but they
4009 // have not moved enough to determine whether they are part of a drag or part of a
4010 // freeform gesture, or just a press or long-press at the pointer location.
4011 //
4012 // When there are two fingers we start with the PRESS hypothesis and we generate a
4013 // down at the pointer location.
4014 //
4015 // When the two fingers move enough or when additional fingers are added, we make
4016 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004017 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004018
Jeff Brown214eaf42011-05-26 19:17:02 -07004019 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004020 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004021 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004022 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4023 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004024 *outFinishPreviousGesture = true;
Jeff Brown19c97d462011-06-01 12:33:19 -07004025 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
4026 // Additional pointers have gone down but not yet settled.
4027 // Reset the gesture.
4028#if DEBUG_GESTURES
4029 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004030 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004031 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004032 * 0.000001f);
4033#endif
4034 *outCancelPreviousGesture = true;
4035 } else {
4036 // Continue previous gesture.
4037 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4038 }
4039
4040 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004041 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4042 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004043 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004044 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004045
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004046 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004047#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004048 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4049 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004050 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004051 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004052#endif
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004053 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
4054 &mPointerGesture.referenceTouchY);
4055 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4056 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004057 }
Jeff Brownace13b12011-03-09 17:39:48 -08004058
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004059 // Clear the reference deltas for fingers not yet included in the reference calculation.
4060 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4061 !idBits.isEmpty(); ) {
4062 uint32_t id = idBits.firstMarkedBit();
4063 idBits.clearBit(id);
4064
4065 mPointerGesture.referenceDeltas[id].dx = 0;
4066 mPointerGesture.referenceDeltas[id].dy = 0;
4067 }
4068 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4069
4070 // Add delta for all fingers and calculate a common movement delta.
4071 float commonDeltaX = 0, commonDeltaY = 0;
4072 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4073 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4074 bool first = (idBits == commonIdBits);
4075 uint32_t id = idBits.firstMarkedBit();
4076 idBits.clearBit(id);
4077
4078 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4079 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
4080 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4081 delta.dx += cpd.x - lpd.x;
4082 delta.dy += cpd.y - lpd.y;
4083
4084 if (first) {
4085 commonDeltaX = delta.dx;
4086 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004087 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004088 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4089 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4090 }
4091 }
Jeff Brownace13b12011-03-09 17:39:48 -08004092
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004093 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4094 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4095 float dist[MAX_POINTER_ID + 1];
4096 int32_t distOverThreshold = 0;
4097 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
4098 uint32_t id = idBits.firstMarkedBit();
4099 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08004100
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004101 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4102 dist[id] = hypotf(delta.dx * mLocked.pointerGestureXZoomScale,
4103 delta.dy * mLocked.pointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004104 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004105 distOverThreshold += 1;
4106 }
4107 }
4108
4109 // Only transition when at least two pointers have moved further than
4110 // the minimum distance threshold.
4111 if (distOverThreshold >= 2) {
4112 float d;
4113 if (mCurrentTouch.pointerCount > 2) {
4114 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004115#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004116 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
4117 mCurrentTouch.pointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004118#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004119 *outCancelPreviousGesture = true;
4120 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4121 } else if (((d = distance(
4122 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
4123 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
4124 > mLocked.pointerGestureMaxSwipeWidth)) {
4125 // There are two pointers but they are too far apart for a SWIPE,
4126 // switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004127#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004128 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4129 d, mLocked.pointerGestureMaxSwipeWidth);
Jeff Brown2352b972011-04-12 22:39:53 -07004130#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004131 *outCancelPreviousGesture = true;
4132 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4133 } else {
4134 // There are two pointers. Wait for both pointers to start moving
4135 // before deciding whether this is a SWIPE or FREEFORM gesture.
4136 uint32_t id1 = mCurrentTouch.pointers[0].id;
4137 uint32_t id2 = mCurrentTouch.pointers[1].id;
4138 float dist1 = dist[id1];
4139 float dist2 = dist[id2];
Jeff Brown474dcb52011-06-14 20:22:50 -07004140 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4141 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004142 // Calculate the dot product of the displacement vectors.
4143 // When the vectors are oriented in approximately the same direction,
4144 // the angle betweeen them is near zero and the cosine of the angle
4145 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4146 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4147 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown6674d9b2011-06-07 16:50:14 -07004148 float dx1 = delta1.dx * mLocked.pointerGestureXZoomScale;
4149 float dy1 = delta1.dy * mLocked.pointerGestureYZoomScale;
4150 float dx2 = delta2.dx * mLocked.pointerGestureXZoomScale;
4151 float dy2 = delta2.dy * mLocked.pointerGestureYZoomScale;
4152 float dot = dx1 * dx2 + dy1 * dy2;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004153 float cosine = dot / (dist1 * dist2); // denominator always > 0
Jeff Brown474dcb52011-06-14 20:22:50 -07004154 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004155 // Pointers are moving in the same direction. Switch to SWIPE.
4156#if DEBUG_GESTURES
4157 LOGD("Gestures: PRESS transitioned to SWIPE, "
4158 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4159 "cosine %0.3f >= %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004160 dist1, mConfig.pointerGestureMultitouchMinDistance,
4161 dist2, mConfig.pointerGestureMultitouchMinDistance,
4162 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004163#endif
4164 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4165 } else {
4166 // Pointers are moving in different directions. Switch to FREEFORM.
4167#if DEBUG_GESTURES
4168 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4169 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4170 "cosine %0.3f < %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004171 dist1, mConfig.pointerGestureMultitouchMinDistance,
4172 dist2, mConfig.pointerGestureMultitouchMinDistance,
4173 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004174#endif
4175 *outCancelPreviousGesture = true;
4176 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4177 }
Jeff Brownace13b12011-03-09 17:39:48 -08004178 }
4179 }
Jeff Brownace13b12011-03-09 17:39:48 -08004180 }
4181 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004182 // Switch from SWIPE to FREEFORM if additional pointers go down.
4183 // Cancel previous gesture.
4184 if (mCurrentTouch.pointerCount > 2) {
4185#if DEBUG_GESTURES
4186 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4187 mCurrentTouch.pointerCount);
4188#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004189 *outCancelPreviousGesture = true;
4190 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004191 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004192 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004193
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004194 // Move the reference points based on the overall group motion of the fingers
4195 // except in PRESS mode while waiting for a transition to occur.
4196 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4197 && (commonDeltaX || commonDeltaY)) {
4198 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brown2352b972011-04-12 22:39:53 -07004199 uint32_t id = idBits.firstMarkedBit();
4200 idBits.clearBit(id);
4201
Jeff Brown538881e2011-05-25 18:23:38 -07004202 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004203 delta.dx = 0;
4204 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004205 }
4206
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004207 mPointerGesture.referenceTouchX += commonDeltaX;
4208 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004209
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004210 commonDeltaX *= mLocked.pointerGestureXMovementScale;
4211 commonDeltaY *= mLocked.pointerGestureYMovementScale;
4212 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004213
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004214 mPointerGesture.referenceGestureX += commonDeltaX;
4215 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004216 }
4217
4218 // Report gestures.
4219 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4220 // PRESS mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004221#if DEBUG_GESTURES
Jeff Brown2352b972011-04-12 22:39:53 -07004222 LOGD("Gestures: PRESS activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004223 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004224 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004225#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004226 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004227
Jeff Brownace13b12011-03-09 17:39:48 -08004228 mPointerGesture.currentGestureIdBits.clear();
4229 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4230 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004231 mPointerGesture.currentGestureProperties[0].clear();
4232 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4233 mPointerGesture.currentGestureProperties[0].toolType =
4234 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004235 mPointerGesture.currentGestureCoords[0].clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004236 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4237 mPointerGesture.referenceGestureX);
4238 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4239 mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004240 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004241 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4242 // SWIPE mode.
4243#if DEBUG_GESTURES
4244 LOGD("Gestures: SWIPE activeTouchId=%d,"
4245 "activeGestureId=%d, currentTouchPointerCount=%d",
4246 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4247#endif
4248 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4249
4250 mPointerGesture.currentGestureIdBits.clear();
4251 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4252 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004253 mPointerGesture.currentGestureProperties[0].clear();
4254 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4255 mPointerGesture.currentGestureProperties[0].toolType =
4256 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004257 mPointerGesture.currentGestureCoords[0].clear();
4258 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4259 mPointerGesture.referenceGestureX);
4260 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4261 mPointerGesture.referenceGestureY);
4262 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004263 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4264 // FREEFORM mode.
4265#if DEBUG_GESTURES
4266 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4267 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004268 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004269#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004270 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004271
Jeff Brownace13b12011-03-09 17:39:48 -08004272 mPointerGesture.currentGestureIdBits.clear();
4273
4274 BitSet32 mappedTouchIdBits;
4275 BitSet32 usedGestureIdBits;
4276 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4277 // Initially, assign the active gesture id to the active touch point
4278 // if there is one. No other touch id bits are mapped yet.
4279 if (!*outCancelPreviousGesture) {
4280 mappedTouchIdBits.markBit(activeTouchId);
4281 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4282 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4283 mPointerGesture.activeGestureId;
4284 } else {
4285 mPointerGesture.activeGestureId = -1;
4286 }
4287 } else {
4288 // Otherwise, assume we mapped all touches from the previous frame.
4289 // Reuse all mappings that are still applicable.
4290 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4291 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4292
4293 // Check whether we need to choose a new active gesture id because the
4294 // current went went up.
4295 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4296 !upTouchIdBits.isEmpty(); ) {
4297 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4298 upTouchIdBits.clearBit(upTouchId);
4299 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4300 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4301 mPointerGesture.activeGestureId = -1;
4302 break;
4303 }
4304 }
4305 }
4306
4307#if DEBUG_GESTURES
4308 LOGD("Gestures: FREEFORM follow up "
4309 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4310 "activeGestureId=%d",
4311 mappedTouchIdBits.value, usedGestureIdBits.value,
4312 mPointerGesture.activeGestureId);
4313#endif
4314
Jeff Brown2352b972011-04-12 22:39:53 -07004315 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004316 uint32_t touchId = mCurrentTouch.pointers[i].id;
4317 uint32_t gestureId;
4318 if (!mappedTouchIdBits.hasBit(touchId)) {
4319 gestureId = usedGestureIdBits.firstUnmarkedBit();
4320 usedGestureIdBits.markBit(gestureId);
4321 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4322#if DEBUG_GESTURES
4323 LOGD("Gestures: FREEFORM "
4324 "new mapping for touch id %d -> gesture id %d",
4325 touchId, gestureId);
4326#endif
4327 } else {
4328 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4329#if DEBUG_GESTURES
4330 LOGD("Gestures: FREEFORM "
4331 "existing mapping for touch id %d -> gesture id %d",
4332 touchId, gestureId);
4333#endif
4334 }
4335 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4336 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4337
Jeff Brown2352b972011-04-12 22:39:53 -07004338 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4339 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4340 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4341 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
Jeff Brownace13b12011-03-09 17:39:48 -08004342
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004343 mPointerGesture.currentGestureProperties[i].clear();
4344 mPointerGesture.currentGestureProperties[i].id = gestureId;
4345 mPointerGesture.currentGestureProperties[i].toolType =
4346 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004347 mPointerGesture.currentGestureCoords[i].clear();
4348 mPointerGesture.currentGestureCoords[i].setAxisValue(
4349 AMOTION_EVENT_AXIS_X, x);
4350 mPointerGesture.currentGestureCoords[i].setAxisValue(
4351 AMOTION_EVENT_AXIS_Y, y);
4352 mPointerGesture.currentGestureCoords[i].setAxisValue(
4353 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4354 }
4355
4356 if (mPointerGesture.activeGestureId < 0) {
4357 mPointerGesture.activeGestureId =
4358 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4359#if DEBUG_GESTURES
4360 LOGD("Gestures: FREEFORM new "
4361 "activeGestureId=%d", mPointerGesture.activeGestureId);
4362#endif
4363 }
Jeff Brown2352b972011-04-12 22:39:53 -07004364 }
Jeff Brownace13b12011-03-09 17:39:48 -08004365 }
4366
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004367 mPointerController->setButtonState(mCurrentTouch.buttonState);
4368
Jeff Brownace13b12011-03-09 17:39:48 -08004369#if DEBUG_GESTURES
4370 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004371 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4372 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004373 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004374 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4375 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004376 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4377 uint32_t id = idBits.firstMarkedBit();
4378 idBits.clearBit(id);
4379 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004380 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004381 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004382 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4383 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4384 id, index, properties.toolType,
4385 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004386 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4387 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4388 }
4389 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4390 uint32_t id = idBits.firstMarkedBit();
4391 idBits.clearBit(id);
4392 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004393 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004394 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004395 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4396 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4397 id, index, properties.toolType,
4398 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004399 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4400 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4401 }
4402#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004403 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004404}
4405
4406void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004407 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4408 const PointerProperties* properties, const PointerCoords* coords,
4409 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004410 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4411 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004412 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004413 uint32_t pointerCount = 0;
4414 while (!idBits.isEmpty()) {
4415 uint32_t id = idBits.firstMarkedBit();
4416 idBits.clearBit(id);
4417 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004418 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004419 pointerCoords[pointerCount].copyFrom(coords[index]);
4420
4421 if (changedId >= 0 && id == uint32_t(changedId)) {
4422 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4423 }
4424
4425 pointerCount += 1;
4426 }
4427
Jeff Brownb6110c22011-04-01 16:15:13 -07004428 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004429
4430 if (changedId >= 0 && pointerCount == 1) {
4431 // Replace initial down and final up action.
4432 // We can compare the action without masking off the changed pointer index
4433 // because we know the index is 0.
4434 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4435 action = AMOTION_EVENT_ACTION_DOWN;
4436 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4437 action = AMOTION_EVENT_ACTION_UP;
4438 } else {
4439 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004440 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004441 }
4442 }
4443
4444 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004445 action, flags, metaState, buttonState, edgeFlags,
4446 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004447}
4448
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004449bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004450 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004451 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4452 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004453 bool changed = false;
4454 while (!idBits.isEmpty()) {
4455 uint32_t id = idBits.firstMarkedBit();
4456 idBits.clearBit(id);
4457
4458 uint32_t inIndex = inIdToIndex[id];
4459 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004460
4461 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004462 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004463 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004464 PointerCoords& curOutCoords = outCoords[outIndex];
4465
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004466 if (curInProperties != curOutProperties) {
4467 curOutProperties.copyFrom(curInProperties);
4468 changed = true;
4469 }
4470
Jeff Brownace13b12011-03-09 17:39:48 -08004471 if (curInCoords != curOutCoords) {
4472 curOutCoords.copyFrom(curInCoords);
4473 changed = true;
4474 }
4475 }
4476 return changed;
4477}
4478
4479void TouchInputMapper::fadePointer() {
4480 { // acquire lock
4481 AutoMutex _l(mLock);
4482 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004483 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownace13b12011-03-09 17:39:48 -08004484 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004485 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07004486}
4487
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004488int32_t TouchInputMapper::getTouchToolType(bool isStylus) const {
4489 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
4490 return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER;
4491 } else {
4492 return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS
4493 : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
4494 }
4495}
4496
Jeff Brown6328cdc2010-07-29 18:18:33 -07004497bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004498 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4499 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004500}
4501
Jeff Brown6328cdc2010-07-29 18:18:33 -07004502const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4503 int32_t x, int32_t y) {
4504 size_t numVirtualKeys = mLocked.virtualKeys.size();
4505 for (size_t i = 0; i < numVirtualKeys; i++) {
4506 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004507
4508#if DEBUG_VIRTUAL_KEYS
4509 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4510 "left=%d, top=%d, right=%d, bottom=%d",
4511 x, y,
4512 virtualKey.keyCode, virtualKey.scanCode,
4513 virtualKey.hitLeft, virtualKey.hitTop,
4514 virtualKey.hitRight, virtualKey.hitBottom);
4515#endif
4516
4517 if (virtualKey.isHit(x, y)) {
4518 return & virtualKey;
4519 }
4520 }
4521
4522 return NULL;
4523}
4524
4525void TouchInputMapper::calculatePointerIds() {
4526 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4527 uint32_t lastPointerCount = mLastTouch.pointerCount;
4528
4529 if (currentPointerCount == 0) {
4530 // No pointers to assign.
4531 mCurrentTouch.idBits.clear();
4532 } else if (lastPointerCount == 0) {
4533 // All pointers are new.
4534 mCurrentTouch.idBits.clear();
4535 for (uint32_t i = 0; i < currentPointerCount; i++) {
4536 mCurrentTouch.pointers[i].id = i;
4537 mCurrentTouch.idToIndex[i] = i;
4538 mCurrentTouch.idBits.markBit(i);
4539 }
4540 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4541 // Only one pointer and no change in count so it must have the same id as before.
4542 uint32_t id = mLastTouch.pointers[0].id;
4543 mCurrentTouch.pointers[0].id = id;
4544 mCurrentTouch.idToIndex[id] = 0;
4545 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4546 } else {
4547 // General case.
4548 // We build a heap of squared euclidean distances between current and last pointers
4549 // associated with the current and last pointer indices. Then, we find the best
4550 // match (by distance) for each current pointer.
4551 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4552
4553 uint32_t heapSize = 0;
4554 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4555 currentPointerIndex++) {
4556 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4557 lastPointerIndex++) {
4558 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4559 - mLastTouch.pointers[lastPointerIndex].x;
4560 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4561 - mLastTouch.pointers[lastPointerIndex].y;
4562
4563 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4564
4565 // Insert new element into the heap (sift up).
4566 heap[heapSize].currentPointerIndex = currentPointerIndex;
4567 heap[heapSize].lastPointerIndex = lastPointerIndex;
4568 heap[heapSize].distance = distance;
4569 heapSize += 1;
4570 }
4571 }
4572
4573 // Heapify
4574 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4575 startIndex -= 1;
4576 for (uint32_t parentIndex = startIndex; ;) {
4577 uint32_t childIndex = parentIndex * 2 + 1;
4578 if (childIndex >= heapSize) {
4579 break;
4580 }
4581
4582 if (childIndex + 1 < heapSize
4583 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4584 childIndex += 1;
4585 }
4586
4587 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4588 break;
4589 }
4590
4591 swap(heap[parentIndex], heap[childIndex]);
4592 parentIndex = childIndex;
4593 }
4594 }
4595
4596#if DEBUG_POINTER_ASSIGNMENT
4597 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4598 for (size_t i = 0; i < heapSize; i++) {
4599 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4600 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4601 heap[i].distance);
4602 }
4603#endif
4604
4605 // Pull matches out by increasing order of distance.
4606 // To avoid reassigning pointers that have already been matched, the loop keeps track
4607 // of which last and current pointers have been matched using the matchedXXXBits variables.
4608 // It also tracks the used pointer id bits.
4609 BitSet32 matchedLastBits(0);
4610 BitSet32 matchedCurrentBits(0);
4611 BitSet32 usedIdBits(0);
4612 bool first = true;
4613 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4614 for (;;) {
4615 if (first) {
4616 // The first time through the loop, we just consume the root element of
4617 // the heap (the one with smallest distance).
4618 first = false;
4619 } else {
4620 // Previous iterations consumed the root element of the heap.
4621 // Pop root element off of the heap (sift down).
4622 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004623 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004624
4625 // Sift down.
4626 heap[0] = heap[heapSize];
4627 for (uint32_t parentIndex = 0; ;) {
4628 uint32_t childIndex = parentIndex * 2 + 1;
4629 if (childIndex >= heapSize) {
4630 break;
4631 }
4632
4633 if (childIndex + 1 < heapSize
4634 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4635 childIndex += 1;
4636 }
4637
4638 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4639 break;
4640 }
4641
4642 swap(heap[parentIndex], heap[childIndex]);
4643 parentIndex = childIndex;
4644 }
4645
4646#if DEBUG_POINTER_ASSIGNMENT
4647 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4648 for (size_t i = 0; i < heapSize; i++) {
4649 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4650 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4651 heap[i].distance);
4652 }
4653#endif
4654 }
4655
4656 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4657 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4658
4659 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4660 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4661
4662 matchedCurrentBits.markBit(currentPointerIndex);
4663 matchedLastBits.markBit(lastPointerIndex);
4664
4665 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4666 mCurrentTouch.pointers[currentPointerIndex].id = id;
4667 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4668 usedIdBits.markBit(id);
4669
4670#if DEBUG_POINTER_ASSIGNMENT
4671 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4672 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4673#endif
4674 break;
4675 }
4676 }
4677
4678 // Assign fresh ids to new pointers.
4679 if (currentPointerCount > lastPointerCount) {
4680 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4681 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4682 uint32_t id = usedIdBits.firstUnmarkedBit();
4683
4684 mCurrentTouch.pointers[currentPointerIndex].id = id;
4685 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4686 usedIdBits.markBit(id);
4687
4688#if DEBUG_POINTER_ASSIGNMENT
4689 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4690 currentPointerIndex, id);
4691#endif
4692
4693 if (--i == 0) break; // done
4694 matchedCurrentBits.markBit(currentPointerIndex);
4695 }
4696 }
4697
4698 // Fix id bits.
4699 mCurrentTouch.idBits = usedIdBits;
4700 }
4701}
4702
Jeff Brown6d0fec22010-07-23 21:28:06 -07004703int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004704 { // acquire lock
4705 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004706
Jeff Brown6328cdc2010-07-29 18:18:33 -07004707 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004708 return AKEY_STATE_VIRTUAL;
4709 }
4710
Jeff Brown6328cdc2010-07-29 18:18:33 -07004711 size_t numVirtualKeys = mLocked.virtualKeys.size();
4712 for (size_t i = 0; i < numVirtualKeys; i++) {
4713 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004714 if (virtualKey.keyCode == keyCode) {
4715 return AKEY_STATE_UP;
4716 }
4717 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004718 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004719
4720 return AKEY_STATE_UNKNOWN;
4721}
4722
4723int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004724 { // acquire lock
4725 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004726
Jeff Brown6328cdc2010-07-29 18:18:33 -07004727 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004728 return AKEY_STATE_VIRTUAL;
4729 }
4730
Jeff Brown6328cdc2010-07-29 18:18:33 -07004731 size_t numVirtualKeys = mLocked.virtualKeys.size();
4732 for (size_t i = 0; i < numVirtualKeys; i++) {
4733 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004734 if (virtualKey.scanCode == scanCode) {
4735 return AKEY_STATE_UP;
4736 }
4737 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004738 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004739
4740 return AKEY_STATE_UNKNOWN;
4741}
4742
4743bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4744 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004745 { // acquire lock
4746 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004747
Jeff Brown6328cdc2010-07-29 18:18:33 -07004748 size_t numVirtualKeys = mLocked.virtualKeys.size();
4749 for (size_t i = 0; i < numVirtualKeys; i++) {
4750 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004751
4752 for (size_t i = 0; i < numCodes; i++) {
4753 if (virtualKey.keyCode == keyCodes[i]) {
4754 outFlags[i] = 1;
4755 }
4756 }
4757 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004758 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004759
4760 return true;
4761}
4762
4763
4764// --- SingleTouchInputMapper ---
4765
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004766SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
4767 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004768 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004769}
4770
4771SingleTouchInputMapper::~SingleTouchInputMapper() {
4772}
4773
Jeff Brown80fd47c2011-05-24 01:07:44 -07004774void SingleTouchInputMapper::clearState() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004775 mAccumulator.clear();
4776
4777 mDown = false;
4778 mX = 0;
4779 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07004780 mPressure = 0; // default to 0 for devices that don't report pressure
4781 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brownace13b12011-03-09 17:39:48 -08004782 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004783}
4784
4785void SingleTouchInputMapper::reset() {
4786 TouchInputMapper::reset();
4787
Jeff Brown80fd47c2011-05-24 01:07:44 -07004788 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004789 }
4790
4791void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
4792 switch (rawEvent->type) {
4793 case EV_KEY:
4794 switch (rawEvent->scanCode) {
4795 case BTN_TOUCH:
4796 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
4797 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004798 // Don't sync immediately. Wait until the next SYN_REPORT since we might
4799 // not have received valid position information yet. This logic assumes that
4800 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07004801 break;
Jeff Brownace13b12011-03-09 17:39:48 -08004802 default:
4803 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004804 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08004805 if (buttonState) {
4806 if (rawEvent->value) {
4807 mAccumulator.buttonDown |= buttonState;
4808 } else {
4809 mAccumulator.buttonUp |= buttonState;
4810 }
4811 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
4812 }
4813 }
4814 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004815 }
4816 break;
4817
4818 case EV_ABS:
4819 switch (rawEvent->scanCode) {
4820 case ABS_X:
4821 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
4822 mAccumulator.absX = rawEvent->value;
4823 break;
4824 case ABS_Y:
4825 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
4826 mAccumulator.absY = rawEvent->value;
4827 break;
4828 case ABS_PRESSURE:
4829 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
4830 mAccumulator.absPressure = rawEvent->value;
4831 break;
4832 case ABS_TOOL_WIDTH:
4833 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
4834 mAccumulator.absToolWidth = rawEvent->value;
4835 break;
4836 }
4837 break;
4838
4839 case EV_SYN:
4840 switch (rawEvent->scanCode) {
4841 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004842 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004843 break;
4844 }
4845 break;
4846 }
4847}
4848
4849void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004850 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004851 if (fields == 0) {
4852 return; // no new state changes, so nothing to do
4853 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004854
4855 if (fields & Accumulator::FIELD_BTN_TOUCH) {
4856 mDown = mAccumulator.btnTouch;
4857 }
4858
4859 if (fields & Accumulator::FIELD_ABS_X) {
4860 mX = mAccumulator.absX;
4861 }
4862
4863 if (fields & Accumulator::FIELD_ABS_Y) {
4864 mY = mAccumulator.absY;
4865 }
4866
4867 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
4868 mPressure = mAccumulator.absPressure;
4869 }
4870
4871 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07004872 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004873 }
4874
Jeff Brownace13b12011-03-09 17:39:48 -08004875 if (fields & Accumulator::FIELD_BUTTONS) {
4876 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
4877 }
4878
Jeff Brown6d0fec22010-07-23 21:28:06 -07004879 mCurrentTouch.clear();
4880
4881 if (mDown) {
4882 mCurrentTouch.pointerCount = 1;
4883 mCurrentTouch.pointers[0].id = 0;
4884 mCurrentTouch.pointers[0].x = mX;
4885 mCurrentTouch.pointers[0].y = mY;
4886 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07004887 mCurrentTouch.pointers[0].touchMajor = 0;
4888 mCurrentTouch.pointers[0].touchMinor = 0;
4889 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
4890 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004891 mCurrentTouch.pointers[0].orientation = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004892 mCurrentTouch.pointers[0].distance = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004893 mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus
Jeff Brown6d0fec22010-07-23 21:28:06 -07004894 mCurrentTouch.idToIndex[0] = 0;
4895 mCurrentTouch.idBits.markBit(0);
Jeff Brownace13b12011-03-09 17:39:48 -08004896 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004897 }
4898
4899 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004900
4901 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004902}
4903
Jeff Brown8d608662010-08-30 03:02:23 -07004904void SingleTouchInputMapper::configureRawAxes() {
4905 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004906
Jeff Brown8d608662010-08-30 03:02:23 -07004907 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
4908 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
4909 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
4910 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004911}
4912
4913
4914// --- MultiTouchInputMapper ---
4915
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004916MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown80fd47c2011-05-24 01:07:44 -07004917 TouchInputMapper(device), mSlotCount(0), mUsingSlotsProtocol(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004918}
4919
4920MultiTouchInputMapper::~MultiTouchInputMapper() {
4921}
4922
Jeff Brown80fd47c2011-05-24 01:07:44 -07004923void MultiTouchInputMapper::clearState() {
Jeff Brown441a9c22011-06-02 18:22:25 -07004924 mAccumulator.clearSlots(mSlotCount);
4925 mAccumulator.clearButtons();
Jeff Brownace13b12011-03-09 17:39:48 -08004926 mButtonState = 0;
Jeff Brown6894a292011-07-01 17:59:27 -07004927 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07004928
4929 if (mUsingSlotsProtocol) {
4930 // Query the driver for the current slot index and use it as the initial slot
4931 // before we start reading events from the device. It is possible that the
4932 // current slot index will not be the same as it was when the first event was
4933 // written into the evdev buffer, which means the input mapper could start
4934 // out of sync with the initial state of the events in the evdev buffer.
4935 // In the extremely unlikely case that this happens, the data from
4936 // two slots will be confused until the next ABS_MT_SLOT event is received.
4937 // This can cause the touch point to "jump", but at least there will be
4938 // no stuck touches.
4939 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
4940 &mAccumulator.currentSlot);
4941 if (status) {
4942 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
4943 mAccumulator.currentSlot = -1;
4944 }
4945 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004946}
4947
4948void MultiTouchInputMapper::reset() {
4949 TouchInputMapper::reset();
4950
Jeff Brown80fd47c2011-05-24 01:07:44 -07004951 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004952}
4953
4954void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
4955 switch (rawEvent->type) {
Jeff Brownace13b12011-03-09 17:39:48 -08004956 case EV_KEY: {
4957 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004958 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08004959 if (buttonState) {
4960 if (rawEvent->value) {
4961 mAccumulator.buttonDown |= buttonState;
4962 } else {
4963 mAccumulator.buttonUp |= buttonState;
4964 }
4965 }
4966 }
4967 break;
4968 }
4969
Jeff Brown6d0fec22010-07-23 21:28:06 -07004970 case EV_ABS: {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004971 bool newSlot = false;
4972 if (mUsingSlotsProtocol && rawEvent->scanCode == ABS_MT_SLOT) {
4973 mAccumulator.currentSlot = rawEvent->value;
4974 newSlot = true;
4975 }
4976
4977 if (mAccumulator.currentSlot < 0 || size_t(mAccumulator.currentSlot) >= mSlotCount) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004978#if DEBUG_POINTERS
Jeff Brown441a9c22011-06-02 18:22:25 -07004979 if (newSlot) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004980 LOGW("MultiTouch device %s emitted invalid slot index %d but it "
4981 "should be between 0 and %d; ignoring this slot.",
4982 getDeviceName().string(), mAccumulator.currentSlot, mSlotCount);
Jeff Brown80fd47c2011-05-24 01:07:44 -07004983 }
Jeff Brown441a9c22011-06-02 18:22:25 -07004984#endif
Jeff Brown80fd47c2011-05-24 01:07:44 -07004985 break;
4986 }
4987
4988 Accumulator::Slot* slot = &mAccumulator.slots[mAccumulator.currentSlot];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004989
4990 switch (rawEvent->scanCode) {
4991 case ABS_MT_POSITION_X:
Jeff Brown80fd47c2011-05-24 01:07:44 -07004992 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
4993 slot->absMTPositionX = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004994 break;
4995 case ABS_MT_POSITION_Y:
Jeff Brown80fd47c2011-05-24 01:07:44 -07004996 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
4997 slot->absMTPositionY = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004998 break;
4999 case ABS_MT_TOUCH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005000 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
5001 slot->absMTTouchMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005002 break;
5003 case ABS_MT_TOUCH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005004 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
5005 slot->absMTTouchMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005006 break;
5007 case ABS_MT_WIDTH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005008 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
5009 slot->absMTWidthMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005010 break;
5011 case ABS_MT_WIDTH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005012 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5013 slot->absMTWidthMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005014 break;
5015 case ABS_MT_ORIENTATION:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005016 slot->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5017 slot->absMTOrientation = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005018 break;
5019 case ABS_MT_TRACKING_ID:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005020 if (mUsingSlotsProtocol && rawEvent->value < 0) {
5021 slot->clear();
5022 } else {
5023 slot->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5024 slot->absMTTrackingId = rawEvent->value;
5025 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005026 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005027 case ABS_MT_PRESSURE:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005028 slot->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5029 slot->absMTPressure = rawEvent->value;
5030 break;
5031 case ABS_MT_TOOL_TYPE:
5032 slot->fields |= Accumulator::FIELD_ABS_MT_TOOL_TYPE;
5033 slot->absMTToolType = rawEvent->value;
Jeff Brown8d608662010-08-30 03:02:23 -07005034 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005035 }
5036 break;
5037 }
5038
5039 case EV_SYN:
5040 switch (rawEvent->scanCode) {
5041 case SYN_MT_REPORT: {
5042 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005043 mAccumulator.currentSlot += 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005044 break;
5045 }
5046
5047 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005048 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005049 break;
5050 }
5051 break;
5052 }
5053}
5054
5055void MultiTouchInputMapper::sync(nsecs_t when) {
5056 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005057 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005058
Jeff Brown80fd47c2011-05-24 01:07:44 -07005059 size_t inCount = mSlotCount;
5060 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005061 bool havePointerIds = true;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005062
Jeff Brown6d0fec22010-07-23 21:28:06 -07005063 mCurrentTouch.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005064
Jeff Brown80fd47c2011-05-24 01:07:44 -07005065 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
5066 const Accumulator::Slot& inSlot = mAccumulator.slots[inIndex];
5067 uint32_t fields = inSlot.fields;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005068
Jeff Brown6d0fec22010-07-23 21:28:06 -07005069 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005070 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005071 // This may also indicate an unused slot.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005072 // Drop this finger.
Jeff Brown46b9ac02010-04-22 18:58:52 -07005073 continue;
5074 }
5075
Jeff Brown80fd47c2011-05-24 01:07:44 -07005076 if (outCount >= MAX_POINTERS) {
5077#if DEBUG_POINTERS
5078 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5079 "ignoring the rest.",
5080 getDeviceName().string(), MAX_POINTERS);
5081#endif
5082 break; // too many fingers!
5083 }
5084
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005085 PointerData& outPointer = mCurrentTouch.pointers[outCount];
Jeff Brown80fd47c2011-05-24 01:07:44 -07005086 outPointer.x = inSlot.absMTPositionX;
5087 outPointer.y = inSlot.absMTPositionY;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005088
Jeff Brown8d608662010-08-30 03:02:23 -07005089 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005090 outPointer.pressure = inSlot.absMTPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005091 } else {
5092 // Default pressure to 0 if absent.
5093 outPointer.pressure = 0;
5094 }
5095
5096 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005097 if (inSlot.absMTTouchMajor <= 0) {
Jeff Brown8d608662010-08-30 03:02:23 -07005098 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5099 // a pointer going up. Drop this finger.
5100 continue;
5101 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07005102 outPointer.touchMajor = inSlot.absMTTouchMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005103 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005104 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005105 outPointer.touchMajor = 0;
5106 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005107
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005108 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005109 outPointer.touchMinor = inSlot.absMTTouchMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005110 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005111 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005112 outPointer.touchMinor = outPointer.touchMajor;
5113 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005114
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005115 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005116 outPointer.toolMajor = inSlot.absMTWidthMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005117 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005118 // Default tool area to 0 if absent.
5119 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005120 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005121
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005122 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005123 outPointer.toolMinor = inSlot.absMTWidthMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005124 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005125 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005126 outPointer.toolMinor = outPointer.toolMajor;
5127 }
5128
5129 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005130 outPointer.orientation = inSlot.absMTOrientation;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005131 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005132 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005133 outPointer.orientation = 0;
5134 }
5135
Jeff Brown80fd47c2011-05-24 01:07:44 -07005136 if (fields & Accumulator::FIELD_ABS_MT_DISTANCE) {
5137 outPointer.distance = inSlot.absMTDistance;
5138 } else {
5139 // Default distance is 0 (direct contact).
5140 outPointer.distance = 0;
5141 }
5142
5143 if (fields & Accumulator::FIELD_ABS_MT_TOOL_TYPE) {
5144 outPointer.isStylus = (inSlot.absMTToolType == MT_TOOL_PEN);
5145 } else {
5146 // Assume this is not a stylus.
5147 outPointer.isStylus = false;
5148 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005149
Jeff Brown8d608662010-08-30 03:02:23 -07005150 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005151 if (havePointerIds) {
Jeff Brown6894a292011-07-01 17:59:27 -07005152 int32_t id = -1;
5153 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5154 int32_t trackingId = inSlot.absMTTrackingId;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005155
Jeff Brown6894a292011-07-01 17:59:27 -07005156 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
5157 uint32_t n = idBits.firstMarkedBit();
5158 idBits.clearBit(n);
5159
5160 if (mPointerTrackingIdMap[n] == trackingId) {
5161 id = n;
5162 }
5163 }
5164
5165 if (id < 0 && !mPointerIdBits.isFull()) {
5166 id = mPointerIdBits.firstUnmarkedBit();
5167 mPointerIdBits.markBit(id);
5168 mPointerTrackingIdMap[id] = trackingId;
5169 }
5170 }
5171 if (id < 0) {
5172 havePointerIds = false;
5173 mCurrentTouch.idBits.clear();
5174 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005175 outPointer.id = id;
5176 mCurrentTouch.idToIndex[id] = outCount;
5177 mCurrentTouch.idBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005178 }
5179 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005180
Jeff Brown6d0fec22010-07-23 21:28:06 -07005181 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005182 }
5183
Jeff Brown6d0fec22010-07-23 21:28:06 -07005184 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005185
Jeff Brownace13b12011-03-09 17:39:48 -08005186 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5187 mCurrentTouch.buttonState = mButtonState;
5188
Jeff Brown6894a292011-07-01 17:59:27 -07005189 mPointerIdBits = mCurrentTouch.idBits;
5190
Jeff Brown6d0fec22010-07-23 21:28:06 -07005191 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005192
Jeff Brown441a9c22011-06-02 18:22:25 -07005193 if (!mUsingSlotsProtocol) {
5194 mAccumulator.clearSlots(mSlotCount);
5195 }
5196 mAccumulator.clearButtons();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005197}
5198
Jeff Brown8d608662010-08-30 03:02:23 -07005199void MultiTouchInputMapper::configureRawAxes() {
5200 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005201
Jeff Brown80fd47c2011-05-24 01:07:44 -07005202 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x);
5203 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y);
5204 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor);
5205 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor);
5206 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor);
5207 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor);
5208 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation);
5209 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure);
5210 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance);
5211 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId);
5212 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot);
5213
5214 if (mRawAxes.trackingId.valid
5215 && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) {
5216 mSlotCount = mRawAxes.slot.maxValue + 1;
5217 if (mSlotCount > MAX_SLOTS) {
5218 LOGW("MultiTouch Device %s reported %d slots but the framework "
5219 "only supports a maximum of %d slots at this time.",
5220 getDeviceName().string(), mSlotCount, MAX_SLOTS);
5221 mSlotCount = MAX_SLOTS;
5222 }
5223 mUsingSlotsProtocol = true;
5224 } else {
5225 mSlotCount = MAX_POINTERS;
5226 mUsingSlotsProtocol = false;
5227 }
5228
5229 mAccumulator.allocateSlots(mSlotCount);
Jeff Brown2717eff2011-06-30 23:53:07 -07005230
5231 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005232}
5233
Jeff Brown46b9ac02010-04-22 18:58:52 -07005234
Jeff Browncb1404e2011-01-15 18:14:15 -08005235// --- JoystickInputMapper ---
5236
5237JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5238 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005239}
5240
5241JoystickInputMapper::~JoystickInputMapper() {
5242}
5243
5244uint32_t JoystickInputMapper::getSources() {
5245 return AINPUT_SOURCE_JOYSTICK;
5246}
5247
5248void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5249 InputMapper::populateDeviceInfo(info);
5250
Jeff Brown6f2fba42011-02-19 01:08:02 -08005251 for (size_t i = 0; i < mAxes.size(); i++) {
5252 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005253 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5254 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005255 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005256 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5257 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005258 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005259 }
5260}
5261
5262void JoystickInputMapper::dump(String8& dump) {
5263 dump.append(INDENT2 "Joystick Input Mapper:\n");
5264
Jeff Brown6f2fba42011-02-19 01:08:02 -08005265 dump.append(INDENT3 "Axes:\n");
5266 size_t numAxes = mAxes.size();
5267 for (size_t i = 0; i < numAxes; i++) {
5268 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005269 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005270 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005271 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005272 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005273 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005274 }
Jeff Brown85297452011-03-04 13:07:49 -08005275 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5276 label = getAxisLabel(axis.axisInfo.highAxis);
5277 if (label) {
5278 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5279 } else {
5280 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5281 axis.axisInfo.splitValue);
5282 }
5283 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5284 dump.append(" (invert)");
5285 }
5286
5287 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5288 axis.min, axis.max, axis.flat, axis.fuzz);
5289 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5290 "highScale=%0.5f, highOffset=%0.5f\n",
5291 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005292 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5293 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005294 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005295 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005296 }
5297}
5298
Jeff Brown474dcb52011-06-14 20:22:50 -07005299void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5300 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005301
Jeff Brown474dcb52011-06-14 20:22:50 -07005302 if (!changes) { // first time only
5303 // Collect all axes.
5304 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5305 RawAbsoluteAxisInfo rawAxisInfo;
5306 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5307 if (rawAxisInfo.valid) {
5308 // Map axis.
5309 AxisInfo axisInfo;
5310 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5311 if (!explicitlyMapped) {
5312 // Axis is not explicitly mapped, will choose a generic axis later.
5313 axisInfo.mode = AxisInfo::MODE_NORMAL;
5314 axisInfo.axis = -1;
5315 }
5316
5317 // Apply flat override.
5318 int32_t rawFlat = axisInfo.flatOverride < 0
5319 ? rawAxisInfo.flat : axisInfo.flatOverride;
5320
5321 // Calculate scaling factors and limits.
5322 Axis axis;
5323 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5324 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5325 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5326 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5327 scale, 0.0f, highScale, 0.0f,
5328 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5329 } else if (isCenteredAxis(axisInfo.axis)) {
5330 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5331 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5332 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5333 scale, offset, scale, offset,
5334 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5335 } else {
5336 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5337 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5338 scale, 0.0f, scale, 0.0f,
5339 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5340 }
5341
5342 // To eliminate noise while the joystick is at rest, filter out small variations
5343 // in axis values up front.
5344 axis.filter = axis.flat * 0.25f;
5345
5346 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005347 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005348 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005349
Jeff Brown474dcb52011-06-14 20:22:50 -07005350 // If there are too many axes, start dropping them.
5351 // Prefer to keep explicitly mapped axes.
5352 if (mAxes.size() > PointerCoords::MAX_AXES) {
5353 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5354 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5355 pruneAxes(true);
5356 pruneAxes(false);
5357 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005358
Jeff Brown474dcb52011-06-14 20:22:50 -07005359 // Assign generic axis ids to remaining axes.
5360 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5361 size_t numAxes = mAxes.size();
5362 for (size_t i = 0; i < numAxes; i++) {
5363 Axis& axis = mAxes.editValueAt(i);
5364 if (axis.axisInfo.axis < 0) {
5365 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5366 && haveAxis(nextGenericAxisId)) {
5367 nextGenericAxisId += 1;
5368 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005369
Jeff Brown474dcb52011-06-14 20:22:50 -07005370 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5371 axis.axisInfo.axis = nextGenericAxisId;
5372 nextGenericAxisId += 1;
5373 } else {
5374 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5375 "have already been assigned to other axes.",
5376 getDeviceName().string(), mAxes.keyAt(i));
5377 mAxes.removeItemsAt(i--);
5378 numAxes -= 1;
5379 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005380 }
5381 }
5382 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005383}
5384
Jeff Brown85297452011-03-04 13:07:49 -08005385bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005386 size_t numAxes = mAxes.size();
5387 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005388 const Axis& axis = mAxes.valueAt(i);
5389 if (axis.axisInfo.axis == axisId
5390 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5391 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005392 return true;
5393 }
5394 }
5395 return false;
5396}
Jeff Browncb1404e2011-01-15 18:14:15 -08005397
Jeff Brown6f2fba42011-02-19 01:08:02 -08005398void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5399 size_t i = mAxes.size();
5400 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5401 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5402 continue;
5403 }
5404 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5405 getDeviceName().string(), mAxes.keyAt(i));
5406 mAxes.removeItemsAt(i);
5407 }
5408}
5409
5410bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5411 switch (axis) {
5412 case AMOTION_EVENT_AXIS_X:
5413 case AMOTION_EVENT_AXIS_Y:
5414 case AMOTION_EVENT_AXIS_Z:
5415 case AMOTION_EVENT_AXIS_RX:
5416 case AMOTION_EVENT_AXIS_RY:
5417 case AMOTION_EVENT_AXIS_RZ:
5418 case AMOTION_EVENT_AXIS_HAT_X:
5419 case AMOTION_EVENT_AXIS_HAT_Y:
5420 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005421 case AMOTION_EVENT_AXIS_RUDDER:
5422 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005423 return true;
5424 default:
5425 return false;
5426 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005427}
5428
5429void JoystickInputMapper::reset() {
5430 // Recenter all axes.
5431 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005432
Jeff Brown6f2fba42011-02-19 01:08:02 -08005433 size_t numAxes = mAxes.size();
5434 for (size_t i = 0; i < numAxes; i++) {
5435 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005436 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005437 }
5438
5439 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005440
5441 InputMapper::reset();
5442}
5443
5444void JoystickInputMapper::process(const RawEvent* rawEvent) {
5445 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005446 case EV_ABS: {
5447 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5448 if (index >= 0) {
5449 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005450 float newValue, highNewValue;
5451 switch (axis.axisInfo.mode) {
5452 case AxisInfo::MODE_INVERT:
5453 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5454 * axis.scale + axis.offset;
5455 highNewValue = 0.0f;
5456 break;
5457 case AxisInfo::MODE_SPLIT:
5458 if (rawEvent->value < axis.axisInfo.splitValue) {
5459 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5460 * axis.scale + axis.offset;
5461 highNewValue = 0.0f;
5462 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5463 newValue = 0.0f;
5464 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5465 * axis.highScale + axis.highOffset;
5466 } else {
5467 newValue = 0.0f;
5468 highNewValue = 0.0f;
5469 }
5470 break;
5471 default:
5472 newValue = rawEvent->value * axis.scale + axis.offset;
5473 highNewValue = 0.0f;
5474 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005475 }
Jeff Brown85297452011-03-04 13:07:49 -08005476 axis.newValue = newValue;
5477 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005478 }
5479 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005480 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005481
5482 case EV_SYN:
5483 switch (rawEvent->scanCode) {
5484 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005485 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005486 break;
5487 }
5488 break;
5489 }
5490}
5491
Jeff Brown6f2fba42011-02-19 01:08:02 -08005492void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005493 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005494 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005495 }
5496
5497 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005498 int32_t buttonState = 0;
5499
5500 PointerProperties pointerProperties;
5501 pointerProperties.clear();
5502 pointerProperties.id = 0;
5503 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005504
Jeff Brown6f2fba42011-02-19 01:08:02 -08005505 PointerCoords pointerCoords;
5506 pointerCoords.clear();
5507
5508 size_t numAxes = mAxes.size();
5509 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005510 const Axis& axis = mAxes.valueAt(i);
5511 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5512 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5513 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5514 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005515 }
5516
Jeff Brown56194eb2011-03-02 19:23:13 -08005517 // Moving a joystick axis should not wake the devide because joysticks can
5518 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5519 // button will likely wake the device.
5520 // TODO: Use the input device configuration to control this behavior more finely.
5521 uint32_t policyFlags = 0;
5522
Jeff Brown56194eb2011-03-02 19:23:13 -08005523 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005524 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5525 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005526}
5527
Jeff Brown85297452011-03-04 13:07:49 -08005528bool JoystickInputMapper::filterAxes(bool force) {
5529 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005530 size_t numAxes = mAxes.size();
5531 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005532 Axis& axis = mAxes.editValueAt(i);
5533 if (force || hasValueChangedSignificantly(axis.filter,
5534 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5535 axis.currentValue = axis.newValue;
5536 atLeastOneSignificantChange = true;
5537 }
5538 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5539 if (force || hasValueChangedSignificantly(axis.filter,
5540 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5541 axis.highCurrentValue = axis.highNewValue;
5542 atLeastOneSignificantChange = true;
5543 }
5544 }
5545 }
5546 return atLeastOneSignificantChange;
5547}
5548
5549bool JoystickInputMapper::hasValueChangedSignificantly(
5550 float filter, float newValue, float currentValue, float min, float max) {
5551 if (newValue != currentValue) {
5552 // Filter out small changes in value unless the value is converging on the axis
5553 // bounds or center point. This is intended to reduce the amount of information
5554 // sent to applications by particularly noisy joysticks (such as PS3).
5555 if (fabs(newValue - currentValue) > filter
5556 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5557 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5558 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5559 return true;
5560 }
5561 }
5562 return false;
5563}
5564
5565bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5566 float filter, float newValue, float currentValue, float thresholdValue) {
5567 float newDistance = fabs(newValue - thresholdValue);
5568 if (newDistance < filter) {
5569 float oldDistance = fabs(currentValue - thresholdValue);
5570 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005571 return true;
5572 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005573 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005574 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005575}
5576
Jeff Brown46b9ac02010-04-22 18:58:52 -07005577} // namespace android