blob: b2fbcb15bfba0db6b902b3e9a325e78689ab4841 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
123int32_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
128static const int32_t edgeFlagRotationMap[][4] = {
129 // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first
130 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
131 { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT,
132 AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT },
133 { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP,
134 AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM },
135 { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT,
136 AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT },
137 { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM,
138 AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP },
139};
140static const size_t edgeFlagRotationMapSize =
141 sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]);
142
143static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) {
144 return rotateValueUsingRotationMap(edgeFlag, orientation,
145 edgeFlagRotationMap, edgeFlagRotationMapSize);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700146}
147
Jeff Brown6d0fec22010-07-23 21:28:06 -0700148static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
149 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
150}
151
Jeff Brownefd32662011-03-08 15:13:06 -0800152static uint32_t getButtonStateForScanCode(int32_t scanCode) {
153 // Currently all buttons are mapped to the primary button.
154 switch (scanCode) {
155 case BTN_LEFT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700156 return AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800157 case BTN_RIGHT:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700158 case BTN_STYLUS:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700159 return AMOTION_EVENT_BUTTON_SECONDARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800160 case BTN_MIDDLE:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700161 case BTN_STYLUS2:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700162 return AMOTION_EVENT_BUTTON_TERTIARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800163 case BTN_SIDE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700164 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800165 case BTN_FORWARD:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700166 case BTN_EXTRA:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700167 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800168 case BTN_BACK:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700169 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800170 case BTN_TASK:
Jeff Brownefd32662011-03-08 15:13:06 -0800171 default:
172 return 0;
173 }
174}
175
176// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700177// button states. This determines whether the event is reported as a touch event.
178static bool isPointerDown(int32_t buttonState) {
179 return buttonState &
180 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700181 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800182}
183
Jeff Brown2352b972011-04-12 22:39:53 -0700184static float calculateCommonVector(float a, float b) {
185 if (a > 0 && b > 0) {
186 return a < b ? a : b;
187 } else if (a < 0 && b < 0) {
188 return a > b ? a : b;
189 } else {
190 return 0;
191 }
192}
193
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700194static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
195 nsecs_t when, int32_t deviceId, uint32_t source,
196 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
197 int32_t buttonState, int32_t keyCode) {
198 if (
199 (action == AKEY_EVENT_ACTION_DOWN
200 && !(lastButtonState & buttonState)
201 && (currentButtonState & buttonState))
202 || (action == AKEY_EVENT_ACTION_UP
203 && (lastButtonState & buttonState)
204 && !(currentButtonState & buttonState))) {
205 context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags,
206 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
207 }
208}
209
210static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
211 nsecs_t when, int32_t deviceId, uint32_t source,
212 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
213 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
214 lastButtonState, currentButtonState,
215 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
216 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
217 lastButtonState, currentButtonState,
218 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
219}
220
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700221
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700222// --- InputReader ---
223
224InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700225 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700226 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700227 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700228 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700229 mConfigurationChangesToRefresh(0) {
230 refreshConfiguration(0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700231 updateGlobalMetaState();
232 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700233}
234
235InputReader::~InputReader() {
236 for (size_t i = 0; i < mDevices.size(); i++) {
237 delete mDevices.valueAt(i);
238 }
239}
240
241void InputReader::loopOnce() {
Jeff Brown474dcb52011-06-14 20:22:50 -0700242 uint32_t changes;
243 { // acquire lock
244 AutoMutex _l(mStateLock);
245
246 changes = mConfigurationChangesToRefresh;
247 mConfigurationChangesToRefresh = 0;
248 } // release lock
249
250 if (changes) {
251 refreshConfiguration(changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700252 }
253
Jeff Brownaa3855d2011-03-17 01:34:19 -0700254 int32_t timeoutMillis = -1;
255 if (mNextTimeout != LLONG_MAX) {
256 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
257 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
258 }
259
Jeff Brownb7198742011-03-18 18:14:26 -0700260 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
261 if (count) {
262 processEvents(mEventBuffer, count);
263 }
264 if (!count || timeoutMillis == 0) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700265 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
266#if DEBUG_RAW_EVENTS
267 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
268#endif
269 mNextTimeout = LLONG_MAX;
270 timeoutExpired(now);
271 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700272}
273
Jeff Brownb7198742011-03-18 18:14:26 -0700274void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
275 for (const RawEvent* rawEvent = rawEvents; count;) {
276 int32_t type = rawEvent->type;
277 size_t batchSize = 1;
278 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
279 int32_t deviceId = rawEvent->deviceId;
280 while (batchSize < count) {
281 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
282 || rawEvent[batchSize].deviceId != deviceId) {
283 break;
284 }
285 batchSize += 1;
286 }
287#if DEBUG_RAW_EVENTS
288 LOGD("BatchSize: %d Count: %d", batchSize, count);
289#endif
290 processEventsForDevice(deviceId, rawEvent, batchSize);
291 } else {
292 switch (rawEvent->type) {
293 case EventHubInterface::DEVICE_ADDED:
294 addDevice(rawEvent->deviceId);
295 break;
296 case EventHubInterface::DEVICE_REMOVED:
297 removeDevice(rawEvent->deviceId);
298 break;
299 case EventHubInterface::FINISHED_DEVICE_SCAN:
300 handleConfigurationChanged(rawEvent->when);
301 break;
302 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700303 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700304 break;
305 }
306 }
307 count -= batchSize;
308 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700309 }
310}
311
Jeff Brown7342bb92010-10-01 18:55:43 -0700312void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700313 String8 name = mEventHub->getDeviceName(deviceId);
314 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
315
316 InputDevice* device = createDevice(deviceId, name, classes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700317 device->configure(&mConfig, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700318
Jeff Brown8d608662010-08-30 03:02:23 -0700319 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800320 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700321 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800322 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700323 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700324 }
325
Jeff Brown6d0fec22010-07-23 21:28:06 -0700326 bool added = false;
327 { // acquire device registry writer lock
328 RWLock::AutoWLock _wl(mDeviceRegistryLock);
329
330 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
331 if (deviceIndex < 0) {
332 mDevices.add(deviceId, device);
333 added = true;
334 }
335 } // release device registry writer lock
336
337 if (! added) {
338 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
339 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700340 return;
341 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342}
343
Jeff Brown7342bb92010-10-01 18:55:43 -0700344void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700345 bool removed = false;
346 InputDevice* device = NULL;
347 { // acquire device registry writer lock
348 RWLock::AutoWLock _wl(mDeviceRegistryLock);
349
350 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
351 if (deviceIndex >= 0) {
352 device = mDevices.valueAt(deviceIndex);
353 mDevices.removeItemsAt(deviceIndex, 1);
354 removed = true;
355 }
356 } // release device registry writer lock
357
358 if (! removed) {
359 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700360 return;
361 }
362
Jeff Brown6d0fec22010-07-23 21:28:06 -0700363 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800364 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700365 device->getId(), device->getName().string());
366 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800367 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700368 device->getId(), device->getName().string(), device->getSources());
369 }
370
Jeff Brown8d608662010-08-30 03:02:23 -0700371 device->reset();
372
Jeff Brown6d0fec22010-07-23 21:28:06 -0700373 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700374}
375
Jeff Brown6d0fec22010-07-23 21:28:06 -0700376InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
377 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700378
Jeff Brown56194eb2011-03-02 19:23:13 -0800379 // External devices.
380 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
381 device->setExternal(true);
382 }
383
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384 // Switch-like devices.
385 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
386 device->addMapper(new SwitchInputMapper(device));
387 }
388
389 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800390 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700391 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
392 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800393 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700394 }
395 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
396 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
397 }
398 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800399 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700400 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800401 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800402 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800403 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700404
Jeff Brownefd32662011-03-08 15:13:06 -0800405 if (keyboardSource != 0) {
406 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 }
408
Jeff Brown83c09682010-12-23 17:50:18 -0800409 // Cursor-like devices.
410 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
411 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700412 }
413
Jeff Brown58a2da82011-01-25 16:02:22 -0800414 // Touchscreens and touchpad devices.
415 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800416 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800417 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800418 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700419 }
420
Jeff Browncb1404e2011-01-15 18:14:15 -0800421 // Joystick-like devices.
422 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
423 device->addMapper(new JoystickInputMapper(device));
424 }
425
Jeff Brown6d0fec22010-07-23 21:28:06 -0700426 return device;
427}
428
Jeff Brownb7198742011-03-18 18:14:26 -0700429void InputReader::processEventsForDevice(int32_t deviceId,
430 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700431 { // acquire device registry reader lock
432 RWLock::AutoRLock _rl(mDeviceRegistryLock);
433
434 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
435 if (deviceIndex < 0) {
436 LOGW("Discarding event for unknown deviceId %d.", deviceId);
437 return;
438 }
439
440 InputDevice* device = mDevices.valueAt(deviceIndex);
441 if (device->isIgnored()) {
442 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
443 return;
444 }
445
Jeff Brownb7198742011-03-18 18:14:26 -0700446 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447 } // release device registry reader lock
448}
449
Jeff Brownaa3855d2011-03-17 01:34:19 -0700450void InputReader::timeoutExpired(nsecs_t when) {
451 { // acquire device registry reader lock
452 RWLock::AutoRLock _rl(mDeviceRegistryLock);
453
454 for (size_t i = 0; i < mDevices.size(); i++) {
455 InputDevice* device = mDevices.valueAt(i);
456 if (!device->isIgnored()) {
457 device->timeoutExpired(when);
458 }
459 }
460 } // release device registry reader lock
461}
462
Jeff Brownc3db8582010-10-20 15:33:38 -0700463void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700464 // Reset global meta state because it depends on the list of all configured devices.
465 updateGlobalMetaState();
466
467 // Update input configuration.
468 updateInputConfiguration();
469
470 // Enqueue configuration changed.
471 mDispatcher->notifyConfigurationChanged(when);
472}
473
Jeff Brown474dcb52011-06-14 20:22:50 -0700474void InputReader::refreshConfiguration(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700475 mPolicy->getReaderConfiguration(&mConfig);
476 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
477
Jeff Brown474dcb52011-06-14 20:22:50 -0700478 if (changes) {
479 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
480
481 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
482 mEventHub->requestReopenDevices();
483 } else {
484 { // acquire device registry reader lock
485 RWLock::AutoRLock _rl(mDeviceRegistryLock);
486
487 for (size_t i = 0; i < mDevices.size(); i++) {
488 InputDevice* device = mDevices.valueAt(i);
489 device->configure(&mConfig, changes);
490 }
491 } // release device registry reader lock
492 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493 }
494}
495
496void InputReader::updateGlobalMetaState() {
497 { // acquire state lock
498 AutoMutex _l(mStateLock);
499
500 mGlobalMetaState = 0;
501
502 { // acquire device registry reader lock
503 RWLock::AutoRLock _rl(mDeviceRegistryLock);
504
505 for (size_t i = 0; i < mDevices.size(); i++) {
506 InputDevice* device = mDevices.valueAt(i);
507 mGlobalMetaState |= device->getMetaState();
508 }
509 } // release device registry reader lock
510 } // release state lock
511}
512
513int32_t InputReader::getGlobalMetaState() {
514 { // acquire state lock
515 AutoMutex _l(mStateLock);
516
517 return mGlobalMetaState;
518 } // release state lock
519}
520
521void InputReader::updateInputConfiguration() {
522 { // acquire state lock
523 AutoMutex _l(mStateLock);
524
525 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
526 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
527 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
528 { // acquire device registry reader lock
529 RWLock::AutoRLock _rl(mDeviceRegistryLock);
530
531 InputDeviceInfo deviceInfo;
532 for (size_t i = 0; i < mDevices.size(); i++) {
533 InputDevice* device = mDevices.valueAt(i);
534 device->getDeviceInfo(& deviceInfo);
535 uint32_t sources = deviceInfo.getSources();
536
537 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
538 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
539 }
540 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
541 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
542 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
543 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
544 }
545 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
546 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700547 }
548 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700549 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700550
Jeff Brown6d0fec22010-07-23 21:28:06 -0700551 mInputConfiguration.touchScreen = touchScreenConfig;
552 mInputConfiguration.keyboard = keyboardConfig;
553 mInputConfiguration.navigation = navigationConfig;
554 } // release state lock
555}
556
Jeff Brownfe508922011-01-18 15:10:10 -0800557void InputReader::disableVirtualKeysUntil(nsecs_t time) {
558 mDisableVirtualKeysTimeout = time;
559}
560
561bool InputReader::shouldDropVirtualKey(nsecs_t now,
562 InputDevice* device, int32_t keyCode, int32_t scanCode) {
563 if (now < mDisableVirtualKeysTimeout) {
564 LOGI("Dropping virtual key from device %s because virtual keys are "
565 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
566 device->getName().string(),
567 (mDisableVirtualKeysTimeout - now) * 0.000001,
568 keyCode, scanCode);
569 return true;
570 } else {
571 return false;
572 }
573}
574
Jeff Brown05dc66a2011-03-02 14:41:58 -0800575void InputReader::fadePointer() {
576 { // acquire device registry reader lock
577 RWLock::AutoRLock _rl(mDeviceRegistryLock);
578
579 for (size_t i = 0; i < mDevices.size(); i++) {
580 InputDevice* device = mDevices.valueAt(i);
581 device->fadePointer();
582 }
583 } // release device registry reader lock
584}
585
Jeff Brownaa3855d2011-03-17 01:34:19 -0700586void InputReader::requestTimeoutAtTime(nsecs_t when) {
587 if (when < mNextTimeout) {
588 mNextTimeout = when;
589 }
590}
591
Jeff Brown6d0fec22010-07-23 21:28:06 -0700592void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
593 { // acquire state lock
594 AutoMutex _l(mStateLock);
595
596 *outConfiguration = mInputConfiguration;
597 } // release state lock
598}
599
600status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
601 { // acquire device registry reader lock
602 RWLock::AutoRLock _rl(mDeviceRegistryLock);
603
604 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
605 if (deviceIndex < 0) {
606 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700607 }
608
Jeff Brown6d0fec22010-07-23 21:28:06 -0700609 InputDevice* device = mDevices.valueAt(deviceIndex);
610 if (device->isIgnored()) {
611 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700612 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700613
614 device->getDeviceInfo(outDeviceInfo);
615 return OK;
616 } // release device registy reader lock
617}
618
619void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
620 outDeviceIds.clear();
621
622 { // acquire device registry reader lock
623 RWLock::AutoRLock _rl(mDeviceRegistryLock);
624
625 size_t numDevices = mDevices.size();
626 for (size_t i = 0; i < numDevices; i++) {
627 InputDevice* device = mDevices.valueAt(i);
628 if (! device->isIgnored()) {
629 outDeviceIds.add(device->getId());
630 }
631 }
632 } // release device registy reader lock
633}
634
635int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
636 int32_t keyCode) {
637 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
638}
639
640int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
641 int32_t scanCode) {
642 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
643}
644
645int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
646 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
647}
648
649int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
650 GetStateFunc getStateFunc) {
651 { // acquire device registry reader lock
652 RWLock::AutoRLock _rl(mDeviceRegistryLock);
653
654 int32_t result = AKEY_STATE_UNKNOWN;
655 if (deviceId >= 0) {
656 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
657 if (deviceIndex >= 0) {
658 InputDevice* device = mDevices.valueAt(deviceIndex);
659 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
660 result = (device->*getStateFunc)(sourceMask, code);
661 }
662 }
663 } else {
664 size_t numDevices = mDevices.size();
665 for (size_t i = 0; i < numDevices; i++) {
666 InputDevice* device = mDevices.valueAt(i);
667 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
668 result = (device->*getStateFunc)(sourceMask, code);
669 if (result >= AKEY_STATE_DOWN) {
670 return result;
671 }
672 }
673 }
674 }
675 return result;
676 } // release device registy reader lock
677}
678
679bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
680 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
681 memset(outFlags, 0, numCodes);
682 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
683}
684
685bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
686 const int32_t* keyCodes, uint8_t* outFlags) {
687 { // acquire device registry reader lock
688 RWLock::AutoRLock _rl(mDeviceRegistryLock);
689 bool result = false;
690 if (deviceId >= 0) {
691 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
692 if (deviceIndex >= 0) {
693 InputDevice* device = mDevices.valueAt(deviceIndex);
694 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
695 result = device->markSupportedKeyCodes(sourceMask,
696 numCodes, keyCodes, outFlags);
697 }
698 }
699 } else {
700 size_t numDevices = mDevices.size();
701 for (size_t i = 0; i < numDevices; i++) {
702 InputDevice* device = mDevices.valueAt(i);
703 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
704 result |= device->markSupportedKeyCodes(sourceMask,
705 numCodes, keyCodes, outFlags);
706 }
707 }
708 }
709 return result;
710 } // release device registy reader lock
711}
712
Jeff Brown474dcb52011-06-14 20:22:50 -0700713void InputReader::requestRefreshConfiguration(uint32_t changes) {
714 if (changes) {
715 bool needWake;
716 { // acquire lock
717 AutoMutex _l(mStateLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700718
Jeff Brown474dcb52011-06-14 20:22:50 -0700719 needWake = !mConfigurationChangesToRefresh;
720 mConfigurationChangesToRefresh |= changes;
721 } // release lock
722
723 if (needWake) {
724 mEventHub->wake();
725 }
726 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700727}
728
Jeff Brownb88102f2010-09-08 11:49:43 -0700729void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700730 mEventHub->dump(dump);
731 dump.append("\n");
732
733 dump.append("Input Reader State:\n");
734
Jeff Brownef3d7e82010-09-30 14:33:04 -0700735 { // acquire device registry reader lock
736 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700737
Jeff Brownef3d7e82010-09-30 14:33:04 -0700738 for (size_t i = 0; i < mDevices.size(); i++) {
739 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700740 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700741 } // release device registy reader lock
Jeff Brown214eaf42011-05-26 19:17:02 -0700742
743 dump.append(INDENT "Configuration:\n");
744 dump.append(INDENT2 "ExcludedDeviceNames: [");
745 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
746 if (i != 0) {
747 dump.append(", ");
748 }
749 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
750 }
751 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700752 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
753 mConfig.virtualKeyQuietTime * 0.000001f);
754
Jeff Brown19c97d462011-06-01 12:33:19 -0700755 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
756 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
757 mConfig.pointerVelocityControlParameters.scale,
758 mConfig.pointerVelocityControlParameters.lowThreshold,
759 mConfig.pointerVelocityControlParameters.highThreshold,
760 mConfig.pointerVelocityControlParameters.acceleration);
761
762 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
763 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
764 mConfig.wheelVelocityControlParameters.scale,
765 mConfig.wheelVelocityControlParameters.lowThreshold,
766 mConfig.wheelVelocityControlParameters.highThreshold,
767 mConfig.wheelVelocityControlParameters.acceleration);
768
Jeff Brown214eaf42011-05-26 19:17:02 -0700769 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700770 dump.appendFormat(INDENT3 "Enabled: %s\n",
771 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700772 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
773 mConfig.pointerGestureQuietInterval * 0.000001f);
774 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
775 mConfig.pointerGestureDragMinSwitchSpeed);
776 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
777 mConfig.pointerGestureTapInterval * 0.000001f);
778 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
779 mConfig.pointerGestureTapDragInterval * 0.000001f);
780 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
781 mConfig.pointerGestureTapSlop);
782 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
783 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700784 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
785 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700786 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
787 mConfig.pointerGestureSwipeTransitionAngleCosine);
788 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
789 mConfig.pointerGestureSwipeMaxWidthRatio);
790 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
791 mConfig.pointerGestureMovementSpeedRatio);
792 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
793 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700794}
795
Jeff Brown6d0fec22010-07-23 21:28:06 -0700796
797// --- InputReaderThread ---
798
799InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
800 Thread(/*canCallJava*/ true), mReader(reader) {
801}
802
803InputReaderThread::~InputReaderThread() {
804}
805
806bool InputReaderThread::threadLoop() {
807 mReader->loopOnce();
808 return true;
809}
810
811
812// --- InputDevice ---
813
814InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700815 mContext(context), mId(id), mName(name), mSources(0),
816 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700817}
818
819InputDevice::~InputDevice() {
820 size_t numMappers = mMappers.size();
821 for (size_t i = 0; i < numMappers; i++) {
822 delete mMappers[i];
823 }
824 mMappers.clear();
825}
826
Jeff Brownef3d7e82010-09-30 14:33:04 -0700827void InputDevice::dump(String8& dump) {
828 InputDeviceInfo deviceInfo;
829 getDeviceInfo(& deviceInfo);
830
Jeff Brown90655042010-12-02 13:50:46 -0800831 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700832 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800833 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700834 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
835 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800836
Jeff Brownefd32662011-03-08 15:13:06 -0800837 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800838 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700839 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800840 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800841 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
842 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800843 char name[32];
844 if (label) {
845 strncpy(name, label, sizeof(name));
846 name[sizeof(name) - 1] = '\0';
847 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800848 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800849 }
Jeff Brownefd32662011-03-08 15:13:06 -0800850 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
851 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
852 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800853 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700854 }
855
856 size_t numMappers = mMappers.size();
857 for (size_t i = 0; i < numMappers; i++) {
858 InputMapper* mapper = mMappers[i];
859 mapper->dump(dump);
860 }
861}
862
Jeff Brown6d0fec22010-07-23 21:28:06 -0700863void InputDevice::addMapper(InputMapper* mapper) {
864 mMappers.add(mapper);
865}
866
Jeff Brown474dcb52011-06-14 20:22:50 -0700867void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700868 mSources = 0;
869
Jeff Brown474dcb52011-06-14 20:22:50 -0700870 if (!isIgnored()) {
871 if (!changes) { // first time only
872 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
873 }
874
875 size_t numMappers = mMappers.size();
876 for (size_t i = 0; i < numMappers; i++) {
877 InputMapper* mapper = mMappers[i];
878 mapper->configure(config, changes);
879 mSources |= mapper->getSources();
880 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700881 }
882}
883
Jeff Brown6d0fec22010-07-23 21:28:06 -0700884void InputDevice::reset() {
885 size_t numMappers = mMappers.size();
886 for (size_t i = 0; i < numMappers; i++) {
887 InputMapper* mapper = mMappers[i];
888 mapper->reset();
889 }
890}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700891
Jeff Brownb7198742011-03-18 18:14:26 -0700892void InputDevice::process(const RawEvent* rawEvents, size_t count) {
893 // Process all of the events in order for each mapper.
894 // We cannot simply ask each mapper to process them in bulk because mappers may
895 // have side-effects that must be interleaved. For example, joystick movement events and
896 // gamepad button presses are handled by different mappers but they should be dispatched
897 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700898 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700899 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
900#if DEBUG_RAW_EVENTS
901 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700902 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700903 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
904 rawEvent->value, rawEvent->flags);
905#endif
906
Jeff Brown80fd47c2011-05-24 01:07:44 -0700907 if (mDropUntilNextSync) {
908 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
909 mDropUntilNextSync = false;
910#if DEBUG_RAW_EVENTS
911 LOGD("Recovered from input event buffer overrun.");
912#endif
913 } else {
914#if DEBUG_RAW_EVENTS
915 LOGD("Dropped input event while waiting for next input sync.");
916#endif
917 }
918 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
919 LOGI("Detected input event buffer overrun for device %s.", mName.string());
920 mDropUntilNextSync = true;
921 reset();
922 } else {
923 for (size_t i = 0; i < numMappers; i++) {
924 InputMapper* mapper = mMappers[i];
925 mapper->process(rawEvent);
926 }
Jeff Brownb7198742011-03-18 18:14:26 -0700927 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700928 }
929}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700930
Jeff Brownaa3855d2011-03-17 01:34:19 -0700931void InputDevice::timeoutExpired(nsecs_t when) {
932 size_t numMappers = mMappers.size();
933 for (size_t i = 0; i < numMappers; i++) {
934 InputMapper* mapper = mMappers[i];
935 mapper->timeoutExpired(when);
936 }
937}
938
Jeff Brown6d0fec22010-07-23 21:28:06 -0700939void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
940 outDeviceInfo->initialize(mId, mName);
941
942 size_t numMappers = mMappers.size();
943 for (size_t i = 0; i < numMappers; i++) {
944 InputMapper* mapper = mMappers[i];
945 mapper->populateDeviceInfo(outDeviceInfo);
946 }
947}
948
949int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
950 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
951}
952
953int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
954 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
955}
956
957int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
958 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
959}
960
961int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
962 int32_t result = AKEY_STATE_UNKNOWN;
963 size_t numMappers = mMappers.size();
964 for (size_t i = 0; i < numMappers; i++) {
965 InputMapper* mapper = mMappers[i];
966 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
967 result = (mapper->*getStateFunc)(sourceMask, code);
968 if (result >= AKEY_STATE_DOWN) {
969 return result;
970 }
971 }
972 }
973 return result;
974}
975
976bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
977 const int32_t* keyCodes, uint8_t* outFlags) {
978 bool result = false;
979 size_t numMappers = mMappers.size();
980 for (size_t i = 0; i < numMappers; i++) {
981 InputMapper* mapper = mMappers[i];
982 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
983 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
984 }
985 }
986 return result;
987}
988
989int32_t InputDevice::getMetaState() {
990 int32_t result = 0;
991 size_t numMappers = mMappers.size();
992 for (size_t i = 0; i < numMappers; i++) {
993 InputMapper* mapper = mMappers[i];
994 result |= mapper->getMetaState();
995 }
996 return result;
997}
998
Jeff Brown05dc66a2011-03-02 14:41:58 -0800999void InputDevice::fadePointer() {
1000 size_t numMappers = mMappers.size();
1001 for (size_t i = 0; i < numMappers; i++) {
1002 InputMapper* mapper = mMappers[i];
1003 mapper->fadePointer();
1004 }
1005}
1006
Jeff Brown6d0fec22010-07-23 21:28:06 -07001007
1008// --- InputMapper ---
1009
1010InputMapper::InputMapper(InputDevice* device) :
1011 mDevice(device), mContext(device->getContext()) {
1012}
1013
1014InputMapper::~InputMapper() {
1015}
1016
1017void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1018 info->addSource(getSources());
1019}
1020
Jeff Brownef3d7e82010-09-30 14:33:04 -07001021void InputMapper::dump(String8& dump) {
1022}
1023
Jeff Brown474dcb52011-06-14 20:22:50 -07001024void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001025}
1026
1027void InputMapper::reset() {
1028}
1029
Jeff Brownaa3855d2011-03-17 01:34:19 -07001030void InputMapper::timeoutExpired(nsecs_t when) {
1031}
1032
Jeff Brown6d0fec22010-07-23 21:28:06 -07001033int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1034 return AKEY_STATE_UNKNOWN;
1035}
1036
1037int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1038 return AKEY_STATE_UNKNOWN;
1039}
1040
1041int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1042 return AKEY_STATE_UNKNOWN;
1043}
1044
1045bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1046 const int32_t* keyCodes, uint8_t* outFlags) {
1047 return false;
1048}
1049
1050int32_t InputMapper::getMetaState() {
1051 return 0;
1052}
1053
Jeff Brown05dc66a2011-03-02 14:41:58 -08001054void InputMapper::fadePointer() {
1055}
1056
Jeff Browncb1404e2011-01-15 18:14:15 -08001057void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1058 const RawAbsoluteAxisInfo& axis, const char* name) {
1059 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001060 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1061 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001062 } else {
1063 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1064 }
1065}
1066
Jeff Brown6d0fec22010-07-23 21:28:06 -07001067
1068// --- SwitchInputMapper ---
1069
1070SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1071 InputMapper(device) {
1072}
1073
1074SwitchInputMapper::~SwitchInputMapper() {
1075}
1076
1077uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001078 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001079}
1080
1081void SwitchInputMapper::process(const RawEvent* rawEvent) {
1082 switch (rawEvent->type) {
1083 case EV_SW:
1084 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1085 break;
1086 }
1087}
1088
1089void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001090 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001091}
1092
1093int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1094 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1095}
1096
1097
1098// --- KeyboardInputMapper ---
1099
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001100KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001101 uint32_t source, int32_t keyboardType) :
1102 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001103 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001104 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001105}
1106
1107KeyboardInputMapper::~KeyboardInputMapper() {
1108}
1109
Jeff Brown6328cdc2010-07-29 18:18:33 -07001110void KeyboardInputMapper::initializeLocked() {
1111 mLocked.metaState = AMETA_NONE;
1112 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001113}
1114
1115uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001116 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001117}
1118
1119void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1120 InputMapper::populateDeviceInfo(info);
1121
1122 info->setKeyboardType(mKeyboardType);
1123}
1124
Jeff Brownef3d7e82010-09-30 14:33:04 -07001125void KeyboardInputMapper::dump(String8& dump) {
1126 { // acquire lock
1127 AutoMutex _l(mLock);
1128 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001129 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001130 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1131 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1132 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1133 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1134 } // release lock
1135}
1136
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001137
Jeff Brown474dcb52011-06-14 20:22:50 -07001138void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1139 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001140
Jeff Brown474dcb52011-06-14 20:22:50 -07001141 if (!changes) { // first time only
1142 // Configure basic parameters.
1143 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001144
Jeff Brown474dcb52011-06-14 20:22:50 -07001145 // Reset LEDs.
1146 {
1147 AutoMutex _l(mLock);
1148 resetLedStateLocked();
1149 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001150 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001151}
1152
1153void KeyboardInputMapper::configureParameters() {
1154 mParameters.orientationAware = false;
1155 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1156 mParameters.orientationAware);
1157
1158 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1159}
1160
1161void KeyboardInputMapper::dumpParameters(String8& dump) {
1162 dump.append(INDENT3 "Parameters:\n");
1163 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1164 mParameters.associatedDisplayId);
1165 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1166 toString(mParameters.orientationAware));
1167}
1168
Jeff Brown6d0fec22010-07-23 21:28:06 -07001169void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001170 for (;;) {
1171 int32_t keyCode, scanCode;
1172 { // acquire lock
1173 AutoMutex _l(mLock);
1174
1175 // Synthesize key up event on reset if keys are currently down.
1176 if (mLocked.keyDowns.isEmpty()) {
1177 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001178 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001179 break; // done
1180 }
1181
1182 const KeyDown& keyDown = mLocked.keyDowns.top();
1183 keyCode = keyDown.keyCode;
1184 scanCode = keyDown.scanCode;
1185 } // release lock
1186
Jeff Brown6d0fec22010-07-23 21:28:06 -07001187 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001188 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001189 }
1190
1191 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001192 getContext()->updateGlobalMetaState();
1193}
1194
1195void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1196 switch (rawEvent->type) {
1197 case EV_KEY: {
1198 int32_t scanCode = rawEvent->scanCode;
1199 if (isKeyboardOrGamepadKey(scanCode)) {
1200 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1201 rawEvent->flags);
1202 }
1203 break;
1204 }
1205 }
1206}
1207
1208bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1209 return scanCode < BTN_MOUSE
1210 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001211 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001212 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001213}
1214
Jeff Brown6328cdc2010-07-29 18:18:33 -07001215void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1216 int32_t scanCode, uint32_t policyFlags) {
1217 int32_t newMetaState;
1218 nsecs_t downTime;
1219 bool metaStateChanged = false;
1220
1221 { // acquire lock
1222 AutoMutex _l(mLock);
1223
1224 if (down) {
1225 // Rotate key codes according to orientation if needed.
1226 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001227 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001228 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001229 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1230 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001231 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001232 }
1233
1234 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001235 }
1236
Jeff Brown6328cdc2010-07-29 18:18:33 -07001237 // Add key down.
1238 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1239 if (keyDownIndex >= 0) {
1240 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001241 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001242 } else {
1243 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001244 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1245 && mContext->shouldDropVirtualKey(when,
1246 getDevice(), keyCode, scanCode)) {
1247 return;
1248 }
1249
Jeff Brown6328cdc2010-07-29 18:18:33 -07001250 mLocked.keyDowns.push();
1251 KeyDown& keyDown = mLocked.keyDowns.editTop();
1252 keyDown.keyCode = keyCode;
1253 keyDown.scanCode = scanCode;
1254 }
1255
1256 mLocked.downTime = when;
1257 } else {
1258 // Remove key down.
1259 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1260 if (keyDownIndex >= 0) {
1261 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001262 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001263 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1264 } else {
1265 // key was not actually down
1266 LOGI("Dropping key up from device %s because the key was not down. "
1267 "keyCode=%d, scanCode=%d",
1268 getDeviceName().string(), keyCode, scanCode);
1269 return;
1270 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001271 }
1272
Jeff Brown6328cdc2010-07-29 18:18:33 -07001273 int32_t oldMetaState = mLocked.metaState;
1274 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1275 if (oldMetaState != newMetaState) {
1276 mLocked.metaState = newMetaState;
1277 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001278 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001279 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001280
Jeff Brown6328cdc2010-07-29 18:18:33 -07001281 downTime = mLocked.downTime;
1282 } // release lock
1283
Jeff Brown56194eb2011-03-02 19:23:13 -08001284 // Key down on external an keyboard should wake the device.
1285 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1286 // For internal keyboards, the key layout file should specify the policy flags for
1287 // each wake key individually.
1288 // TODO: Use the input device configuration to control this behavior more finely.
1289 if (down && getDevice()->isExternal()
1290 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1291 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1292 }
1293
Jeff Brown6328cdc2010-07-29 18:18:33 -07001294 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001295 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001296 }
1297
Jeff Brown05dc66a2011-03-02 14:41:58 -08001298 if (down && !isMetaKey(keyCode)) {
1299 getContext()->fadePointer();
1300 }
1301
Jeff Brownefd32662011-03-08 15:13:06 -08001302 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001303 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1304 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001305}
1306
Jeff Brown6328cdc2010-07-29 18:18:33 -07001307ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1308 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001309 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001310 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001311 return i;
1312 }
1313 }
1314 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001315}
1316
Jeff Brown6d0fec22010-07-23 21:28:06 -07001317int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1318 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1319}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001320
Jeff Brown6d0fec22010-07-23 21:28:06 -07001321int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1322 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1323}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001324
Jeff Brown6d0fec22010-07-23 21:28:06 -07001325bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1326 const int32_t* keyCodes, uint8_t* outFlags) {
1327 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1328}
1329
1330int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001331 { // acquire lock
1332 AutoMutex _l(mLock);
1333 return mLocked.metaState;
1334 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001335}
1336
Jeff Brown49ed71d2010-12-06 17:13:33 -08001337void KeyboardInputMapper::resetLedStateLocked() {
1338 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1339 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1340 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1341
1342 updateLedStateLocked(true);
1343}
1344
1345void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1346 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1347 ledState.on = false;
1348}
1349
Jeff Brown497a92c2010-09-12 17:55:08 -07001350void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1351 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001352 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001353 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001354 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001355 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001356 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001357}
1358
1359void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1360 int32_t led, int32_t modifier, bool reset) {
1361 if (ledState.avail) {
1362 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001363 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001364 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1365 ledState.on = desiredState;
1366 }
1367 }
1368}
1369
Jeff Brown6d0fec22010-07-23 21:28:06 -07001370
Jeff Brown83c09682010-12-23 17:50:18 -08001371// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001372
Jeff Brown83c09682010-12-23 17:50:18 -08001373CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001374 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001375 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001376}
1377
Jeff Brown83c09682010-12-23 17:50:18 -08001378CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001379}
1380
Jeff Brown83c09682010-12-23 17:50:18 -08001381uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001382 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001383}
1384
Jeff Brown83c09682010-12-23 17:50:18 -08001385void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001386 InputMapper::populateDeviceInfo(info);
1387
Jeff Brown83c09682010-12-23 17:50:18 -08001388 if (mParameters.mode == Parameters::MODE_POINTER) {
1389 float minX, minY, maxX, maxY;
1390 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001391 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1392 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001393 }
1394 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001395 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1396 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001397 }
Jeff Brownefd32662011-03-08 15:13:06 -08001398 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001399
1400 if (mHaveVWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001401 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001402 }
1403 if (mHaveHWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001404 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001405 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001406}
1407
Jeff Brown83c09682010-12-23 17:50:18 -08001408void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001409 { // acquire lock
1410 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001411 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001412 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001413 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1414 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001415 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1416 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001417 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1418 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1419 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1420 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001421 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1422 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001423 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1424 } // release lock
1425}
1426
Jeff Brown474dcb52011-06-14 20:22:50 -07001427void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1428 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001429
Jeff Brown474dcb52011-06-14 20:22:50 -07001430 if (!changes) { // first time only
1431 // Configure basic parameters.
1432 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001433
Jeff Brown474dcb52011-06-14 20:22:50 -07001434 // Configure device mode.
1435 switch (mParameters.mode) {
1436 case Parameters::MODE_POINTER:
1437 mSource = AINPUT_SOURCE_MOUSE;
1438 mXPrecision = 1.0f;
1439 mYPrecision = 1.0f;
1440 mXScale = 1.0f;
1441 mYScale = 1.0f;
1442 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1443 break;
1444 case Parameters::MODE_NAVIGATION:
1445 mSource = AINPUT_SOURCE_TRACKBALL;
1446 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1447 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1448 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1449 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1450 break;
1451 }
1452
1453 mVWheelScale = 1.0f;
1454 mHWheelScale = 1.0f;
1455
1456 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1457 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown83c09682010-12-23 17:50:18 -08001458 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001459
Jeff Brown474dcb52011-06-14 20:22:50 -07001460 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1461 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1462 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1463 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1464 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001465}
1466
Jeff Brown83c09682010-12-23 17:50:18 -08001467void CursorInputMapper::configureParameters() {
1468 mParameters.mode = Parameters::MODE_POINTER;
1469 String8 cursorModeString;
1470 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1471 if (cursorModeString == "navigation") {
1472 mParameters.mode = Parameters::MODE_NAVIGATION;
1473 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1474 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1475 }
1476 }
1477
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001478 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001479 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001480 mParameters.orientationAware);
1481
Jeff Brown83c09682010-12-23 17:50:18 -08001482 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1483 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001484}
1485
Jeff Brown83c09682010-12-23 17:50:18 -08001486void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001487 dump.append(INDENT3 "Parameters:\n");
1488 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1489 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001490
1491 switch (mParameters.mode) {
1492 case Parameters::MODE_POINTER:
1493 dump.append(INDENT4 "Mode: pointer\n");
1494 break;
1495 case Parameters::MODE_NAVIGATION:
1496 dump.append(INDENT4 "Mode: navigation\n");
1497 break;
1498 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001499 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001500 }
1501
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001502 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1503 toString(mParameters.orientationAware));
1504}
1505
Jeff Brown83c09682010-12-23 17:50:18 -08001506void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001507 mAccumulator.clear();
1508
Jeff Brownefd32662011-03-08 15:13:06 -08001509 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001510 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001511}
1512
Jeff Brown83c09682010-12-23 17:50:18 -08001513void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001514 for (;;) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001515 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001516 { // acquire lock
1517 AutoMutex _l(mLock);
1518
Jeff Brownefd32662011-03-08 15:13:06 -08001519 buttonState = mLocked.buttonState;
1520 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001521 initializeLocked();
1522 break; // done
1523 }
1524 } // release lock
1525
Jeff Brown19c97d462011-06-01 12:33:19 -07001526 // Reset velocity.
1527 mPointerVelocityControl.reset();
1528 mWheelXVelocityControl.reset();
1529 mWheelYVelocityControl.reset();
1530
Jeff Brown83c09682010-12-23 17:50:18 -08001531 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001532 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownefd32662011-03-08 15:13:06 -08001533 mAccumulator.clear();
1534 mAccumulator.buttonDown = 0;
1535 mAccumulator.buttonUp = buttonState;
1536 mAccumulator.fields = Accumulator::FIELD_BUTTONS;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001537 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001538 }
1539
Jeff Brown6d0fec22010-07-23 21:28:06 -07001540 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001541}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001542
Jeff Brown83c09682010-12-23 17:50:18 -08001543void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001544 switch (rawEvent->type) {
Jeff Brownefd32662011-03-08 15:13:06 -08001545 case EV_KEY: {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001546 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownefd32662011-03-08 15:13:06 -08001547 if (buttonState) {
1548 if (rawEvent->value) {
1549 mAccumulator.buttonDown = buttonState;
1550 mAccumulator.buttonUp = 0;
1551 } else {
1552 mAccumulator.buttonDown = 0;
1553 mAccumulator.buttonUp = buttonState;
1554 }
1555 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
1556
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001557 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1558 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001559 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001560 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001561 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001562 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001563 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001564
Jeff Brown6d0fec22010-07-23 21:28:06 -07001565 case EV_REL:
1566 switch (rawEvent->scanCode) {
1567 case REL_X:
1568 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1569 mAccumulator.relX = rawEvent->value;
1570 break;
1571 case REL_Y:
1572 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1573 mAccumulator.relY = rawEvent->value;
1574 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001575 case REL_WHEEL:
1576 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1577 mAccumulator.relWheel = rawEvent->value;
1578 break;
1579 case REL_HWHEEL:
1580 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1581 mAccumulator.relHWheel = rawEvent->value;
1582 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001583 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001584 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001585
Jeff Brown6d0fec22010-07-23 21:28:06 -07001586 case EV_SYN:
1587 switch (rawEvent->scanCode) {
1588 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001589 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001590 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001591 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001592 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001593 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001594}
1595
Jeff Brown83c09682010-12-23 17:50:18 -08001596void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001597 uint32_t fields = mAccumulator.fields;
1598 if (fields == 0) {
1599 return; // no new state changes, so nothing to do
1600 }
1601
Jeff Brown9626b142011-03-03 02:09:54 -08001602 int32_t motionEventAction;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001603 int32_t lastButtonState, currentButtonState;
1604 PointerProperties pointerProperties;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001605 PointerCoords pointerCoords;
1606 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001607 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001608 { // acquire lock
1609 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001610
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001611 lastButtonState = mLocked.buttonState;
1612
Jeff Brownefd32662011-03-08 15:13:06 -08001613 bool down, downChanged;
1614 bool wasDown = isPointerDown(mLocked.buttonState);
1615 bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS;
1616 if (buttonsChanged) {
1617 mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown)
1618 & ~mAccumulator.buttonUp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001619
Jeff Brownefd32662011-03-08 15:13:06 -08001620 down = isPointerDown(mLocked.buttonState);
1621
1622 if (!wasDown && down) {
1623 mLocked.downTime = when;
1624 downChanged = true;
1625 } else if (wasDown && !down) {
1626 downChanged = true;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001627 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001628 downChanged = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001629 }
Jeff Brownefd32662011-03-08 15:13:06 -08001630 } else {
1631 down = wasDown;
1632 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001633 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001634
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001635 currentButtonState = mLocked.buttonState;
1636
Jeff Brown6328cdc2010-07-29 18:18:33 -07001637 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001638 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1639 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001640
Jeff Brown6328cdc2010-07-29 18:18:33 -07001641 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001642 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1643 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001644 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001645 } else {
1646 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001647 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001648
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001649 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001650 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001651 // Rotate motion based on display orientation if needed.
1652 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1653 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001654 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1655 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001656 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001657 }
1658
1659 float temp;
1660 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001661 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001662 temp = deltaX;
1663 deltaX = deltaY;
1664 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001665 break;
1666
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001667 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001668 deltaX = -deltaX;
1669 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001670 break;
1671
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001672 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001673 temp = deltaX;
1674 deltaX = -deltaY;
1675 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001676 break;
1677 }
1678 }
Jeff Brown83c09682010-12-23 17:50:18 -08001679
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001680 pointerProperties.clear();
1681 pointerProperties.id = 0;
1682 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
1683
1684 pointerCoords.clear();
1685
Jeff Brown2352b972011-04-12 22:39:53 -07001686 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
1687 vscroll = mAccumulator.relWheel;
1688 } else {
1689 vscroll = 0;
1690 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001691 mWheelYVelocityControl.move(when, NULL, &vscroll);
1692
Jeff Brown2352b972011-04-12 22:39:53 -07001693 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
1694 hscroll = mAccumulator.relHWheel;
1695 } else {
1696 hscroll = 0;
1697 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001698 mWheelXVelocityControl.move(when, &hscroll, NULL);
1699
1700 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown2352b972011-04-12 22:39:53 -07001701
Jeff Brown83c09682010-12-23 17:50:18 -08001702 if (mPointerController != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -07001703 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
1704 || buttonsChanged) {
1705 mPointerController->setPresentation(
1706 PointerControllerInterface::PRESENTATION_POINTER);
1707
1708 if (deltaX != 0 || deltaY != 0) {
1709 mPointerController->move(deltaX, deltaY);
1710 }
1711
1712 if (buttonsChanged) {
1713 mPointerController->setButtonState(mLocked.buttonState);
1714 }
1715
Jeff Brown538881e2011-05-25 18:23:38 -07001716 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08001717 }
Jeff Brownefd32662011-03-08 15:13:06 -08001718
Jeff Brown91c69ab2011-02-14 17:03:18 -08001719 float x, y;
1720 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001721 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1722 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83c09682010-12-23 17:50:18 -08001723 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001724 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1725 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001726 }
1727
Jeff Brownefd32662011-03-08 15:13:06 -08001728 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001729 } // release lock
1730
Jeff Brown56194eb2011-03-02 19:23:13 -08001731 // Moving an external trackball or mouse should wake the device.
1732 // We don't do this for internal cursor devices to prevent them from waking up
1733 // the device in your pocket.
1734 // TODO: Use the input device configuration to control this behavior more finely.
1735 uint32_t policyFlags = 0;
1736 if (getDevice()->isExternal()) {
1737 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1738 }
1739
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001740 // Synthesize key down from buttons if needed.
1741 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
1742 policyFlags, lastButtonState, currentButtonState);
1743
1744 // Send motion event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001745 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownefd32662011-03-08 15:13:06 -08001746 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Browna6111372011-07-14 21:48:23 -07001747 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001748 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownb6997262010-10-08 22:31:17 -07001749
Jeff Browna032cc02011-03-07 16:56:21 -08001750 // Send hover move after UP to tell the application that the mouse is hovering now.
1751 if (motionEventAction == AMOTION_EVENT_ACTION_UP
1752 && mPointerController != NULL) {
1753 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001754 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
1755 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1756 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Browna032cc02011-03-07 16:56:21 -08001757 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001758
Jeff Browna032cc02011-03-07 16:56:21 -08001759 // Send scroll events.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001760 if (vscroll != 0 || hscroll != 0) {
1761 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1762 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1763
Jeff Brownefd32662011-03-08 15:13:06 -08001764 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001765 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
1766 AMOTION_EVENT_EDGE_FLAG_NONE,
1767 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001768 }
Jeff Browna032cc02011-03-07 16:56:21 -08001769
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001770 // Synthesize key up from buttons if needed.
1771 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
1772 policyFlags, lastButtonState, currentButtonState);
1773
Jeff Browna032cc02011-03-07 16:56:21 -08001774 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001775}
1776
Jeff Brown83c09682010-12-23 17:50:18 -08001777int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001778 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1779 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1780 } else {
1781 return AKEY_STATE_UNKNOWN;
1782 }
1783}
1784
Jeff Brown05dc66a2011-03-02 14:41:58 -08001785void CursorInputMapper::fadePointer() {
1786 { // acquire lock
1787 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08001788 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07001789 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownefd32662011-03-08 15:13:06 -08001790 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001791 } // release lock
1792}
1793
Jeff Brown6d0fec22010-07-23 21:28:06 -07001794
1795// --- TouchInputMapper ---
1796
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001797TouchInputMapper::TouchInputMapper(InputDevice* device) :
1798 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001799 mLocked.surfaceOrientation = -1;
1800 mLocked.surfaceWidth = -1;
1801 mLocked.surfaceHeight = -1;
1802
1803 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001804}
1805
1806TouchInputMapper::~TouchInputMapper() {
1807}
1808
1809uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08001810 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001811}
1812
1813void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1814 InputMapper::populateDeviceInfo(info);
1815
Jeff Brown6328cdc2010-07-29 18:18:33 -07001816 { // acquire lock
1817 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001818
Jeff Brown6328cdc2010-07-29 18:18:33 -07001819 // Ensure surface information is up to date so that orientation changes are
1820 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08001821 if (!configureSurfaceLocked()) {
1822 return;
1823 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001824
Jeff Brownefd32662011-03-08 15:13:06 -08001825 info->addMotionRange(mLocked.orientedRanges.x);
1826 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001827
1828 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08001829 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07001830 }
1831
1832 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001833 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07001834 }
1835
Jeff Brownc6d282b2010-10-14 21:42:15 -07001836 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001837 info->addMotionRange(mLocked.orientedRanges.touchMajor);
1838 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001839 }
1840
Jeff Brownc6d282b2010-10-14 21:42:15 -07001841 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001842 info->addMotionRange(mLocked.orientedRanges.toolMajor);
1843 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001844 }
1845
1846 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08001847 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07001848 }
Jeff Brownace13b12011-03-09 17:39:48 -08001849
Jeff Brown80fd47c2011-05-24 01:07:44 -07001850 if (mLocked.orientedRanges.haveDistance) {
1851 info->addMotionRange(mLocked.orientedRanges.distance);
1852 }
1853
Jeff Brownace13b12011-03-09 17:39:48 -08001854 if (mPointerController != NULL) {
1855 float minX, minY, maxX, maxY;
1856 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1857 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
1858 minX, maxX, 0.0f, 0.0f);
1859 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
1860 minY, maxY, 0.0f, 0.0f);
1861 }
1862 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
1863 0.0f, 1.0f, 0.0f, 0.0f);
1864 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001865 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001866}
1867
Jeff Brownef3d7e82010-09-30 14:33:04 -07001868void TouchInputMapper::dump(String8& dump) {
1869 { // acquire lock
1870 AutoMutex _l(mLock);
1871 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001872 dumpParameters(dump);
1873 dumpVirtualKeysLocked(dump);
1874 dumpRawAxes(dump);
1875 dumpCalibration(dump);
1876 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08001877
Jeff Brown511ee5f2010-10-18 13:32:20 -07001878 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001879 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1880 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1881 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1882 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1883 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1884 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1885 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1886 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1887 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1888 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1889 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001890 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001891 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mLocked.distanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001892
1893 dump.appendFormat(INDENT3 "Last Touch:\n");
Jeff Brownace13b12011-03-09 17:39:48 -08001894 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
Jeff Brownaba321a2011-06-28 20:34:40 -07001895 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
1896 for (uint32_t i = 0; i < mLastTouch.pointerCount; i++) {
1897 const PointerData& pointer = mLastTouch.pointers[i];
1898 dump.appendFormat(INDENT5 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
1899 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
1900 "orientation=%d, distance=%d, isStylus=%s\n", i,
1901 pointer.id, pointer.x, pointer.y, pointer.pressure,
1902 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor, pointer.toolMinor,
1903 pointer.orientation, pointer.distance, toString(pointer.isStylus));
1904 }
Jeff Brownace13b12011-03-09 17:39:48 -08001905
1906 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
1907 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
1908 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
1909 mLocked.pointerGestureXMovementScale);
1910 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
1911 mLocked.pointerGestureYMovementScale);
1912 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
1913 mLocked.pointerGestureXZoomScale);
1914 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
1915 mLocked.pointerGestureYZoomScale);
Jeff Brown2352b972011-04-12 22:39:53 -07001916 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
1917 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brownace13b12011-03-09 17:39:48 -08001918 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07001919 } // release lock
1920}
1921
Jeff Brown6328cdc2010-07-29 18:18:33 -07001922void TouchInputMapper::initializeLocked() {
1923 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001924 mLastTouch.clear();
1925 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001926
Jeff Brown6328cdc2010-07-29 18:18:33 -07001927 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001928
1929 mLocked.orientedRanges.havePressure = false;
1930 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001931 mLocked.orientedRanges.haveTouchSize = false;
1932 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001933 mLocked.orientedRanges.haveOrientation = false;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001934 mLocked.orientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08001935
1936 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07001937}
1938
Jeff Brown474dcb52011-06-14 20:22:50 -07001939void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1940 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001941
Jeff Brown474dcb52011-06-14 20:22:50 -07001942 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001943
Jeff Brown474dcb52011-06-14 20:22:50 -07001944 if (!changes) { // first time only
1945 // Configure basic parameters.
1946 configureParameters();
1947
1948 // Configure sources.
1949 switch (mParameters.deviceType) {
1950 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1951 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
1952 mPointerSource = 0;
1953 break;
1954 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1955 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1956 mPointerSource = 0;
1957 break;
1958 case Parameters::DEVICE_TYPE_POINTER:
1959 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1960 mPointerSource = AINPUT_SOURCE_MOUSE;
1961 break;
1962 default:
1963 LOG_ASSERT(false);
1964 }
1965
1966 // Configure absolute axis information.
1967 configureRawAxes();
1968
1969 // Prepare input device calibration.
1970 parseCalibration();
1971 resolveCalibration();
1972
1973 { // acquire lock
1974 AutoMutex _l(mLock);
1975
1976 // Configure surface dimensions and orientation.
1977 configureSurfaceLocked();
1978 } // release lock
Jeff Brown83c09682010-12-23 17:50:18 -08001979 }
1980
Jeff Brown474dcb52011-06-14 20:22:50 -07001981 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1982 mPointerGesture.pointerVelocityControl.setParameters(
1983 mConfig.pointerVelocityControlParameters);
1984 }
Jeff Brown8d608662010-08-30 03:02:23 -07001985
Jeff Brown474dcb52011-06-14 20:22:50 -07001986 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
1987 // Reset the touch screen when pointer gesture enablement changes.
1988 reset();
1989 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001990}
1991
Jeff Brown8d608662010-08-30 03:02:23 -07001992void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07001993 // Use the pointer presentation mode for devices that do not support distinct
1994 // multitouch. The spot-based presentation relies on being able to accurately
1995 // locate two or more fingers on the touch pad.
1996 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
1997 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07001998
Jeff Brown538881e2011-05-25 18:23:38 -07001999 String8 gestureModeString;
2000 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2001 gestureModeString)) {
2002 if (gestureModeString == "pointer") {
2003 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2004 } else if (gestureModeString == "spots") {
2005 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2006 } else if (gestureModeString != "default") {
2007 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2008 }
2009 }
2010
Jeff Brownace13b12011-03-09 17:39:48 -08002011 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2012 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2013 // The device is a cursor device with a touch pad attached.
2014 // By default don't use the touch pad to move the pointer.
2015 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002016 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2017 // The device is a pointing device like a track pad.
2018 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2019 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2020 // The device is a touch screen.
2021 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002022 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002023 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002024 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2025 }
2026
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002027 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002028 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2029 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002030 if (deviceTypeString == "touchScreen") {
2031 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002032 } else if (deviceTypeString == "touchPad") {
2033 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002034 } else if (deviceTypeString == "pointer") {
2035 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002036 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002037 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2038 }
2039 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002040
Jeff Brownefd32662011-03-08 15:13:06 -08002041 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002042 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2043 mParameters.orientationAware);
2044
Jeff Brownefd32662011-03-08 15:13:06 -08002045 mParameters.associatedDisplayId = mParameters.orientationAware
2046 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownace13b12011-03-09 17:39:48 -08002047 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08002048 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07002049}
2050
Jeff Brownef3d7e82010-09-30 14:33:04 -07002051void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002052 dump.append(INDENT3 "Parameters:\n");
2053
Jeff Brown538881e2011-05-25 18:23:38 -07002054 switch (mParameters.gestureMode) {
2055 case Parameters::GESTURE_MODE_POINTER:
2056 dump.append(INDENT4 "GestureMode: pointer\n");
2057 break;
2058 case Parameters::GESTURE_MODE_SPOTS:
2059 dump.append(INDENT4 "GestureMode: spots\n");
2060 break;
2061 default:
2062 assert(false);
2063 }
2064
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002065 switch (mParameters.deviceType) {
2066 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2067 dump.append(INDENT4 "DeviceType: touchScreen\n");
2068 break;
2069 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2070 dump.append(INDENT4 "DeviceType: touchPad\n");
2071 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002072 case Parameters::DEVICE_TYPE_POINTER:
2073 dump.append(INDENT4 "DeviceType: pointer\n");
2074 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002075 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002076 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002077 }
2078
2079 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2080 mParameters.associatedDisplayId);
2081 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2082 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002083}
2084
Jeff Brown8d608662010-08-30 03:02:23 -07002085void TouchInputMapper::configureRawAxes() {
2086 mRawAxes.x.clear();
2087 mRawAxes.y.clear();
2088 mRawAxes.pressure.clear();
2089 mRawAxes.touchMajor.clear();
2090 mRawAxes.touchMinor.clear();
2091 mRawAxes.toolMajor.clear();
2092 mRawAxes.toolMinor.clear();
2093 mRawAxes.orientation.clear();
Jeff Brown80fd47c2011-05-24 01:07:44 -07002094 mRawAxes.distance.clear();
2095 mRawAxes.trackingId.clear();
2096 mRawAxes.slot.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002097}
2098
Jeff Brownef3d7e82010-09-30 14:33:04 -07002099void TouchInputMapper::dumpRawAxes(String8& dump) {
2100 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002101 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2102 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2103 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2104 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2105 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2106 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2107 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2108 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown80fd47c2011-05-24 01:07:44 -07002109 dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance");
2110 dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId");
2111 dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002112}
2113
Jeff Brown6328cdc2010-07-29 18:18:33 -07002114bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002115 // Ensure we have valid X and Y axes.
2116 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2117 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2118 "The device will be inoperable.", getDeviceName().string());
2119 return false;
2120 }
2121
Jeff Brown6d0fec22010-07-23 21:28:06 -07002122 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002123 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002124 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2125 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002126
2127 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002128 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002129 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002130 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2131 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002132 return false;
2133 }
Jeff Brownefd32662011-03-08 15:13:06 -08002134
2135 // A touch screen inherits the dimensions of the display.
2136 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2137 width = mLocked.associatedDisplayWidth;
2138 height = mLocked.associatedDisplayHeight;
2139 }
2140
2141 // The device inherits the orientation of the display if it is orientation aware.
2142 if (mParameters.orientationAware) {
2143 orientation = mLocked.associatedDisplayOrientation;
2144 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002145 }
2146
Jeff Brownace13b12011-03-09 17:39:48 -08002147 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2148 && mPointerController == NULL) {
2149 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2150 }
2151
Jeff Brown6328cdc2010-07-29 18:18:33 -07002152 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002153 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002154 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002155 }
2156
Jeff Brown6328cdc2010-07-29 18:18:33 -07002157 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002158 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002159 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002160 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002161
Jeff Brown6328cdc2010-07-29 18:18:33 -07002162 mLocked.surfaceWidth = width;
2163 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002164
Jeff Brown8d608662010-08-30 03:02:23 -07002165 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002166 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2167 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2168 mLocked.xPrecision = 1.0f / mLocked.xScale;
2169 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002170
Jeff Brownefd32662011-03-08 15:13:06 -08002171 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2172 mLocked.orientedRanges.x.source = mTouchSource;
2173 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2174 mLocked.orientedRanges.y.source = mTouchSource;
2175
Jeff Brown9626b142011-03-03 02:09:54 -08002176 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002177
Jeff Brown8d608662010-08-30 03:02:23 -07002178 // Scale factor for terms that are not oriented in a particular axis.
2179 // If the pixels are square then xScale == yScale otherwise we fake it
2180 // by choosing an average.
2181 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002182
Jeff Brown8d608662010-08-30 03:02:23 -07002183 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002184 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002185
Jeff Brown8d608662010-08-30 03:02:23 -07002186 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002187 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2188 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002189
2190 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2191 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002192 mLocked.orientedRanges.touchMajor.min = 0;
2193 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2194 mLocked.orientedRanges.touchMajor.flat = 0;
2195 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002196
Jeff Brown8d608662010-08-30 03:02:23 -07002197 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002198 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002199 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002200
Jeff Brown8d608662010-08-30 03:02:23 -07002201 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002202 mLocked.toolSizeLinearScale = 0;
2203 mLocked.toolSizeLinearBias = 0;
2204 mLocked.toolSizeAreaScale = 0;
2205 mLocked.toolSizeAreaBias = 0;
2206 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2207 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2208 if (mCalibration.haveToolSizeLinearScale) {
2209 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002210 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002211 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002212 / mRawAxes.toolMajor.maxValue;
2213 }
2214
Jeff Brownc6d282b2010-10-14 21:42:15 -07002215 if (mCalibration.haveToolSizeLinearBias) {
2216 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2217 }
2218 } else if (mCalibration.toolSizeCalibration ==
2219 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2220 if (mCalibration.haveToolSizeLinearScale) {
2221 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2222 } else {
2223 mLocked.toolSizeLinearScale = min(width, height);
2224 }
2225
2226 if (mCalibration.haveToolSizeLinearBias) {
2227 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2228 }
2229
2230 if (mCalibration.haveToolSizeAreaScale) {
2231 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2232 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2233 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2234 }
2235
2236 if (mCalibration.haveToolSizeAreaBias) {
2237 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002238 }
2239 }
2240
Jeff Brownc6d282b2010-10-14 21:42:15 -07002241 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002242
2243 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2244 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002245 mLocked.orientedRanges.toolMajor.min = 0;
2246 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2247 mLocked.orientedRanges.toolMajor.flat = 0;
2248 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002249
Jeff Brown8d608662010-08-30 03:02:23 -07002250 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002251 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002252 }
2253
2254 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002255 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002256 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2257 RawAbsoluteAxisInfo rawPressureAxis;
2258 switch (mCalibration.pressureSource) {
2259 case Calibration::PRESSURE_SOURCE_PRESSURE:
2260 rawPressureAxis = mRawAxes.pressure;
2261 break;
2262 case Calibration::PRESSURE_SOURCE_TOUCH:
2263 rawPressureAxis = mRawAxes.touchMajor;
2264 break;
2265 default:
2266 rawPressureAxis.clear();
2267 }
2268
Jeff Brown8d608662010-08-30 03:02:23 -07002269 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2270 || mCalibration.pressureCalibration
2271 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2272 if (mCalibration.havePressureScale) {
2273 mLocked.pressureScale = mCalibration.pressureScale;
2274 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2275 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2276 }
2277 }
2278
2279 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002280
2281 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2282 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002283 mLocked.orientedRanges.pressure.min = 0;
2284 mLocked.orientedRanges.pressure.max = 1.0;
2285 mLocked.orientedRanges.pressure.flat = 0;
2286 mLocked.orientedRanges.pressure.fuzz = 0;
2287 }
2288
2289 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002290 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002291 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002292 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2293 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2294 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2295 }
2296 }
2297
2298 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002299
2300 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2301 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002302 mLocked.orientedRanges.size.min = 0;
2303 mLocked.orientedRanges.size.max = 1.0;
2304 mLocked.orientedRanges.size.flat = 0;
2305 mLocked.orientedRanges.size.fuzz = 0;
2306 }
2307
2308 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002309 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002310 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002311 if (mCalibration.orientationCalibration
2312 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2313 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2314 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2315 }
2316 }
2317
Jeff Brown80fd47c2011-05-24 01:07:44 -07002318 mLocked.orientedRanges.haveOrientation = true;
2319
Jeff Brownefd32662011-03-08 15:13:06 -08002320 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2321 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002322 mLocked.orientedRanges.orientation.min = - M_PI_2;
2323 mLocked.orientedRanges.orientation.max = M_PI_2;
2324 mLocked.orientedRanges.orientation.flat = 0;
2325 mLocked.orientedRanges.orientation.fuzz = 0;
2326 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002327
2328 // Distance
2329 mLocked.distanceScale = 0;
2330 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2331 if (mCalibration.distanceCalibration
2332 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2333 if (mCalibration.haveDistanceScale) {
2334 mLocked.distanceScale = mCalibration.distanceScale;
2335 } else {
2336 mLocked.distanceScale = 1.0f;
2337 }
2338 }
2339
2340 mLocked.orientedRanges.haveDistance = true;
2341
2342 mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2343 mLocked.orientedRanges.distance.source = mTouchSource;
2344 mLocked.orientedRanges.distance.min =
2345 mRawAxes.distance.minValue * mLocked.distanceScale;
2346 mLocked.orientedRanges.distance.max =
2347 mRawAxes.distance.minValue * mLocked.distanceScale;
2348 mLocked.orientedRanges.distance.flat = 0;
2349 mLocked.orientedRanges.distance.fuzz =
2350 mRawAxes.distance.fuzz * mLocked.distanceScale;
2351 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002352 }
2353
2354 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002355 // Compute oriented surface dimensions, precision, scales and ranges.
2356 // Note that the maximum value reported is an inclusive maximum value so it is one
2357 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002358 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002359 case DISPLAY_ORIENTATION_90:
2360 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002361 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2362 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002363
Jeff Brown6328cdc2010-07-29 18:18:33 -07002364 mLocked.orientedXPrecision = mLocked.yPrecision;
2365 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002366
2367 mLocked.orientedRanges.x.min = 0;
2368 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2369 * mLocked.yScale;
2370 mLocked.orientedRanges.x.flat = 0;
2371 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2372
2373 mLocked.orientedRanges.y.min = 0;
2374 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2375 * mLocked.xScale;
2376 mLocked.orientedRanges.y.flat = 0;
2377 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002378 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002379
Jeff Brown6d0fec22010-07-23 21:28:06 -07002380 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002381 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2382 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002383
Jeff Brown6328cdc2010-07-29 18:18:33 -07002384 mLocked.orientedXPrecision = mLocked.xPrecision;
2385 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002386
2387 mLocked.orientedRanges.x.min = 0;
2388 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2389 * mLocked.xScale;
2390 mLocked.orientedRanges.x.flat = 0;
2391 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2392
2393 mLocked.orientedRanges.y.min = 0;
2394 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2395 * mLocked.yScale;
2396 mLocked.orientedRanges.y.flat = 0;
2397 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002398 break;
2399 }
Jeff Brownace13b12011-03-09 17:39:48 -08002400
2401 // Compute pointer gesture detection parameters.
2402 // TODO: These factors should not be hardcoded.
2403 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2404 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2405 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002406 float rawDiagonal = hypotf(rawWidth, rawHeight);
2407 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2408 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002409
Jeff Brown2352b972011-04-12 22:39:53 -07002410 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002411 // given area relative to the diagonal size of the display when no acceleration
2412 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002413 // Assume that the touch pad has a square aspect ratio such that movements in
2414 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown474dcb52011-06-14 20:22:50 -07002415 mLocked.pointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002416 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002417 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2418
2419 // Scale zooms to cover a smaller range of the display than movements do.
2420 // This value determines the area around the pointer that is affected by freeform
2421 // pointer gestures.
Jeff Brown474dcb52011-06-14 20:22:50 -07002422 mLocked.pointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002423 * displayDiagonal / rawDiagonal;
2424 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002425
Jeff Brown2352b972011-04-12 22:39:53 -07002426 // Max width between pointers to detect a swipe gesture is more than some fraction
2427 // of the diagonal axis of the touch pad. Touches that are wider than this are
2428 // translated into freeform gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002429 mLocked.pointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002430 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002431
2432 // Reset the current pointer gesture.
2433 mPointerGesture.reset();
2434
2435 // Remove any current spots.
2436 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2437 mPointerController->clearSpots();
2438 }
Jeff Brownace13b12011-03-09 17:39:48 -08002439 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002440 }
2441
2442 return true;
2443}
2444
Jeff Brownef3d7e82010-09-30 14:33:04 -07002445void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2446 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2447 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2448 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002449}
2450
Jeff Brown6328cdc2010-07-29 18:18:33 -07002451void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002452 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002453 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002454
Jeff Brown6328cdc2010-07-29 18:18:33 -07002455 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002456
Jeff Brown6328cdc2010-07-29 18:18:33 -07002457 if (virtualKeyDefinitions.size() == 0) {
2458 return;
2459 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002460
Jeff Brown6328cdc2010-07-29 18:18:33 -07002461 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2462
Jeff Brown8d608662010-08-30 03:02:23 -07002463 int32_t touchScreenLeft = mRawAxes.x.minValue;
2464 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002465 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2466 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002467
2468 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002469 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002470 virtualKeyDefinitions[i];
2471
2472 mLocked.virtualKeys.add();
2473 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2474
2475 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2476 int32_t keyCode;
2477 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002478 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002479 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002480 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2481 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002482 mLocked.virtualKeys.pop(); // drop the key
2483 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002484 }
2485
Jeff Brown6328cdc2010-07-29 18:18:33 -07002486 virtualKey.keyCode = keyCode;
2487 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002488
Jeff Brown6328cdc2010-07-29 18:18:33 -07002489 // convert the key definition's display coordinates into touch coordinates for a hit box
2490 int32_t halfWidth = virtualKeyDefinition.width / 2;
2491 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002492
Jeff Brown6328cdc2010-07-29 18:18:33 -07002493 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2494 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2495 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2496 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2497 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2498 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2499 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2500 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002501 }
2502}
2503
2504void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2505 if (!mLocked.virtualKeys.isEmpty()) {
2506 dump.append(INDENT3 "Virtual Keys:\n");
2507
2508 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2509 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2510 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2511 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2512 i, virtualKey.scanCode, virtualKey.keyCode,
2513 virtualKey.hitLeft, virtualKey.hitRight,
2514 virtualKey.hitTop, virtualKey.hitBottom);
2515 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002516 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002517}
2518
Jeff Brown8d608662010-08-30 03:02:23 -07002519void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002520 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002521 Calibration& out = mCalibration;
2522
Jeff Brownc6d282b2010-10-14 21:42:15 -07002523 // Touch Size
2524 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2525 String8 touchSizeCalibrationString;
2526 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2527 if (touchSizeCalibrationString == "none") {
2528 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2529 } else if (touchSizeCalibrationString == "geometric") {
2530 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2531 } else if (touchSizeCalibrationString == "pressure") {
2532 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2533 } else if (touchSizeCalibrationString != "default") {
2534 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2535 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002536 }
2537 }
2538
Jeff Brownc6d282b2010-10-14 21:42:15 -07002539 // Tool Size
2540 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2541 String8 toolSizeCalibrationString;
2542 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2543 if (toolSizeCalibrationString == "none") {
2544 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2545 } else if (toolSizeCalibrationString == "geometric") {
2546 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2547 } else if (toolSizeCalibrationString == "linear") {
2548 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2549 } else if (toolSizeCalibrationString == "area") {
2550 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2551 } else if (toolSizeCalibrationString != "default") {
2552 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2553 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002554 }
2555 }
2556
Jeff Brownc6d282b2010-10-14 21:42:15 -07002557 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2558 out.toolSizeLinearScale);
2559 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2560 out.toolSizeLinearBias);
2561 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2562 out.toolSizeAreaScale);
2563 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2564 out.toolSizeAreaBias);
2565 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2566 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002567
2568 // Pressure
2569 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2570 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002571 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002572 if (pressureCalibrationString == "none") {
2573 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2574 } else if (pressureCalibrationString == "physical") {
2575 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2576 } else if (pressureCalibrationString == "amplitude") {
2577 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2578 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002579 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002580 pressureCalibrationString.string());
2581 }
2582 }
2583
2584 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2585 String8 pressureSourceString;
2586 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2587 if (pressureSourceString == "pressure") {
2588 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2589 } else if (pressureSourceString == "touch") {
2590 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2591 } else if (pressureSourceString != "default") {
2592 LOGW("Invalid value for touch.pressure.source: '%s'",
2593 pressureSourceString.string());
2594 }
2595 }
2596
2597 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2598 out.pressureScale);
2599
2600 // Size
2601 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2602 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002603 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002604 if (sizeCalibrationString == "none") {
2605 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2606 } else if (sizeCalibrationString == "normalized") {
2607 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2608 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002609 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002610 sizeCalibrationString.string());
2611 }
2612 }
2613
2614 // Orientation
2615 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2616 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002617 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002618 if (orientationCalibrationString == "none") {
2619 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2620 } else if (orientationCalibrationString == "interpolated") {
2621 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002622 } else if (orientationCalibrationString == "vector") {
2623 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002624 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002625 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002626 orientationCalibrationString.string());
2627 }
2628 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002629
2630 // Distance
2631 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2632 String8 distanceCalibrationString;
2633 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2634 if (distanceCalibrationString == "none") {
2635 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2636 } else if (distanceCalibrationString == "scaled") {
2637 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2638 } else if (distanceCalibrationString != "default") {
2639 LOGW("Invalid value for touch.distance.calibration: '%s'",
2640 distanceCalibrationString.string());
2641 }
2642 }
2643
2644 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2645 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002646}
2647
2648void TouchInputMapper::resolveCalibration() {
2649 // Pressure
2650 switch (mCalibration.pressureSource) {
2651 case Calibration::PRESSURE_SOURCE_DEFAULT:
2652 if (mRawAxes.pressure.valid) {
2653 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2654 } else if (mRawAxes.touchMajor.valid) {
2655 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2656 }
2657 break;
2658
2659 case Calibration::PRESSURE_SOURCE_PRESSURE:
2660 if (! mRawAxes.pressure.valid) {
2661 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2662 "the pressure axis is not available.");
2663 }
2664 break;
2665
2666 case Calibration::PRESSURE_SOURCE_TOUCH:
2667 if (! mRawAxes.touchMajor.valid) {
2668 LOGW("Calibration property touch.pressure.source is 'touch' but "
2669 "the touchMajor axis is not available.");
2670 }
2671 break;
2672
2673 default:
2674 break;
2675 }
2676
2677 switch (mCalibration.pressureCalibration) {
2678 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2679 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2680 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2681 } else {
2682 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2683 }
2684 break;
2685
2686 default:
2687 break;
2688 }
2689
Jeff Brownc6d282b2010-10-14 21:42:15 -07002690 // Tool Size
2691 switch (mCalibration.toolSizeCalibration) {
2692 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002693 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002694 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002695 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002696 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002697 }
2698 break;
2699
2700 default:
2701 break;
2702 }
2703
Jeff Brownc6d282b2010-10-14 21:42:15 -07002704 // Touch Size
2705 switch (mCalibration.touchSizeCalibration) {
2706 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002707 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002708 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2709 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002710 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002711 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002712 }
2713 break;
2714
2715 default:
2716 break;
2717 }
2718
2719 // Size
2720 switch (mCalibration.sizeCalibration) {
2721 case Calibration::SIZE_CALIBRATION_DEFAULT:
2722 if (mRawAxes.toolMajor.valid) {
2723 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2724 } else {
2725 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2726 }
2727 break;
2728
2729 default:
2730 break;
2731 }
2732
2733 // Orientation
2734 switch (mCalibration.orientationCalibration) {
2735 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2736 if (mRawAxes.orientation.valid) {
2737 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2738 } else {
2739 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2740 }
2741 break;
2742
2743 default:
2744 break;
2745 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002746
2747 // Distance
2748 switch (mCalibration.distanceCalibration) {
2749 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
2750 if (mRawAxes.distance.valid) {
2751 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2752 } else {
2753 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2754 }
2755 break;
2756
2757 default:
2758 break;
2759 }
Jeff Brown8d608662010-08-30 03:02:23 -07002760}
2761
Jeff Brownef3d7e82010-09-30 14:33:04 -07002762void TouchInputMapper::dumpCalibration(String8& dump) {
2763 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002764
Jeff Brownc6d282b2010-10-14 21:42:15 -07002765 // Touch Size
2766 switch (mCalibration.touchSizeCalibration) {
2767 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2768 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002769 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002770 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2771 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002772 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002773 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2774 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002775 break;
2776 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002777 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002778 }
2779
Jeff Brownc6d282b2010-10-14 21:42:15 -07002780 // Tool Size
2781 switch (mCalibration.toolSizeCalibration) {
2782 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2783 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002784 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002785 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2786 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002787 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002788 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2789 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2790 break;
2791 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2792 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002793 break;
2794 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002795 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002796 }
2797
Jeff Brownc6d282b2010-10-14 21:42:15 -07002798 if (mCalibration.haveToolSizeLinearScale) {
2799 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2800 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002801 }
2802
Jeff Brownc6d282b2010-10-14 21:42:15 -07002803 if (mCalibration.haveToolSizeLinearBias) {
2804 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2805 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002806 }
2807
Jeff Brownc6d282b2010-10-14 21:42:15 -07002808 if (mCalibration.haveToolSizeAreaScale) {
2809 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2810 mCalibration.toolSizeAreaScale);
2811 }
2812
2813 if (mCalibration.haveToolSizeAreaBias) {
2814 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2815 mCalibration.toolSizeAreaBias);
2816 }
2817
2818 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002819 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002820 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002821 }
2822
2823 // Pressure
2824 switch (mCalibration.pressureCalibration) {
2825 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002826 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002827 break;
2828 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002829 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002830 break;
2831 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002832 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002833 break;
2834 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002835 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002836 }
2837
2838 switch (mCalibration.pressureSource) {
2839 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002840 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002841 break;
2842 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002843 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002844 break;
2845 case Calibration::PRESSURE_SOURCE_DEFAULT:
2846 break;
2847 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002848 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002849 }
2850
2851 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002852 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2853 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002854 }
2855
2856 // Size
2857 switch (mCalibration.sizeCalibration) {
2858 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002859 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002860 break;
2861 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002862 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002863 break;
2864 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002865 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002866 }
2867
2868 // Orientation
2869 switch (mCalibration.orientationCalibration) {
2870 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002871 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002872 break;
2873 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002874 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002875 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002876 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2877 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2878 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002879 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002880 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002881 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002882
2883 // Distance
2884 switch (mCalibration.distanceCalibration) {
2885 case Calibration::DISTANCE_CALIBRATION_NONE:
2886 dump.append(INDENT4 "touch.distance.calibration: none\n");
2887 break;
2888 case Calibration::DISTANCE_CALIBRATION_SCALED:
2889 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
2890 break;
2891 default:
2892 LOG_ASSERT(false);
2893 }
2894
2895 if (mCalibration.haveDistanceScale) {
2896 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
2897 mCalibration.distanceScale);
2898 }
Jeff Brown8d608662010-08-30 03:02:23 -07002899}
2900
Jeff Brown6d0fec22010-07-23 21:28:06 -07002901void TouchInputMapper::reset() {
2902 // Synthesize touch up event if touch is currently down.
2903 // This will also take care of finishing virtual key processing if needed.
2904 if (mLastTouch.pointerCount != 0) {
2905 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2906 mCurrentTouch.clear();
2907 syncTouch(when, true);
2908 }
2909
Jeff Brown6328cdc2010-07-29 18:18:33 -07002910 { // acquire lock
2911 AutoMutex _l(mLock);
2912 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07002913
2914 if (mPointerController != NULL
2915 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown474dcb52011-06-14 20:22:50 -07002916 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brown2352b972011-04-12 22:39:53 -07002917 mPointerController->clearSpots();
2918 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002919 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002920
Jeff Brown6328cdc2010-07-29 18:18:33 -07002921 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002922}
2923
2924void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07002925#if DEBUG_RAW_EVENTS
2926 if (!havePointerIds) {
2927 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2928 } else {
2929 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2930 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2931 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2932 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2933 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2934 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2935 }
2936#endif
2937
Jeff Brown6328cdc2010-07-29 18:18:33 -07002938 // Preprocess pointer data.
Jeff Brownaa3855d2011-03-17 01:34:19 -07002939 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002940 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002941 }
2942
Jeff Brown56194eb2011-03-02 19:23:13 -08002943 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002944 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002945 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2946 // If this is a touch screen, hide the pointer on an initial down.
2947 getContext()->fadePointer();
2948 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002949
2950 // Initial downs on external touch devices should wake the device.
2951 // We don't do this for internal touch screens to prevent them from waking
2952 // up in your pocket.
2953 // TODO: Use the input device configuration to control this behavior more finely.
2954 if (getDevice()->isExternal()) {
2955 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2956 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002957 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002958
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002959 // Synthesize key down from buttons if needed.
2960 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
2961 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2962
2963 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07002964 TouchResult touchResult;
2965 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2966 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2967 // Drop spurious syncs.
2968 touchResult = DROP_STROKE;
2969 } else {
2970 // Process touches and virtual keys.
2971 touchResult = consumeOffScreenTouches(when, policyFlags);
2972 if (touchResult == DISPATCH_TOUCH) {
2973 suppressSwipeOntoVirtualKeys(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002974 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
Jeff Brown79ac9692011-04-19 21:20:10 -07002975 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
2976 }
2977 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08002978 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002979 }
2980
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002981 // Synthesize key up from buttons if needed.
2982 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
2983 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2984
Jeff Brown6328cdc2010-07-29 18:18:33 -07002985 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08002986 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002987 if (touchResult == DROP_STROKE) {
2988 mLastTouch.clear();
Jeff Browna4d1bc52011-07-01 19:23:40 -07002989 mLastTouch.buttonState = mCurrentTouch.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002990 } else {
Jeff Browna4d1bc52011-07-01 19:23:40 -07002991 mLastTouch.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002992 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002993}
2994
Jeff Brown79ac9692011-04-19 21:20:10 -07002995void TouchInputMapper::timeoutExpired(nsecs_t when) {
2996 if (mPointerController != NULL) {
2997 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
2998 }
2999}
3000
Jeff Brown6d0fec22010-07-23 21:28:06 -07003001TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
3002 nsecs_t when, uint32_t policyFlags) {
3003 int32_t keyEventAction, keyEventFlags;
3004 int32_t keyCode, scanCode, downTime;
3005 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07003006
Jeff Brown6328cdc2010-07-29 18:18:33 -07003007 { // acquire lock
3008 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003009
Jeff Brown6328cdc2010-07-29 18:18:33 -07003010 // Update surface size and orientation, including virtual key positions.
3011 if (! configureSurfaceLocked()) {
3012 return DROP_STROKE;
3013 }
3014
3015 // Check for virtual key press.
3016 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003017 if (mCurrentTouch.pointerCount == 0) {
3018 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003019 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003020#if DEBUG_VIRTUAL_KEYS
3021 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003022 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003023#endif
3024 keyEventAction = AKEY_EVENT_ACTION_UP;
3025 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3026 touchResult = SKIP_TOUCH;
3027 goto DispatchVirtualKey;
3028 }
3029
3030 if (mCurrentTouch.pointerCount == 1) {
3031 int32_t x = mCurrentTouch.pointers[0].x;
3032 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003033 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
3034 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003035 // Pointer is still within the space of the virtual key.
3036 return SKIP_TOUCH;
3037 }
3038 }
3039
3040 // Pointer left virtual key area or another pointer also went down.
3041 // Send key cancellation and drop the stroke so subsequent motions will be
3042 // considered fresh downs. This is useful when the user swipes away from the
3043 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003044 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003045#if DEBUG_VIRTUAL_KEYS
3046 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003047 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003048#endif
3049 keyEventAction = AKEY_EVENT_ACTION_UP;
3050 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3051 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07003052
3053 // Check whether the pointer moved inside the display area where we should
3054 // start a new stroke.
3055 int32_t x = mCurrentTouch.pointers[0].x;
3056 int32_t y = mCurrentTouch.pointers[0].y;
3057 if (isPointInsideSurfaceLocked(x, y)) {
3058 mLastTouch.clear();
3059 touchResult = DISPATCH_TOUCH;
3060 } else {
3061 touchResult = DROP_STROKE;
3062 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003063 } else {
3064 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
3065 // Pointer just went down. Handle off-screen touches, if needed.
3066 int32_t x = mCurrentTouch.pointers[0].x;
3067 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003068 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003069 // If exactly one pointer went down, check for virtual key hit.
3070 // Otherwise we will drop the entire stroke.
3071 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003072 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003073 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08003074 if (mContext->shouldDropVirtualKey(when, getDevice(),
3075 virtualKey->keyCode, virtualKey->scanCode)) {
3076 return DROP_STROKE;
3077 }
3078
Jeff Brown6328cdc2010-07-29 18:18:33 -07003079 mLocked.currentVirtualKey.down = true;
3080 mLocked.currentVirtualKey.downTime = when;
3081 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
3082 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003083#if DEBUG_VIRTUAL_KEYS
3084 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003085 mLocked.currentVirtualKey.keyCode,
3086 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003087#endif
3088 keyEventAction = AKEY_EVENT_ACTION_DOWN;
3089 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
3090 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3091 touchResult = SKIP_TOUCH;
3092 goto DispatchVirtualKey;
3093 }
3094 }
3095 return DROP_STROKE;
3096 }
3097 }
3098 return DISPATCH_TOUCH;
3099 }
3100
3101 DispatchVirtualKey:
3102 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003103 keyCode = mLocked.currentVirtualKey.keyCode;
3104 scanCode = mLocked.currentVirtualKey.scanCode;
3105 downTime = mLocked.currentVirtualKey.downTime;
3106 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003107
3108 // Dispatch virtual key.
3109 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07003110 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07003111 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3112 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3113 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003114}
3115
Jeff Brownefd32662011-03-08 15:13:06 -08003116void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08003117 // Disable all virtual key touches that happen within a short time interval of the
3118 // most recent touch. The idea is to filter out stray virtual key presses when
3119 // interacting with the touch screen.
3120 //
3121 // Problems we're trying to solve:
3122 //
3123 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3124 // virtual key area that is implemented by a separate touch panel and accidentally
3125 // triggers a virtual key.
3126 //
3127 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3128 // area and accidentally triggers a virtual key. This often happens when virtual keys
3129 // are layed out below the screen near to where the on screen keyboard's space bar
3130 // is displayed.
Jeff Brown474dcb52011-06-14 20:22:50 -07003131 if (mConfig.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
3132 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003133 }
3134}
3135
Jeff Brown6d0fec22010-07-23 21:28:06 -07003136void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3137 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3138 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003139 if (currentPointerCount == 0 && lastPointerCount == 0) {
3140 return; // nothing to do!
3141 }
3142
Jeff Brownace13b12011-03-09 17:39:48 -08003143 // Update current touch coordinates.
Jeff Brownace13b12011-03-09 17:39:48 -08003144 float xPrecision, yPrecision;
Jeff Browna6111372011-07-14 21:48:23 -07003145 prepareTouches(&xPrecision, &yPrecision);
Jeff Brownace13b12011-03-09 17:39:48 -08003146
3147 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003148 BitSet32 currentIdBits = mCurrentTouch.idBits;
3149 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003150 int32_t metaState = getContext()->getGlobalMetaState();
3151 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003152
3153 if (currentIdBits == lastIdBits) {
3154 // No pointer id changes so this is a move event.
3155 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003156 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003157 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3158 AMOTION_EVENT_EDGE_FLAG_NONE,
3159 mCurrentTouchProperties, mCurrentTouchCoords,
3160 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003161 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003162 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003163 // There may be pointers going up and pointers going down and pointers moving
3164 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003165 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3166 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003167 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003168 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003169
Jeff Brownace13b12011-03-09 17:39:48 -08003170 // Update last coordinates of pointers that have moved so that we observe the new
3171 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003172 bool moveNeeded = updateMovedPointers(
3173 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3174 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003175 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003176 if (buttonState != mLastTouch.buttonState) {
3177 moveNeeded = true;
3178 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003179
Jeff Brownace13b12011-03-09 17:39:48 -08003180 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003181 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003182 uint32_t upId = upIdBits.firstMarkedBit();
3183 upIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003184
Jeff Brownace13b12011-03-09 17:39:48 -08003185 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003186 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3187 mLastTouchProperties, mLastTouchCoords,
3188 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003189 xPrecision, yPrecision, mDownTime);
3190 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003191 }
3192
Jeff Brownc3db8582010-10-20 15:33:38 -07003193 // Dispatch move events if any of the remaining pointers moved from their old locations.
3194 // Although applications receive new locations as part of individual pointer up
3195 // events, they do not generally handle them except when presented in a move event.
3196 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003197 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003198 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003199 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3200 mCurrentTouchProperties, mCurrentTouchCoords,
3201 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003202 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003203 }
3204
3205 // Dispatch pointer down events using the new pointer locations.
3206 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003207 uint32_t downId = downIdBits.firstMarkedBit();
3208 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003209 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003210
Jeff Brownace13b12011-03-09 17:39:48 -08003211 if (dispatchedIdBits.count() == 1) {
3212 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003213 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003214 }
3215
Jeff Brownace13b12011-03-09 17:39:48 -08003216 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003217 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003218 mCurrentTouchProperties, mCurrentTouchCoords,
3219 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003220 xPrecision, yPrecision, mDownTime);
3221 }
3222 }
3223
3224 // Update state for next time.
3225 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003226 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003227 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3228 }
3229}
3230
Jeff Browna6111372011-07-14 21:48:23 -07003231void TouchInputMapper::prepareTouches(float* outXPrecision, float* outYPrecision) {
Jeff Brownace13b12011-03-09 17:39:48 -08003232 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3233 uint32_t lastPointerCount = mLastTouch.pointerCount;
3234
3235 AutoMutex _l(mLock);
3236
3237 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3238 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3239 for (uint32_t i = 0; i < currentPointerCount; i++) {
3240 const PointerData& in = mCurrentTouch.pointers[i];
3241
3242 // ToolMajor and ToolMinor
3243 float toolMajor, toolMinor;
3244 switch (mCalibration.toolSizeCalibration) {
3245 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3246 toolMajor = in.toolMajor * mLocked.geometricScale;
3247 if (mRawAxes.toolMinor.valid) {
3248 toolMinor = in.toolMinor * mLocked.geometricScale;
3249 } else {
3250 toolMinor = toolMajor;
3251 }
3252 break;
3253 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3254 toolMajor = in.toolMajor != 0
3255 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3256 : 0;
3257 if (mRawAxes.toolMinor.valid) {
3258 toolMinor = in.toolMinor != 0
3259 ? in.toolMinor * mLocked.toolSizeLinearScale
3260 + mLocked.toolSizeLinearBias
3261 : 0;
3262 } else {
3263 toolMinor = toolMajor;
3264 }
3265 break;
3266 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3267 if (in.toolMajor != 0) {
3268 float diameter = sqrtf(in.toolMajor
3269 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3270 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3271 } else {
3272 toolMajor = 0;
3273 }
3274 toolMinor = toolMajor;
3275 break;
3276 default:
3277 toolMajor = 0;
3278 toolMinor = 0;
3279 break;
3280 }
3281
3282 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3283 toolMajor /= currentPointerCount;
3284 toolMinor /= currentPointerCount;
3285 }
3286
3287 // Pressure
3288 float rawPressure;
3289 switch (mCalibration.pressureSource) {
3290 case Calibration::PRESSURE_SOURCE_PRESSURE:
3291 rawPressure = in.pressure;
3292 break;
3293 case Calibration::PRESSURE_SOURCE_TOUCH:
3294 rawPressure = in.touchMajor;
3295 break;
3296 default:
3297 rawPressure = 0;
3298 }
3299
3300 float pressure;
3301 switch (mCalibration.pressureCalibration) {
3302 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3303 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3304 pressure = rawPressure * mLocked.pressureScale;
3305 break;
3306 default:
3307 pressure = 1;
3308 break;
3309 }
3310
3311 // TouchMajor and TouchMinor
3312 float touchMajor, touchMinor;
3313 switch (mCalibration.touchSizeCalibration) {
3314 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3315 touchMajor = in.touchMajor * mLocked.geometricScale;
3316 if (mRawAxes.touchMinor.valid) {
3317 touchMinor = in.touchMinor * mLocked.geometricScale;
3318 } else {
3319 touchMinor = touchMajor;
3320 }
3321 break;
3322 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3323 touchMajor = toolMajor * pressure;
3324 touchMinor = toolMinor * pressure;
3325 break;
3326 default:
3327 touchMajor = 0;
3328 touchMinor = 0;
3329 break;
3330 }
3331
3332 if (touchMajor > toolMajor) {
3333 touchMajor = toolMajor;
3334 }
3335 if (touchMinor > toolMinor) {
3336 touchMinor = toolMinor;
3337 }
3338
3339 // Size
3340 float size;
3341 switch (mCalibration.sizeCalibration) {
3342 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3343 float rawSize = mRawAxes.toolMinor.valid
3344 ? avg(in.toolMajor, in.toolMinor)
3345 : in.toolMajor;
3346 size = rawSize * mLocked.sizeScale;
3347 break;
3348 }
3349 default:
3350 size = 0;
3351 break;
3352 }
3353
3354 // Orientation
3355 float orientation;
3356 switch (mCalibration.orientationCalibration) {
3357 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3358 orientation = in.orientation * mLocked.orientationScale;
3359 break;
3360 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3361 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3362 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3363 if (c1 != 0 || c2 != 0) {
3364 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003365 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003366 touchMajor *= scale;
3367 touchMinor /= scale;
3368 toolMajor *= scale;
3369 toolMinor /= scale;
3370 } else {
3371 orientation = 0;
3372 }
3373 break;
3374 }
3375 default:
3376 orientation = 0;
3377 }
3378
Jeff Brown80fd47c2011-05-24 01:07:44 -07003379 // Distance
3380 float distance;
3381 switch (mCalibration.distanceCalibration) {
3382 case Calibration::DISTANCE_CALIBRATION_SCALED:
3383 distance = in.distance * mLocked.distanceScale;
3384 break;
3385 default:
3386 distance = 0;
3387 }
3388
Jeff Brownace13b12011-03-09 17:39:48 -08003389 // X and Y
3390 // Adjust coords for surface orientation.
3391 float x, y;
3392 switch (mLocked.surfaceOrientation) {
3393 case DISPLAY_ORIENTATION_90:
3394 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3395 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3396 orientation -= M_PI_2;
3397 if (orientation < - M_PI_2) {
3398 orientation += M_PI;
3399 }
3400 break;
3401 case DISPLAY_ORIENTATION_180:
3402 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3403 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3404 break;
3405 case DISPLAY_ORIENTATION_270:
3406 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3407 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3408 orientation += M_PI_2;
3409 if (orientation > M_PI_2) {
3410 orientation -= M_PI;
3411 }
3412 break;
3413 default:
3414 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3415 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3416 break;
3417 }
3418
3419 // Write output coords.
3420 PointerCoords& out = mCurrentTouchCoords[i];
3421 out.clear();
3422 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3423 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3424 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3425 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3426 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3427 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3428 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3429 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3430 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003431 if (distance != 0) {
3432 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
3433 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003434
3435 // Write output properties.
3436 PointerProperties& properties = mCurrentTouchProperties[i];
3437 properties.clear();
3438 properties.id = mCurrentTouch.pointers[i].id;
3439 properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus);
Jeff Brownace13b12011-03-09 17:39:48 -08003440 }
3441
Jeff Brownace13b12011-03-09 17:39:48 -08003442 *outXPrecision = mLocked.orientedXPrecision;
3443 *outYPrecision = mLocked.orientedYPrecision;
3444}
3445
Jeff Brown79ac9692011-04-19 21:20:10 -07003446void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3447 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003448 // Update current gesture coordinates.
3449 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003450 bool sendEvents = preparePointerGestures(when,
3451 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3452 if (!sendEvents) {
3453 return;
3454 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003455 if (finishPreviousGesture) {
3456 cancelPreviousGesture = false;
3457 }
Jeff Brownace13b12011-03-09 17:39:48 -08003458
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003459 // Update the pointer presentation and spots.
3460 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3461 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3462 if (finishPreviousGesture || cancelPreviousGesture) {
3463 mPointerController->clearSpots();
3464 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003465 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3466 mPointerGesture.currentGestureIdToIndex,
3467 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003468 } else {
3469 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3470 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003471
Jeff Brown538881e2011-05-25 18:23:38 -07003472 // Show or hide the pointer if needed.
3473 switch (mPointerGesture.currentGestureMode) {
3474 case PointerGesture::NEUTRAL:
3475 case PointerGesture::QUIET:
3476 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3477 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3478 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3479 // Remind the user of where the pointer is after finishing a gesture with spots.
3480 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3481 }
3482 break;
3483 case PointerGesture::TAP:
3484 case PointerGesture::TAP_DRAG:
3485 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3486 case PointerGesture::HOVER:
3487 case PointerGesture::PRESS:
3488 // Unfade the pointer when the current gesture manipulates the
3489 // area directly under the pointer.
3490 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3491 break;
3492 case PointerGesture::SWIPE:
3493 case PointerGesture::FREEFORM:
3494 // Fade the pointer when the current gesture manipulates a different
3495 // area and there are spots to guide the user experience.
3496 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3497 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3498 } else {
3499 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3500 }
3501 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003502 }
3503
Jeff Brownace13b12011-03-09 17:39:48 -08003504 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003505 int32_t metaState = getContext()->getGlobalMetaState();
3506 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003507
3508 // Update last coordinates of pointers that have moved so that we observe the new
3509 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003510 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3511 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3512 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003513 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003514 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3515 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3516 bool moveNeeded = false;
3517 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003518 && !mPointerGesture.lastGestureIdBits.isEmpty()
3519 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003520 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3521 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003522 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003523 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003524 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003525 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3526 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003527 if (buttonState != mLastTouch.buttonState) {
3528 moveNeeded = true;
3529 }
Jeff Brownace13b12011-03-09 17:39:48 -08003530 }
3531
3532 // Send motion events for all pointers that went up or were canceled.
3533 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3534 if (!dispatchedGestureIdBits.isEmpty()) {
3535 if (cancelPreviousGesture) {
3536 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003537 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3538 AMOTION_EVENT_EDGE_FLAG_NONE,
3539 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003540 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3541 dispatchedGestureIdBits, -1,
3542 0, 0, mPointerGesture.downTime);
3543
3544 dispatchedGestureIdBits.clear();
3545 } else {
3546 BitSet32 upGestureIdBits;
3547 if (finishPreviousGesture) {
3548 upGestureIdBits = dispatchedGestureIdBits;
3549 } else {
3550 upGestureIdBits.value = dispatchedGestureIdBits.value
3551 & ~mPointerGesture.currentGestureIdBits.value;
3552 }
3553 while (!upGestureIdBits.isEmpty()) {
3554 uint32_t id = upGestureIdBits.firstMarkedBit();
3555 upGestureIdBits.clearBit(id);
3556
3557 dispatchMotion(when, policyFlags, mPointerSource,
3558 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003559 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3560 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003561 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3562 dispatchedGestureIdBits, id,
3563 0, 0, mPointerGesture.downTime);
3564
3565 dispatchedGestureIdBits.clearBit(id);
3566 }
3567 }
3568 }
3569
3570 // Send motion events for all pointers that moved.
3571 if (moveNeeded) {
3572 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003573 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3574 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003575 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3576 dispatchedGestureIdBits, -1,
3577 0, 0, mPointerGesture.downTime);
3578 }
3579
3580 // Send motion events for all pointers that went down.
3581 if (down) {
3582 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3583 & ~dispatchedGestureIdBits.value);
3584 while (!downGestureIdBits.isEmpty()) {
3585 uint32_t id = downGestureIdBits.firstMarkedBit();
3586 downGestureIdBits.clearBit(id);
3587 dispatchedGestureIdBits.markBit(id);
3588
Jeff Brownace13b12011-03-09 17:39:48 -08003589 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003590 mPointerGesture.downTime = when;
3591 }
3592
3593 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003594 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003595 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003596 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3597 dispatchedGestureIdBits, id,
3598 0, 0, mPointerGesture.downTime);
3599 }
3600 }
3601
Jeff Brownace13b12011-03-09 17:39:48 -08003602 // Send motion events for hover.
3603 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3604 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003605 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3606 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3607 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003608 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3609 mPointerGesture.currentGestureIdBits, -1,
3610 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003611 } else if (dispatchedGestureIdBits.isEmpty()
3612 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3613 // Synthesize a hover move event after all pointers go up to indicate that
3614 // the pointer is hovering again even if the user is not currently touching
3615 // the touch pad. This ensures that a view will receive a fresh hover enter
3616 // event after a tap.
3617 float x, y;
3618 mPointerController->getPosition(&x, &y);
3619
3620 PointerProperties pointerProperties;
3621 pointerProperties.clear();
3622 pointerProperties.id = 0;
3623 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
3624
3625 PointerCoords pointerCoords;
3626 pointerCoords.clear();
3627 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3628 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3629
3630 getDispatcher()->notifyMotion(when, getDeviceId(), mPointerSource, policyFlags,
3631 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3632 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3633 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003634 }
3635
3636 // Update state.
3637 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3638 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003639 mPointerGesture.lastGestureIdBits.clear();
3640 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003641 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3642 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3643 uint32_t id = idBits.firstMarkedBit();
3644 idBits.clearBit(id);
3645 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003646 mPointerGesture.lastGestureProperties[index].copyFrom(
3647 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003648 mPointerGesture.lastGestureCoords[index].copyFrom(
3649 mPointerGesture.currentGestureCoords[index]);
3650 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003651 }
3652 }
3653}
3654
Jeff Brown79ac9692011-04-19 21:20:10 -07003655bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3656 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003657 *outCancelPreviousGesture = false;
3658 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003659
Jeff Brownace13b12011-03-09 17:39:48 -08003660 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003661
Jeff Brown79ac9692011-04-19 21:20:10 -07003662 // Handle TAP timeout.
3663 if (isTimeout) {
3664#if DEBUG_GESTURES
3665 LOGD("Gestures: Processing timeout");
3666#endif
3667
3668 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003669 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003670 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003671 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003672 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003673 } else {
3674 // The tap is finished.
3675#if DEBUG_GESTURES
3676 LOGD("Gestures: TAP finished");
3677#endif
3678 *outFinishPreviousGesture = true;
3679
3680 mPointerGesture.activeGestureId = -1;
3681 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3682 mPointerGesture.currentGestureIdBits.clear();
3683
Jeff Brown19c97d462011-06-01 12:33:19 -07003684 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07003685 return true;
3686 }
3687 }
3688
3689 // We did not handle this timeout.
3690 return false;
3691 }
3692
Jeff Brownace13b12011-03-09 17:39:48 -08003693 // Update the velocity tracker.
3694 {
3695 VelocityTracker::Position positions[MAX_POINTERS];
3696 uint32_t count = 0;
3697 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003698 uint32_t id = idBits.firstMarkedBit();
3699 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003700 uint32_t index = mCurrentTouch.idToIndex[id];
3701 positions[count].x = mCurrentTouch.pointers[index].x
3702 * mLocked.pointerGestureXMovementScale;
3703 positions[count].y = mCurrentTouch.pointers[index].y
3704 * mLocked.pointerGestureYMovementScale;
3705 }
3706 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3707 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003708
Jeff Brownace13b12011-03-09 17:39:48 -08003709 // Pick a new active touch id if needed.
3710 // Choose an arbitrary pointer that just went down, if there is one.
3711 // Otherwise choose an arbitrary remaining pointer.
3712 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003713 // We keep the same active touch id for as long as possible.
3714 bool activeTouchChanged = false;
3715 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3716 int32_t activeTouchId = lastActiveTouchId;
3717 if (activeTouchId < 0) {
3718 if (!mCurrentTouch.idBits.isEmpty()) {
3719 activeTouchChanged = true;
3720 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3721 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003722 }
Jeff Brown2352b972011-04-12 22:39:53 -07003723 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3724 activeTouchChanged = true;
3725 if (!mCurrentTouch.idBits.isEmpty()) {
3726 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3727 } else {
3728 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003729 }
3730 }
3731
3732 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003733 bool isQuietTime = false;
3734 if (activeTouchId < 0) {
3735 mPointerGesture.resetQuietTime();
3736 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07003737 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07003738 if (!isQuietTime) {
3739 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3740 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3741 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3742 && mCurrentTouch.pointerCount < 2) {
3743 // Enter quiet time when exiting swipe or freeform state.
3744 // This is to prevent accidentally entering the hover state and flinging the
3745 // pointer when finishing a swipe and there is still one pointer left onscreen.
3746 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003747 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003748 && mCurrentTouch.pointerCount >= 2
3749 && !isPointerDown(mCurrentTouch.buttonState)) {
3750 // Enter quiet time when releasing the button and there are still two or more
3751 // fingers down. This may indicate that one finger was used to press the button
3752 // but it has not gone up yet.
3753 isQuietTime = true;
3754 }
3755 if (isQuietTime) {
3756 mPointerGesture.quietTime = when;
3757 }
Jeff Brownace13b12011-03-09 17:39:48 -08003758 }
3759 }
3760
3761 // Switch states based on button and pointer state.
3762 if (isQuietTime) {
3763 // Case 1: Quiet time. (QUIET)
3764#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003765 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003766 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003767#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003768 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
3769 *outFinishPreviousGesture = true;
3770 }
Jeff Brownace13b12011-03-09 17:39:48 -08003771
3772 mPointerGesture.activeGestureId = -1;
3773 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003774 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003775
Jeff Brown19c97d462011-06-01 12:33:19 -07003776 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08003777 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003778 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003779 // The pointer follows the active touch point.
3780 // Emit DOWN, MOVE, UP events at the pointer location.
3781 //
3782 // Only the active touch matters; other fingers are ignored. This policy helps
3783 // to handle the case where the user places a second finger on the touch pad
3784 // to apply the necessary force to depress an integrated button below the surface.
3785 // We don't want the second finger to be delivered to applications.
3786 //
3787 // For this to work well, we need to make sure to track the pointer that is really
3788 // active. If the user first puts one finger down to click then adds another
3789 // finger to drag then the active pointer should switch to the finger that is
3790 // being dragged.
3791#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003792 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08003793 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3794#endif
3795 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003796 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003797 *outFinishPreviousGesture = true;
3798 mPointerGesture.activeGestureId = 0;
3799 }
3800
3801 // Switch pointers if needed.
3802 // Find the fastest pointer and follow it.
Jeff Brown19c97d462011-06-01 12:33:19 -07003803 if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) {
3804 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07003805 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown19c97d462011-06-01 12:33:19 -07003806 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3807 uint32_t id = mCurrentTouch.pointers[i].id;
3808 float vx, vy;
3809 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3810 float speed = hypotf(vx, vy);
3811 if (speed > bestSpeed) {
3812 bestId = id;
3813 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08003814 }
Jeff Brown8d608662010-08-30 03:02:23 -07003815 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003816 }
3817 if (bestId >= 0 && bestId != activeTouchId) {
3818 mPointerGesture.activeTouchId = activeTouchId = bestId;
3819 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003820#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07003821 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3822 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08003823#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003824 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003825 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003826
Jeff Brown19c97d462011-06-01 12:33:19 -07003827 if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) {
3828 const PointerData& currentPointer =
3829 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3830 const PointerData& lastPointer =
3831 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3832 float deltaX = (currentPointer.x - lastPointer.x)
3833 * mLocked.pointerGestureXMovementScale;
3834 float deltaY = (currentPointer.y - lastPointer.y)
3835 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003836
Jeff Brown19c97d462011-06-01 12:33:19 -07003837 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3838
3839 // Move the pointer using a relative motion.
3840 // When using spots, the click will occur at the position of the anchor
3841 // spot and all other spots will move there.
3842 mPointerController->move(deltaX, deltaY);
3843 } else {
3844 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003845 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003846
Jeff Brownace13b12011-03-09 17:39:48 -08003847 float x, y;
3848 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003849
Jeff Brown79ac9692011-04-19 21:20:10 -07003850 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003851 mPointerGesture.currentGestureIdBits.clear();
3852 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3853 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003854 mPointerGesture.currentGestureProperties[0].clear();
3855 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3856 mPointerGesture.currentGestureProperties[0].toolType =
3857 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003858 mPointerGesture.currentGestureCoords[0].clear();
3859 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3860 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3861 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3862 } else if (mCurrentTouch.pointerCount == 0) {
3863 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003864 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
3865 *outFinishPreviousGesture = true;
3866 }
Jeff Brownace13b12011-03-09 17:39:48 -08003867
Jeff Brown79ac9692011-04-19 21:20:10 -07003868 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003869 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08003870 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003871 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3872 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07003873 && mLastTouch.pointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003874 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08003875 float x, y;
3876 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003877 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3878 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08003879#if DEBUG_GESTURES
3880 LOGD("Gestures: TAP");
3881#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003882
3883 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07003884 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07003885 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003886
Jeff Brownace13b12011-03-09 17:39:48 -08003887 mPointerGesture.activeGestureId = 0;
3888 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08003889 mPointerGesture.currentGestureIdBits.clear();
3890 mPointerGesture.currentGestureIdBits.markBit(
3891 mPointerGesture.activeGestureId);
3892 mPointerGesture.currentGestureIdToIndex[
3893 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003894 mPointerGesture.currentGestureProperties[0].clear();
3895 mPointerGesture.currentGestureProperties[0].id =
3896 mPointerGesture.activeGestureId;
3897 mPointerGesture.currentGestureProperties[0].toolType =
3898 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003899 mPointerGesture.currentGestureCoords[0].clear();
3900 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003901 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08003902 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003903 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003904 mPointerGesture.currentGestureCoords[0].setAxisValue(
3905 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003906
Jeff Brownace13b12011-03-09 17:39:48 -08003907 tapped = true;
3908 } else {
3909#if DEBUG_GESTURES
3910 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07003911 x - mPointerGesture.tapX,
3912 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003913#endif
3914 }
3915 } else {
3916#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003917 LOGD("Gestures: Not a TAP, %0.3fms since down",
3918 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003919#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003920 }
Jeff Brownace13b12011-03-09 17:39:48 -08003921 }
Jeff Brown2352b972011-04-12 22:39:53 -07003922
Jeff Brown19c97d462011-06-01 12:33:19 -07003923 mPointerGesture.pointerVelocityControl.reset();
3924
Jeff Brownace13b12011-03-09 17:39:48 -08003925 if (!tapped) {
3926#if DEBUG_GESTURES
3927 LOGD("Gestures: NEUTRAL");
3928#endif
3929 mPointerGesture.activeGestureId = -1;
3930 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08003931 mPointerGesture.currentGestureIdBits.clear();
3932 }
3933 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003934 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003935 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07003936 // When in HOVER, emit HOVER_MOVE events at the pointer location.
3937 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07003938 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08003939
Jeff Brown79ac9692011-04-19 21:20:10 -07003940 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
3941 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003942 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003943 float x, y;
3944 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003945 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3946 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003947 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3948 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003949#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003950 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3951 x - mPointerGesture.tapX,
3952 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003953#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003954 }
3955 } else {
3956#if DEBUG_GESTURES
3957 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3958 (when - mPointerGesture.tapUpTime) * 0.000001f);
3959#endif
3960 }
3961 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
3962 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3963 }
Jeff Brownace13b12011-03-09 17:39:48 -08003964
3965 if (mLastTouch.idBits.hasBit(activeTouchId)) {
3966 const PointerData& currentPointer =
3967 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3968 const PointerData& lastPointer =
3969 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3970 float deltaX = (currentPointer.x - lastPointer.x)
3971 * mLocked.pointerGestureXMovementScale;
3972 float deltaY = (currentPointer.y - lastPointer.y)
3973 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003974
Jeff Brown19c97d462011-06-01 12:33:19 -07003975 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3976
Jeff Brown2352b972011-04-12 22:39:53 -07003977 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07003978 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08003979 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003980 } else {
3981 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08003982 }
3983
Jeff Brown79ac9692011-04-19 21:20:10 -07003984 bool down;
3985 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
3986#if DEBUG_GESTURES
3987 LOGD("Gestures: TAP_DRAG");
3988#endif
3989 down = true;
3990 } else {
3991#if DEBUG_GESTURES
3992 LOGD("Gestures: HOVER");
3993#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003994 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
3995 *outFinishPreviousGesture = true;
3996 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003997 mPointerGesture.activeGestureId = 0;
3998 down = false;
3999 }
Jeff Brownace13b12011-03-09 17:39:48 -08004000
4001 float x, y;
4002 mPointerController->getPosition(&x, &y);
4003
Jeff Brownace13b12011-03-09 17:39:48 -08004004 mPointerGesture.currentGestureIdBits.clear();
4005 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4006 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004007 mPointerGesture.currentGestureProperties[0].clear();
4008 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4009 mPointerGesture.currentGestureProperties[0].toolType =
4010 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004011 mPointerGesture.currentGestureCoords[0].clear();
4012 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4013 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004014 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4015 down ? 1.0f : 0.0f);
4016
Jeff Brownace13b12011-03-09 17:39:48 -08004017 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004018 mPointerGesture.resetTap();
4019 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004020 mPointerGesture.tapX = x;
4021 mPointerGesture.tapY = y;
4022 }
Jeff Brownace13b12011-03-09 17:39:48 -08004023 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004024 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4025 // We need to provide feedback for each finger that goes down so we cannot wait
4026 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004027 //
Jeff Brown2352b972011-04-12 22:39:53 -07004028 // The ambiguous case is deciding what to do when there are two fingers down but they
4029 // have not moved enough to determine whether they are part of a drag or part of a
4030 // freeform gesture, or just a press or long-press at the pointer location.
4031 //
4032 // When there are two fingers we start with the PRESS hypothesis and we generate a
4033 // down at the pointer location.
4034 //
4035 // When the two fingers move enough or when additional fingers are added, we make
4036 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004037 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004038
Jeff Brown214eaf42011-05-26 19:17:02 -07004039 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004040 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004041 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004042 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4043 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004044 *outFinishPreviousGesture = true;
Jeff Brown19c97d462011-06-01 12:33:19 -07004045 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
4046 // Additional pointers have gone down but not yet settled.
4047 // Reset the gesture.
4048#if DEBUG_GESTURES
4049 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004050 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004051 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004052 * 0.000001f);
4053#endif
4054 *outCancelPreviousGesture = true;
4055 } else {
4056 // Continue previous gesture.
4057 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4058 }
4059
4060 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004061 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4062 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004063 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004064 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004065
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004066 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004067#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004068 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4069 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004070 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004071 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004072#endif
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004073 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
4074 &mPointerGesture.referenceTouchY);
4075 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4076 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004077 }
Jeff Brownace13b12011-03-09 17:39:48 -08004078
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004079 // Clear the reference deltas for fingers not yet included in the reference calculation.
4080 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4081 !idBits.isEmpty(); ) {
4082 uint32_t id = idBits.firstMarkedBit();
4083 idBits.clearBit(id);
4084
4085 mPointerGesture.referenceDeltas[id].dx = 0;
4086 mPointerGesture.referenceDeltas[id].dy = 0;
4087 }
4088 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4089
4090 // Add delta for all fingers and calculate a common movement delta.
4091 float commonDeltaX = 0, commonDeltaY = 0;
4092 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4093 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4094 bool first = (idBits == commonIdBits);
4095 uint32_t id = idBits.firstMarkedBit();
4096 idBits.clearBit(id);
4097
4098 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4099 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
4100 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4101 delta.dx += cpd.x - lpd.x;
4102 delta.dy += cpd.y - lpd.y;
4103
4104 if (first) {
4105 commonDeltaX = delta.dx;
4106 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004107 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004108 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4109 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4110 }
4111 }
Jeff Brownace13b12011-03-09 17:39:48 -08004112
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004113 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4114 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4115 float dist[MAX_POINTER_ID + 1];
4116 int32_t distOverThreshold = 0;
4117 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
4118 uint32_t id = idBits.firstMarkedBit();
4119 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08004120
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004121 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4122 dist[id] = hypotf(delta.dx * mLocked.pointerGestureXZoomScale,
4123 delta.dy * mLocked.pointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004124 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004125 distOverThreshold += 1;
4126 }
4127 }
4128
4129 // Only transition when at least two pointers have moved further than
4130 // the minimum distance threshold.
4131 if (distOverThreshold >= 2) {
4132 float d;
4133 if (mCurrentTouch.pointerCount > 2) {
4134 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004135#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004136 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
4137 mCurrentTouch.pointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004138#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004139 *outCancelPreviousGesture = true;
4140 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4141 } else if (((d = distance(
4142 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
4143 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
4144 > mLocked.pointerGestureMaxSwipeWidth)) {
4145 // There are two pointers but they are too far apart for a SWIPE,
4146 // switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004147#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004148 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4149 d, mLocked.pointerGestureMaxSwipeWidth);
Jeff Brown2352b972011-04-12 22:39:53 -07004150#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004151 *outCancelPreviousGesture = true;
4152 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4153 } else {
4154 // There are two pointers. Wait for both pointers to start moving
4155 // before deciding whether this is a SWIPE or FREEFORM gesture.
4156 uint32_t id1 = mCurrentTouch.pointers[0].id;
4157 uint32_t id2 = mCurrentTouch.pointers[1].id;
4158 float dist1 = dist[id1];
4159 float dist2 = dist[id2];
Jeff Brown474dcb52011-06-14 20:22:50 -07004160 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4161 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004162 // Calculate the dot product of the displacement vectors.
4163 // When the vectors are oriented in approximately the same direction,
4164 // the angle betweeen them is near zero and the cosine of the angle
4165 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4166 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4167 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown6674d9b2011-06-07 16:50:14 -07004168 float dx1 = delta1.dx * mLocked.pointerGestureXZoomScale;
4169 float dy1 = delta1.dy * mLocked.pointerGestureYZoomScale;
4170 float dx2 = delta2.dx * mLocked.pointerGestureXZoomScale;
4171 float dy2 = delta2.dy * mLocked.pointerGestureYZoomScale;
4172 float dot = dx1 * dx2 + dy1 * dy2;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004173 float cosine = dot / (dist1 * dist2); // denominator always > 0
Jeff Brown474dcb52011-06-14 20:22:50 -07004174 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004175 // Pointers are moving in the same direction. Switch to SWIPE.
4176#if DEBUG_GESTURES
4177 LOGD("Gestures: PRESS transitioned to SWIPE, "
4178 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4179 "cosine %0.3f >= %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004180 dist1, mConfig.pointerGestureMultitouchMinDistance,
4181 dist2, mConfig.pointerGestureMultitouchMinDistance,
4182 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004183#endif
4184 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4185 } else {
4186 // Pointers are moving in different directions. Switch to FREEFORM.
4187#if DEBUG_GESTURES
4188 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4189 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4190 "cosine %0.3f < %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004191 dist1, mConfig.pointerGestureMultitouchMinDistance,
4192 dist2, mConfig.pointerGestureMultitouchMinDistance,
4193 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004194#endif
4195 *outCancelPreviousGesture = true;
4196 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4197 }
Jeff Brownace13b12011-03-09 17:39:48 -08004198 }
4199 }
Jeff Brownace13b12011-03-09 17:39:48 -08004200 }
4201 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004202 // Switch from SWIPE to FREEFORM if additional pointers go down.
4203 // Cancel previous gesture.
4204 if (mCurrentTouch.pointerCount > 2) {
4205#if DEBUG_GESTURES
4206 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4207 mCurrentTouch.pointerCount);
4208#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004209 *outCancelPreviousGesture = true;
4210 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004211 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004212 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004213
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004214 // Move the reference points based on the overall group motion of the fingers
4215 // except in PRESS mode while waiting for a transition to occur.
4216 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4217 && (commonDeltaX || commonDeltaY)) {
4218 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brown2352b972011-04-12 22:39:53 -07004219 uint32_t id = idBits.firstMarkedBit();
4220 idBits.clearBit(id);
4221
Jeff Brown538881e2011-05-25 18:23:38 -07004222 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004223 delta.dx = 0;
4224 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004225 }
4226
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004227 mPointerGesture.referenceTouchX += commonDeltaX;
4228 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004229
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004230 commonDeltaX *= mLocked.pointerGestureXMovementScale;
4231 commonDeltaY *= mLocked.pointerGestureYMovementScale;
4232 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004233
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004234 mPointerGesture.referenceGestureX += commonDeltaX;
4235 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004236 }
4237
4238 // Report gestures.
4239 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4240 // PRESS mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004241#if DEBUG_GESTURES
Jeff Brown2352b972011-04-12 22:39:53 -07004242 LOGD("Gestures: PRESS activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004243 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004244 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004245#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004246 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004247
Jeff Brownace13b12011-03-09 17:39:48 -08004248 mPointerGesture.currentGestureIdBits.clear();
4249 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4250 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004251 mPointerGesture.currentGestureProperties[0].clear();
4252 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4253 mPointerGesture.currentGestureProperties[0].toolType =
4254 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004255 mPointerGesture.currentGestureCoords[0].clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004256 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4257 mPointerGesture.referenceGestureX);
4258 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4259 mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004260 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004261 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4262 // SWIPE mode.
4263#if DEBUG_GESTURES
4264 LOGD("Gestures: SWIPE activeTouchId=%d,"
4265 "activeGestureId=%d, currentTouchPointerCount=%d",
4266 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4267#endif
4268 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4269
4270 mPointerGesture.currentGestureIdBits.clear();
4271 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4272 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004273 mPointerGesture.currentGestureProperties[0].clear();
4274 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4275 mPointerGesture.currentGestureProperties[0].toolType =
4276 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004277 mPointerGesture.currentGestureCoords[0].clear();
4278 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4279 mPointerGesture.referenceGestureX);
4280 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4281 mPointerGesture.referenceGestureY);
4282 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004283 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4284 // FREEFORM mode.
4285#if DEBUG_GESTURES
4286 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4287 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004288 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004289#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004290 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004291
Jeff Brownace13b12011-03-09 17:39:48 -08004292 mPointerGesture.currentGestureIdBits.clear();
4293
4294 BitSet32 mappedTouchIdBits;
4295 BitSet32 usedGestureIdBits;
4296 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4297 // Initially, assign the active gesture id to the active touch point
4298 // if there is one. No other touch id bits are mapped yet.
4299 if (!*outCancelPreviousGesture) {
4300 mappedTouchIdBits.markBit(activeTouchId);
4301 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4302 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4303 mPointerGesture.activeGestureId;
4304 } else {
4305 mPointerGesture.activeGestureId = -1;
4306 }
4307 } else {
4308 // Otherwise, assume we mapped all touches from the previous frame.
4309 // Reuse all mappings that are still applicable.
4310 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4311 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4312
4313 // Check whether we need to choose a new active gesture id because the
4314 // current went went up.
4315 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4316 !upTouchIdBits.isEmpty(); ) {
4317 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4318 upTouchIdBits.clearBit(upTouchId);
4319 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4320 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4321 mPointerGesture.activeGestureId = -1;
4322 break;
4323 }
4324 }
4325 }
4326
4327#if DEBUG_GESTURES
4328 LOGD("Gestures: FREEFORM follow up "
4329 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4330 "activeGestureId=%d",
4331 mappedTouchIdBits.value, usedGestureIdBits.value,
4332 mPointerGesture.activeGestureId);
4333#endif
4334
Jeff Brown2352b972011-04-12 22:39:53 -07004335 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004336 uint32_t touchId = mCurrentTouch.pointers[i].id;
4337 uint32_t gestureId;
4338 if (!mappedTouchIdBits.hasBit(touchId)) {
4339 gestureId = usedGestureIdBits.firstUnmarkedBit();
4340 usedGestureIdBits.markBit(gestureId);
4341 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4342#if DEBUG_GESTURES
4343 LOGD("Gestures: FREEFORM "
4344 "new mapping for touch id %d -> gesture id %d",
4345 touchId, gestureId);
4346#endif
4347 } else {
4348 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4349#if DEBUG_GESTURES
4350 LOGD("Gestures: FREEFORM "
4351 "existing mapping for touch id %d -> gesture id %d",
4352 touchId, gestureId);
4353#endif
4354 }
4355 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4356 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4357
Jeff Brown2352b972011-04-12 22:39:53 -07004358 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4359 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4360 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4361 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
Jeff Brownace13b12011-03-09 17:39:48 -08004362
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004363 mPointerGesture.currentGestureProperties[i].clear();
4364 mPointerGesture.currentGestureProperties[i].id = gestureId;
4365 mPointerGesture.currentGestureProperties[i].toolType =
4366 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004367 mPointerGesture.currentGestureCoords[i].clear();
4368 mPointerGesture.currentGestureCoords[i].setAxisValue(
4369 AMOTION_EVENT_AXIS_X, x);
4370 mPointerGesture.currentGestureCoords[i].setAxisValue(
4371 AMOTION_EVENT_AXIS_Y, y);
4372 mPointerGesture.currentGestureCoords[i].setAxisValue(
4373 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4374 }
4375
4376 if (mPointerGesture.activeGestureId < 0) {
4377 mPointerGesture.activeGestureId =
4378 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4379#if DEBUG_GESTURES
4380 LOGD("Gestures: FREEFORM new "
4381 "activeGestureId=%d", mPointerGesture.activeGestureId);
4382#endif
4383 }
Jeff Brown2352b972011-04-12 22:39:53 -07004384 }
Jeff Brownace13b12011-03-09 17:39:48 -08004385 }
4386
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004387 mPointerController->setButtonState(mCurrentTouch.buttonState);
4388
Jeff Brownace13b12011-03-09 17:39:48 -08004389#if DEBUG_GESTURES
4390 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004391 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4392 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004393 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004394 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4395 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004396 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4397 uint32_t id = idBits.firstMarkedBit();
4398 idBits.clearBit(id);
4399 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004400 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004401 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004402 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4403 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4404 id, index, properties.toolType,
4405 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004406 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4407 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4408 }
4409 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4410 uint32_t id = idBits.firstMarkedBit();
4411 idBits.clearBit(id);
4412 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004413 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004414 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004415 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4416 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4417 id, index, properties.toolType,
4418 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004419 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4420 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4421 }
4422#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004423 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004424}
4425
4426void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004427 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4428 const PointerProperties* properties, const PointerCoords* coords,
4429 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004430 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4431 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004432 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004433 uint32_t pointerCount = 0;
4434 while (!idBits.isEmpty()) {
4435 uint32_t id = idBits.firstMarkedBit();
4436 idBits.clearBit(id);
4437 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004438 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004439 pointerCoords[pointerCount].copyFrom(coords[index]);
4440
4441 if (changedId >= 0 && id == uint32_t(changedId)) {
4442 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4443 }
4444
4445 pointerCount += 1;
4446 }
4447
Jeff Brownb6110c22011-04-01 16:15:13 -07004448 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004449
4450 if (changedId >= 0 && pointerCount == 1) {
4451 // Replace initial down and final up action.
4452 // We can compare the action without masking off the changed pointer index
4453 // because we know the index is 0.
4454 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4455 action = AMOTION_EVENT_ACTION_DOWN;
4456 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4457 action = AMOTION_EVENT_ACTION_UP;
4458 } else {
4459 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004460 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004461 }
4462 }
4463
4464 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004465 action, flags, metaState, buttonState, edgeFlags,
4466 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004467}
4468
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004469bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004470 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004471 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4472 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004473 bool changed = false;
4474 while (!idBits.isEmpty()) {
4475 uint32_t id = idBits.firstMarkedBit();
4476 idBits.clearBit(id);
4477
4478 uint32_t inIndex = inIdToIndex[id];
4479 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004480
4481 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004482 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004483 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004484 PointerCoords& curOutCoords = outCoords[outIndex];
4485
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004486 if (curInProperties != curOutProperties) {
4487 curOutProperties.copyFrom(curInProperties);
4488 changed = true;
4489 }
4490
Jeff Brownace13b12011-03-09 17:39:48 -08004491 if (curInCoords != curOutCoords) {
4492 curOutCoords.copyFrom(curInCoords);
4493 changed = true;
4494 }
4495 }
4496 return changed;
4497}
4498
4499void TouchInputMapper::fadePointer() {
4500 { // acquire lock
4501 AutoMutex _l(mLock);
4502 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004503 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownace13b12011-03-09 17:39:48 -08004504 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004505 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004506}
4507
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004508int32_t TouchInputMapper::getTouchToolType(bool isStylus) const {
4509 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
4510 return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER;
4511 } else {
4512 return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS
4513 : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
4514 }
4515}
4516
Jeff Brown6328cdc2010-07-29 18:18:33 -07004517bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004518 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4519 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004520}
4521
Jeff Brown6328cdc2010-07-29 18:18:33 -07004522const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4523 int32_t x, int32_t y) {
4524 size_t numVirtualKeys = mLocked.virtualKeys.size();
4525 for (size_t i = 0; i < numVirtualKeys; i++) {
4526 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004527
4528#if DEBUG_VIRTUAL_KEYS
4529 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4530 "left=%d, top=%d, right=%d, bottom=%d",
4531 x, y,
4532 virtualKey.keyCode, virtualKey.scanCode,
4533 virtualKey.hitLeft, virtualKey.hitTop,
4534 virtualKey.hitRight, virtualKey.hitBottom);
4535#endif
4536
4537 if (virtualKey.isHit(x, y)) {
4538 return & virtualKey;
4539 }
4540 }
4541
4542 return NULL;
4543}
4544
4545void TouchInputMapper::calculatePointerIds() {
4546 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4547 uint32_t lastPointerCount = mLastTouch.pointerCount;
4548
4549 if (currentPointerCount == 0) {
4550 // No pointers to assign.
4551 mCurrentTouch.idBits.clear();
4552 } else if (lastPointerCount == 0) {
4553 // All pointers are new.
4554 mCurrentTouch.idBits.clear();
4555 for (uint32_t i = 0; i < currentPointerCount; i++) {
4556 mCurrentTouch.pointers[i].id = i;
4557 mCurrentTouch.idToIndex[i] = i;
4558 mCurrentTouch.idBits.markBit(i);
4559 }
4560 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4561 // Only one pointer and no change in count so it must have the same id as before.
4562 uint32_t id = mLastTouch.pointers[0].id;
4563 mCurrentTouch.pointers[0].id = id;
4564 mCurrentTouch.idToIndex[id] = 0;
4565 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4566 } else {
4567 // General case.
4568 // We build a heap of squared euclidean distances between current and last pointers
4569 // associated with the current and last pointer indices. Then, we find the best
4570 // match (by distance) for each current pointer.
4571 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4572
4573 uint32_t heapSize = 0;
4574 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4575 currentPointerIndex++) {
4576 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4577 lastPointerIndex++) {
4578 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4579 - mLastTouch.pointers[lastPointerIndex].x;
4580 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4581 - mLastTouch.pointers[lastPointerIndex].y;
4582
4583 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4584
4585 // Insert new element into the heap (sift up).
4586 heap[heapSize].currentPointerIndex = currentPointerIndex;
4587 heap[heapSize].lastPointerIndex = lastPointerIndex;
4588 heap[heapSize].distance = distance;
4589 heapSize += 1;
4590 }
4591 }
4592
4593 // Heapify
4594 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4595 startIndex -= 1;
4596 for (uint32_t parentIndex = startIndex; ;) {
4597 uint32_t childIndex = parentIndex * 2 + 1;
4598 if (childIndex >= heapSize) {
4599 break;
4600 }
4601
4602 if (childIndex + 1 < heapSize
4603 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4604 childIndex += 1;
4605 }
4606
4607 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4608 break;
4609 }
4610
4611 swap(heap[parentIndex], heap[childIndex]);
4612 parentIndex = childIndex;
4613 }
4614 }
4615
4616#if DEBUG_POINTER_ASSIGNMENT
4617 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4618 for (size_t i = 0; i < heapSize; i++) {
4619 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4620 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4621 heap[i].distance);
4622 }
4623#endif
4624
4625 // Pull matches out by increasing order of distance.
4626 // To avoid reassigning pointers that have already been matched, the loop keeps track
4627 // of which last and current pointers have been matched using the matchedXXXBits variables.
4628 // It also tracks the used pointer id bits.
4629 BitSet32 matchedLastBits(0);
4630 BitSet32 matchedCurrentBits(0);
4631 BitSet32 usedIdBits(0);
4632 bool first = true;
4633 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4634 for (;;) {
4635 if (first) {
4636 // The first time through the loop, we just consume the root element of
4637 // the heap (the one with smallest distance).
4638 first = false;
4639 } else {
4640 // Previous iterations consumed the root element of the heap.
4641 // Pop root element off of the heap (sift down).
4642 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004643 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004644
4645 // Sift down.
4646 heap[0] = heap[heapSize];
4647 for (uint32_t parentIndex = 0; ;) {
4648 uint32_t childIndex = parentIndex * 2 + 1;
4649 if (childIndex >= heapSize) {
4650 break;
4651 }
4652
4653 if (childIndex + 1 < heapSize
4654 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4655 childIndex += 1;
4656 }
4657
4658 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4659 break;
4660 }
4661
4662 swap(heap[parentIndex], heap[childIndex]);
4663 parentIndex = childIndex;
4664 }
4665
4666#if DEBUG_POINTER_ASSIGNMENT
4667 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4668 for (size_t i = 0; i < heapSize; i++) {
4669 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4670 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4671 heap[i].distance);
4672 }
4673#endif
4674 }
4675
4676 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4677 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4678
4679 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4680 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4681
4682 matchedCurrentBits.markBit(currentPointerIndex);
4683 matchedLastBits.markBit(lastPointerIndex);
4684
4685 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4686 mCurrentTouch.pointers[currentPointerIndex].id = id;
4687 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4688 usedIdBits.markBit(id);
4689
4690#if DEBUG_POINTER_ASSIGNMENT
4691 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4692 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4693#endif
4694 break;
4695 }
4696 }
4697
4698 // Assign fresh ids to new pointers.
4699 if (currentPointerCount > lastPointerCount) {
4700 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4701 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4702 uint32_t id = usedIdBits.firstUnmarkedBit();
4703
4704 mCurrentTouch.pointers[currentPointerIndex].id = id;
4705 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4706 usedIdBits.markBit(id);
4707
4708#if DEBUG_POINTER_ASSIGNMENT
4709 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4710 currentPointerIndex, id);
4711#endif
4712
4713 if (--i == 0) break; // done
4714 matchedCurrentBits.markBit(currentPointerIndex);
4715 }
4716 }
4717
4718 // Fix id bits.
4719 mCurrentTouch.idBits = usedIdBits;
4720 }
4721}
4722
Jeff Brown6d0fec22010-07-23 21:28:06 -07004723int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
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.keyCode == keyCode) {
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.keyCode == keyCode) {
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
4743int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004744 { // acquire lock
4745 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004746
Jeff Brown6328cdc2010-07-29 18:18:33 -07004747 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004748 return AKEY_STATE_VIRTUAL;
4749 }
4750
Jeff Brown6328cdc2010-07-29 18:18:33 -07004751 size_t numVirtualKeys = mLocked.virtualKeys.size();
4752 for (size_t i = 0; i < numVirtualKeys; i++) {
4753 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004754 if (virtualKey.scanCode == scanCode) {
4755 return AKEY_STATE_UP;
4756 }
4757 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004758 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004759
4760 return AKEY_STATE_UNKNOWN;
4761}
4762
4763bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4764 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004765 { // acquire lock
4766 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004767
Jeff Brown6328cdc2010-07-29 18:18:33 -07004768 size_t numVirtualKeys = mLocked.virtualKeys.size();
4769 for (size_t i = 0; i < numVirtualKeys; i++) {
4770 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004771
4772 for (size_t i = 0; i < numCodes; i++) {
4773 if (virtualKey.keyCode == keyCodes[i]) {
4774 outFlags[i] = 1;
4775 }
4776 }
4777 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004778 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004779
4780 return true;
4781}
4782
4783
4784// --- SingleTouchInputMapper ---
4785
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004786SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
4787 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004788 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004789}
4790
4791SingleTouchInputMapper::~SingleTouchInputMapper() {
4792}
4793
Jeff Brown80fd47c2011-05-24 01:07:44 -07004794void SingleTouchInputMapper::clearState() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004795 mAccumulator.clear();
4796
4797 mDown = false;
4798 mX = 0;
4799 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07004800 mPressure = 0; // default to 0 for devices that don't report pressure
4801 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brownace13b12011-03-09 17:39:48 -08004802 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004803}
4804
4805void SingleTouchInputMapper::reset() {
4806 TouchInputMapper::reset();
4807
Jeff Brown80fd47c2011-05-24 01:07:44 -07004808 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004809 }
4810
4811void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
4812 switch (rawEvent->type) {
4813 case EV_KEY:
4814 switch (rawEvent->scanCode) {
4815 case BTN_TOUCH:
4816 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
4817 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004818 // Don't sync immediately. Wait until the next SYN_REPORT since we might
4819 // not have received valid position information yet. This logic assumes that
4820 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07004821 break;
Jeff Brownace13b12011-03-09 17:39:48 -08004822 default:
4823 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004824 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08004825 if (buttonState) {
4826 if (rawEvent->value) {
4827 mAccumulator.buttonDown |= buttonState;
4828 } else {
4829 mAccumulator.buttonUp |= buttonState;
4830 }
4831 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
4832 }
4833 }
4834 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004835 }
4836 break;
4837
4838 case EV_ABS:
4839 switch (rawEvent->scanCode) {
4840 case ABS_X:
4841 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
4842 mAccumulator.absX = rawEvent->value;
4843 break;
4844 case ABS_Y:
4845 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
4846 mAccumulator.absY = rawEvent->value;
4847 break;
4848 case ABS_PRESSURE:
4849 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
4850 mAccumulator.absPressure = rawEvent->value;
4851 break;
4852 case ABS_TOOL_WIDTH:
4853 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
4854 mAccumulator.absToolWidth = rawEvent->value;
4855 break;
4856 }
4857 break;
4858
4859 case EV_SYN:
4860 switch (rawEvent->scanCode) {
4861 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004862 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004863 break;
4864 }
4865 break;
4866 }
4867}
4868
4869void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004870 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004871 if (fields == 0) {
4872 return; // no new state changes, so nothing to do
4873 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004874
4875 if (fields & Accumulator::FIELD_BTN_TOUCH) {
4876 mDown = mAccumulator.btnTouch;
4877 }
4878
4879 if (fields & Accumulator::FIELD_ABS_X) {
4880 mX = mAccumulator.absX;
4881 }
4882
4883 if (fields & Accumulator::FIELD_ABS_Y) {
4884 mY = mAccumulator.absY;
4885 }
4886
4887 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
4888 mPressure = mAccumulator.absPressure;
4889 }
4890
4891 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07004892 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004893 }
4894
Jeff Brownace13b12011-03-09 17:39:48 -08004895 if (fields & Accumulator::FIELD_BUTTONS) {
4896 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
4897 }
4898
Jeff Brown6d0fec22010-07-23 21:28:06 -07004899 mCurrentTouch.clear();
4900
4901 if (mDown) {
4902 mCurrentTouch.pointerCount = 1;
4903 mCurrentTouch.pointers[0].id = 0;
4904 mCurrentTouch.pointers[0].x = mX;
4905 mCurrentTouch.pointers[0].y = mY;
4906 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07004907 mCurrentTouch.pointers[0].touchMajor = 0;
4908 mCurrentTouch.pointers[0].touchMinor = 0;
4909 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
4910 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004911 mCurrentTouch.pointers[0].orientation = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004912 mCurrentTouch.pointers[0].distance = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004913 mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus
Jeff Brown6d0fec22010-07-23 21:28:06 -07004914 mCurrentTouch.idToIndex[0] = 0;
4915 mCurrentTouch.idBits.markBit(0);
Jeff Brownace13b12011-03-09 17:39:48 -08004916 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004917 }
4918
4919 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004920
4921 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004922}
4923
Jeff Brown8d608662010-08-30 03:02:23 -07004924void SingleTouchInputMapper::configureRawAxes() {
4925 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004926
Jeff Brown8d608662010-08-30 03:02:23 -07004927 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
4928 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
4929 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
4930 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004931}
4932
4933
4934// --- MultiTouchInputMapper ---
4935
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004936MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown80fd47c2011-05-24 01:07:44 -07004937 TouchInputMapper(device), mSlotCount(0), mUsingSlotsProtocol(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004938}
4939
4940MultiTouchInputMapper::~MultiTouchInputMapper() {
4941}
4942
Jeff Brown80fd47c2011-05-24 01:07:44 -07004943void MultiTouchInputMapper::clearState() {
Jeff Brown441a9c22011-06-02 18:22:25 -07004944 mAccumulator.clearSlots(mSlotCount);
4945 mAccumulator.clearButtons();
Jeff Brownace13b12011-03-09 17:39:48 -08004946 mButtonState = 0;
Jeff Brown6894a292011-07-01 17:59:27 -07004947 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07004948
4949 if (mUsingSlotsProtocol) {
4950 // Query the driver for the current slot index and use it as the initial slot
4951 // before we start reading events from the device. It is possible that the
4952 // current slot index will not be the same as it was when the first event was
4953 // written into the evdev buffer, which means the input mapper could start
4954 // out of sync with the initial state of the events in the evdev buffer.
4955 // In the extremely unlikely case that this happens, the data from
4956 // two slots will be confused until the next ABS_MT_SLOT event is received.
4957 // This can cause the touch point to "jump", but at least there will be
4958 // no stuck touches.
4959 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
4960 &mAccumulator.currentSlot);
4961 if (status) {
4962 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
4963 mAccumulator.currentSlot = -1;
4964 }
4965 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004966}
4967
4968void MultiTouchInputMapper::reset() {
4969 TouchInputMapper::reset();
4970
Jeff Brown80fd47c2011-05-24 01:07:44 -07004971 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004972}
4973
4974void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
4975 switch (rawEvent->type) {
Jeff Brownace13b12011-03-09 17:39:48 -08004976 case EV_KEY: {
4977 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004978 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08004979 if (buttonState) {
4980 if (rawEvent->value) {
4981 mAccumulator.buttonDown |= buttonState;
4982 } else {
4983 mAccumulator.buttonUp |= buttonState;
4984 }
4985 }
4986 }
4987 break;
4988 }
4989
Jeff Brown6d0fec22010-07-23 21:28:06 -07004990 case EV_ABS: {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004991 bool newSlot = false;
4992 if (mUsingSlotsProtocol && rawEvent->scanCode == ABS_MT_SLOT) {
4993 mAccumulator.currentSlot = rawEvent->value;
4994 newSlot = true;
4995 }
4996
4997 if (mAccumulator.currentSlot < 0 || size_t(mAccumulator.currentSlot) >= mSlotCount) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004998#if DEBUG_POINTERS
Jeff Brown441a9c22011-06-02 18:22:25 -07004999 if (newSlot) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005000 LOGW("MultiTouch device %s emitted invalid slot index %d but it "
5001 "should be between 0 and %d; ignoring this slot.",
5002 getDeviceName().string(), mAccumulator.currentSlot, mSlotCount);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005003 }
Jeff Brown441a9c22011-06-02 18:22:25 -07005004#endif
Jeff Brown80fd47c2011-05-24 01:07:44 -07005005 break;
5006 }
5007
5008 Accumulator::Slot* slot = &mAccumulator.slots[mAccumulator.currentSlot];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005009
5010 switch (rawEvent->scanCode) {
5011 case ABS_MT_POSITION_X:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005012 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
5013 slot->absMTPositionX = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005014 break;
5015 case ABS_MT_POSITION_Y:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005016 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
5017 slot->absMTPositionY = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005018 break;
5019 case ABS_MT_TOUCH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005020 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
5021 slot->absMTTouchMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005022 break;
5023 case ABS_MT_TOUCH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005024 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
5025 slot->absMTTouchMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005026 break;
5027 case ABS_MT_WIDTH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005028 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
5029 slot->absMTWidthMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005030 break;
5031 case ABS_MT_WIDTH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005032 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5033 slot->absMTWidthMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005034 break;
5035 case ABS_MT_ORIENTATION:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005036 slot->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5037 slot->absMTOrientation = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005038 break;
5039 case ABS_MT_TRACKING_ID:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005040 if (mUsingSlotsProtocol && rawEvent->value < 0) {
5041 slot->clear();
5042 } else {
5043 slot->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5044 slot->absMTTrackingId = rawEvent->value;
5045 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005046 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005047 case ABS_MT_PRESSURE:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005048 slot->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5049 slot->absMTPressure = rawEvent->value;
5050 break;
5051 case ABS_MT_TOOL_TYPE:
5052 slot->fields |= Accumulator::FIELD_ABS_MT_TOOL_TYPE;
5053 slot->absMTToolType = rawEvent->value;
Jeff Brown8d608662010-08-30 03:02:23 -07005054 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005055 }
5056 break;
5057 }
5058
5059 case EV_SYN:
5060 switch (rawEvent->scanCode) {
5061 case SYN_MT_REPORT: {
5062 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005063 mAccumulator.currentSlot += 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005064 break;
5065 }
5066
5067 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005068 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005069 break;
5070 }
5071 break;
5072 }
5073}
5074
5075void MultiTouchInputMapper::sync(nsecs_t when) {
5076 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005077 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005078
Jeff Brown80fd47c2011-05-24 01:07:44 -07005079 size_t inCount = mSlotCount;
5080 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005081 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005082
Jeff Brown6d0fec22010-07-23 21:28:06 -07005083 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005084
Jeff Brown80fd47c2011-05-24 01:07:44 -07005085 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
5086 const Accumulator::Slot& inSlot = mAccumulator.slots[inIndex];
5087 uint32_t fields = inSlot.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005088
Jeff Brown6d0fec22010-07-23 21:28:06 -07005089 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005090 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005091 // This may also indicate an unused slot.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005092 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005093 continue;
5094 }
5095
Jeff Brown80fd47c2011-05-24 01:07:44 -07005096 if (outCount >= MAX_POINTERS) {
5097#if DEBUG_POINTERS
5098 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5099 "ignoring the rest.",
5100 getDeviceName().string(), MAX_POINTERS);
5101#endif
5102 break; // too many fingers!
5103 }
5104
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005105 PointerData& outPointer = mCurrentTouch.pointers[outCount];
Jeff Brown80fd47c2011-05-24 01:07:44 -07005106 outPointer.x = inSlot.absMTPositionX;
5107 outPointer.y = inSlot.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005108
Jeff Brown8d608662010-08-30 03:02:23 -07005109 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005110 outPointer.pressure = inSlot.absMTPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005111 } else {
5112 // Default pressure to 0 if absent.
5113 outPointer.pressure = 0;
5114 }
5115
5116 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005117 if (inSlot.absMTTouchMajor <= 0) {
Jeff Brown8d608662010-08-30 03:02:23 -07005118 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5119 // a pointer going up. Drop this finger.
5120 continue;
5121 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07005122 outPointer.touchMajor = inSlot.absMTTouchMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005123 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005124 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005125 outPointer.touchMajor = 0;
5126 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005127
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005128 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005129 outPointer.touchMinor = inSlot.absMTTouchMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005130 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005131 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005132 outPointer.touchMinor = outPointer.touchMajor;
5133 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005134
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005135 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005136 outPointer.toolMajor = inSlot.absMTWidthMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005137 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005138 // Default tool area to 0 if absent.
5139 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005140 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005141
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005142 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005143 outPointer.toolMinor = inSlot.absMTWidthMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005144 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005145 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005146 outPointer.toolMinor = outPointer.toolMajor;
5147 }
5148
5149 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005150 outPointer.orientation = inSlot.absMTOrientation;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005151 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005152 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005153 outPointer.orientation = 0;
5154 }
5155
Jeff Brown80fd47c2011-05-24 01:07:44 -07005156 if (fields & Accumulator::FIELD_ABS_MT_DISTANCE) {
5157 outPointer.distance = inSlot.absMTDistance;
5158 } else {
5159 // Default distance is 0 (direct contact).
5160 outPointer.distance = 0;
5161 }
5162
5163 if (fields & Accumulator::FIELD_ABS_MT_TOOL_TYPE) {
5164 outPointer.isStylus = (inSlot.absMTToolType == MT_TOOL_PEN);
5165 } else {
5166 // Assume this is not a stylus.
5167 outPointer.isStylus = false;
5168 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005169
Jeff Brown8d608662010-08-30 03:02:23 -07005170 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005171 if (havePointerIds) {
Jeff Brown6894a292011-07-01 17:59:27 -07005172 int32_t id = -1;
5173 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5174 int32_t trackingId = inSlot.absMTTrackingId;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005175
Jeff Brown6894a292011-07-01 17:59:27 -07005176 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
5177 uint32_t n = idBits.firstMarkedBit();
5178 idBits.clearBit(n);
5179
5180 if (mPointerTrackingIdMap[n] == trackingId) {
5181 id = n;
5182 }
5183 }
5184
5185 if (id < 0 && !mPointerIdBits.isFull()) {
5186 id = mPointerIdBits.firstUnmarkedBit();
5187 mPointerIdBits.markBit(id);
5188 mPointerTrackingIdMap[id] = trackingId;
5189 }
5190 }
5191 if (id < 0) {
5192 havePointerIds = false;
5193 mCurrentTouch.idBits.clear();
5194 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005195 outPointer.id = id;
5196 mCurrentTouch.idToIndex[id] = outCount;
5197 mCurrentTouch.idBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005198 }
5199 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005200
Jeff Brown6d0fec22010-07-23 21:28:06 -07005201 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005202 }
5203
Jeff Brown6d0fec22010-07-23 21:28:06 -07005204 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005205
Jeff Brownace13b12011-03-09 17:39:48 -08005206 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5207 mCurrentTouch.buttonState = mButtonState;
5208
Jeff Brown6894a292011-07-01 17:59:27 -07005209 mPointerIdBits = mCurrentTouch.idBits;
5210
Jeff Brown6d0fec22010-07-23 21:28:06 -07005211 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005212
Jeff Brown441a9c22011-06-02 18:22:25 -07005213 if (!mUsingSlotsProtocol) {
5214 mAccumulator.clearSlots(mSlotCount);
5215 }
5216 mAccumulator.clearButtons();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005217}
5218
Jeff Brown8d608662010-08-30 03:02:23 -07005219void MultiTouchInputMapper::configureRawAxes() {
5220 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005221
Jeff Brown80fd47c2011-05-24 01:07:44 -07005222 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x);
5223 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y);
5224 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor);
5225 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor);
5226 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor);
5227 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor);
5228 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation);
5229 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure);
5230 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance);
5231 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId);
5232 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot);
5233
5234 if (mRawAxes.trackingId.valid
5235 && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) {
5236 mSlotCount = mRawAxes.slot.maxValue + 1;
5237 if (mSlotCount > MAX_SLOTS) {
5238 LOGW("MultiTouch Device %s reported %d slots but the framework "
5239 "only supports a maximum of %d slots at this time.",
5240 getDeviceName().string(), mSlotCount, MAX_SLOTS);
5241 mSlotCount = MAX_SLOTS;
5242 }
5243 mUsingSlotsProtocol = true;
5244 } else {
5245 mSlotCount = MAX_POINTERS;
5246 mUsingSlotsProtocol = false;
5247 }
5248
5249 mAccumulator.allocateSlots(mSlotCount);
Jeff Brown2717eff2011-06-30 23:53:07 -07005250
5251 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005252}
5253
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005254
Jeff Browncb1404e2011-01-15 18:14:15 -08005255// --- JoystickInputMapper ---
5256
5257JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5258 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005259}
5260
5261JoystickInputMapper::~JoystickInputMapper() {
5262}
5263
5264uint32_t JoystickInputMapper::getSources() {
5265 return AINPUT_SOURCE_JOYSTICK;
5266}
5267
5268void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5269 InputMapper::populateDeviceInfo(info);
5270
Jeff Brown6f2fba42011-02-19 01:08:02 -08005271 for (size_t i = 0; i < mAxes.size(); i++) {
5272 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005273 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5274 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005275 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005276 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5277 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005278 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005279 }
5280}
5281
5282void JoystickInputMapper::dump(String8& dump) {
5283 dump.append(INDENT2 "Joystick Input Mapper:\n");
5284
Jeff Brown6f2fba42011-02-19 01:08:02 -08005285 dump.append(INDENT3 "Axes:\n");
5286 size_t numAxes = mAxes.size();
5287 for (size_t i = 0; i < numAxes; i++) {
5288 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005289 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005290 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005291 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005292 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005293 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005294 }
Jeff Brown85297452011-03-04 13:07:49 -08005295 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5296 label = getAxisLabel(axis.axisInfo.highAxis);
5297 if (label) {
5298 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5299 } else {
5300 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5301 axis.axisInfo.splitValue);
5302 }
5303 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5304 dump.append(" (invert)");
5305 }
5306
5307 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5308 axis.min, axis.max, axis.flat, axis.fuzz);
5309 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5310 "highScale=%0.5f, highOffset=%0.5f\n",
5311 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005312 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5313 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005314 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005315 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005316 }
5317}
5318
Jeff Brown474dcb52011-06-14 20:22:50 -07005319void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5320 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005321
Jeff Brown474dcb52011-06-14 20:22:50 -07005322 if (!changes) { // first time only
5323 // Collect all axes.
5324 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5325 RawAbsoluteAxisInfo rawAxisInfo;
5326 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5327 if (rawAxisInfo.valid) {
5328 // Map axis.
5329 AxisInfo axisInfo;
5330 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5331 if (!explicitlyMapped) {
5332 // Axis is not explicitly mapped, will choose a generic axis later.
5333 axisInfo.mode = AxisInfo::MODE_NORMAL;
5334 axisInfo.axis = -1;
5335 }
5336
5337 // Apply flat override.
5338 int32_t rawFlat = axisInfo.flatOverride < 0
5339 ? rawAxisInfo.flat : axisInfo.flatOverride;
5340
5341 // Calculate scaling factors and limits.
5342 Axis axis;
5343 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5344 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5345 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5346 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5347 scale, 0.0f, highScale, 0.0f,
5348 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5349 } else if (isCenteredAxis(axisInfo.axis)) {
5350 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5351 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5352 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5353 scale, offset, scale, offset,
5354 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5355 } else {
5356 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5357 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5358 scale, 0.0f, scale, 0.0f,
5359 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5360 }
5361
5362 // To eliminate noise while the joystick is at rest, filter out small variations
5363 // in axis values up front.
5364 axis.filter = axis.flat * 0.25f;
5365
5366 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005367 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005368 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005369
Jeff Brown474dcb52011-06-14 20:22:50 -07005370 // If there are too many axes, start dropping them.
5371 // Prefer to keep explicitly mapped axes.
5372 if (mAxes.size() > PointerCoords::MAX_AXES) {
5373 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5374 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5375 pruneAxes(true);
5376 pruneAxes(false);
5377 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005378
Jeff Brown474dcb52011-06-14 20:22:50 -07005379 // Assign generic axis ids to remaining axes.
5380 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5381 size_t numAxes = mAxes.size();
5382 for (size_t i = 0; i < numAxes; i++) {
5383 Axis& axis = mAxes.editValueAt(i);
5384 if (axis.axisInfo.axis < 0) {
5385 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5386 && haveAxis(nextGenericAxisId)) {
5387 nextGenericAxisId += 1;
5388 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005389
Jeff Brown474dcb52011-06-14 20:22:50 -07005390 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5391 axis.axisInfo.axis = nextGenericAxisId;
5392 nextGenericAxisId += 1;
5393 } else {
5394 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5395 "have already been assigned to other axes.",
5396 getDeviceName().string(), mAxes.keyAt(i));
5397 mAxes.removeItemsAt(i--);
5398 numAxes -= 1;
5399 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005400 }
5401 }
5402 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005403}
5404
Jeff Brown85297452011-03-04 13:07:49 -08005405bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005406 size_t numAxes = mAxes.size();
5407 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005408 const Axis& axis = mAxes.valueAt(i);
5409 if (axis.axisInfo.axis == axisId
5410 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5411 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005412 return true;
5413 }
5414 }
5415 return false;
5416}
Jeff Browncb1404e2011-01-15 18:14:15 -08005417
Jeff Brown6f2fba42011-02-19 01:08:02 -08005418void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5419 size_t i = mAxes.size();
5420 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5421 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5422 continue;
5423 }
5424 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5425 getDeviceName().string(), mAxes.keyAt(i));
5426 mAxes.removeItemsAt(i);
5427 }
5428}
5429
5430bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5431 switch (axis) {
5432 case AMOTION_EVENT_AXIS_X:
5433 case AMOTION_EVENT_AXIS_Y:
5434 case AMOTION_EVENT_AXIS_Z:
5435 case AMOTION_EVENT_AXIS_RX:
5436 case AMOTION_EVENT_AXIS_RY:
5437 case AMOTION_EVENT_AXIS_RZ:
5438 case AMOTION_EVENT_AXIS_HAT_X:
5439 case AMOTION_EVENT_AXIS_HAT_Y:
5440 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005441 case AMOTION_EVENT_AXIS_RUDDER:
5442 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005443 return true;
5444 default:
5445 return false;
5446 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005447}
5448
5449void JoystickInputMapper::reset() {
5450 // Recenter all axes.
5451 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005452
Jeff Brown6f2fba42011-02-19 01:08:02 -08005453 size_t numAxes = mAxes.size();
5454 for (size_t i = 0; i < numAxes; i++) {
5455 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005456 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005457 }
5458
5459 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005460
5461 InputMapper::reset();
5462}
5463
5464void JoystickInputMapper::process(const RawEvent* rawEvent) {
5465 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005466 case EV_ABS: {
5467 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5468 if (index >= 0) {
5469 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005470 float newValue, highNewValue;
5471 switch (axis.axisInfo.mode) {
5472 case AxisInfo::MODE_INVERT:
5473 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5474 * axis.scale + axis.offset;
5475 highNewValue = 0.0f;
5476 break;
5477 case AxisInfo::MODE_SPLIT:
5478 if (rawEvent->value < axis.axisInfo.splitValue) {
5479 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5480 * axis.scale + axis.offset;
5481 highNewValue = 0.0f;
5482 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5483 newValue = 0.0f;
5484 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5485 * axis.highScale + axis.highOffset;
5486 } else {
5487 newValue = 0.0f;
5488 highNewValue = 0.0f;
5489 }
5490 break;
5491 default:
5492 newValue = rawEvent->value * axis.scale + axis.offset;
5493 highNewValue = 0.0f;
5494 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005495 }
Jeff Brown85297452011-03-04 13:07:49 -08005496 axis.newValue = newValue;
5497 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005498 }
5499 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005500 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005501
5502 case EV_SYN:
5503 switch (rawEvent->scanCode) {
5504 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005505 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005506 break;
5507 }
5508 break;
5509 }
5510}
5511
Jeff Brown6f2fba42011-02-19 01:08:02 -08005512void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005513 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005514 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005515 }
5516
5517 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005518 int32_t buttonState = 0;
5519
5520 PointerProperties pointerProperties;
5521 pointerProperties.clear();
5522 pointerProperties.id = 0;
5523 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005524
Jeff Brown6f2fba42011-02-19 01:08:02 -08005525 PointerCoords pointerCoords;
5526 pointerCoords.clear();
5527
5528 size_t numAxes = mAxes.size();
5529 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005530 const Axis& axis = mAxes.valueAt(i);
5531 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5532 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5533 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5534 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005535 }
5536
Jeff Brown56194eb2011-03-02 19:23:13 -08005537 // Moving a joystick axis should not wake the devide because joysticks can
5538 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5539 // button will likely wake the device.
5540 // TODO: Use the input device configuration to control this behavior more finely.
5541 uint32_t policyFlags = 0;
5542
Jeff Brown56194eb2011-03-02 19:23:13 -08005543 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005544 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5545 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005546}
5547
Jeff Brown85297452011-03-04 13:07:49 -08005548bool JoystickInputMapper::filterAxes(bool force) {
5549 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005550 size_t numAxes = mAxes.size();
5551 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005552 Axis& axis = mAxes.editValueAt(i);
5553 if (force || hasValueChangedSignificantly(axis.filter,
5554 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5555 axis.currentValue = axis.newValue;
5556 atLeastOneSignificantChange = true;
5557 }
5558 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5559 if (force || hasValueChangedSignificantly(axis.filter,
5560 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5561 axis.highCurrentValue = axis.highNewValue;
5562 atLeastOneSignificantChange = true;
5563 }
5564 }
5565 }
5566 return atLeastOneSignificantChange;
5567}
5568
5569bool JoystickInputMapper::hasValueChangedSignificantly(
5570 float filter, float newValue, float currentValue, float min, float max) {
5571 if (newValue != currentValue) {
5572 // Filter out small changes in value unless the value is converging on the axis
5573 // bounds or center point. This is intended to reduce the amount of information
5574 // sent to applications by particularly noisy joysticks (such as PS3).
5575 if (fabs(newValue - currentValue) > filter
5576 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5577 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5578 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5579 return true;
5580 }
5581 }
5582 return false;
5583}
5584
5585bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5586 float filter, float newValue, float currentValue, float thresholdValue) {
5587 float newDistance = fabs(newValue - thresholdValue);
5588 if (newDistance < filter) {
5589 float oldDistance = fabs(currentValue - thresholdValue);
5590 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005591 return true;
5592 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005593 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005594 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005595}
5596
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005597} // namespace android