blob: ea614ad393c3092c44afbcdef16956d6e4702884 [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>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080042#include <androidfw/Keyboard.h>
43#include <androidfw/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700183 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700185 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 }
187}
188
189static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
190 nsecs_t when, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
192 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
193 lastButtonState, currentButtonState,
194 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
198}
199
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700200
Jeff Brown65fd2512011-08-18 11:20:58 -0700201// --- InputReaderConfiguration ---
202
203bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external,
204 int32_t* width, int32_t* height, int32_t* orientation) const {
205 if (displayId == 0) {
206 const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
207 if (info.width > 0 && info.height > 0) {
208 if (width) {
209 *width = info.width;
210 }
211 if (height) {
212 *height = info.height;
213 }
214 if (orientation) {
215 *orientation = info.orientation;
216 }
217 return true;
218 }
219 }
220 return false;
221}
222
223void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external,
224 int32_t width, int32_t height, int32_t orientation) {
225 if (displayId == 0) {
226 DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay;
227 info.width = width;
228 info.height = height;
229 info.orientation = orientation;
230 }
231}
232
233
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234// --- InputReader ---
235
236InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700238 const sp<InputListenerInterface>& listener) :
239 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700240 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700241 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700242 mQueuedListener = new QueuedInputListener(listener);
243
244 { // acquire lock
245 AutoMutex _l(mLock);
246
247 refreshConfigurationLocked(0);
248 updateGlobalMetaStateLocked();
249 updateInputConfigurationLocked();
250 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700251}
252
253InputReader::~InputReader() {
254 for (size_t i = 0; i < mDevices.size(); i++) {
255 delete mDevices.valueAt(i);
256 }
257}
258
259void InputReader::loopOnce() {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700260 int32_t timeoutMillis;
Jeff Brown474dcb52011-06-14 20:22:50 -0700261 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700262 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700263
Jeff Brownbe1aa822011-07-27 16:04:54 -0700264 uint32_t changes = mConfigurationChangesToRefresh;
265 if (changes) {
266 mConfigurationChangesToRefresh = 0;
267 refreshConfigurationLocked(changes);
268 }
269
270 timeoutMillis = -1;
271 if (mNextTimeout != LLONG_MAX) {
272 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
273 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
274 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700275 } // release lock
276
Jeff Brownb7198742011-03-18 18:14:26 -0700277 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700278
279 { // acquire lock
280 AutoMutex _l(mLock);
Jeff Brown112b5f52012-01-27 17:32:06 -0800281 mReaderIsAliveCondition.broadcast();
Jeff Brownbe1aa822011-07-27 16:04:54 -0700282
283 if (count) {
284 processEventsLocked(mEventBuffer, count);
285 }
286 if (!count || timeoutMillis == 0) {
287 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown112b5f52012-01-27 17:32:06 -0800288 if (now >= mNextTimeout) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700289#if DEBUG_RAW_EVENTS
Jeff Brown112b5f52012-01-27 17:32:06 -0800290 ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700291#endif
Jeff Brown112b5f52012-01-27 17:32:06 -0800292 mNextTimeout = LLONG_MAX;
293 timeoutExpiredLocked(now);
294 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700295 }
296 } // release lock
297
298 // Flush queued events out to the listener.
299 // This must happen outside of the lock because the listener could potentially call
300 // back into the InputReader's methods, such as getScanCodeState, or become blocked
301 // on another thread similarly waiting to acquire the InputReader lock thereby
302 // resulting in a deadlock. This situation is actually quite plausible because the
303 // listener is actually the input dispatcher, which calls into the window manager,
304 // which occasionally calls into the input reader.
305 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700306}
307
Jeff Brownbe1aa822011-07-27 16:04:54 -0700308void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700309 for (const RawEvent* rawEvent = rawEvents; count;) {
310 int32_t type = rawEvent->type;
311 size_t batchSize = 1;
312 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
313 int32_t deviceId = rawEvent->deviceId;
314 while (batchSize < count) {
315 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
316 || rawEvent[batchSize].deviceId != deviceId) {
317 break;
318 }
319 batchSize += 1;
320 }
321#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000322 ALOGD("BatchSize: %d Count: %d", batchSize, count);
Jeff Brownb7198742011-03-18 18:14:26 -0700323#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700324 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700325 } else {
326 switch (rawEvent->type) {
327 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700329 break;
330 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700331 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700332 break;
333 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700334 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700335 break;
336 default:
Steve Blockec193de2012-01-09 18:35:44 +0000337 ALOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700338 break;
339 }
340 }
341 count -= batchSize;
342 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700343 }
344}
345
Jeff Brown65fd2512011-08-18 11:20:58 -0700346void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700347 InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700348 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
349
Jeff Browne38fdfa2012-04-06 14:51:01 -0700350 InputDevice* device = createDeviceLocked(deviceId, identifier, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700351 device->configure(when, &mConfig, 0);
352 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700353
Jeff Brown8d608662010-08-30 03:02:23 -0700354 if (device->isIgnored()) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700355 ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId,
356 identifier.name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700357 } else {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700358 ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId,
359 identifier.name.string(), device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700360 }
361
Jeff Brownbe1aa822011-07-27 16:04:54 -0700362 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
363 if (deviceIndex < 0) {
364 mDevices.add(deviceId, device);
365 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000366 ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700367 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700368 return;
369 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700370}
371
Jeff Brown65fd2512011-08-18 11:20:58 -0700372void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700373 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700374 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
375 if (deviceIndex >= 0) {
376 device = mDevices.valueAt(deviceIndex);
377 mDevices.removeItemsAt(deviceIndex, 1);
378 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000379 ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700380 return;
381 }
382
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383 if (device->isIgnored()) {
Steve Block6215d3f2012-01-04 20:05:49 +0000384 ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700385 device->getId(), device->getName().string());
386 } else {
Steve Block6215d3f2012-01-04 20:05:49 +0000387 ALOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 device->getId(), device->getName().string(), device->getSources());
389 }
390
Jeff Brown65fd2512011-08-18 11:20:58 -0700391 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700393}
394
Jeff Brownbe1aa822011-07-27 16:04:54 -0700395InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
Jeff Browne38fdfa2012-04-06 14:51:01 -0700396 const InputDeviceIdentifier& identifier, uint32_t classes) {
397 InputDevice* device = new InputDevice(&mContext, deviceId, identifier, classes);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700398
Jeff Brown56194eb2011-03-02 19:23:13 -0800399 // External devices.
400 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
401 device->setExternal(true);
402 }
403
Jeff Brown6d0fec22010-07-23 21:28:06 -0700404 // Switch-like devices.
405 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
406 device->addMapper(new SwitchInputMapper(device));
407 }
408
409 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800410 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700411 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
412 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800413 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700414 }
415 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
416 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
417 }
418 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800419 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800421 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800422 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800423 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700424
Jeff Brownefd32662011-03-08 15:13:06 -0800425 if (keyboardSource != 0) {
426 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700427 }
428
Jeff Brown83c09682010-12-23 17:50:18 -0800429 // Cursor-like devices.
430 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
431 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700432 }
433
Jeff Brown58a2da82011-01-25 16:02:22 -0800434 // Touchscreens and touchpad devices.
435 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800436 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800437 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800438 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439 }
440
Jeff Browncb1404e2011-01-15 18:14:15 -0800441 // Joystick-like devices.
442 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
443 device->addMapper(new JoystickInputMapper(device));
444 }
445
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446 return device;
447}
448
Jeff Brownbe1aa822011-07-27 16:04:54 -0700449void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700450 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700451 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
452 if (deviceIndex < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000453 ALOGW("Discarding event for unknown deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700454 return;
455 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700456
Jeff Brownbe1aa822011-07-27 16:04:54 -0700457 InputDevice* device = mDevices.valueAt(deviceIndex);
458 if (device->isIgnored()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000459 //ALOGD("Discarding event for ignored deviceId %d.", deviceId);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700460 return;
461 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462
Jeff Brownbe1aa822011-07-27 16:04:54 -0700463 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700464}
465
Jeff Brownbe1aa822011-07-27 16:04:54 -0700466void InputReader::timeoutExpiredLocked(nsecs_t when) {
467 for (size_t i = 0; i < mDevices.size(); i++) {
468 InputDevice* device = mDevices.valueAt(i);
469 if (!device->isIgnored()) {
470 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700471 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700472 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700473}
474
Jeff Brownbe1aa822011-07-27 16:04:54 -0700475void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700477 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700478
479 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700480 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481
482 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700483 NotifyConfigurationChangedArgs args(when);
484 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700485}
486
Jeff Brownbe1aa822011-07-27 16:04:54 -0700487void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700488 mPolicy->getReaderConfiguration(&mConfig);
489 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
490
Jeff Brown474dcb52011-06-14 20:22:50 -0700491 if (changes) {
Steve Block6215d3f2012-01-04 20:05:49 +0000492 ALOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700493 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700494
495 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
496 mEventHub->requestReopenDevices();
497 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700498 for (size_t i = 0; i < mDevices.size(); i++) {
499 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700500 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700501 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700502 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700503 }
504}
505
Jeff Brownbe1aa822011-07-27 16:04:54 -0700506void InputReader::updateGlobalMetaStateLocked() {
507 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700508
Jeff Brownbe1aa822011-07-27 16:04:54 -0700509 for (size_t i = 0; i < mDevices.size(); i++) {
510 InputDevice* device = mDevices.valueAt(i);
511 mGlobalMetaState |= device->getMetaState();
512 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700513}
514
Jeff Brownbe1aa822011-07-27 16:04:54 -0700515int32_t InputReader::getGlobalMetaStateLocked() {
516 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700517}
518
Jeff Brownbe1aa822011-07-27 16:04:54 -0700519void InputReader::updateInputConfigurationLocked() {
520 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
521 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
522 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
523 InputDeviceInfo deviceInfo;
524 for (size_t i = 0; i < mDevices.size(); i++) {
525 InputDevice* device = mDevices.valueAt(i);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700526 if (!(device->getClasses() & INPUT_DEVICE_CLASS_VIRTUAL)) {
527 device->getDeviceInfo(& deviceInfo);
528 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700529
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700530 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
531 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
532 }
533 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
534 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
535 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
536 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
537 }
538 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
539 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
540 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700541 }
542 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700543
Jeff Brownbe1aa822011-07-27 16:04:54 -0700544 mInputConfiguration.touchScreen = touchScreenConfig;
545 mInputConfiguration.keyboard = keyboardConfig;
546 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700547}
548
Jeff Brownbe1aa822011-07-27 16:04:54 -0700549void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800550 mDisableVirtualKeysTimeout = time;
551}
552
Jeff Brownbe1aa822011-07-27 16:04:54 -0700553bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800554 InputDevice* device, int32_t keyCode, int32_t scanCode) {
555 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000556 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800557 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
558 device->getName().string(),
559 (mDisableVirtualKeysTimeout - now) * 0.000001,
560 keyCode, scanCode);
561 return true;
562 } else {
563 return false;
564 }
565}
566
Jeff Brownbe1aa822011-07-27 16:04:54 -0700567void InputReader::fadePointerLocked() {
568 for (size_t i = 0; i < mDevices.size(); i++) {
569 InputDevice* device = mDevices.valueAt(i);
570 device->fadePointer();
571 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800572}
573
Jeff Brownbe1aa822011-07-27 16:04:54 -0700574void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700575 if (when < mNextTimeout) {
576 mNextTimeout = when;
577 }
578}
579
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582
Jeff Brownbe1aa822011-07-27 16:04:54 -0700583 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700584}
585
586status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700587 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700588
Jeff Brownbe1aa822011-07-27 16:04:54 -0700589 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
590 if (deviceIndex < 0) {
591 return NAME_NOT_FOUND;
592 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700593
Jeff Brownbe1aa822011-07-27 16:04:54 -0700594 InputDevice* device = mDevices.valueAt(deviceIndex);
595 if (device->isIgnored()) {
596 return NAME_NOT_FOUND;
597 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598
Jeff Brownbe1aa822011-07-27 16:04:54 -0700599 device->getDeviceInfo(outDeviceInfo);
600 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700601}
602
603void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700604 AutoMutex _l(mLock);
605
Jeff Brown6d0fec22010-07-23 21:28:06 -0700606 outDeviceIds.clear();
607
Jeff Brownbe1aa822011-07-27 16:04:54 -0700608 size_t numDevices = mDevices.size();
609 for (size_t i = 0; i < numDevices; i++) {
610 InputDevice* device = mDevices.valueAt(i);
611 if (!device->isIgnored()) {
612 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700613 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700614 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700615}
616
617int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
618 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700619 AutoMutex _l(mLock);
620
621 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700622}
623
624int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
625 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700626 AutoMutex _l(mLock);
627
628 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629}
630
631int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700632 AutoMutex _l(mLock);
633
634 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700635}
636
Jeff Brownbe1aa822011-07-27 16:04:54 -0700637int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700638 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700639 int32_t result = AKEY_STATE_UNKNOWN;
640 if (deviceId >= 0) {
641 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
642 if (deviceIndex >= 0) {
643 InputDevice* device = mDevices.valueAt(deviceIndex);
644 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
645 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700646 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700647 }
648 } else {
649 size_t numDevices = mDevices.size();
650 for (size_t i = 0; i < numDevices; i++) {
651 InputDevice* device = mDevices.valueAt(i);
652 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800653 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
654 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
655 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
656 if (currentResult >= AKEY_STATE_DOWN) {
657 return currentResult;
658 } else if (currentResult == AKEY_STATE_UP) {
659 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700660 }
661 }
662 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700663 }
664 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700665}
666
667bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
668 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700669 AutoMutex _l(mLock);
670
Jeff Brown6d0fec22010-07-23 21:28:06 -0700671 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700672 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700673}
674
Jeff Brownbe1aa822011-07-27 16:04:54 -0700675bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
676 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
677 bool result = false;
678 if (deviceId >= 0) {
679 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
680 if (deviceIndex >= 0) {
681 InputDevice* device = mDevices.valueAt(deviceIndex);
682 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
683 result = device->markSupportedKeyCodes(sourceMask,
684 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700685 }
686 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700687 } else {
688 size_t numDevices = mDevices.size();
689 for (size_t i = 0; i < numDevices; i++) {
690 InputDevice* device = mDevices.valueAt(i);
691 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
692 result |= device->markSupportedKeyCodes(sourceMask,
693 numCodes, keyCodes, outFlags);
694 }
695 }
696 }
697 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700698}
699
Jeff Brown474dcb52011-06-14 20:22:50 -0700700void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700701 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700702
Jeff Brownbe1aa822011-07-27 16:04:54 -0700703 if (changes) {
704 bool needWake = !mConfigurationChangesToRefresh;
705 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700706
707 if (needWake) {
708 mEventHub->wake();
709 }
710 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700711}
712
Jeff Brownb88102f2010-09-08 11:49:43 -0700713void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700714 AutoMutex _l(mLock);
715
Jeff Brownf2f487182010-10-01 17:46:21 -0700716 mEventHub->dump(dump);
717 dump.append("\n");
718
719 dump.append("Input Reader State:\n");
720
Jeff Brownbe1aa822011-07-27 16:04:54 -0700721 for (size_t i = 0; i < mDevices.size(); i++) {
722 mDevices.valueAt(i)->dump(dump);
723 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700724
725 dump.append(INDENT "Configuration:\n");
726 dump.append(INDENT2 "ExcludedDeviceNames: [");
727 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
728 if (i != 0) {
729 dump.append(", ");
730 }
731 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
732 }
733 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700734 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
735 mConfig.virtualKeyQuietTime * 0.000001f);
736
Jeff Brown19c97d462011-06-01 12:33:19 -0700737 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
738 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
739 mConfig.pointerVelocityControlParameters.scale,
740 mConfig.pointerVelocityControlParameters.lowThreshold,
741 mConfig.pointerVelocityControlParameters.highThreshold,
742 mConfig.pointerVelocityControlParameters.acceleration);
743
744 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
745 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
746 mConfig.wheelVelocityControlParameters.scale,
747 mConfig.wheelVelocityControlParameters.lowThreshold,
748 mConfig.wheelVelocityControlParameters.highThreshold,
749 mConfig.wheelVelocityControlParameters.acceleration);
750
Jeff Brown214eaf42011-05-26 19:17:02 -0700751 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700752 dump.appendFormat(INDENT3 "Enabled: %s\n",
753 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700754 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
755 mConfig.pointerGestureQuietInterval * 0.000001f);
756 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
757 mConfig.pointerGestureDragMinSwitchSpeed);
758 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
759 mConfig.pointerGestureTapInterval * 0.000001f);
760 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
761 mConfig.pointerGestureTapDragInterval * 0.000001f);
762 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
763 mConfig.pointerGestureTapSlop);
764 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
765 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700766 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
767 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700768 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
769 mConfig.pointerGestureSwipeTransitionAngleCosine);
770 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
771 mConfig.pointerGestureSwipeMaxWidthRatio);
772 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
773 mConfig.pointerGestureMovementSpeedRatio);
774 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
775 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700776}
777
Jeff Brown89ef0722011-08-10 16:25:21 -0700778void InputReader::monitor() {
779 // Acquire and release the lock to ensure that the reader has not deadlocked.
780 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800781 mEventHub->wake();
782 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700783 mLock.unlock();
784
785 // Check the EventHub
786 mEventHub->monitor();
787}
788
Jeff Brown6d0fec22010-07-23 21:28:06 -0700789
Jeff Brownbe1aa822011-07-27 16:04:54 -0700790// --- InputReader::ContextImpl ---
791
792InputReader::ContextImpl::ContextImpl(InputReader* reader) :
793 mReader(reader) {
794}
795
796void InputReader::ContextImpl::updateGlobalMetaState() {
797 // lock is already held by the input loop
798 mReader->updateGlobalMetaStateLocked();
799}
800
801int32_t InputReader::ContextImpl::getGlobalMetaState() {
802 // lock is already held by the input loop
803 return mReader->getGlobalMetaStateLocked();
804}
805
806void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
807 // lock is already held by the input loop
808 mReader->disableVirtualKeysUntilLocked(time);
809}
810
811bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
812 InputDevice* device, int32_t keyCode, int32_t scanCode) {
813 // lock is already held by the input loop
814 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
815}
816
817void InputReader::ContextImpl::fadePointer() {
818 // lock is already held by the input loop
819 mReader->fadePointerLocked();
820}
821
822void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
823 // lock is already held by the input loop
824 mReader->requestTimeoutAtTimeLocked(when);
825}
826
827InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
828 return mReader->mPolicy.get();
829}
830
831InputListenerInterface* InputReader::ContextImpl::getListener() {
832 return mReader->mQueuedListener.get();
833}
834
835EventHubInterface* InputReader::ContextImpl::getEventHub() {
836 return mReader->mEventHub.get();
837}
838
839
Jeff Brown6d0fec22010-07-23 21:28:06 -0700840// --- InputReaderThread ---
841
842InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
843 Thread(/*canCallJava*/ true), mReader(reader) {
844}
845
846InputReaderThread::~InputReaderThread() {
847}
848
849bool InputReaderThread::threadLoop() {
850 mReader->loopOnce();
851 return true;
852}
853
854
855// --- InputDevice ---
856
Jeff Browne38fdfa2012-04-06 14:51:01 -0700857InputDevice::InputDevice(InputReaderContext* context, int32_t id,
858 const InputDeviceIdentifier& identifier, uint32_t classes) :
859 mContext(context), mId(id), mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285af2011-08-31 12:56:34 -0700860 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700861}
862
863InputDevice::~InputDevice() {
864 size_t numMappers = mMappers.size();
865 for (size_t i = 0; i < numMappers; i++) {
866 delete mMappers[i];
867 }
868 mMappers.clear();
869}
870
Jeff Brownef3d7e82010-09-30 14:33:04 -0700871void InputDevice::dump(String8& dump) {
872 InputDeviceInfo deviceInfo;
873 getDeviceInfo(& deviceInfo);
874
Jeff Brown90655042010-12-02 13:50:46 -0800875 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700876 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800877 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700878 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
879 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800880
Jeff Brownefd32662011-03-08 15:13:06 -0800881 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800882 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700883 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800884 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800885 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
886 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800887 char name[32];
888 if (label) {
889 strncpy(name, label, sizeof(name));
890 name[sizeof(name) - 1] = '\0';
891 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800892 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800893 }
Jeff Brownefd32662011-03-08 15:13:06 -0800894 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
895 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
896 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800897 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700898 }
899
900 size_t numMappers = mMappers.size();
901 for (size_t i = 0; i < numMappers; i++) {
902 InputMapper* mapper = mMappers[i];
903 mapper->dump(dump);
904 }
905}
906
Jeff Brown6d0fec22010-07-23 21:28:06 -0700907void InputDevice::addMapper(InputMapper* mapper) {
908 mMappers.add(mapper);
909}
910
Jeff Brown65fd2512011-08-18 11:20:58 -0700911void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700912 mSources = 0;
913
Jeff Brown474dcb52011-06-14 20:22:50 -0700914 if (!isIgnored()) {
915 if (!changes) { // first time only
916 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
917 }
918
919 size_t numMappers = mMappers.size();
920 for (size_t i = 0; i < numMappers; i++) {
921 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700922 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700923 mSources |= mapper->getSources();
924 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700925 }
926}
927
Jeff Brown65fd2512011-08-18 11:20:58 -0700928void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700929 size_t numMappers = mMappers.size();
930 for (size_t i = 0; i < numMappers; i++) {
931 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700932 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700933 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700934
935 mContext->updateGlobalMetaState();
936
937 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700938}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700939
Jeff Brownb7198742011-03-18 18:14:26 -0700940void InputDevice::process(const RawEvent* rawEvents, size_t count) {
941 // Process all of the events in order for each mapper.
942 // We cannot simply ask each mapper to process them in bulk because mappers may
943 // have side-effects that must be interleaved. For example, joystick movement events and
944 // gamepad button presses are handled by different mappers but they should be dispatched
945 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700946 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700947 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
948#if DEBUG_RAW_EVENTS
Jeff Brown49ccac52012-04-11 18:27:33 -0700949 ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
950 rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
Jeff Brownb7198742011-03-18 18:14:26 -0700951#endif
952
Jeff Brown80fd47c2011-05-24 01:07:44 -0700953 if (mDropUntilNextSync) {
Jeff Brown49ccac52012-04-11 18:27:33 -0700954 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -0700955 mDropUntilNextSync = false;
956#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000957 ALOGD("Recovered from input event buffer overrun.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700958#endif
959 } else {
960#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000961 ALOGD("Dropped input event while waiting for next input sync.");
Jeff Brown80fd47c2011-05-24 01:07:44 -0700962#endif
963 }
Jeff Brown49ccac52012-04-11 18:27:33 -0700964 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700965 ALOGI("Detected input event buffer overrun for device %s.", getName().string());
Jeff Brown80fd47c2011-05-24 01:07:44 -0700966 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700967 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700968 } else {
969 for (size_t i = 0; i < numMappers; i++) {
970 InputMapper* mapper = mMappers[i];
971 mapper->process(rawEvent);
972 }
Jeff Brownb7198742011-03-18 18:14:26 -0700973 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700974 }
975}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700976
Jeff Brownaa3855d2011-03-17 01:34:19 -0700977void InputDevice::timeoutExpired(nsecs_t when) {
978 size_t numMappers = mMappers.size();
979 for (size_t i = 0; i < numMappers; i++) {
980 InputMapper* mapper = mMappers[i];
981 mapper->timeoutExpired(when);
982 }
983}
984
Jeff Brown6d0fec22010-07-23 21:28:06 -0700985void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
Jeff Browne38fdfa2012-04-06 14:51:01 -0700986 outDeviceInfo->initialize(mId, mIdentifier.name, mIdentifier.descriptor);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700987
988 size_t numMappers = mMappers.size();
989 for (size_t i = 0; i < numMappers; i++) {
990 InputMapper* mapper = mMappers[i];
991 mapper->populateDeviceInfo(outDeviceInfo);
992 }
993}
994
995int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
996 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
997}
998
999int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1000 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
1001}
1002
1003int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1004 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
1005}
1006
1007int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
1008 int32_t result = AKEY_STATE_UNKNOWN;
1009 size_t numMappers = mMappers.size();
1010 for (size_t i = 0; i < numMappers; i++) {
1011 InputMapper* mapper = mMappers[i];
1012 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -08001013 // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
1014 // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it.
1015 int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code);
1016 if (currentResult >= AKEY_STATE_DOWN) {
1017 return currentResult;
1018 } else if (currentResult == AKEY_STATE_UP) {
1019 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001020 }
1021 }
1022 }
1023 return result;
1024}
1025
1026bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1027 const int32_t* keyCodes, uint8_t* outFlags) {
1028 bool result = false;
1029 size_t numMappers = mMappers.size();
1030 for (size_t i = 0; i < numMappers; i++) {
1031 InputMapper* mapper = mMappers[i];
1032 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1033 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1034 }
1035 }
1036 return result;
1037}
1038
1039int32_t InputDevice::getMetaState() {
1040 int32_t result = 0;
1041 size_t numMappers = mMappers.size();
1042 for (size_t i = 0; i < numMappers; i++) {
1043 InputMapper* mapper = mMappers[i];
1044 result |= mapper->getMetaState();
1045 }
1046 return result;
1047}
1048
Jeff Brown05dc66a2011-03-02 14:41:58 -08001049void InputDevice::fadePointer() {
1050 size_t numMappers = mMappers.size();
1051 for (size_t i = 0; i < numMappers; i++) {
1052 InputMapper* mapper = mMappers[i];
1053 mapper->fadePointer();
1054 }
1055}
1056
Jeff Brown65fd2512011-08-18 11:20:58 -07001057void InputDevice::notifyReset(nsecs_t when) {
1058 NotifyDeviceResetArgs args(when, mId);
1059 mContext->getListener()->notifyDeviceReset(&args);
1060}
1061
Jeff Brown6d0fec22010-07-23 21:28:06 -07001062
Jeff Brown49754db2011-07-01 17:37:58 -07001063// --- CursorButtonAccumulator ---
1064
1065CursorButtonAccumulator::CursorButtonAccumulator() {
1066 clearButtons();
1067}
1068
Jeff Brown65fd2512011-08-18 11:20:58 -07001069void CursorButtonAccumulator::reset(InputDevice* device) {
1070 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1071 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1072 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1073 mBtnBack = device->isKeyPressed(BTN_BACK);
1074 mBtnSide = device->isKeyPressed(BTN_SIDE);
1075 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1076 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1077 mBtnTask = device->isKeyPressed(BTN_TASK);
1078}
1079
Jeff Brown49754db2011-07-01 17:37:58 -07001080void CursorButtonAccumulator::clearButtons() {
1081 mBtnLeft = 0;
1082 mBtnRight = 0;
1083 mBtnMiddle = 0;
1084 mBtnBack = 0;
1085 mBtnSide = 0;
1086 mBtnForward = 0;
1087 mBtnExtra = 0;
1088 mBtnTask = 0;
1089}
1090
1091void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1092 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001093 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001094 case BTN_LEFT:
1095 mBtnLeft = rawEvent->value;
1096 break;
1097 case BTN_RIGHT:
1098 mBtnRight = rawEvent->value;
1099 break;
1100 case BTN_MIDDLE:
1101 mBtnMiddle = rawEvent->value;
1102 break;
1103 case BTN_BACK:
1104 mBtnBack = rawEvent->value;
1105 break;
1106 case BTN_SIDE:
1107 mBtnSide = rawEvent->value;
1108 break;
1109 case BTN_FORWARD:
1110 mBtnForward = rawEvent->value;
1111 break;
1112 case BTN_EXTRA:
1113 mBtnExtra = rawEvent->value;
1114 break;
1115 case BTN_TASK:
1116 mBtnTask = rawEvent->value;
1117 break;
1118 }
1119 }
1120}
1121
1122uint32_t CursorButtonAccumulator::getButtonState() const {
1123 uint32_t result = 0;
1124 if (mBtnLeft) {
1125 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1126 }
1127 if (mBtnRight) {
1128 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1129 }
1130 if (mBtnMiddle) {
1131 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1132 }
1133 if (mBtnBack || mBtnSide) {
1134 result |= AMOTION_EVENT_BUTTON_BACK;
1135 }
1136 if (mBtnForward || mBtnExtra) {
1137 result |= AMOTION_EVENT_BUTTON_FORWARD;
1138 }
1139 return result;
1140}
1141
1142
1143// --- CursorMotionAccumulator ---
1144
Jeff Brown65fd2512011-08-18 11:20:58 -07001145CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001146 clearRelativeAxes();
1147}
1148
Jeff Brown65fd2512011-08-18 11:20:58 -07001149void CursorMotionAccumulator::reset(InputDevice* device) {
1150 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001151}
1152
1153void CursorMotionAccumulator::clearRelativeAxes() {
1154 mRelX = 0;
1155 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001156}
1157
1158void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1159 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001160 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001161 case REL_X:
1162 mRelX = rawEvent->value;
1163 break;
1164 case REL_Y:
1165 mRelY = rawEvent->value;
1166 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001167 }
1168 }
1169}
1170
1171void CursorMotionAccumulator::finishSync() {
1172 clearRelativeAxes();
1173}
1174
1175
1176// --- CursorScrollAccumulator ---
1177
1178CursorScrollAccumulator::CursorScrollAccumulator() :
1179 mHaveRelWheel(false), mHaveRelHWheel(false) {
1180 clearRelativeAxes();
1181}
1182
1183void CursorScrollAccumulator::configure(InputDevice* device) {
1184 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1185 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1186}
1187
1188void CursorScrollAccumulator::reset(InputDevice* device) {
1189 clearRelativeAxes();
1190}
1191
1192void CursorScrollAccumulator::clearRelativeAxes() {
1193 mRelWheel = 0;
1194 mRelHWheel = 0;
1195}
1196
1197void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1198 if (rawEvent->type == EV_REL) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001199 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001200 case REL_WHEEL:
1201 mRelWheel = rawEvent->value;
1202 break;
1203 case REL_HWHEEL:
1204 mRelHWheel = rawEvent->value;
1205 break;
1206 }
1207 }
1208}
1209
Jeff Brown65fd2512011-08-18 11:20:58 -07001210void CursorScrollAccumulator::finishSync() {
1211 clearRelativeAxes();
1212}
1213
Jeff Brown49754db2011-07-01 17:37:58 -07001214
1215// --- TouchButtonAccumulator ---
1216
1217TouchButtonAccumulator::TouchButtonAccumulator() :
1218 mHaveBtnTouch(false) {
1219 clearButtons();
1220}
1221
1222void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001223 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1224}
1225
1226void TouchButtonAccumulator::reset(InputDevice* device) {
1227 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1228 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1229 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1230 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1231 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1232 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1233 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1234 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1235 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1236 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1237 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001238 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1239 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1240 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001241}
1242
1243void TouchButtonAccumulator::clearButtons() {
1244 mBtnTouch = 0;
1245 mBtnStylus = 0;
1246 mBtnStylus2 = 0;
1247 mBtnToolFinger = 0;
1248 mBtnToolPen = 0;
1249 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001250 mBtnToolBrush = 0;
1251 mBtnToolPencil = 0;
1252 mBtnToolAirbrush = 0;
1253 mBtnToolMouse = 0;
1254 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001255 mBtnToolDoubleTap = 0;
1256 mBtnToolTripleTap = 0;
1257 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001258}
1259
1260void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1261 if (rawEvent->type == EV_KEY) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001262 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001263 case BTN_TOUCH:
1264 mBtnTouch = rawEvent->value;
1265 break;
1266 case BTN_STYLUS:
1267 mBtnStylus = rawEvent->value;
1268 break;
1269 case BTN_STYLUS2:
1270 mBtnStylus2 = rawEvent->value;
1271 break;
1272 case BTN_TOOL_FINGER:
1273 mBtnToolFinger = rawEvent->value;
1274 break;
1275 case BTN_TOOL_PEN:
1276 mBtnToolPen = rawEvent->value;
1277 break;
1278 case BTN_TOOL_RUBBER:
1279 mBtnToolRubber = rawEvent->value;
1280 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001281 case BTN_TOOL_BRUSH:
1282 mBtnToolBrush = rawEvent->value;
1283 break;
1284 case BTN_TOOL_PENCIL:
1285 mBtnToolPencil = rawEvent->value;
1286 break;
1287 case BTN_TOOL_AIRBRUSH:
1288 mBtnToolAirbrush = rawEvent->value;
1289 break;
1290 case BTN_TOOL_MOUSE:
1291 mBtnToolMouse = rawEvent->value;
1292 break;
1293 case BTN_TOOL_LENS:
1294 mBtnToolLens = rawEvent->value;
1295 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001296 case BTN_TOOL_DOUBLETAP:
1297 mBtnToolDoubleTap = rawEvent->value;
1298 break;
1299 case BTN_TOOL_TRIPLETAP:
1300 mBtnToolTripleTap = rawEvent->value;
1301 break;
1302 case BTN_TOOL_QUADTAP:
1303 mBtnToolQuadTap = rawEvent->value;
1304 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001305 }
1306 }
1307}
1308
1309uint32_t TouchButtonAccumulator::getButtonState() const {
1310 uint32_t result = 0;
1311 if (mBtnStylus) {
1312 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1313 }
1314 if (mBtnStylus2) {
1315 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1316 }
1317 return result;
1318}
1319
1320int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001321 if (mBtnToolMouse || mBtnToolLens) {
1322 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1323 }
Jeff Brown49754db2011-07-01 17:37:58 -07001324 if (mBtnToolRubber) {
1325 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1326 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001327 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001328 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1329 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001330 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001331 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1332 }
1333 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1334}
1335
Jeff Brownd87c6d52011-08-10 14:55:59 -07001336bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001337 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1338 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001339 || mBtnToolMouse || mBtnToolLens
1340 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001341}
1342
1343bool TouchButtonAccumulator::isHovering() const {
1344 return mHaveBtnTouch && !mBtnTouch;
1345}
1346
1347
Jeff Brownbe1aa822011-07-27 16:04:54 -07001348// --- RawPointerAxes ---
1349
1350RawPointerAxes::RawPointerAxes() {
1351 clear();
1352}
1353
1354void RawPointerAxes::clear() {
1355 x.clear();
1356 y.clear();
1357 pressure.clear();
1358 touchMajor.clear();
1359 touchMinor.clear();
1360 toolMajor.clear();
1361 toolMinor.clear();
1362 orientation.clear();
1363 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001364 tiltX.clear();
1365 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001366 trackingId.clear();
1367 slot.clear();
1368}
1369
1370
1371// --- RawPointerData ---
1372
1373RawPointerData::RawPointerData() {
1374 clear();
1375}
1376
1377void RawPointerData::clear() {
1378 pointerCount = 0;
1379 clearIdBits();
1380}
1381
1382void RawPointerData::copyFrom(const RawPointerData& other) {
1383 pointerCount = other.pointerCount;
1384 hoveringIdBits = other.hoveringIdBits;
1385 touchingIdBits = other.touchingIdBits;
1386
1387 for (uint32_t i = 0; i < pointerCount; i++) {
1388 pointers[i] = other.pointers[i];
1389
1390 int id = pointers[i].id;
1391 idToIndex[id] = other.idToIndex[id];
1392 }
1393}
1394
1395void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1396 float x = 0, y = 0;
1397 uint32_t count = touchingIdBits.count();
1398 if (count) {
1399 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1400 uint32_t id = idBits.clearFirstMarkedBit();
1401 const Pointer& pointer = pointerForId(id);
1402 x += pointer.x;
1403 y += pointer.y;
1404 }
1405 x /= count;
1406 y /= count;
1407 }
1408 *outX = x;
1409 *outY = y;
1410}
1411
1412
1413// --- CookedPointerData ---
1414
1415CookedPointerData::CookedPointerData() {
1416 clear();
1417}
1418
1419void CookedPointerData::clear() {
1420 pointerCount = 0;
1421 hoveringIdBits.clear();
1422 touchingIdBits.clear();
1423}
1424
1425void CookedPointerData::copyFrom(const CookedPointerData& other) {
1426 pointerCount = other.pointerCount;
1427 hoveringIdBits = other.hoveringIdBits;
1428 touchingIdBits = other.touchingIdBits;
1429
1430 for (uint32_t i = 0; i < pointerCount; i++) {
1431 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1432 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1433
1434 int id = pointerProperties[i].id;
1435 idToIndex[id] = other.idToIndex[id];
1436 }
1437}
1438
1439
Jeff Brown49754db2011-07-01 17:37:58 -07001440// --- SingleTouchMotionAccumulator ---
1441
1442SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1443 clearAbsoluteAxes();
1444}
1445
Jeff Brown65fd2512011-08-18 11:20:58 -07001446void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1447 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1448 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1449 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1450 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1451 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1452 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1453 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1454}
1455
Jeff Brown49754db2011-07-01 17:37:58 -07001456void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1457 mAbsX = 0;
1458 mAbsY = 0;
1459 mAbsPressure = 0;
1460 mAbsToolWidth = 0;
1461 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001462 mAbsTiltX = 0;
1463 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001464}
1465
1466void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1467 if (rawEvent->type == EV_ABS) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001468 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001469 case ABS_X:
1470 mAbsX = rawEvent->value;
1471 break;
1472 case ABS_Y:
1473 mAbsY = rawEvent->value;
1474 break;
1475 case ABS_PRESSURE:
1476 mAbsPressure = rawEvent->value;
1477 break;
1478 case ABS_TOOL_WIDTH:
1479 mAbsToolWidth = rawEvent->value;
1480 break;
1481 case ABS_DISTANCE:
1482 mAbsDistance = rawEvent->value;
1483 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001484 case ABS_TILT_X:
1485 mAbsTiltX = rawEvent->value;
1486 break;
1487 case ABS_TILT_Y:
1488 mAbsTiltY = rawEvent->value;
1489 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001490 }
1491 }
1492}
1493
1494
1495// --- MultiTouchMotionAccumulator ---
1496
1497MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1498 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1499}
1500
1501MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1502 delete[] mSlots;
1503}
1504
1505void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1506 mSlotCount = slotCount;
1507 mUsingSlotsProtocol = usingSlotsProtocol;
1508
1509 delete[] mSlots;
1510 mSlots = new Slot[slotCount];
1511}
1512
Jeff Brown65fd2512011-08-18 11:20:58 -07001513void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1514 // Unfortunately there is no way to read the initial contents of the slots.
1515 // So when we reset the accumulator, we must assume they are all zeroes.
1516 if (mUsingSlotsProtocol) {
1517 // Query the driver for the current slot index and use it as the initial slot
1518 // before we start reading events from the device. It is possible that the
1519 // current slot index will not be the same as it was when the first event was
1520 // written into the evdev buffer, which means the input mapper could start
1521 // out of sync with the initial state of the events in the evdev buffer.
1522 // In the extremely unlikely case that this happens, the data from
1523 // two slots will be confused until the next ABS_MT_SLOT event is received.
1524 // This can cause the touch point to "jump", but at least there will be
1525 // no stuck touches.
1526 int32_t initialSlot;
1527 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1528 ABS_MT_SLOT, &initialSlot);
1529 if (status) {
Steve Block5baa3a62011-12-20 16:23:08 +00001530 ALOGD("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown65fd2512011-08-18 11:20:58 -07001531 initialSlot = -1;
1532 }
1533 clearSlots(initialSlot);
1534 } else {
1535 clearSlots(-1);
1536 }
1537}
1538
Jeff Brown49754db2011-07-01 17:37:58 -07001539void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001540 if (mSlots) {
1541 for (size_t i = 0; i < mSlotCount; i++) {
1542 mSlots[i].clear();
1543 }
Jeff Brown49754db2011-07-01 17:37:58 -07001544 }
1545 mCurrentSlot = initialSlot;
1546}
1547
1548void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1549 if (rawEvent->type == EV_ABS) {
1550 bool newSlot = false;
1551 if (mUsingSlotsProtocol) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001552 if (rawEvent->code == ABS_MT_SLOT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001553 mCurrentSlot = rawEvent->value;
1554 newSlot = true;
1555 }
1556 } else if (mCurrentSlot < 0) {
1557 mCurrentSlot = 0;
1558 }
1559
1560 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1561#if DEBUG_POINTERS
1562 if (newSlot) {
Steve Block8564c8d2012-01-05 23:22:43 +00001563 ALOGW("MultiTouch device emitted invalid slot index %d but it "
Jeff Brown49754db2011-07-01 17:37:58 -07001564 "should be between 0 and %d; ignoring this slot.",
1565 mCurrentSlot, mSlotCount - 1);
1566 }
1567#endif
1568 } else {
1569 Slot* slot = &mSlots[mCurrentSlot];
1570
Jeff Brown49ccac52012-04-11 18:27:33 -07001571 switch (rawEvent->code) {
Jeff Brown49754db2011-07-01 17:37:58 -07001572 case ABS_MT_POSITION_X:
1573 slot->mInUse = true;
1574 slot->mAbsMTPositionX = rawEvent->value;
1575 break;
1576 case ABS_MT_POSITION_Y:
1577 slot->mInUse = true;
1578 slot->mAbsMTPositionY = rawEvent->value;
1579 break;
1580 case ABS_MT_TOUCH_MAJOR:
1581 slot->mInUse = true;
1582 slot->mAbsMTTouchMajor = rawEvent->value;
1583 break;
1584 case ABS_MT_TOUCH_MINOR:
1585 slot->mInUse = true;
1586 slot->mAbsMTTouchMinor = rawEvent->value;
1587 slot->mHaveAbsMTTouchMinor = true;
1588 break;
1589 case ABS_MT_WIDTH_MAJOR:
1590 slot->mInUse = true;
1591 slot->mAbsMTWidthMajor = rawEvent->value;
1592 break;
1593 case ABS_MT_WIDTH_MINOR:
1594 slot->mInUse = true;
1595 slot->mAbsMTWidthMinor = rawEvent->value;
1596 slot->mHaveAbsMTWidthMinor = true;
1597 break;
1598 case ABS_MT_ORIENTATION:
1599 slot->mInUse = true;
1600 slot->mAbsMTOrientation = rawEvent->value;
1601 break;
1602 case ABS_MT_TRACKING_ID:
1603 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001604 // The slot is no longer in use but it retains its previous contents,
1605 // which may be reused for subsequent touches.
1606 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001607 } else {
1608 slot->mInUse = true;
1609 slot->mAbsMTTrackingId = rawEvent->value;
1610 }
1611 break;
1612 case ABS_MT_PRESSURE:
1613 slot->mInUse = true;
1614 slot->mAbsMTPressure = rawEvent->value;
1615 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001616 case ABS_MT_DISTANCE:
1617 slot->mInUse = true;
1618 slot->mAbsMTDistance = rawEvent->value;
1619 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001620 case ABS_MT_TOOL_TYPE:
1621 slot->mInUse = true;
1622 slot->mAbsMTToolType = rawEvent->value;
1623 slot->mHaveAbsMTToolType = true;
1624 break;
1625 }
1626 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001627 } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07001628 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1629 mCurrentSlot += 1;
1630 }
1631}
1632
Jeff Brown65fd2512011-08-18 11:20:58 -07001633void MultiTouchMotionAccumulator::finishSync() {
1634 if (!mUsingSlotsProtocol) {
1635 clearSlots(-1);
1636 }
1637}
1638
Jeff Brown49754db2011-07-01 17:37:58 -07001639
1640// --- MultiTouchMotionAccumulator::Slot ---
1641
1642MultiTouchMotionAccumulator::Slot::Slot() {
1643 clear();
1644}
1645
Jeff Brown49754db2011-07-01 17:37:58 -07001646void MultiTouchMotionAccumulator::Slot::clear() {
1647 mInUse = false;
1648 mHaveAbsMTTouchMinor = false;
1649 mHaveAbsMTWidthMinor = false;
1650 mHaveAbsMTToolType = false;
1651 mAbsMTPositionX = 0;
1652 mAbsMTPositionY = 0;
1653 mAbsMTTouchMajor = 0;
1654 mAbsMTTouchMinor = 0;
1655 mAbsMTWidthMajor = 0;
1656 mAbsMTWidthMinor = 0;
1657 mAbsMTOrientation = 0;
1658 mAbsMTTrackingId = -1;
1659 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001660 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001661 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001662}
1663
1664int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1665 if (mHaveAbsMTToolType) {
1666 switch (mAbsMTToolType) {
1667 case MT_TOOL_FINGER:
1668 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1669 case MT_TOOL_PEN:
1670 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1671 }
1672 }
1673 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1674}
1675
1676
Jeff Brown6d0fec22010-07-23 21:28:06 -07001677// --- InputMapper ---
1678
1679InputMapper::InputMapper(InputDevice* device) :
1680 mDevice(device), mContext(device->getContext()) {
1681}
1682
1683InputMapper::~InputMapper() {
1684}
1685
1686void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1687 info->addSource(getSources());
1688}
1689
Jeff Brownef3d7e82010-09-30 14:33:04 -07001690void InputMapper::dump(String8& dump) {
1691}
1692
Jeff Brown65fd2512011-08-18 11:20:58 -07001693void InputMapper::configure(nsecs_t when,
1694 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001695}
1696
Jeff Brown65fd2512011-08-18 11:20:58 -07001697void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001698}
1699
Jeff Brownaa3855d2011-03-17 01:34:19 -07001700void InputMapper::timeoutExpired(nsecs_t when) {
1701}
1702
Jeff Brown6d0fec22010-07-23 21:28:06 -07001703int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1704 return AKEY_STATE_UNKNOWN;
1705}
1706
1707int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1708 return AKEY_STATE_UNKNOWN;
1709}
1710
1711int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1712 return AKEY_STATE_UNKNOWN;
1713}
1714
1715bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1716 const int32_t* keyCodes, uint8_t* outFlags) {
1717 return false;
1718}
1719
1720int32_t InputMapper::getMetaState() {
1721 return 0;
1722}
1723
Jeff Brown05dc66a2011-03-02 14:41:58 -08001724void InputMapper::fadePointer() {
1725}
1726
Jeff Brownbe1aa822011-07-27 16:04:54 -07001727status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1728 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1729}
1730
Jeff Browncb1404e2011-01-15 18:14:15 -08001731void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1732 const RawAbsoluteAxisInfo& axis, const char* name) {
1733 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001734 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1735 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001736 } else {
1737 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1738 }
1739}
1740
Jeff Brown6d0fec22010-07-23 21:28:06 -07001741
1742// --- SwitchInputMapper ---
1743
1744SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1745 InputMapper(device) {
1746}
1747
1748SwitchInputMapper::~SwitchInputMapper() {
1749}
1750
1751uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001752 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001753}
1754
1755void SwitchInputMapper::process(const RawEvent* rawEvent) {
1756 switch (rawEvent->type) {
1757 case EV_SW:
Jeff Brown49ccac52012-04-11 18:27:33 -07001758 processSwitch(rawEvent->when, rawEvent->code, rawEvent->value);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001759 break;
1760 }
1761}
1762
1763void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001764 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1765 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001766}
1767
1768int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1769 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1770}
1771
1772
1773// --- KeyboardInputMapper ---
1774
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001775KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001776 uint32_t source, int32_t keyboardType) :
1777 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001778 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001779}
1780
1781KeyboardInputMapper::~KeyboardInputMapper() {
1782}
1783
Jeff Brown6d0fec22010-07-23 21:28:06 -07001784uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001785 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001786}
1787
1788void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1789 InputMapper::populateDeviceInfo(info);
1790
1791 info->setKeyboardType(mKeyboardType);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001792 info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId()));
Jeff Brown6d0fec22010-07-23 21:28:06 -07001793}
1794
Jeff Brownef3d7e82010-09-30 14:33:04 -07001795void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001796 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1797 dumpParameters(dump);
1798 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001799 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001800 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1801 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1802 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001803}
1804
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001805
Jeff Brown65fd2512011-08-18 11:20:58 -07001806void KeyboardInputMapper::configure(nsecs_t when,
1807 const InputReaderConfiguration* config, uint32_t changes) {
1808 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001809
Jeff Brown474dcb52011-06-14 20:22:50 -07001810 if (!changes) { // first time only
1811 // Configure basic parameters.
1812 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001813 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001814
Jeff Brown65fd2512011-08-18 11:20:58 -07001815 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1816 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1817 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1818 false /*external*/, NULL, NULL, &mOrientation)) {
1819 mOrientation = DISPLAY_ORIENTATION_0;
1820 }
1821 } else {
1822 mOrientation = DISPLAY_ORIENTATION_0;
1823 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001824 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001825}
1826
1827void KeyboardInputMapper::configureParameters() {
1828 mParameters.orientationAware = false;
1829 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1830 mParameters.orientationAware);
1831
Jeff Brownbc68a592011-07-25 12:58:12 -07001832 mParameters.associatedDisplayId = -1;
1833 if (mParameters.orientationAware) {
1834 mParameters.associatedDisplayId = 0;
1835 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001836}
1837
1838void KeyboardInputMapper::dumpParameters(String8& dump) {
1839 dump.append(INDENT3 "Parameters:\n");
1840 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1841 mParameters.associatedDisplayId);
1842 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1843 toString(mParameters.orientationAware));
1844}
1845
Jeff Brown65fd2512011-08-18 11:20:58 -07001846void KeyboardInputMapper::reset(nsecs_t when) {
1847 mMetaState = AMETA_NONE;
1848 mDownTime = 0;
1849 mKeyDowns.clear();
Jeff Brown49ccac52012-04-11 18:27:33 -07001850 mCurrentHidUsage = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001851
Jeff Brownbe1aa822011-07-27 16:04:54 -07001852 resetLedState();
1853
Jeff Brown65fd2512011-08-18 11:20:58 -07001854 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001855}
1856
1857void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1858 switch (rawEvent->type) {
1859 case EV_KEY: {
Jeff Brown49ccac52012-04-11 18:27:33 -07001860 int32_t scanCode = rawEvent->code;
1861 int32_t usageCode = mCurrentHidUsage;
1862 mCurrentHidUsage = 0;
1863
Jeff Brown6d0fec22010-07-23 21:28:06 -07001864 if (isKeyboardOrGamepadKey(scanCode)) {
Jeff Brown49ccac52012-04-11 18:27:33 -07001865 int32_t keyCode;
1866 uint32_t flags;
1867 if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) {
1868 keyCode = AKEYCODE_UNKNOWN;
1869 flags = 0;
1870 }
1871 processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001872 }
1873 break;
1874 }
Jeff Brown49ccac52012-04-11 18:27:33 -07001875 case EV_MSC: {
1876 if (rawEvent->code == MSC_SCAN) {
1877 mCurrentHidUsage = rawEvent->value;
1878 }
1879 break;
1880 }
1881 case EV_SYN: {
1882 if (rawEvent->code == SYN_REPORT) {
1883 mCurrentHidUsage = 0;
1884 }
1885 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001886 }
1887}
1888
1889bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1890 return scanCode < BTN_MOUSE
1891 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001892 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001893 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001894}
1895
Jeff Brown6328cdc2010-07-29 18:18:33 -07001896void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1897 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001898
Jeff Brownbe1aa822011-07-27 16:04:54 -07001899 if (down) {
1900 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001901 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001902 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001903 }
Jeff Brownfe508922011-01-18 15:10:10 -08001904
Jeff Brownbe1aa822011-07-27 16:04:54 -07001905 // Add key down.
1906 ssize_t keyDownIndex = findKeyDown(scanCode);
1907 if (keyDownIndex >= 0) {
1908 // key repeat, be sure to use same keycode as before in case of rotation
1909 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001910 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001911 // key down
1912 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1913 && mContext->shouldDropVirtualKey(when,
1914 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001915 return;
1916 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001917
1918 mKeyDowns.push();
1919 KeyDown& keyDown = mKeyDowns.editTop();
1920 keyDown.keyCode = keyCode;
1921 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001922 }
1923
Jeff Brownbe1aa822011-07-27 16:04:54 -07001924 mDownTime = when;
1925 } else {
1926 // Remove key down.
1927 ssize_t keyDownIndex = findKeyDown(scanCode);
1928 if (keyDownIndex >= 0) {
1929 // key up, be sure to use same keycode as before in case of rotation
1930 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1931 mKeyDowns.removeAt(size_t(keyDownIndex));
1932 } else {
1933 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00001934 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07001935 "keyCode=%d, scanCode=%d",
1936 getDeviceName().string(), keyCode, scanCode);
1937 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001938 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001939 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001940
Jeff Brownbe1aa822011-07-27 16:04:54 -07001941 bool metaStateChanged = false;
1942 int32_t oldMetaState = mMetaState;
1943 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1944 if (oldMetaState != newMetaState) {
1945 mMetaState = newMetaState;
1946 metaStateChanged = true;
1947 updateLedState(false);
1948 }
1949
1950 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001951
Jeff Brown56194eb2011-03-02 19:23:13 -08001952 // Key down on external an keyboard should wake the device.
1953 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1954 // For internal keyboards, the key layout file should specify the policy flags for
1955 // each wake key individually.
1956 // TODO: Use the input device configuration to control this behavior more finely.
1957 if (down && getDevice()->isExternal()
1958 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1959 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1960 }
1961
Jeff Brown6328cdc2010-07-29 18:18:33 -07001962 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001963 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001964 }
1965
Jeff Brown05dc66a2011-03-02 14:41:58 -08001966 if (down && !isMetaKey(keyCode)) {
1967 getContext()->fadePointer();
1968 }
1969
Jeff Brownbe1aa822011-07-27 16:04:54 -07001970 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001971 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1972 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001973 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001974}
1975
Jeff Brownbe1aa822011-07-27 16:04:54 -07001976ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1977 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001978 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001979 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001980 return i;
1981 }
1982 }
1983 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001984}
1985
Jeff Brown6d0fec22010-07-23 21:28:06 -07001986int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1987 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1988}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001989
Jeff Brown6d0fec22010-07-23 21:28:06 -07001990int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1991 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1992}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001993
Jeff Brown6d0fec22010-07-23 21:28:06 -07001994bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1995 const int32_t* keyCodes, uint8_t* outFlags) {
1996 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1997}
1998
1999int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002000 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002001}
2002
Jeff Brownbe1aa822011-07-27 16:04:54 -07002003void KeyboardInputMapper::resetLedState() {
2004 initializeLedState(mCapsLockLedState, LED_CAPSL);
2005 initializeLedState(mNumLockLedState, LED_NUML);
2006 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002007
Jeff Brownbe1aa822011-07-27 16:04:54 -07002008 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08002009}
2010
Jeff Brownbe1aa822011-07-27 16:04:54 -07002011void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08002012 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
2013 ledState.on = false;
2014}
2015
Jeff Brownbe1aa822011-07-27 16:04:54 -07002016void KeyboardInputMapper::updateLedState(bool reset) {
2017 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002018 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002019 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002020 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002021 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002022 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002023}
2024
Jeff Brownbe1aa822011-07-27 16:04:54 -07002025void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002026 int32_t led, int32_t modifier, bool reset) {
2027 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002028 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002029 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002030 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2031 ledState.on = desiredState;
2032 }
2033 }
2034}
2035
Jeff Brown6d0fec22010-07-23 21:28:06 -07002036
Jeff Brown83c09682010-12-23 17:50:18 -08002037// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002038
Jeff Brown83c09682010-12-23 17:50:18 -08002039CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002040 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002041}
2042
Jeff Brown83c09682010-12-23 17:50:18 -08002043CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002044}
2045
Jeff Brown83c09682010-12-23 17:50:18 -08002046uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002047 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002048}
2049
Jeff Brown83c09682010-12-23 17:50:18 -08002050void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002051 InputMapper::populateDeviceInfo(info);
2052
Jeff Brown83c09682010-12-23 17:50:18 -08002053 if (mParameters.mode == Parameters::MODE_POINTER) {
2054 float minX, minY, maxX, maxY;
2055 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002056 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2057 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002058 }
2059 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002060 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2061 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002062 }
Jeff Brownefd32662011-03-08 15:13:06 -08002063 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002064
Jeff Brown65fd2512011-08-18 11:20:58 -07002065 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002066 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002067 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002068 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002069 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002070 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002071}
2072
Jeff Brown83c09682010-12-23 17:50:18 -08002073void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002074 dump.append(INDENT2 "Cursor Input Mapper:\n");
2075 dumpParameters(dump);
2076 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2077 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2078 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2079 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2080 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002081 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002082 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002083 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002084 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2085 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002086 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002087 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2088 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2089 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002090}
2091
Jeff Brown65fd2512011-08-18 11:20:58 -07002092void CursorInputMapper::configure(nsecs_t when,
2093 const InputReaderConfiguration* config, uint32_t changes) {
2094 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002095
Jeff Brown474dcb52011-06-14 20:22:50 -07002096 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002097 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002098
Jeff Brown474dcb52011-06-14 20:22:50 -07002099 // Configure basic parameters.
2100 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002101
Jeff Brown474dcb52011-06-14 20:22:50 -07002102 // Configure device mode.
2103 switch (mParameters.mode) {
2104 case Parameters::MODE_POINTER:
2105 mSource = AINPUT_SOURCE_MOUSE;
2106 mXPrecision = 1.0f;
2107 mYPrecision = 1.0f;
2108 mXScale = 1.0f;
2109 mYScale = 1.0f;
2110 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2111 break;
2112 case Parameters::MODE_NAVIGATION:
2113 mSource = AINPUT_SOURCE_TRACKBALL;
2114 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2115 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2116 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2117 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2118 break;
2119 }
2120
2121 mVWheelScale = 1.0f;
2122 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002123 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002124
Jeff Brown474dcb52011-06-14 20:22:50 -07002125 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2126 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2127 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2128 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2129 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002130
2131 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2132 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2133 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2134 false /*external*/, NULL, NULL, &mOrientation)) {
2135 mOrientation = DISPLAY_ORIENTATION_0;
2136 }
2137 } else {
2138 mOrientation = DISPLAY_ORIENTATION_0;
2139 }
2140 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002141}
2142
Jeff Brown83c09682010-12-23 17:50:18 -08002143void CursorInputMapper::configureParameters() {
2144 mParameters.mode = Parameters::MODE_POINTER;
2145 String8 cursorModeString;
2146 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2147 if (cursorModeString == "navigation") {
2148 mParameters.mode = Parameters::MODE_NAVIGATION;
2149 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002150 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002151 }
2152 }
2153
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002154 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002155 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002156 mParameters.orientationAware);
2157
Jeff Brownbc68a592011-07-25 12:58:12 -07002158 mParameters.associatedDisplayId = -1;
2159 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2160 mParameters.associatedDisplayId = 0;
2161 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002162}
2163
Jeff Brown83c09682010-12-23 17:50:18 -08002164void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002165 dump.append(INDENT3 "Parameters:\n");
2166 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2167 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002168
2169 switch (mParameters.mode) {
2170 case Parameters::MODE_POINTER:
2171 dump.append(INDENT4 "Mode: pointer\n");
2172 break;
2173 case Parameters::MODE_NAVIGATION:
2174 dump.append(INDENT4 "Mode: navigation\n");
2175 break;
2176 default:
Steve Blockec193de2012-01-09 18:35:44 +00002177 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002178 }
2179
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002180 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2181 toString(mParameters.orientationAware));
2182}
2183
Jeff Brown65fd2512011-08-18 11:20:58 -07002184void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002185 mButtonState = 0;
2186 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002187
Jeff Brownbe1aa822011-07-27 16:04:54 -07002188 mPointerVelocityControl.reset();
2189 mWheelXVelocityControl.reset();
2190 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002191
Jeff Brown65fd2512011-08-18 11:20:58 -07002192 mCursorButtonAccumulator.reset(getDevice());
2193 mCursorMotionAccumulator.reset(getDevice());
2194 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002195
Jeff Brown65fd2512011-08-18 11:20:58 -07002196 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002197}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002198
Jeff Brown83c09682010-12-23 17:50:18 -08002199void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002200 mCursorButtonAccumulator.process(rawEvent);
2201 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002202 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002203
Jeff Brown49ccac52012-04-11 18:27:33 -07002204 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown49754db2011-07-01 17:37:58 -07002205 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002206 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002207}
2208
Jeff Brown83c09682010-12-23 17:50:18 -08002209void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002210 int32_t lastButtonState = mButtonState;
2211 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2212 mButtonState = currentButtonState;
2213
2214 bool wasDown = isPointerDown(lastButtonState);
2215 bool down = isPointerDown(currentButtonState);
2216 bool downChanged;
2217 if (!wasDown && down) {
2218 mDownTime = when;
2219 downChanged = true;
2220 } else if (wasDown && !down) {
2221 downChanged = true;
2222 } else {
2223 downChanged = false;
2224 }
2225 nsecs_t downTime = mDownTime;
2226 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002227 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002228
2229 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2230 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2231 bool moved = deltaX != 0 || deltaY != 0;
2232
Jeff Brown65fd2512011-08-18 11:20:58 -07002233 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002234 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2235 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002236 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002237 }
2238
Jeff Brown65fd2512011-08-18 11:20:58 -07002239 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002240 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002241 pointerProperties.clear();
2242 pointerProperties.id = 0;
2243 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2244
Jeff Brown6328cdc2010-07-29 18:18:33 -07002245 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002246 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002247
Jeff Brown65fd2512011-08-18 11:20:58 -07002248 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2249 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002250 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002251
Jeff Brownbe1aa822011-07-27 16:04:54 -07002252 mWheelYVelocityControl.move(when, NULL, &vscroll);
2253 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002254
Jeff Brownbe1aa822011-07-27 16:04:54 -07002255 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002256
Jeff Brownbe1aa822011-07-27 16:04:54 -07002257 if (mPointerController != NULL) {
2258 if (moved || scrolled || buttonsChanged) {
2259 mPointerController->setPresentation(
2260 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002261
Jeff Brownbe1aa822011-07-27 16:04:54 -07002262 if (moved) {
2263 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002264 }
2265
Jeff Brownbe1aa822011-07-27 16:04:54 -07002266 if (buttonsChanged) {
2267 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002268 }
Jeff Brownefd32662011-03-08 15:13:06 -08002269
Jeff Brownbe1aa822011-07-27 16:04:54 -07002270 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002271 }
2272
Jeff Brownbe1aa822011-07-27 16:04:54 -07002273 float x, y;
2274 mPointerController->getPosition(&x, &y);
2275 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2276 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2277 } else {
2278 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2279 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2280 }
2281
2282 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002283
Jeff Brown56194eb2011-03-02 19:23:13 -08002284 // Moving an external trackball or mouse should wake the device.
2285 // We don't do this for internal cursor devices to prevent them from waking up
2286 // the device in your pocket.
2287 // TODO: Use the input device configuration to control this behavior more finely.
2288 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002289 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002290 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2291 }
2292
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002293 // Synthesize key down from buttons if needed.
2294 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2295 policyFlags, lastButtonState, currentButtonState);
2296
2297 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002298 if (downChanged || moved || scrolled || buttonsChanged) {
2299 int32_t metaState = mContext->getGlobalMetaState();
2300 int32_t motionEventAction;
2301 if (downChanged) {
2302 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2303 } else if (down || mPointerController == NULL) {
2304 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2305 } else {
2306 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2307 }
Jeff Brownb6997262010-10-08 22:31:17 -07002308
Jeff Brownbe1aa822011-07-27 16:04:54 -07002309 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2310 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002311 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002312 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002313
Jeff Brownbe1aa822011-07-27 16:04:54 -07002314 // Send hover move after UP to tell the application that the mouse is hovering now.
2315 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2316 && mPointerController != NULL) {
2317 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2318 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2319 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2320 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2321 getListener()->notifyMotion(&hoverArgs);
2322 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002323
Jeff Brownbe1aa822011-07-27 16:04:54 -07002324 // Send scroll events.
2325 if (scrolled) {
2326 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2327 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2328
2329 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2330 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2331 AMOTION_EVENT_EDGE_FLAG_NONE,
2332 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2333 getListener()->notifyMotion(&scrollArgs);
2334 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002335 }
Jeff Browna032cc02011-03-07 16:56:21 -08002336
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002337 // Synthesize key up from buttons if needed.
2338 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2339 policyFlags, lastButtonState, currentButtonState);
2340
Jeff Brown65fd2512011-08-18 11:20:58 -07002341 mCursorMotionAccumulator.finishSync();
2342 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002343}
2344
Jeff Brown83c09682010-12-23 17:50:18 -08002345int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002346 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2347 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2348 } else {
2349 return AKEY_STATE_UNKNOWN;
2350 }
2351}
2352
Jeff Brown05dc66a2011-03-02 14:41:58 -08002353void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002354 if (mPointerController != NULL) {
2355 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2356 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002357}
2358
Jeff Brown6d0fec22010-07-23 21:28:06 -07002359
2360// --- TouchInputMapper ---
2361
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002362TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002363 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002364 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002365 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002366}
2367
2368TouchInputMapper::~TouchInputMapper() {
2369}
2370
2371uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002372 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002373}
2374
2375void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2376 InputMapper::populateDeviceInfo(info);
2377
Jeff Brown65fd2512011-08-18 11:20:58 -07002378 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2379 info->addMotionRange(mOrientedRanges.x);
2380 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002381 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002382
Jeff Brown65fd2512011-08-18 11:20:58 -07002383 if (mOrientedRanges.haveSize) {
2384 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002385 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002386
2387 if (mOrientedRanges.haveTouchSize) {
2388 info->addMotionRange(mOrientedRanges.touchMajor);
2389 info->addMotionRange(mOrientedRanges.touchMinor);
2390 }
2391
2392 if (mOrientedRanges.haveToolSize) {
2393 info->addMotionRange(mOrientedRanges.toolMajor);
2394 info->addMotionRange(mOrientedRanges.toolMinor);
2395 }
2396
2397 if (mOrientedRanges.haveOrientation) {
2398 info->addMotionRange(mOrientedRanges.orientation);
2399 }
2400
2401 if (mOrientedRanges.haveDistance) {
2402 info->addMotionRange(mOrientedRanges.distance);
2403 }
2404
2405 if (mOrientedRanges.haveTilt) {
2406 info->addMotionRange(mOrientedRanges.tilt);
2407 }
2408
2409 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2410 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2411 }
2412 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2413 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2414 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002415 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002416}
2417
Jeff Brownef3d7e82010-09-30 14:33:04 -07002418void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002419 dump.append(INDENT2 "Touch Input Mapper:\n");
2420 dumpParameters(dump);
2421 dumpVirtualKeys(dump);
2422 dumpRawPointerAxes(dump);
2423 dumpCalibration(dump);
2424 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002425
Jeff Brownbe1aa822011-07-27 16:04:54 -07002426 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2427 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2428 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2429 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2430 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2431 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002432 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2433 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002434 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002435 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2436 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002437 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2438 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2439 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2440 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2441 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002442
Jeff Brownbe1aa822011-07-27 16:04:54 -07002443 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002444
Jeff Brownbe1aa822011-07-27 16:04:54 -07002445 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2446 mLastRawPointerData.pointerCount);
2447 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2448 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2449 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2450 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002451 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2452 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002453 pointer.id, pointer.x, pointer.y, pointer.pressure,
2454 pointer.touchMajor, pointer.touchMinor,
2455 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002456 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002457 pointer.toolType, toString(pointer.isHovering));
2458 }
2459
2460 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2461 mLastCookedPointerData.pointerCount);
2462 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2463 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2464 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2465 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2466 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002467 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2468 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002469 pointerProperties.id,
2470 pointerCoords.getX(),
2471 pointerCoords.getY(),
2472 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2473 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2474 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2475 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2476 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2477 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002478 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002479 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2480 pointerProperties.toolType,
2481 toString(mLastCookedPointerData.isHovering(i)));
2482 }
2483
Jeff Brown65fd2512011-08-18 11:20:58 -07002484 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002485 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2486 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002487 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002488 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002489 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002490 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002491 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002492 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002493 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002494 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2495 mPointerGestureMaxSwipeWidth);
2496 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002497}
2498
Jeff Brown65fd2512011-08-18 11:20:58 -07002499void TouchInputMapper::configure(nsecs_t when,
2500 const InputReaderConfiguration* config, uint32_t changes) {
2501 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002502
Jeff Brown474dcb52011-06-14 20:22:50 -07002503 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002504
Jeff Brown474dcb52011-06-14 20:22:50 -07002505 if (!changes) { // first time only
2506 // Configure basic parameters.
2507 configureParameters();
2508
Jeff Brown65fd2512011-08-18 11:20:58 -07002509 // Configure common accumulators.
2510 mCursorScrollAccumulator.configure(getDevice());
2511 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002512
2513 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002514 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002515
2516 // Prepare input device calibration.
2517 parseCalibration();
2518 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002519 }
2520
Jeff Brown474dcb52011-06-14 20:22:50 -07002521 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002522 // Update pointer speed.
2523 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2524 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2525 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002526 }
Jeff Brown8d608662010-08-30 03:02:23 -07002527
Jeff Brown65fd2512011-08-18 11:20:58 -07002528 bool resetNeeded = false;
2529 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002530 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2531 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002532 // Configure device sources, surface dimensions, orientation and
2533 // scaling factors.
2534 configureSurface(when, &resetNeeded);
2535 }
2536
2537 if (changes && resetNeeded) {
2538 // Send reset, unless this is the first time the device has been configured,
2539 // in which case the reader will call reset itself after all mappers are ready.
2540 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002541 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002542}
2543
Jeff Brown8d608662010-08-30 03:02:23 -07002544void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002545 // Use the pointer presentation mode for devices that do not support distinct
2546 // multitouch. The spot-based presentation relies on being able to accurately
2547 // locate two or more fingers on the touch pad.
2548 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2549 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002550
Jeff Brown538881e2011-05-25 18:23:38 -07002551 String8 gestureModeString;
2552 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2553 gestureModeString)) {
2554 if (gestureModeString == "pointer") {
2555 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2556 } else if (gestureModeString == "spots") {
2557 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2558 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002559 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002560 }
2561 }
2562
Jeff Browndeffe072011-08-26 18:38:46 -07002563 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2564 // The device is a touch screen.
2565 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2566 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2567 // The device is a pointing device like a track pad.
2568 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2569 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002570 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2571 // The device is a cursor device with a touch pad attached.
2572 // By default don't use the touch pad to move the pointer.
2573 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2574 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002575 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002576 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2577 }
2578
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002579 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002580 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2581 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002582 if (deviceTypeString == "touchScreen") {
2583 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002584 } else if (deviceTypeString == "touchPad") {
2585 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002586 } else if (deviceTypeString == "pointer") {
2587 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002588 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002589 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002590 }
2591 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002592
Jeff Brownefd32662011-03-08 15:13:06 -08002593 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002594 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2595 mParameters.orientationAware);
2596
Jeff Brownbc68a592011-07-25 12:58:12 -07002597 mParameters.associatedDisplayId = -1;
2598 mParameters.associatedDisplayIsExternal = false;
2599 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002600 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002601 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2602 mParameters.associatedDisplayIsExternal =
2603 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2604 && getDevice()->isExternal();
2605 mParameters.associatedDisplayId = 0;
2606 }
Jeff Brown8d608662010-08-30 03:02:23 -07002607}
2608
Jeff Brownef3d7e82010-09-30 14:33:04 -07002609void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002610 dump.append(INDENT3 "Parameters:\n");
2611
Jeff Brown538881e2011-05-25 18:23:38 -07002612 switch (mParameters.gestureMode) {
2613 case Parameters::GESTURE_MODE_POINTER:
2614 dump.append(INDENT4 "GestureMode: pointer\n");
2615 break;
2616 case Parameters::GESTURE_MODE_SPOTS:
2617 dump.append(INDENT4 "GestureMode: spots\n");
2618 break;
2619 default:
2620 assert(false);
2621 }
2622
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002623 switch (mParameters.deviceType) {
2624 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2625 dump.append(INDENT4 "DeviceType: touchScreen\n");
2626 break;
2627 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2628 dump.append(INDENT4 "DeviceType: touchPad\n");
2629 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002630 case Parameters::DEVICE_TYPE_POINTER:
2631 dump.append(INDENT4 "DeviceType: pointer\n");
2632 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002633 default:
Steve Blockec193de2012-01-09 18:35:44 +00002634 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002635 }
2636
Jeff Brown65fd2512011-08-18 11:20:58 -07002637 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2638 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002639 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2640 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002641}
2642
Jeff Brownbe1aa822011-07-27 16:04:54 -07002643void TouchInputMapper::configureRawPointerAxes() {
2644 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002645}
2646
Jeff Brownbe1aa822011-07-27 16:04:54 -07002647void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2648 dump.append(INDENT3 "Raw Touch Axes:\n");
2649 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2650 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2651 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2652 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2653 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2654 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2655 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2656 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2657 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002658 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2659 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002660 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2661 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002662}
2663
Jeff Brown65fd2512011-08-18 11:20:58 -07002664void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2665 int32_t oldDeviceMode = mDeviceMode;
2666
2667 // Determine device mode.
2668 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2669 && mConfig.pointerGesturesEnabled) {
2670 mSource = AINPUT_SOURCE_MOUSE;
2671 mDeviceMode = DEVICE_MODE_POINTER;
2672 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2673 && mParameters.associatedDisplayId >= 0) {
2674 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2675 mDeviceMode = DEVICE_MODE_DIRECT;
2676 } else {
2677 mSource = AINPUT_SOURCE_TOUCHPAD;
2678 mDeviceMode = DEVICE_MODE_UNSCALED;
2679 }
2680
Jeff Brown9626b142011-03-03 02:09:54 -08002681 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002682 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002683 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002684 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002685 mDeviceMode = DEVICE_MODE_DISABLED;
2686 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002687 }
2688
Jeff Brown65fd2512011-08-18 11:20:58 -07002689 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002690 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002691 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002692 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002693 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2694 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002695 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002696 "display %d. The device will be inoperable until the display size "
2697 "becomes available.",
2698 getDeviceName().string(), mParameters.associatedDisplayId);
2699 mDeviceMode = DEVICE_MODE_DISABLED;
2700 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002701 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002702 }
2703
Jeff Brown65fd2512011-08-18 11:20:58 -07002704 // Configure dimensions.
2705 int32_t width, height, orientation;
2706 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2707 width = mAssociatedDisplayWidth;
2708 height = mAssociatedDisplayHeight;
2709 orientation = mParameters.orientationAware ?
2710 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2711 } else {
2712 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2713 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2714 orientation = DISPLAY_ORIENTATION_0;
2715 }
2716
2717 // If moving between pointer modes, need to reset some state.
2718 bool deviceModeChanged;
2719 if (mDeviceMode != oldDeviceMode) {
2720 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002721 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002722 }
2723
Jeff Browndaf4a122011-08-26 17:14:14 -07002724 // Create pointer controller if needed.
2725 if (mDeviceMode == DEVICE_MODE_POINTER ||
2726 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2727 if (mPointerController == NULL) {
2728 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2729 }
2730 } else {
2731 mPointerController.clear();
2732 }
2733
Jeff Brownbe1aa822011-07-27 16:04:54 -07002734 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002735 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002736 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002737 }
2738
Jeff Brownbe1aa822011-07-27 16:04:54 -07002739 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002740 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002741 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002742 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002743
Jeff Brownbe1aa822011-07-27 16:04:54 -07002744 mSurfaceWidth = width;
2745 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002746
Jeff Brown8d608662010-08-30 03:02:23 -07002747 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002748 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2749 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2750 mXPrecision = 1.0f / mXScale;
2751 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002752
Jeff Brownbe1aa822011-07-27 16:04:54 -07002753 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002754 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002755 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002756 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002757
Jeff Brownbe1aa822011-07-27 16:04:54 -07002758 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002759
Jeff Brown8d608662010-08-30 03:02:23 -07002760 // Scale factor for terms that are not oriented in a particular axis.
2761 // If the pixels are square then xScale == yScale otherwise we fake it
2762 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002763 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002764
Jeff Brown8d608662010-08-30 03:02:23 -07002765 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002766 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002767
Jeff Browna1f89ce2011-08-11 00:05:01 -07002768 // Size factors.
2769 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2770 if (mRawPointerAxes.touchMajor.valid
2771 && mRawPointerAxes.touchMajor.maxValue != 0) {
2772 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2773 } else if (mRawPointerAxes.toolMajor.valid
2774 && mRawPointerAxes.toolMajor.maxValue != 0) {
2775 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2776 } else {
2777 mSizeScale = 0.0f;
2778 }
2779
Jeff Brownbe1aa822011-07-27 16:04:54 -07002780 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002781 mOrientedRanges.haveToolSize = true;
2782 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002783
Jeff Brownbe1aa822011-07-27 16:04:54 -07002784 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002785 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002786 mOrientedRanges.touchMajor.min = 0;
2787 mOrientedRanges.touchMajor.max = diagonalSize;
2788 mOrientedRanges.touchMajor.flat = 0;
2789 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002790
Jeff Brownbe1aa822011-07-27 16:04:54 -07002791 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2792 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002793
Jeff Brownbe1aa822011-07-27 16:04:54 -07002794 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002795 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002796 mOrientedRanges.toolMajor.min = 0;
2797 mOrientedRanges.toolMajor.max = diagonalSize;
2798 mOrientedRanges.toolMajor.flat = 0;
2799 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002800
Jeff Brownbe1aa822011-07-27 16:04:54 -07002801 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2802 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002803
2804 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002805 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002806 mOrientedRanges.size.min = 0;
2807 mOrientedRanges.size.max = 1.0;
2808 mOrientedRanges.size.flat = 0;
2809 mOrientedRanges.size.fuzz = 0;
2810 } else {
2811 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002812 }
2813
2814 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002815 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002816 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2817 || mCalibration.pressureCalibration
2818 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2819 if (mCalibration.havePressureScale) {
2820 mPressureScale = mCalibration.pressureScale;
2821 } else if (mRawPointerAxes.pressure.valid
2822 && mRawPointerAxes.pressure.maxValue != 0) {
2823 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002824 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002825 }
Jeff Brown8d608662010-08-30 03:02:23 -07002826
Jeff Brown65fd2512011-08-18 11:20:58 -07002827 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2828 mOrientedRanges.pressure.source = mSource;
2829 mOrientedRanges.pressure.min = 0;
2830 mOrientedRanges.pressure.max = 1.0;
2831 mOrientedRanges.pressure.flat = 0;
2832 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002833
Jeff Brown65fd2512011-08-18 11:20:58 -07002834 // Tilt
2835 mTiltXCenter = 0;
2836 mTiltXScale = 0;
2837 mTiltYCenter = 0;
2838 mTiltYScale = 0;
2839 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2840 if (mHaveTilt) {
2841 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2842 mRawPointerAxes.tiltX.maxValue);
2843 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2844 mRawPointerAxes.tiltY.maxValue);
2845 mTiltXScale = M_PI / 180;
2846 mTiltYScale = M_PI / 180;
2847
2848 mOrientedRanges.haveTilt = true;
2849
2850 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2851 mOrientedRanges.tilt.source = mSource;
2852 mOrientedRanges.tilt.min = 0;
2853 mOrientedRanges.tilt.max = M_PI_2;
2854 mOrientedRanges.tilt.flat = 0;
2855 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002856 }
2857
Jeff Brown8d608662010-08-30 03:02:23 -07002858 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002859 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002860 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002861 if (mHaveTilt) {
2862 mOrientedRanges.haveOrientation = true;
2863
2864 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2865 mOrientedRanges.orientation.source = mSource;
2866 mOrientedRanges.orientation.min = -M_PI;
2867 mOrientedRanges.orientation.max = M_PI;
2868 mOrientedRanges.orientation.flat = 0;
2869 mOrientedRanges.orientation.fuzz = 0;
2870 } else if (mCalibration.orientationCalibration !=
2871 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002872 if (mCalibration.orientationCalibration
2873 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002874 if (mRawPointerAxes.orientation.valid) {
2875 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2876 mRawPointerAxes.orientation.maxValue);
2877 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2878 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002879 }
2880 }
2881
Jeff Brownbe1aa822011-07-27 16:04:54 -07002882 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002883
Jeff Brownbe1aa822011-07-27 16:04:54 -07002884 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002885 mOrientedRanges.orientation.source = mSource;
2886 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002887 mOrientedRanges.orientation.max = M_PI_2;
2888 mOrientedRanges.orientation.flat = 0;
2889 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002890 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002891
2892 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002893 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002894 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2895 if (mCalibration.distanceCalibration
2896 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2897 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002898 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002899 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002900 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002901 }
2902 }
2903
Jeff Brownbe1aa822011-07-27 16:04:54 -07002904 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002905
Jeff Brownbe1aa822011-07-27 16:04:54 -07002906 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002907 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002908 mOrientedRanges.distance.min =
2909 mRawPointerAxes.distance.minValue * mDistanceScale;
2910 mOrientedRanges.distance.max =
2911 mRawPointerAxes.distance.minValue * mDistanceScale;
2912 mOrientedRanges.distance.flat = 0;
2913 mOrientedRanges.distance.fuzz =
2914 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002915 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002916 }
2917
Jeff Brown65fd2512011-08-18 11:20:58 -07002918 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002919 // Compute oriented surface dimensions, precision, scales and ranges.
2920 // Note that the maximum value reported is an inclusive maximum value so it is one
2921 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002922 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002923 case DISPLAY_ORIENTATION_90:
2924 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002925 mOrientedSurfaceWidth = mSurfaceHeight;
2926 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002927
Jeff Brownbe1aa822011-07-27 16:04:54 -07002928 mOrientedXPrecision = mYPrecision;
2929 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002930
Jeff Brownbe1aa822011-07-27 16:04:54 -07002931 mOrientedRanges.x.min = 0;
2932 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2933 * mYScale;
2934 mOrientedRanges.x.flat = 0;
2935 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002936
Jeff Brownbe1aa822011-07-27 16:04:54 -07002937 mOrientedRanges.y.min = 0;
2938 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2939 * mXScale;
2940 mOrientedRanges.y.flat = 0;
2941 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002942 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002943
Jeff Brown6d0fec22010-07-23 21:28:06 -07002944 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002945 mOrientedSurfaceWidth = mSurfaceWidth;
2946 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002947
Jeff Brownbe1aa822011-07-27 16:04:54 -07002948 mOrientedXPrecision = mXPrecision;
2949 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002950
Jeff Brownbe1aa822011-07-27 16:04:54 -07002951 mOrientedRanges.x.min = 0;
2952 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2953 * mXScale;
2954 mOrientedRanges.x.flat = 0;
2955 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002956
Jeff Brownbe1aa822011-07-27 16:04:54 -07002957 mOrientedRanges.y.min = 0;
2958 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2959 * mYScale;
2960 mOrientedRanges.y.flat = 0;
2961 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002962 break;
2963 }
Jeff Brownace13b12011-03-09 17:39:48 -08002964
2965 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002966 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002967 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2968 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002969 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002970 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2971 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002972
Jeff Brown2352b972011-04-12 22:39:53 -07002973 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002974 // given area relative to the diagonal size of the display when no acceleration
2975 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002976 // Assume that the touch pad has a square aspect ratio such that movements in
2977 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002978 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002979 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002980 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002981
2982 // Scale zooms to cover a smaller range of the display than movements do.
2983 // This value determines the area around the pointer that is affected by freeform
2984 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002985 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002986 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002987 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002988
Jeff Brown2352b972011-04-12 22:39:53 -07002989 // Max width between pointers to detect a swipe gesture is more than some fraction
2990 // of the diagonal axis of the touch pad. Touches that are wider than this are
2991 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002992 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002993 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002994 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002995
Jeff Brown65fd2512011-08-18 11:20:58 -07002996 // Abort current pointer usages because the state has changed.
2997 abortPointerUsage(when, 0 /*policyFlags*/);
2998
2999 // Inform the dispatcher about the changes.
3000 *outResetNeeded = true;
3001 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003002}
3003
Jeff Brownbe1aa822011-07-27 16:04:54 -07003004void TouchInputMapper::dumpSurface(String8& dump) {
3005 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
3006 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
3007 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07003008}
3009
Jeff Brownbe1aa822011-07-27 16:04:54 -07003010void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07003011 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08003012 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003013
Jeff Brownbe1aa822011-07-27 16:04:54 -07003014 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003015
Jeff Brown6328cdc2010-07-29 18:18:33 -07003016 if (virtualKeyDefinitions.size() == 0) {
3017 return;
3018 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003019
Jeff Brownbe1aa822011-07-27 16:04:54 -07003020 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003021
Jeff Brownbe1aa822011-07-27 16:04:54 -07003022 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3023 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3024 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3025 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003026
3027 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003028 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003029 virtualKeyDefinitions[i];
3030
Jeff Brownbe1aa822011-07-27 16:04:54 -07003031 mVirtualKeys.add();
3032 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003033
3034 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3035 int32_t keyCode;
3036 uint32_t flags;
Jeff Brown49ccac52012-04-11 18:27:33 -07003037 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003038 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003039 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003040 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003041 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003042 }
3043
Jeff Brown6328cdc2010-07-29 18:18:33 -07003044 virtualKey.keyCode = keyCode;
3045 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003046
Jeff Brown6328cdc2010-07-29 18:18:33 -07003047 // convert the key definition's display coordinates into touch coordinates for a hit box
3048 int32_t halfWidth = virtualKeyDefinition.width / 2;
3049 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003050
Jeff Brown6328cdc2010-07-29 18:18:33 -07003051 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003052 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003053 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003054 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003055 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003056 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003057 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003058 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003059 }
3060}
3061
Jeff Brownbe1aa822011-07-27 16:04:54 -07003062void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3063 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003064 dump.append(INDENT3 "Virtual Keys:\n");
3065
Jeff Brownbe1aa822011-07-27 16:04:54 -07003066 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3067 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003068 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3069 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3070 i, virtualKey.scanCode, virtualKey.keyCode,
3071 virtualKey.hitLeft, virtualKey.hitRight,
3072 virtualKey.hitTop, virtualKey.hitBottom);
3073 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003074 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003075}
3076
Jeff Brown8d608662010-08-30 03:02:23 -07003077void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003078 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003079 Calibration& out = mCalibration;
3080
Jeff Browna1f89ce2011-08-11 00:05:01 -07003081 // Size
3082 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3083 String8 sizeCalibrationString;
3084 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3085 if (sizeCalibrationString == "none") {
3086 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3087 } else if (sizeCalibrationString == "geometric") {
3088 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3089 } else if (sizeCalibrationString == "diameter") {
3090 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3091 } else if (sizeCalibrationString == "area") {
3092 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3093 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003094 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003095 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003096 }
3097 }
3098
Jeff Browna1f89ce2011-08-11 00:05:01 -07003099 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3100 out.sizeScale);
3101 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3102 out.sizeBias);
3103 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3104 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003105
3106 // Pressure
3107 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3108 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003109 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003110 if (pressureCalibrationString == "none") {
3111 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3112 } else if (pressureCalibrationString == "physical") {
3113 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3114 } else if (pressureCalibrationString == "amplitude") {
3115 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3116 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003117 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003118 pressureCalibrationString.string());
3119 }
3120 }
3121
Jeff Brown8d608662010-08-30 03:02:23 -07003122 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3123 out.pressureScale);
3124
Jeff Brown8d608662010-08-30 03:02:23 -07003125 // Orientation
3126 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3127 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003128 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003129 if (orientationCalibrationString == "none") {
3130 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3131 } else if (orientationCalibrationString == "interpolated") {
3132 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003133 } else if (orientationCalibrationString == "vector") {
3134 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003135 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003136 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003137 orientationCalibrationString.string());
3138 }
3139 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003140
3141 // Distance
3142 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3143 String8 distanceCalibrationString;
3144 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3145 if (distanceCalibrationString == "none") {
3146 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3147 } else if (distanceCalibrationString == "scaled") {
3148 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3149 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003150 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003151 distanceCalibrationString.string());
3152 }
3153 }
3154
3155 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3156 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003157}
3158
3159void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003160 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003161 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3162 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3163 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003164 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003165 } else {
3166 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3167 }
Jeff Brown8d608662010-08-30 03:02:23 -07003168
Jeff Browna1f89ce2011-08-11 00:05:01 -07003169 // Pressure
3170 if (mRawPointerAxes.pressure.valid) {
3171 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3172 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3173 }
3174 } else {
3175 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003176 }
3177
3178 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003179 if (mRawPointerAxes.orientation.valid) {
3180 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003181 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003182 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003183 } else {
3184 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003185 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003186
3187 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003188 if (mRawPointerAxes.distance.valid) {
3189 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003190 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003191 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003192 } else {
3193 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003194 }
Jeff Brown8d608662010-08-30 03:02:23 -07003195}
3196
Jeff Brownef3d7e82010-09-30 14:33:04 -07003197void TouchInputMapper::dumpCalibration(String8& dump) {
3198 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003199
Jeff Browna1f89ce2011-08-11 00:05:01 -07003200 // Size
3201 switch (mCalibration.sizeCalibration) {
3202 case Calibration::SIZE_CALIBRATION_NONE:
3203 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003204 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003205 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3206 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003207 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003208 case Calibration::SIZE_CALIBRATION_DIAMETER:
3209 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3210 break;
3211 case Calibration::SIZE_CALIBRATION_AREA:
3212 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003213 break;
3214 default:
Steve Blockec193de2012-01-09 18:35:44 +00003215 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003216 }
3217
Jeff Browna1f89ce2011-08-11 00:05:01 -07003218 if (mCalibration.haveSizeScale) {
3219 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3220 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003221 }
3222
Jeff Browna1f89ce2011-08-11 00:05:01 -07003223 if (mCalibration.haveSizeBias) {
3224 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3225 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003226 }
3227
Jeff Browna1f89ce2011-08-11 00:05:01 -07003228 if (mCalibration.haveSizeIsSummed) {
3229 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3230 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003231 }
3232
3233 // Pressure
3234 switch (mCalibration.pressureCalibration) {
3235 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003236 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003237 break;
3238 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003239 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003240 break;
3241 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003242 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003243 break;
3244 default:
Steve Blockec193de2012-01-09 18:35:44 +00003245 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003246 }
3247
Jeff Brown8d608662010-08-30 03:02:23 -07003248 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003249 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3250 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003251 }
3252
Jeff Brown8d608662010-08-30 03:02:23 -07003253 // Orientation
3254 switch (mCalibration.orientationCalibration) {
3255 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003256 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003257 break;
3258 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003259 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003260 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003261 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3262 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3263 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003264 default:
Steve Blockec193de2012-01-09 18:35:44 +00003265 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003266 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003267
3268 // Distance
3269 switch (mCalibration.distanceCalibration) {
3270 case Calibration::DISTANCE_CALIBRATION_NONE:
3271 dump.append(INDENT4 "touch.distance.calibration: none\n");
3272 break;
3273 case Calibration::DISTANCE_CALIBRATION_SCALED:
3274 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3275 break;
3276 default:
Steve Blockec193de2012-01-09 18:35:44 +00003277 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003278 }
3279
3280 if (mCalibration.haveDistanceScale) {
3281 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3282 mCalibration.distanceScale);
3283 }
Jeff Brown8d608662010-08-30 03:02:23 -07003284}
3285
Jeff Brown65fd2512011-08-18 11:20:58 -07003286void TouchInputMapper::reset(nsecs_t when) {
3287 mCursorButtonAccumulator.reset(getDevice());
3288 mCursorScrollAccumulator.reset(getDevice());
3289 mTouchButtonAccumulator.reset(getDevice());
3290
3291 mPointerVelocityControl.reset();
3292 mWheelXVelocityControl.reset();
3293 mWheelYVelocityControl.reset();
3294
Jeff Brownbe1aa822011-07-27 16:04:54 -07003295 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003296 mLastRawPointerData.clear();
3297 mCurrentCookedPointerData.clear();
3298 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003299 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003300 mLastButtonState = 0;
3301 mCurrentRawVScroll = 0;
3302 mCurrentRawHScroll = 0;
3303 mCurrentFingerIdBits.clear();
3304 mLastFingerIdBits.clear();
3305 mCurrentStylusIdBits.clear();
3306 mLastStylusIdBits.clear();
3307 mCurrentMouseIdBits.clear();
3308 mLastMouseIdBits.clear();
3309 mPointerUsage = POINTER_USAGE_NONE;
3310 mSentHoverEnter = false;
3311 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003312
Jeff Brown65fd2512011-08-18 11:20:58 -07003313 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003314
Jeff Brown65fd2512011-08-18 11:20:58 -07003315 mPointerGesture.reset();
3316 mPointerSimple.reset();
3317
3318 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003319 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3320 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003321 }
3322
Jeff Brown65fd2512011-08-18 11:20:58 -07003323 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003324}
3325
Jeff Brown65fd2512011-08-18 11:20:58 -07003326void TouchInputMapper::process(const RawEvent* rawEvent) {
3327 mCursorButtonAccumulator.process(rawEvent);
3328 mCursorScrollAccumulator.process(rawEvent);
3329 mTouchButtonAccumulator.process(rawEvent);
3330
Jeff Brown49ccac52012-04-11 18:27:33 -07003331 if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003332 sync(rawEvent->when);
3333 }
3334}
3335
3336void TouchInputMapper::sync(nsecs_t when) {
3337 // Sync button state.
3338 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3339 | mCursorButtonAccumulator.getButtonState();
3340
3341 // Sync scroll state.
3342 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3343 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3344 mCursorScrollAccumulator.finishSync();
3345
3346 // Sync touch state.
3347 bool havePointerIds = true;
3348 mCurrentRawPointerData.clear();
3349 syncTouch(when, &havePointerIds);
3350
Jeff Brownaa3855d2011-03-17 01:34:19 -07003351#if DEBUG_RAW_EVENTS
3352 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003353 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003354 mLastRawPointerData.pointerCount,
3355 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003356 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003357 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003358 "hovering ids 0x%08x -> 0x%08x",
3359 mLastRawPointerData.pointerCount,
3360 mCurrentRawPointerData.pointerCount,
3361 mLastRawPointerData.touchingIdBits.value,
3362 mCurrentRawPointerData.touchingIdBits.value,
3363 mLastRawPointerData.hoveringIdBits.value,
3364 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003365 }
3366#endif
3367
Jeff Brown65fd2512011-08-18 11:20:58 -07003368 // Reset state that we will compute below.
3369 mCurrentFingerIdBits.clear();
3370 mCurrentStylusIdBits.clear();
3371 mCurrentMouseIdBits.clear();
3372 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003373
Jeff Brown65fd2512011-08-18 11:20:58 -07003374 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3375 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003376 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003377 mCurrentButtonState = 0;
3378 } else {
3379 // Preprocess pointer data.
3380 if (!havePointerIds) {
3381 assignPointerIds();
3382 }
3383
3384 // Handle policy on initial down or hover events.
3385 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003386 bool initialDown = mLastRawPointerData.pointerCount == 0
3387 && mCurrentRawPointerData.pointerCount != 0;
3388 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3389 if (initialDown || buttonsPressed) {
3390 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003391 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003392 getContext()->fadePointer();
3393 }
3394
3395 // Initial downs on external touch devices should wake the device.
3396 // We don't do this for internal touch screens to prevent them from waking
3397 // up in your pocket.
3398 // TODO: Use the input device configuration to control this behavior more finely.
3399 if (getDevice()->isExternal()) {
3400 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3401 }
3402 }
3403
3404 // Synthesize key down from raw buttons if needed.
3405 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3406 policyFlags, mLastButtonState, mCurrentButtonState);
3407
3408 // Consume raw off-screen touches before cooking pointer data.
3409 // If touches are consumed, subsequent code will not receive any pointer data.
3410 if (consumeRawTouches(when, policyFlags)) {
3411 mCurrentRawPointerData.clear();
3412 }
3413
3414 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3415 // with cooked pointer data that has the same ids and indices as the raw data.
3416 // The following code can use either the raw or cooked data, as needed.
3417 cookPointerData();
3418
3419 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003420 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003421 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3422 uint32_t id = idBits.clearFirstMarkedBit();
3423 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3424 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3425 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3426 mCurrentStylusIdBits.markBit(id);
3427 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3428 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3429 mCurrentFingerIdBits.markBit(id);
3430 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3431 mCurrentMouseIdBits.markBit(id);
3432 }
3433 }
3434 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3435 uint32_t id = idBits.clearFirstMarkedBit();
3436 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3437 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3438 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3439 mCurrentStylusIdBits.markBit(id);
3440 }
3441 }
3442
3443 // Stylus takes precedence over all tools, then mouse, then finger.
3444 PointerUsage pointerUsage = mPointerUsage;
3445 if (!mCurrentStylusIdBits.isEmpty()) {
3446 mCurrentMouseIdBits.clear();
3447 mCurrentFingerIdBits.clear();
3448 pointerUsage = POINTER_USAGE_STYLUS;
3449 } else if (!mCurrentMouseIdBits.isEmpty()) {
3450 mCurrentFingerIdBits.clear();
3451 pointerUsage = POINTER_USAGE_MOUSE;
3452 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3453 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003454 }
3455
3456 dispatchPointerUsage(when, policyFlags, pointerUsage);
3457 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003458 if (mDeviceMode == DEVICE_MODE_DIRECT
3459 && mConfig.showTouches && mPointerController != NULL) {
3460 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3461 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3462
3463 mPointerController->setButtonState(mCurrentButtonState);
3464 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3465 mCurrentCookedPointerData.idToIndex,
3466 mCurrentCookedPointerData.touchingIdBits);
3467 }
3468
Jeff Brown65fd2512011-08-18 11:20:58 -07003469 dispatchHoverExit(when, policyFlags);
3470 dispatchTouches(when, policyFlags);
3471 dispatchHoverEnterAndMove(when, policyFlags);
3472 }
3473
3474 // Synthesize key up from raw buttons if needed.
3475 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3476 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003477 }
3478
Jeff Brown6328cdc2010-07-29 18:18:33 -07003479 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003480 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3481 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3482 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003483 mLastFingerIdBits = mCurrentFingerIdBits;
3484 mLastStylusIdBits = mCurrentStylusIdBits;
3485 mLastMouseIdBits = mCurrentMouseIdBits;
3486
3487 // Clear some transient state.
3488 mCurrentRawVScroll = 0;
3489 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003490}
3491
Jeff Brown79ac9692011-04-19 21:20:10 -07003492void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003493 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003494 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3495 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3496 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003497 }
3498}
3499
Jeff Brownbe1aa822011-07-27 16:04:54 -07003500bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3501 // Check for release of a virtual key.
3502 if (mCurrentVirtualKey.down) {
3503 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3504 // Pointer went up while virtual key was down.
3505 mCurrentVirtualKey.down = false;
3506 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003507#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003508 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003509 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003510#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003511 dispatchVirtualKey(when, policyFlags,
3512 AKEY_EVENT_ACTION_UP,
3513 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003514 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003515 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003516 }
3517
Jeff Brownbe1aa822011-07-27 16:04:54 -07003518 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3519 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3520 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3521 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3522 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3523 // Pointer is still within the space of the virtual key.
3524 return true;
3525 }
3526 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003527
Jeff Brownbe1aa822011-07-27 16:04:54 -07003528 // Pointer left virtual key area or another pointer also went down.
3529 // Send key cancellation but do not consume the touch yet.
3530 // This is useful when the user swipes through from the virtual key area
3531 // into the main display surface.
3532 mCurrentVirtualKey.down = false;
3533 if (!mCurrentVirtualKey.ignored) {
3534#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003535 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003536 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3537#endif
3538 dispatchVirtualKey(when, policyFlags,
3539 AKEY_EVENT_ACTION_UP,
3540 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3541 | AKEY_EVENT_FLAG_CANCELED);
3542 }
3543 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003544
Jeff Brownbe1aa822011-07-27 16:04:54 -07003545 if (mLastRawPointerData.touchingIdBits.isEmpty()
3546 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3547 // Pointer just went down. Check for virtual key press or off-screen touches.
3548 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3549 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3550 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3551 // If exactly one pointer went down, check for virtual key hit.
3552 // Otherwise we will drop the entire stroke.
3553 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3554 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3555 if (virtualKey) {
3556 mCurrentVirtualKey.down = true;
3557 mCurrentVirtualKey.downTime = when;
3558 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3559 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3560 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3561 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3562
3563 if (!mCurrentVirtualKey.ignored) {
3564#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003565 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003566 mCurrentVirtualKey.keyCode,
3567 mCurrentVirtualKey.scanCode);
3568#endif
3569 dispatchVirtualKey(when, policyFlags,
3570 AKEY_EVENT_ACTION_DOWN,
3571 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3572 }
3573 }
3574 }
3575 return true;
3576 }
3577 }
3578
Jeff Brownfe508922011-01-18 15:10:10 -08003579 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003580 // most recent touch within the screen area. The idea is to filter out stray
3581 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003582 //
3583 // Problems we're trying to solve:
3584 //
3585 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3586 // virtual key area that is implemented by a separate touch panel and accidentally
3587 // triggers a virtual key.
3588 //
3589 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3590 // area and accidentally triggers a virtual key. This often happens when virtual keys
3591 // are layed out below the screen near to where the on screen keyboard's space bar
3592 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003593 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003594 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003595 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003596 return false;
3597}
3598
3599void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3600 int32_t keyEventAction, int32_t keyEventFlags) {
3601 int32_t keyCode = mCurrentVirtualKey.keyCode;
3602 int32_t scanCode = mCurrentVirtualKey.scanCode;
3603 nsecs_t downTime = mCurrentVirtualKey.downTime;
3604 int32_t metaState = mContext->getGlobalMetaState();
3605 policyFlags |= POLICY_FLAG_VIRTUAL;
3606
3607 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3608 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3609 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003610}
3611
Jeff Brown6d0fec22010-07-23 21:28:06 -07003612void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003613 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3614 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003615 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003616 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003617
3618 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003619 if (!currentIdBits.isEmpty()) {
3620 // No pointer id changes so this is a move event.
3621 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003622 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003623 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3624 AMOTION_EVENT_EDGE_FLAG_NONE,
3625 mCurrentCookedPointerData.pointerProperties,
3626 mCurrentCookedPointerData.pointerCoords,
3627 mCurrentCookedPointerData.idToIndex,
3628 currentIdBits, -1,
3629 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3630 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003631 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003632 // There may be pointers going up and pointers going down and pointers moving
3633 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003634 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3635 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003636 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003637 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003638
Jeff Brownace13b12011-03-09 17:39:48 -08003639 // Update last coordinates of pointers that have moved so that we observe the new
3640 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003641 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003642 mCurrentCookedPointerData.pointerProperties,
3643 mCurrentCookedPointerData.pointerCoords,
3644 mCurrentCookedPointerData.idToIndex,
3645 mLastCookedPointerData.pointerProperties,
3646 mLastCookedPointerData.pointerCoords,
3647 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003648 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003649 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003650 moveNeeded = true;
3651 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003652
Jeff Brownace13b12011-03-09 17:39:48 -08003653 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003654 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003655 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003656
Jeff Brown65fd2512011-08-18 11:20:58 -07003657 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003658 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003659 mLastCookedPointerData.pointerProperties,
3660 mLastCookedPointerData.pointerCoords,
3661 mLastCookedPointerData.idToIndex,
3662 dispatchedIdBits, upId,
3663 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003664 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003665 }
3666
Jeff Brownc3db8582010-10-20 15:33:38 -07003667 // Dispatch move events if any of the remaining pointers moved from their old locations.
3668 // Although applications receive new locations as part of individual pointer up
3669 // events, they do not generally handle them except when presented in a move event.
3670 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003671 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003672 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003673 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003674 mCurrentCookedPointerData.pointerProperties,
3675 mCurrentCookedPointerData.pointerCoords,
3676 mCurrentCookedPointerData.idToIndex,
3677 dispatchedIdBits, -1,
3678 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003679 }
3680
3681 // Dispatch pointer down events using the new pointer locations.
3682 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003683 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003684 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003685
Jeff Brownace13b12011-03-09 17:39:48 -08003686 if (dispatchedIdBits.count() == 1) {
3687 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003688 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003689 }
3690
Jeff Brown65fd2512011-08-18 11:20:58 -07003691 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003692 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003693 mCurrentCookedPointerData.pointerProperties,
3694 mCurrentCookedPointerData.pointerCoords,
3695 mCurrentCookedPointerData.idToIndex,
3696 dispatchedIdBits, downId,
3697 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003698 }
3699 }
Jeff Brownace13b12011-03-09 17:39:48 -08003700}
3701
Jeff Brownbe1aa822011-07-27 16:04:54 -07003702void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3703 if (mSentHoverEnter &&
3704 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3705 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3706 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003707 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003708 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3709 mLastCookedPointerData.pointerProperties,
3710 mLastCookedPointerData.pointerCoords,
3711 mLastCookedPointerData.idToIndex,
3712 mLastCookedPointerData.hoveringIdBits, -1,
3713 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3714 mSentHoverEnter = false;
3715 }
3716}
Jeff Brownace13b12011-03-09 17:39:48 -08003717
Jeff Brownbe1aa822011-07-27 16:04:54 -07003718void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3719 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3720 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3721 int32_t metaState = getContext()->getGlobalMetaState();
3722 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003723 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003724 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3725 mCurrentCookedPointerData.pointerProperties,
3726 mCurrentCookedPointerData.pointerCoords,
3727 mCurrentCookedPointerData.idToIndex,
3728 mCurrentCookedPointerData.hoveringIdBits, -1,
3729 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3730 mSentHoverEnter = true;
3731 }
Jeff Brownace13b12011-03-09 17:39:48 -08003732
Jeff Brown65fd2512011-08-18 11:20:58 -07003733 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003734 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3735 mCurrentCookedPointerData.pointerProperties,
3736 mCurrentCookedPointerData.pointerCoords,
3737 mCurrentCookedPointerData.idToIndex,
3738 mCurrentCookedPointerData.hoveringIdBits, -1,
3739 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3740 }
3741}
3742
3743void TouchInputMapper::cookPointerData() {
3744 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3745
3746 mCurrentCookedPointerData.clear();
3747 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3748 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3749 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3750
3751 // Walk through the the active pointers and map device coordinates onto
3752 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003753 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003754 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003755
Jeff Browna1f89ce2011-08-11 00:05:01 -07003756 // Size
3757 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3758 switch (mCalibration.sizeCalibration) {
3759 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3760 case Calibration::SIZE_CALIBRATION_DIAMETER:
3761 case Calibration::SIZE_CALIBRATION_AREA:
3762 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3763 touchMajor = in.touchMajor;
3764 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3765 toolMajor = in.toolMajor;
3766 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3767 size = mRawPointerAxes.touchMinor.valid
3768 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3769 } else if (mRawPointerAxes.touchMajor.valid) {
3770 toolMajor = touchMajor = in.touchMajor;
3771 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3772 ? in.touchMinor : in.touchMajor;
3773 size = mRawPointerAxes.touchMinor.valid
3774 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3775 } else if (mRawPointerAxes.toolMajor.valid) {
3776 touchMajor = toolMajor = in.toolMajor;
3777 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3778 ? in.toolMinor : in.toolMajor;
3779 size = mRawPointerAxes.toolMinor.valid
3780 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003781 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003782 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003783 "Size calibration should have been resolved to NONE.");
3784 touchMajor = 0;
3785 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003786 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003787 toolMinor = 0;
3788 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003789 }
Jeff Brownace13b12011-03-09 17:39:48 -08003790
Jeff Browna1f89ce2011-08-11 00:05:01 -07003791 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3792 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3793 if (touchingCount > 1) {
3794 touchMajor /= touchingCount;
3795 touchMinor /= touchingCount;
3796 toolMajor /= touchingCount;
3797 toolMinor /= touchingCount;
3798 size /= touchingCount;
3799 }
3800 }
Jeff Brownace13b12011-03-09 17:39:48 -08003801
Jeff Browna1f89ce2011-08-11 00:05:01 -07003802 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3803 touchMajor *= mGeometricScale;
3804 touchMinor *= mGeometricScale;
3805 toolMajor *= mGeometricScale;
3806 toolMinor *= mGeometricScale;
3807 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3808 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003809 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003810 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3811 toolMinor = toolMajor;
3812 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3813 touchMinor = touchMajor;
3814 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003815 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003816
3817 mCalibration.applySizeScaleAndBias(&touchMajor);
3818 mCalibration.applySizeScaleAndBias(&touchMinor);
3819 mCalibration.applySizeScaleAndBias(&toolMajor);
3820 mCalibration.applySizeScaleAndBias(&toolMinor);
3821 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003822 break;
3823 default:
3824 touchMajor = 0;
3825 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003826 toolMajor = 0;
3827 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003828 size = 0;
3829 break;
3830 }
3831
Jeff Browna1f89ce2011-08-11 00:05:01 -07003832 // Pressure
3833 float pressure;
3834 switch (mCalibration.pressureCalibration) {
3835 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3836 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3837 pressure = in.pressure * mPressureScale;
3838 break;
3839 default:
3840 pressure = in.isHovering ? 0 : 1;
3841 break;
3842 }
3843
Jeff Brown65fd2512011-08-18 11:20:58 -07003844 // Tilt and Orientation
3845 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003846 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003847 if (mHaveTilt) {
3848 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3849 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3850 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3851 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3852 } else {
3853 tilt = 0;
3854
3855 switch (mCalibration.orientationCalibration) {
3856 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3857 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3858 break;
3859 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3860 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3861 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3862 if (c1 != 0 || c2 != 0) {
3863 orientation = atan2f(c1, c2) * 0.5f;
3864 float confidence = hypotf(c1, c2);
3865 float scale = 1.0f + confidence / 16.0f;
3866 touchMajor *= scale;
3867 touchMinor /= scale;
3868 toolMajor *= scale;
3869 toolMinor /= scale;
3870 } else {
3871 orientation = 0;
3872 }
3873 break;
3874 }
3875 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003876 orientation = 0;
3877 }
Jeff Brownace13b12011-03-09 17:39:48 -08003878 }
3879
Jeff Brown80fd47c2011-05-24 01:07:44 -07003880 // Distance
3881 float distance;
3882 switch (mCalibration.distanceCalibration) {
3883 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003884 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003885 break;
3886 default:
3887 distance = 0;
3888 }
3889
Jeff Brownace13b12011-03-09 17:39:48 -08003890 // X and Y
3891 // Adjust coords for surface orientation.
3892 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003893 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003894 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003895 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3896 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003897 orientation -= M_PI_2;
3898 if (orientation < - M_PI_2) {
3899 orientation += M_PI;
3900 }
3901 break;
3902 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003903 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3904 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003905 break;
3906 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003907 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3908 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003909 orientation += M_PI_2;
3910 if (orientation > M_PI_2) {
3911 orientation -= M_PI;
3912 }
3913 break;
3914 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003915 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3916 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003917 break;
3918 }
3919
3920 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003921 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003922 out.clear();
3923 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3924 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3925 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3926 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3927 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3928 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3929 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3930 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3931 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003932 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003933 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003934
3935 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003936 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3937 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003938 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003939 properties.id = id;
3940 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003941
Jeff Brownbe1aa822011-07-27 16:04:54 -07003942 // Write id index.
3943 mCurrentCookedPointerData.idToIndex[id] = i;
3944 }
Jeff Brownace13b12011-03-09 17:39:48 -08003945}
3946
Jeff Brown65fd2512011-08-18 11:20:58 -07003947void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3948 PointerUsage pointerUsage) {
3949 if (pointerUsage != mPointerUsage) {
3950 abortPointerUsage(when, policyFlags);
3951 mPointerUsage = pointerUsage;
3952 }
3953
3954 switch (mPointerUsage) {
3955 case POINTER_USAGE_GESTURES:
3956 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3957 break;
3958 case POINTER_USAGE_STYLUS:
3959 dispatchPointerStylus(when, policyFlags);
3960 break;
3961 case POINTER_USAGE_MOUSE:
3962 dispatchPointerMouse(when, policyFlags);
3963 break;
3964 default:
3965 break;
3966 }
3967}
3968
3969void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3970 switch (mPointerUsage) {
3971 case POINTER_USAGE_GESTURES:
3972 abortPointerGestures(when, policyFlags);
3973 break;
3974 case POINTER_USAGE_STYLUS:
3975 abortPointerStylus(when, policyFlags);
3976 break;
3977 case POINTER_USAGE_MOUSE:
3978 abortPointerMouse(when, policyFlags);
3979 break;
3980 default:
3981 break;
3982 }
3983
3984 mPointerUsage = POINTER_USAGE_NONE;
3985}
3986
Jeff Brown79ac9692011-04-19 21:20:10 -07003987void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3988 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003989 // Update current gesture coordinates.
3990 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003991 bool sendEvents = preparePointerGestures(when,
3992 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3993 if (!sendEvents) {
3994 return;
3995 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003996 if (finishPreviousGesture) {
3997 cancelPreviousGesture = false;
3998 }
Jeff Brownace13b12011-03-09 17:39:48 -08003999
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004000 // Update the pointer presentation and spots.
4001 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4002 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
4003 if (finishPreviousGesture || cancelPreviousGesture) {
4004 mPointerController->clearSpots();
4005 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004006 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
4007 mPointerGesture.currentGestureIdToIndex,
4008 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004009 } else {
4010 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
4011 }
Jeff Brown214eaf42011-05-26 19:17:02 -07004012
Jeff Brown538881e2011-05-25 18:23:38 -07004013 // Show or hide the pointer if needed.
4014 switch (mPointerGesture.currentGestureMode) {
4015 case PointerGesture::NEUTRAL:
4016 case PointerGesture::QUIET:
4017 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4018 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4019 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4020 // Remind the user of where the pointer is after finishing a gesture with spots.
4021 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4022 }
4023 break;
4024 case PointerGesture::TAP:
4025 case PointerGesture::TAP_DRAG:
4026 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4027 case PointerGesture::HOVER:
4028 case PointerGesture::PRESS:
4029 // Unfade the pointer when the current gesture manipulates the
4030 // area directly under the pointer.
4031 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4032 break;
4033 case PointerGesture::SWIPE:
4034 case PointerGesture::FREEFORM:
4035 // Fade the pointer when the current gesture manipulates a different
4036 // area and there are spots to guide the user experience.
4037 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4038 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4039 } else {
4040 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4041 }
4042 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004043 }
4044
Jeff Brownace13b12011-03-09 17:39:48 -08004045 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004046 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004047 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004048
4049 // Update last coordinates of pointers that have moved so that we observe the new
4050 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004051 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4052 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4053 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004054 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004055 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4056 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4057 bool moveNeeded = false;
4058 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004059 && !mPointerGesture.lastGestureIdBits.isEmpty()
4060 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004061 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4062 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004063 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004064 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004065 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004066 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4067 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004068 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004069 moveNeeded = true;
4070 }
Jeff Brownace13b12011-03-09 17:39:48 -08004071 }
4072
4073 // Send motion events for all pointers that went up or were canceled.
4074 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4075 if (!dispatchedGestureIdBits.isEmpty()) {
4076 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004077 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004078 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4079 AMOTION_EVENT_EDGE_FLAG_NONE,
4080 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004081 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4082 dispatchedGestureIdBits, -1,
4083 0, 0, mPointerGesture.downTime);
4084
4085 dispatchedGestureIdBits.clear();
4086 } else {
4087 BitSet32 upGestureIdBits;
4088 if (finishPreviousGesture) {
4089 upGestureIdBits = dispatchedGestureIdBits;
4090 } else {
4091 upGestureIdBits.value = dispatchedGestureIdBits.value
4092 & ~mPointerGesture.currentGestureIdBits.value;
4093 }
4094 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004095 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004096
Jeff Brown65fd2512011-08-18 11:20:58 -07004097 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004098 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004099 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4100 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004101 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4102 dispatchedGestureIdBits, id,
4103 0, 0, mPointerGesture.downTime);
4104
4105 dispatchedGestureIdBits.clearBit(id);
4106 }
4107 }
4108 }
4109
4110 // Send motion events for all pointers that moved.
4111 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004112 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004113 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4114 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004115 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4116 dispatchedGestureIdBits, -1,
4117 0, 0, mPointerGesture.downTime);
4118 }
4119
4120 // Send motion events for all pointers that went down.
4121 if (down) {
4122 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4123 & ~dispatchedGestureIdBits.value);
4124 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004125 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004126 dispatchedGestureIdBits.markBit(id);
4127
Jeff Brownace13b12011-03-09 17:39:48 -08004128 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004129 mPointerGesture.downTime = when;
4130 }
4131
Jeff Brown65fd2512011-08-18 11:20:58 -07004132 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004133 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004134 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004135 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4136 dispatchedGestureIdBits, id,
4137 0, 0, mPointerGesture.downTime);
4138 }
4139 }
4140
Jeff Brownace13b12011-03-09 17:39:48 -08004141 // Send motion events for hover.
4142 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004143 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004144 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4145 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4146 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004147 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4148 mPointerGesture.currentGestureIdBits, -1,
4149 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004150 } else if (dispatchedGestureIdBits.isEmpty()
4151 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4152 // Synthesize a hover move event after all pointers go up to indicate that
4153 // the pointer is hovering again even if the user is not currently touching
4154 // the touch pad. This ensures that a view will receive a fresh hover enter
4155 // event after a tap.
4156 float x, y;
4157 mPointerController->getPosition(&x, &y);
4158
4159 PointerProperties pointerProperties;
4160 pointerProperties.clear();
4161 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004162 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004163
4164 PointerCoords pointerCoords;
4165 pointerCoords.clear();
4166 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4167 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4168
Jeff Brown65fd2512011-08-18 11:20:58 -07004169 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004170 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4171 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4172 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004173 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004174 }
4175
4176 // Update state.
4177 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4178 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004179 mPointerGesture.lastGestureIdBits.clear();
4180 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004181 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4182 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004183 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004184 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004185 mPointerGesture.lastGestureProperties[index].copyFrom(
4186 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004187 mPointerGesture.lastGestureCoords[index].copyFrom(
4188 mPointerGesture.currentGestureCoords[index]);
4189 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004190 }
4191 }
4192}
4193
Jeff Brown65fd2512011-08-18 11:20:58 -07004194void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4195 // Cancel previously dispatches pointers.
4196 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4197 int32_t metaState = getContext()->getGlobalMetaState();
4198 int32_t buttonState = mCurrentButtonState;
4199 dispatchMotion(when, policyFlags, mSource,
4200 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4201 AMOTION_EVENT_EDGE_FLAG_NONE,
4202 mPointerGesture.lastGestureProperties,
4203 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4204 mPointerGesture.lastGestureIdBits, -1,
4205 0, 0, mPointerGesture.downTime);
4206 }
4207
4208 // Reset the current pointer gesture.
4209 mPointerGesture.reset();
4210 mPointerVelocityControl.reset();
4211
4212 // Remove any current spots.
4213 if (mPointerController != NULL) {
4214 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4215 mPointerController->clearSpots();
4216 }
4217}
4218
Jeff Brown79ac9692011-04-19 21:20:10 -07004219bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4220 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004221 *outCancelPreviousGesture = false;
4222 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004223
Jeff Brown79ac9692011-04-19 21:20:10 -07004224 // Handle TAP timeout.
4225 if (isTimeout) {
4226#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004227 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004228#endif
4229
4230 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004231 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004232 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004233 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004234 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004235 } else {
4236 // The tap is finished.
4237#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004238 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004239#endif
4240 *outFinishPreviousGesture = true;
4241
4242 mPointerGesture.activeGestureId = -1;
4243 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4244 mPointerGesture.currentGestureIdBits.clear();
4245
Jeff Brown65fd2512011-08-18 11:20:58 -07004246 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004247 return true;
4248 }
4249 }
4250
4251 // We did not handle this timeout.
4252 return false;
4253 }
4254
Jeff Brown65fd2512011-08-18 11:20:58 -07004255 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4256 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4257
Jeff Brownace13b12011-03-09 17:39:48 -08004258 // Update the velocity tracker.
4259 {
4260 VelocityTracker::Position positions[MAX_POINTERS];
4261 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004262 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004263 uint32_t id = idBits.clearFirstMarkedBit();
4264 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004265 positions[count].x = pointer.x * mPointerXMovementScale;
4266 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004267 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004268 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004269 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004270 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004271
Jeff Brownace13b12011-03-09 17:39:48 -08004272 // Pick a new active touch id if needed.
4273 // Choose an arbitrary pointer that just went down, if there is one.
4274 // Otherwise choose an arbitrary remaining pointer.
4275 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004276 // We keep the same active touch id for as long as possible.
4277 bool activeTouchChanged = false;
4278 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4279 int32_t activeTouchId = lastActiveTouchId;
4280 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004281 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004282 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004283 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004284 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004285 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004286 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004287 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004288 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004289 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004290 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004291 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004292 } else {
4293 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004294 }
4295 }
4296
4297 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004298 bool isQuietTime = false;
4299 if (activeTouchId < 0) {
4300 mPointerGesture.resetQuietTime();
4301 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004302 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004303 if (!isQuietTime) {
4304 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4305 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4306 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004307 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004308 // Enter quiet time when exiting swipe or freeform state.
4309 // This is to prevent accidentally entering the hover state and flinging the
4310 // pointer when finishing a swipe and there is still one pointer left onscreen.
4311 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004312 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004313 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004314 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004315 // Enter quiet time when releasing the button and there are still two or more
4316 // fingers down. This may indicate that one finger was used to press the button
4317 // but it has not gone up yet.
4318 isQuietTime = true;
4319 }
4320 if (isQuietTime) {
4321 mPointerGesture.quietTime = when;
4322 }
Jeff Brownace13b12011-03-09 17:39:48 -08004323 }
4324 }
4325
4326 // Switch states based on button and pointer state.
4327 if (isQuietTime) {
4328 // Case 1: Quiet time. (QUIET)
4329#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004330 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004331 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004332#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004333 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4334 *outFinishPreviousGesture = true;
4335 }
Jeff Brownace13b12011-03-09 17:39:48 -08004336
4337 mPointerGesture.activeGestureId = -1;
4338 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004339 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004340
Jeff Brown65fd2512011-08-18 11:20:58 -07004341 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004342 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004343 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004344 // The pointer follows the active touch point.
4345 // Emit DOWN, MOVE, UP events at the pointer location.
4346 //
4347 // Only the active touch matters; other fingers are ignored. This policy helps
4348 // to handle the case where the user places a second finger on the touch pad
4349 // to apply the necessary force to depress an integrated button below the surface.
4350 // We don't want the second finger to be delivered to applications.
4351 //
4352 // For this to work well, we need to make sure to track the pointer that is really
4353 // active. If the user first puts one finger down to click then adds another
4354 // finger to drag then the active pointer should switch to the finger that is
4355 // being dragged.
4356#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004357 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004358 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004359#endif
4360 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004361 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004362 *outFinishPreviousGesture = true;
4363 mPointerGesture.activeGestureId = 0;
4364 }
4365
4366 // Switch pointers if needed.
4367 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004368 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004369 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004370 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004371 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004372 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004373 float vx, vy;
4374 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4375 float speed = hypotf(vx, vy);
4376 if (speed > bestSpeed) {
4377 bestId = id;
4378 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004379 }
Jeff Brown8d608662010-08-30 03:02:23 -07004380 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004381 }
4382 if (bestId >= 0 && bestId != activeTouchId) {
4383 mPointerGesture.activeTouchId = activeTouchId = bestId;
4384 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004385#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004386 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004387 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004388#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004389 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004390 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004391
Jeff Brown65fd2512011-08-18 11:20:58 -07004392 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004393 const RawPointerData::Pointer& currentPointer =
4394 mCurrentRawPointerData.pointerForId(activeTouchId);
4395 const RawPointerData::Pointer& lastPointer =
4396 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004397 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4398 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004399
Jeff Brownbe1aa822011-07-27 16:04:54 -07004400 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004401 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004402
4403 // Move the pointer using a relative motion.
4404 // When using spots, the click will occur at the position of the anchor
4405 // spot and all other spots will move there.
4406 mPointerController->move(deltaX, deltaY);
4407 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004408 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004409 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004410
Jeff Brownace13b12011-03-09 17:39:48 -08004411 float x, y;
4412 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004413
Jeff Brown79ac9692011-04-19 21:20:10 -07004414 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004415 mPointerGesture.currentGestureIdBits.clear();
4416 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4417 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004418 mPointerGesture.currentGestureProperties[0].clear();
4419 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004420 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004421 mPointerGesture.currentGestureCoords[0].clear();
4422 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4423 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4424 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004425 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004426 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004427 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4428 *outFinishPreviousGesture = true;
4429 }
Jeff Brownace13b12011-03-09 17:39:48 -08004430
Jeff Brown79ac9692011-04-19 21:20:10 -07004431 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004432 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004433 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004434 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4435 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004436 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004437 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004438 float x, y;
4439 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004440 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4441 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004442#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004443 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004444#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004445
4446 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004447 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004448 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004449
Jeff Brownace13b12011-03-09 17:39:48 -08004450 mPointerGesture.activeGestureId = 0;
4451 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004452 mPointerGesture.currentGestureIdBits.clear();
4453 mPointerGesture.currentGestureIdBits.markBit(
4454 mPointerGesture.activeGestureId);
4455 mPointerGesture.currentGestureIdToIndex[
4456 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004457 mPointerGesture.currentGestureProperties[0].clear();
4458 mPointerGesture.currentGestureProperties[0].id =
4459 mPointerGesture.activeGestureId;
4460 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004461 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004462 mPointerGesture.currentGestureCoords[0].clear();
4463 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004464 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004465 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004466 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004467 mPointerGesture.currentGestureCoords[0].setAxisValue(
4468 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004469
Jeff Brownace13b12011-03-09 17:39:48 -08004470 tapped = true;
4471 } else {
4472#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004473 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004474 x - mPointerGesture.tapX,
4475 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004476#endif
4477 }
4478 } else {
4479#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004480 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004481 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004482#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004483 }
Jeff Brownace13b12011-03-09 17:39:48 -08004484 }
Jeff Brown2352b972011-04-12 22:39:53 -07004485
Jeff Brown65fd2512011-08-18 11:20:58 -07004486 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004487
Jeff Brownace13b12011-03-09 17:39:48 -08004488 if (!tapped) {
4489#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004490 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004491#endif
4492 mPointerGesture.activeGestureId = -1;
4493 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004494 mPointerGesture.currentGestureIdBits.clear();
4495 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004496 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004497 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004498 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004499 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4500 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004501 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004502
Jeff Brown79ac9692011-04-19 21:20:10 -07004503 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4504 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004505 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004506 float x, y;
4507 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004508 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4509 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004510 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4511 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004512#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004513 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004514 x - mPointerGesture.tapX,
4515 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004516#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004517 }
4518 } else {
4519#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004520 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004521 (when - mPointerGesture.tapUpTime) * 0.000001f);
4522#endif
4523 }
4524 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4525 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4526 }
Jeff Brownace13b12011-03-09 17:39:48 -08004527
Jeff Brown65fd2512011-08-18 11:20:58 -07004528 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004529 const RawPointerData::Pointer& currentPointer =
4530 mCurrentRawPointerData.pointerForId(activeTouchId);
4531 const RawPointerData::Pointer& lastPointer =
4532 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004533 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004534 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004535 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004536 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004537
Jeff Brownbe1aa822011-07-27 16:04:54 -07004538 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004539 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004540
Jeff Brown2352b972011-04-12 22:39:53 -07004541 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004542 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004543 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004544 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004545 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004546 }
4547
Jeff Brown79ac9692011-04-19 21:20:10 -07004548 bool down;
4549 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4550#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004551 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004552#endif
4553 down = true;
4554 } else {
4555#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004556 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004557#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004558 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4559 *outFinishPreviousGesture = true;
4560 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004561 mPointerGesture.activeGestureId = 0;
4562 down = false;
4563 }
Jeff Brownace13b12011-03-09 17:39:48 -08004564
4565 float x, y;
4566 mPointerController->getPosition(&x, &y);
4567
Jeff Brownace13b12011-03-09 17:39:48 -08004568 mPointerGesture.currentGestureIdBits.clear();
4569 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4570 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004571 mPointerGesture.currentGestureProperties[0].clear();
4572 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4573 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004574 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004575 mPointerGesture.currentGestureCoords[0].clear();
4576 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4577 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004578 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4579 down ? 1.0f : 0.0f);
4580
Jeff Brown65fd2512011-08-18 11:20:58 -07004581 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004582 mPointerGesture.resetTap();
4583 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004584 mPointerGesture.tapX = x;
4585 mPointerGesture.tapY = y;
4586 }
Jeff Brownace13b12011-03-09 17:39:48 -08004587 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004588 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4589 // We need to provide feedback for each finger that goes down so we cannot wait
4590 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004591 //
Jeff Brown2352b972011-04-12 22:39:53 -07004592 // The ambiguous case is deciding what to do when there are two fingers down but they
4593 // have not moved enough to determine whether they are part of a drag or part of a
4594 // freeform gesture, or just a press or long-press at the pointer location.
4595 //
4596 // When there are two fingers we start with the PRESS hypothesis and we generate a
4597 // down at the pointer location.
4598 //
4599 // When the two fingers move enough or when additional fingers are added, we make
4600 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004601 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004602
Jeff Brown214eaf42011-05-26 19:17:02 -07004603 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004604 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004605 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004606 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4607 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004608 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004609 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004610 // Additional pointers have gone down but not yet settled.
4611 // Reset the gesture.
4612#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004613 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004614 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004615 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004616 * 0.000001f);
4617#endif
4618 *outCancelPreviousGesture = true;
4619 } else {
4620 // Continue previous gesture.
4621 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4622 }
4623
4624 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004625 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4626 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004627 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004628 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004629
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004630 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004631#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004632 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004633 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004634 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004635 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004636#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004637 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4638 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004639 &mPointerGesture.referenceTouchY);
4640 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4641 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004642 }
Jeff Brownace13b12011-03-09 17:39:48 -08004643
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004644 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004645 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004646 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4647 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004648 mPointerGesture.referenceDeltas[id].dx = 0;
4649 mPointerGesture.referenceDeltas[id].dy = 0;
4650 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004651 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004652
4653 // Add delta for all fingers and calculate a common movement delta.
4654 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004655 BitSet32 commonIdBits(mLastFingerIdBits.value
4656 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004657 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4658 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004659 uint32_t id = idBits.clearFirstMarkedBit();
4660 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4661 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004662 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4663 delta.dx += cpd.x - lpd.x;
4664 delta.dy += cpd.y - lpd.y;
4665
4666 if (first) {
4667 commonDeltaX = delta.dx;
4668 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004669 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004670 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4671 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4672 }
4673 }
Jeff Brownace13b12011-03-09 17:39:48 -08004674
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004675 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4676 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4677 float dist[MAX_POINTER_ID + 1];
4678 int32_t distOverThreshold = 0;
4679 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004680 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004681 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004682 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4683 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004684 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004685 distOverThreshold += 1;
4686 }
4687 }
4688
4689 // Only transition when at least two pointers have moved further than
4690 // the minimum distance threshold.
4691 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004692 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004693 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004694#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004695 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004696 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004697#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004698 *outCancelPreviousGesture = true;
4699 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4700 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004701 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004702 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004703 uint32_t id1 = idBits.clearFirstMarkedBit();
4704 uint32_t id2 = idBits.firstMarkedBit();
4705 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4706 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4707 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4708 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4709 // There are two pointers but they are too far apart for a SWIPE,
4710 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004711#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004712 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004713 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004714#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004715 *outCancelPreviousGesture = true;
4716 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4717 } else {
4718 // There are two pointers. Wait for both pointers to start moving
4719 // before deciding whether this is a SWIPE or FREEFORM gesture.
4720 float dist1 = dist[id1];
4721 float dist2 = dist[id2];
4722 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4723 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4724 // Calculate the dot product of the displacement vectors.
4725 // When the vectors are oriented in approximately the same direction,
4726 // the angle betweeen them is near zero and the cosine of the angle
4727 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4728 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4729 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004730 float dx1 = delta1.dx * mPointerXZoomScale;
4731 float dy1 = delta1.dy * mPointerYZoomScale;
4732 float dx2 = delta2.dx * mPointerXZoomScale;
4733 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004734 float dot = dx1 * dx2 + dy1 * dy2;
4735 float cosine = dot / (dist1 * dist2); // denominator always > 0
4736 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4737 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004738#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004739 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004740 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4741 "cosine %0.3f >= %0.3f",
4742 dist1, mConfig.pointerGestureMultitouchMinDistance,
4743 dist2, mConfig.pointerGestureMultitouchMinDistance,
4744 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004745#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004746 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4747 } else {
4748 // Pointers are moving in different directions. Switch to FREEFORM.
4749#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004750 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004751 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4752 "cosine %0.3f < %0.3f",
4753 dist1, mConfig.pointerGestureMultitouchMinDistance,
4754 dist2, mConfig.pointerGestureMultitouchMinDistance,
4755 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4756#endif
4757 *outCancelPreviousGesture = true;
4758 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4759 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004760 }
Jeff Brownace13b12011-03-09 17:39:48 -08004761 }
4762 }
Jeff Brownace13b12011-03-09 17:39:48 -08004763 }
4764 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004765 // Switch from SWIPE to FREEFORM if additional pointers go down.
4766 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004767 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004768#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004769 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004770 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004771#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004772 *outCancelPreviousGesture = true;
4773 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004774 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004775 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004776
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004777 // Move the reference points based on the overall group motion of the fingers
4778 // except in PRESS mode while waiting for a transition to occur.
4779 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4780 && (commonDeltaX || commonDeltaY)) {
4781 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004782 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004783 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004784 delta.dx = 0;
4785 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004786 }
4787
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004788 mPointerGesture.referenceTouchX += commonDeltaX;
4789 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004790
Jeff Brown65fd2512011-08-18 11:20:58 -07004791 commonDeltaX *= mPointerXMovementScale;
4792 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004793
Jeff Brownbe1aa822011-07-27 16:04:54 -07004794 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004795 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004796
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004797 mPointerGesture.referenceGestureX += commonDeltaX;
4798 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004799 }
4800
4801 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004802 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4803 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4804 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004805#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004806 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004807 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004808 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004809#endif
Steve Blockec193de2012-01-09 18:35:44 +00004810 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07004811
4812 mPointerGesture.currentGestureIdBits.clear();
4813 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4814 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004815 mPointerGesture.currentGestureProperties[0].clear();
4816 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4817 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004818 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004819 mPointerGesture.currentGestureCoords[0].clear();
4820 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4821 mPointerGesture.referenceGestureX);
4822 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4823 mPointerGesture.referenceGestureY);
4824 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004825 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4826 // FREEFORM mode.
4827#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004828 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004829 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004830 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004831#endif
Steve Blockec193de2012-01-09 18:35:44 +00004832 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004833
Jeff Brownace13b12011-03-09 17:39:48 -08004834 mPointerGesture.currentGestureIdBits.clear();
4835
4836 BitSet32 mappedTouchIdBits;
4837 BitSet32 usedGestureIdBits;
4838 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4839 // Initially, assign the active gesture id to the active touch point
4840 // if there is one. No other touch id bits are mapped yet.
4841 if (!*outCancelPreviousGesture) {
4842 mappedTouchIdBits.markBit(activeTouchId);
4843 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4844 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4845 mPointerGesture.activeGestureId;
4846 } else {
4847 mPointerGesture.activeGestureId = -1;
4848 }
4849 } else {
4850 // Otherwise, assume we mapped all touches from the previous frame.
4851 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004852 mappedTouchIdBits.value = mLastFingerIdBits.value
4853 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004854 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4855
4856 // Check whether we need to choose a new active gesture id because the
4857 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004858 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4859 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004860 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004861 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004862 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4863 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4864 mPointerGesture.activeGestureId = -1;
4865 break;
4866 }
4867 }
4868 }
4869
4870#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004871 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08004872 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4873 "activeGestureId=%d",
4874 mappedTouchIdBits.value, usedGestureIdBits.value,
4875 mPointerGesture.activeGestureId);
4876#endif
4877
Jeff Brown65fd2512011-08-18 11:20:58 -07004878 BitSet32 idBits(mCurrentFingerIdBits);
4879 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004880 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004881 uint32_t gestureId;
4882 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004883 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004884 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4885#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004886 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004887 "new mapping for touch id %d -> gesture id %d",
4888 touchId, gestureId);
4889#endif
4890 } else {
4891 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4892#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004893 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004894 "existing mapping for touch id %d -> gesture id %d",
4895 touchId, gestureId);
4896#endif
4897 }
4898 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4899 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4900
Jeff Brownbe1aa822011-07-27 16:04:54 -07004901 const RawPointerData::Pointer& pointer =
4902 mCurrentRawPointerData.pointerForId(touchId);
4903 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004904 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004905 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004906 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004907 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004908
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004909 mPointerGesture.currentGestureProperties[i].clear();
4910 mPointerGesture.currentGestureProperties[i].id = gestureId;
4911 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004912 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004913 mPointerGesture.currentGestureCoords[i].clear();
4914 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004915 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004916 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004917 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004918 mPointerGesture.currentGestureCoords[i].setAxisValue(
4919 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4920 }
4921
4922 if (mPointerGesture.activeGestureId < 0) {
4923 mPointerGesture.activeGestureId =
4924 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4925#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004926 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08004927 "activeGestureId=%d", mPointerGesture.activeGestureId);
4928#endif
4929 }
Jeff Brown2352b972011-04-12 22:39:53 -07004930 }
Jeff Brownace13b12011-03-09 17:39:48 -08004931 }
4932
Jeff Brownbe1aa822011-07-27 16:04:54 -07004933 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004934
Jeff Brownace13b12011-03-09 17:39:48 -08004935#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004936 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004937 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4938 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004939 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004940 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4941 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004942 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004943 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004944 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004945 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004946 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004947 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004948 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4949 id, index, properties.toolType,
4950 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004951 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4952 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4953 }
4954 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004955 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004956 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004957 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004958 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004959 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004960 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4961 id, index, properties.toolType,
4962 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004963 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4964 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4965 }
4966#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004967 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004968}
4969
Jeff Brown65fd2512011-08-18 11:20:58 -07004970void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4971 mPointerSimple.currentCoords.clear();
4972 mPointerSimple.currentProperties.clear();
4973
4974 bool down, hovering;
4975 if (!mCurrentStylusIdBits.isEmpty()) {
4976 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4977 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4978 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4979 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4980 mPointerController->setPosition(x, y);
4981
4982 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4983 down = !hovering;
4984
4985 mPointerController->getPosition(&x, &y);
4986 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4987 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4988 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4989 mPointerSimple.currentProperties.id = 0;
4990 mPointerSimple.currentProperties.toolType =
4991 mCurrentCookedPointerData.pointerProperties[index].toolType;
4992 } else {
4993 down = false;
4994 hovering = false;
4995 }
4996
4997 dispatchPointerSimple(when, policyFlags, down, hovering);
4998}
4999
5000void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
5001 abortPointerSimple(when, policyFlags);
5002}
5003
5004void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
5005 mPointerSimple.currentCoords.clear();
5006 mPointerSimple.currentProperties.clear();
5007
5008 bool down, hovering;
5009 if (!mCurrentMouseIdBits.isEmpty()) {
5010 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
5011 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
5012 if (mLastMouseIdBits.hasBit(id)) {
5013 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
5014 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
5015 - mLastRawPointerData.pointers[lastIndex].x)
5016 * mPointerXMovementScale;
5017 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
5018 - mLastRawPointerData.pointers[lastIndex].y)
5019 * mPointerYMovementScale;
5020
5021 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5022 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5023
5024 mPointerController->move(deltaX, deltaY);
5025 } else {
5026 mPointerVelocityControl.reset();
5027 }
5028
5029 down = isPointerDown(mCurrentButtonState);
5030 hovering = !down;
5031
5032 float x, y;
5033 mPointerController->getPosition(&x, &y);
5034 mPointerSimple.currentCoords.copyFrom(
5035 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5036 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5037 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5038 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5039 hovering ? 0.0f : 1.0f);
5040 mPointerSimple.currentProperties.id = 0;
5041 mPointerSimple.currentProperties.toolType =
5042 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5043 } else {
5044 mPointerVelocityControl.reset();
5045
5046 down = false;
5047 hovering = false;
5048 }
5049
5050 dispatchPointerSimple(when, policyFlags, down, hovering);
5051}
5052
5053void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5054 abortPointerSimple(when, policyFlags);
5055
5056 mPointerVelocityControl.reset();
5057}
5058
5059void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5060 bool down, bool hovering) {
5061 int32_t metaState = getContext()->getGlobalMetaState();
5062
5063 if (mPointerController != NULL) {
5064 if (down || hovering) {
5065 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5066 mPointerController->clearSpots();
5067 mPointerController->setButtonState(mCurrentButtonState);
5068 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5069 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5070 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5071 }
5072 }
5073
5074 if (mPointerSimple.down && !down) {
5075 mPointerSimple.down = false;
5076
5077 // Send up.
5078 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5079 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5080 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5081 mOrientedXPrecision, mOrientedYPrecision,
5082 mPointerSimple.downTime);
5083 getListener()->notifyMotion(&args);
5084 }
5085
5086 if (mPointerSimple.hovering && !hovering) {
5087 mPointerSimple.hovering = false;
5088
5089 // Send hover exit.
5090 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5091 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5092 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5093 mOrientedXPrecision, mOrientedYPrecision,
5094 mPointerSimple.downTime);
5095 getListener()->notifyMotion(&args);
5096 }
5097
5098 if (down) {
5099 if (!mPointerSimple.down) {
5100 mPointerSimple.down = true;
5101 mPointerSimple.downTime = when;
5102
5103 // Send down.
5104 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5105 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5106 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5107 mOrientedXPrecision, mOrientedYPrecision,
5108 mPointerSimple.downTime);
5109 getListener()->notifyMotion(&args);
5110 }
5111
5112 // Send move.
5113 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5114 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5115 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5116 mOrientedXPrecision, mOrientedYPrecision,
5117 mPointerSimple.downTime);
5118 getListener()->notifyMotion(&args);
5119 }
5120
5121 if (hovering) {
5122 if (!mPointerSimple.hovering) {
5123 mPointerSimple.hovering = true;
5124
5125 // Send hover enter.
5126 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5127 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5128 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5129 mOrientedXPrecision, mOrientedYPrecision,
5130 mPointerSimple.downTime);
5131 getListener()->notifyMotion(&args);
5132 }
5133
5134 // Send hover move.
5135 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5136 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5137 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5138 mOrientedXPrecision, mOrientedYPrecision,
5139 mPointerSimple.downTime);
5140 getListener()->notifyMotion(&args);
5141 }
5142
5143 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5144 float vscroll = mCurrentRawVScroll;
5145 float hscroll = mCurrentRawHScroll;
5146 mWheelYVelocityControl.move(when, NULL, &vscroll);
5147 mWheelXVelocityControl.move(when, &hscroll, NULL);
5148
5149 // Send scroll.
5150 PointerCoords pointerCoords;
5151 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5152 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5153 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5154
5155 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5156 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5157 1, &mPointerSimple.currentProperties, &pointerCoords,
5158 mOrientedXPrecision, mOrientedYPrecision,
5159 mPointerSimple.downTime);
5160 getListener()->notifyMotion(&args);
5161 }
5162
5163 // Save state.
5164 if (down || hovering) {
5165 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5166 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5167 } else {
5168 mPointerSimple.reset();
5169 }
5170}
5171
5172void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5173 mPointerSimple.currentCoords.clear();
5174 mPointerSimple.currentProperties.clear();
5175
5176 dispatchPointerSimple(when, policyFlags, false, false);
5177}
5178
Jeff Brownace13b12011-03-09 17:39:48 -08005179void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005180 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5181 const PointerProperties* properties, const PointerCoords* coords,
5182 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005183 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5184 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005185 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005186 uint32_t pointerCount = 0;
5187 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005188 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005189 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005190 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005191 pointerCoords[pointerCount].copyFrom(coords[index]);
5192
5193 if (changedId >= 0 && id == uint32_t(changedId)) {
5194 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5195 }
5196
5197 pointerCount += 1;
5198 }
5199
Steve Blockec193de2012-01-09 18:35:44 +00005200 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005201
5202 if (changedId >= 0 && pointerCount == 1) {
5203 // Replace initial down and final up action.
5204 // We can compare the action without masking off the changed pointer index
5205 // because we know the index is 0.
5206 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5207 action = AMOTION_EVENT_ACTION_DOWN;
5208 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5209 action = AMOTION_EVENT_ACTION_UP;
5210 } else {
5211 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005212 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005213 }
5214 }
5215
Jeff Brownbe1aa822011-07-27 16:04:54 -07005216 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005217 action, flags, metaState, buttonState, edgeFlags,
5218 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005219 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005220}
5221
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005222bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005223 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005224 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5225 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005226 bool changed = false;
5227 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005228 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005229 uint32_t inIndex = inIdToIndex[id];
5230 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005231
5232 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005233 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005234 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005235 PointerCoords& curOutCoords = outCoords[outIndex];
5236
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005237 if (curInProperties != curOutProperties) {
5238 curOutProperties.copyFrom(curInProperties);
5239 changed = true;
5240 }
5241
Jeff Brownace13b12011-03-09 17:39:48 -08005242 if (curInCoords != curOutCoords) {
5243 curOutCoords.copyFrom(curInCoords);
5244 changed = true;
5245 }
5246 }
5247 return changed;
5248}
5249
5250void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005251 if (mPointerController != NULL) {
5252 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5253 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005254}
5255
Jeff Brownbe1aa822011-07-27 16:04:54 -07005256bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5257 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5258 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005259}
5260
Jeff Brownbe1aa822011-07-27 16:04:54 -07005261const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005262 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005263 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005264 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005265 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005266
5267#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005268 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005269 "left=%d, top=%d, right=%d, bottom=%d",
5270 x, y,
5271 virtualKey.keyCode, virtualKey.scanCode,
5272 virtualKey.hitLeft, virtualKey.hitTop,
5273 virtualKey.hitRight, virtualKey.hitBottom);
5274#endif
5275
5276 if (virtualKey.isHit(x, y)) {
5277 return & virtualKey;
5278 }
5279 }
5280
5281 return NULL;
5282}
5283
Jeff Brownbe1aa822011-07-27 16:04:54 -07005284void TouchInputMapper::assignPointerIds() {
5285 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5286 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5287
5288 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005289
5290 if (currentPointerCount == 0) {
5291 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005292 return;
5293 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005294
Jeff Brownbe1aa822011-07-27 16:04:54 -07005295 if (lastPointerCount == 0) {
5296 // All pointers are new.
5297 for (uint32_t i = 0; i < currentPointerCount; i++) {
5298 uint32_t id = i;
5299 mCurrentRawPointerData.pointers[i].id = id;
5300 mCurrentRawPointerData.idToIndex[id] = i;
5301 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5302 }
5303 return;
5304 }
5305
5306 if (currentPointerCount == 1 && lastPointerCount == 1
5307 && mCurrentRawPointerData.pointers[0].toolType
5308 == mLastRawPointerData.pointers[0].toolType) {
5309 // Only one pointer and no change in count so it must have the same id as before.
5310 uint32_t id = mLastRawPointerData.pointers[0].id;
5311 mCurrentRawPointerData.pointers[0].id = id;
5312 mCurrentRawPointerData.idToIndex[id] = 0;
5313 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5314 return;
5315 }
5316
5317 // General case.
5318 // We build a heap of squared euclidean distances between current and last pointers
5319 // associated with the current and last pointer indices. Then, we find the best
5320 // match (by distance) for each current pointer.
5321 // The pointers must have the same tool type but it is possible for them to
5322 // transition from hovering to touching or vice-versa while retaining the same id.
5323 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5324
5325 uint32_t heapSize = 0;
5326 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5327 currentPointerIndex++) {
5328 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5329 lastPointerIndex++) {
5330 const RawPointerData::Pointer& currentPointer =
5331 mCurrentRawPointerData.pointers[currentPointerIndex];
5332 const RawPointerData::Pointer& lastPointer =
5333 mLastRawPointerData.pointers[lastPointerIndex];
5334 if (currentPointer.toolType == lastPointer.toolType) {
5335 int64_t deltaX = currentPointer.x - lastPointer.x;
5336 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005337
5338 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5339
5340 // Insert new element into the heap (sift up).
5341 heap[heapSize].currentPointerIndex = currentPointerIndex;
5342 heap[heapSize].lastPointerIndex = lastPointerIndex;
5343 heap[heapSize].distance = distance;
5344 heapSize += 1;
5345 }
5346 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005347 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005348
Jeff Brownbe1aa822011-07-27 16:04:54 -07005349 // Heapify
5350 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5351 startIndex -= 1;
5352 for (uint32_t parentIndex = startIndex; ;) {
5353 uint32_t childIndex = parentIndex * 2 + 1;
5354 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005355 break;
5356 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005357
5358 if (childIndex + 1 < heapSize
5359 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5360 childIndex += 1;
5361 }
5362
5363 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5364 break;
5365 }
5366
5367 swap(heap[parentIndex], heap[childIndex]);
5368 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005369 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005370 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005371
5372#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005373 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005374 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005375 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005376 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5377 heap[i].distance);
5378 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005379#endif
5380
Jeff Brownbe1aa822011-07-27 16:04:54 -07005381 // Pull matches out by increasing order of distance.
5382 // To avoid reassigning pointers that have already been matched, the loop keeps track
5383 // of which last and current pointers have been matched using the matchedXXXBits variables.
5384 // It also tracks the used pointer id bits.
5385 BitSet32 matchedLastBits(0);
5386 BitSet32 matchedCurrentBits(0);
5387 BitSet32 usedIdBits(0);
5388 bool first = true;
5389 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5390 while (heapSize > 0) {
5391 if (first) {
5392 // The first time through the loop, we just consume the root element of
5393 // the heap (the one with smallest distance).
5394 first = false;
5395 } else {
5396 // Previous iterations consumed the root element of the heap.
5397 // Pop root element off of the heap (sift down).
5398 heap[0] = heap[heapSize];
5399 for (uint32_t parentIndex = 0; ;) {
5400 uint32_t childIndex = parentIndex * 2 + 1;
5401 if (childIndex >= heapSize) {
5402 break;
5403 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005404
Jeff Brownbe1aa822011-07-27 16:04:54 -07005405 if (childIndex + 1 < heapSize
5406 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5407 childIndex += 1;
5408 }
5409
5410 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5411 break;
5412 }
5413
5414 swap(heap[parentIndex], heap[childIndex]);
5415 parentIndex = childIndex;
5416 }
5417
5418#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005419 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005420 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005421 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005422 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5423 heap[i].distance);
5424 }
5425#endif
5426 }
5427
5428 heapSize -= 1;
5429
5430 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5431 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5432
5433 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5434 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5435
5436 matchedCurrentBits.markBit(currentPointerIndex);
5437 matchedLastBits.markBit(lastPointerIndex);
5438
5439 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5440 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5441 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5442 mCurrentRawPointerData.markIdBit(id,
5443 mCurrentRawPointerData.isHovering(currentPointerIndex));
5444 usedIdBits.markBit(id);
5445
5446#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005447 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005448 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5449#endif
5450 break;
5451 }
5452 }
5453
5454 // Assign fresh ids to pointers that were not matched in the process.
5455 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5456 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5457 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5458
5459 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5460 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5461 mCurrentRawPointerData.markIdBit(id,
5462 mCurrentRawPointerData.isHovering(currentPointerIndex));
5463
5464#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005465 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005466 currentPointerIndex, id);
5467#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005468 }
5469}
5470
Jeff Brown6d0fec22010-07-23 21:28:06 -07005471int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005472 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5473 return AKEY_STATE_VIRTUAL;
5474 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005475
Jeff Brownbe1aa822011-07-27 16:04:54 -07005476 size_t numVirtualKeys = mVirtualKeys.size();
5477 for (size_t i = 0; i < numVirtualKeys; i++) {
5478 const VirtualKey& virtualKey = mVirtualKeys[i];
5479 if (virtualKey.keyCode == keyCode) {
5480 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005481 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005482 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005483
5484 return AKEY_STATE_UNKNOWN;
5485}
5486
5487int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005488 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5489 return AKEY_STATE_VIRTUAL;
5490 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005491
Jeff Brownbe1aa822011-07-27 16:04:54 -07005492 size_t numVirtualKeys = mVirtualKeys.size();
5493 for (size_t i = 0; i < numVirtualKeys; i++) {
5494 const VirtualKey& virtualKey = mVirtualKeys[i];
5495 if (virtualKey.scanCode == scanCode) {
5496 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005497 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005498 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005499
5500 return AKEY_STATE_UNKNOWN;
5501}
5502
5503bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5504 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005505 size_t numVirtualKeys = mVirtualKeys.size();
5506 for (size_t i = 0; i < numVirtualKeys; i++) {
5507 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005508
Jeff Brownbe1aa822011-07-27 16:04:54 -07005509 for (size_t i = 0; i < numCodes; i++) {
5510 if (virtualKey.keyCode == keyCodes[i]) {
5511 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005512 }
5513 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005514 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005515
5516 return true;
5517}
5518
5519
5520// --- SingleTouchInputMapper ---
5521
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005522SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5523 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005524}
5525
5526SingleTouchInputMapper::~SingleTouchInputMapper() {
5527}
5528
Jeff Brown65fd2512011-08-18 11:20:58 -07005529void SingleTouchInputMapper::reset(nsecs_t when) {
5530 mSingleTouchMotionAccumulator.reset(getDevice());
5531
5532 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005533}
5534
Jeff Brown6d0fec22010-07-23 21:28:06 -07005535void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005536 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005537
Jeff Brown65fd2512011-08-18 11:20:58 -07005538 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005539}
5540
Jeff Brown65fd2512011-08-18 11:20:58 -07005541void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005542 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005543 mCurrentRawPointerData.pointerCount = 1;
5544 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005545
Jeff Brown65fd2512011-08-18 11:20:58 -07005546 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5547 && (mTouchButtonAccumulator.isHovering()
5548 || (mRawPointerAxes.pressure.valid
5549 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005550 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005551
Jeff Brownbe1aa822011-07-27 16:04:54 -07005552 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005553 outPointer.id = 0;
5554 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5555 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5556 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5557 outPointer.touchMajor = 0;
5558 outPointer.touchMinor = 0;
5559 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5560 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5561 outPointer.orientation = 0;
5562 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005563 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5564 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005565 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5566 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5567 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5568 }
5569 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005570 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005571}
5572
Jeff Brownbe1aa822011-07-27 16:04:54 -07005573void SingleTouchInputMapper::configureRawPointerAxes() {
5574 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005575
Jeff Brownbe1aa822011-07-27 16:04:54 -07005576 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5577 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5578 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5579 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5580 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005581 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5582 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005583}
5584
5585
5586// --- MultiTouchInputMapper ---
5587
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005588MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005589 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005590}
5591
5592MultiTouchInputMapper::~MultiTouchInputMapper() {
5593}
5594
Jeff Brown65fd2512011-08-18 11:20:58 -07005595void MultiTouchInputMapper::reset(nsecs_t when) {
5596 mMultiTouchMotionAccumulator.reset(getDevice());
5597
Jeff Brown6894a292011-07-01 17:59:27 -07005598 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005599
Jeff Brown65fd2512011-08-18 11:20:58 -07005600 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005601}
5602
5603void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005604 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005605
Jeff Brown65fd2512011-08-18 11:20:58 -07005606 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005607}
5608
Jeff Brown65fd2512011-08-18 11:20:58 -07005609void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005610 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005611 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005612 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005613
Jeff Brown80fd47c2011-05-24 01:07:44 -07005614 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005615 const MultiTouchMotionAccumulator::Slot* inSlot =
5616 mMultiTouchMotionAccumulator.getSlot(inIndex);
5617 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005618 continue;
5619 }
5620
Jeff Brown80fd47c2011-05-24 01:07:44 -07005621 if (outCount >= MAX_POINTERS) {
5622#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005623 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005624 "ignoring the rest.",
5625 getDeviceName().string(), MAX_POINTERS);
5626#endif
5627 break; // too many fingers!
5628 }
5629
Jeff Brownbe1aa822011-07-27 16:04:54 -07005630 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005631 outPointer.x = inSlot->getX();
5632 outPointer.y = inSlot->getY();
5633 outPointer.pressure = inSlot->getPressure();
5634 outPointer.touchMajor = inSlot->getTouchMajor();
5635 outPointer.touchMinor = inSlot->getTouchMinor();
5636 outPointer.toolMajor = inSlot->getToolMajor();
5637 outPointer.toolMinor = inSlot->getToolMinor();
5638 outPointer.orientation = inSlot->getOrientation();
5639 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005640 outPointer.tiltX = 0;
5641 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005642
Jeff Brown49754db2011-07-01 17:37:58 -07005643 outPointer.toolType = inSlot->getToolType();
5644 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5645 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5646 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5647 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5648 }
Jeff Brown8d608662010-08-30 03:02:23 -07005649 }
5650
Jeff Brown65fd2512011-08-18 11:20:58 -07005651 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5652 && (mTouchButtonAccumulator.isHovering()
5653 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005654 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005655
Jeff Brown8d608662010-08-30 03:02:23 -07005656 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005657 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005658 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005659 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005660 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005661 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005662 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005663 if (mPointerTrackingIdMap[n] == trackingId) {
5664 id = n;
5665 }
5666 }
5667
5668 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005669 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005670 mPointerTrackingIdMap[id] = trackingId;
5671 }
5672 }
5673 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005674 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005675 mCurrentRawPointerData.clearIdBits();
5676 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005677 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005678 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005679 mCurrentRawPointerData.idToIndex[id] = outCount;
5680 mCurrentRawPointerData.markIdBit(id, isHovering);
5681 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005682 }
5683 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005684
Jeff Brown6d0fec22010-07-23 21:28:06 -07005685 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005686 }
5687
Jeff Brownbe1aa822011-07-27 16:04:54 -07005688 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005689 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005690
Jeff Brown65fd2512011-08-18 11:20:58 -07005691 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005692}
5693
Jeff Brownbe1aa822011-07-27 16:04:54 -07005694void MultiTouchInputMapper::configureRawPointerAxes() {
5695 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005696
Jeff Brownbe1aa822011-07-27 16:04:54 -07005697 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5698 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5699 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5700 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5701 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5702 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5703 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5704 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5705 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5706 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5707 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005708
Jeff Brownbe1aa822011-07-27 16:04:54 -07005709 if (mRawPointerAxes.trackingId.valid
5710 && mRawPointerAxes.slot.valid
5711 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5712 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005713 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005714 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005715 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005716 getDeviceName().string(), slotCount, MAX_SLOTS);
5717 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005718 }
Jeff Brown49754db2011-07-01 17:37:58 -07005719 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005720 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005721 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005722 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005723}
5724
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005725
Jeff Browncb1404e2011-01-15 18:14:15 -08005726// --- JoystickInputMapper ---
5727
5728JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5729 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005730}
5731
5732JoystickInputMapper::~JoystickInputMapper() {
5733}
5734
5735uint32_t JoystickInputMapper::getSources() {
5736 return AINPUT_SOURCE_JOYSTICK;
5737}
5738
5739void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5740 InputMapper::populateDeviceInfo(info);
5741
Jeff Brown6f2fba42011-02-19 01:08:02 -08005742 for (size_t i = 0; i < mAxes.size(); i++) {
5743 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005744 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5745 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005746 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005747 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5748 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005749 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005750 }
5751}
5752
5753void JoystickInputMapper::dump(String8& dump) {
5754 dump.append(INDENT2 "Joystick Input Mapper:\n");
5755
Jeff Brown6f2fba42011-02-19 01:08:02 -08005756 dump.append(INDENT3 "Axes:\n");
5757 size_t numAxes = mAxes.size();
5758 for (size_t i = 0; i < numAxes; i++) {
5759 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005760 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005761 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005762 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005763 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005764 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005765 }
Jeff Brown85297452011-03-04 13:07:49 -08005766 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5767 label = getAxisLabel(axis.axisInfo.highAxis);
5768 if (label) {
5769 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5770 } else {
5771 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5772 axis.axisInfo.splitValue);
5773 }
5774 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5775 dump.append(" (invert)");
5776 }
5777
5778 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5779 axis.min, axis.max, axis.flat, axis.fuzz);
5780 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5781 "highScale=%0.5f, highOffset=%0.5f\n",
5782 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005783 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5784 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005785 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005786 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005787 }
5788}
5789
Jeff Brown65fd2512011-08-18 11:20:58 -07005790void JoystickInputMapper::configure(nsecs_t when,
5791 const InputReaderConfiguration* config, uint32_t changes) {
5792 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005793
Jeff Brown474dcb52011-06-14 20:22:50 -07005794 if (!changes) { // first time only
5795 // Collect all axes.
5796 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07005797 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5798 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5799 continue; // axis must be claimed by a different device
5800 }
5801
Jeff Brown474dcb52011-06-14 20:22:50 -07005802 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005803 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005804 if (rawAxisInfo.valid) {
5805 // Map axis.
5806 AxisInfo axisInfo;
5807 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5808 if (!explicitlyMapped) {
5809 // Axis is not explicitly mapped, will choose a generic axis later.
5810 axisInfo.mode = AxisInfo::MODE_NORMAL;
5811 axisInfo.axis = -1;
5812 }
5813
5814 // Apply flat override.
5815 int32_t rawFlat = axisInfo.flatOverride < 0
5816 ? rawAxisInfo.flat : axisInfo.flatOverride;
5817
5818 // Calculate scaling factors and limits.
5819 Axis axis;
5820 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5821 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5822 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5823 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5824 scale, 0.0f, highScale, 0.0f,
5825 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5826 } else if (isCenteredAxis(axisInfo.axis)) {
5827 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5828 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5829 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5830 scale, offset, scale, offset,
5831 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5832 } else {
5833 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5834 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5835 scale, 0.0f, scale, 0.0f,
5836 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5837 }
5838
5839 // To eliminate noise while the joystick is at rest, filter out small variations
5840 // in axis values up front.
5841 axis.filter = axis.flat * 0.25f;
5842
5843 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005844 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005845 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005846
Jeff Brown474dcb52011-06-14 20:22:50 -07005847 // If there are too many axes, start dropping them.
5848 // Prefer to keep explicitly mapped axes.
5849 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00005850 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07005851 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5852 pruneAxes(true);
5853 pruneAxes(false);
5854 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005855
Jeff Brown474dcb52011-06-14 20:22:50 -07005856 // Assign generic axis ids to remaining axes.
5857 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5858 size_t numAxes = mAxes.size();
5859 for (size_t i = 0; i < numAxes; i++) {
5860 Axis& axis = mAxes.editValueAt(i);
5861 if (axis.axisInfo.axis < 0) {
5862 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5863 && haveAxis(nextGenericAxisId)) {
5864 nextGenericAxisId += 1;
5865 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005866
Jeff Brown474dcb52011-06-14 20:22:50 -07005867 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5868 axis.axisInfo.axis = nextGenericAxisId;
5869 nextGenericAxisId += 1;
5870 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00005871 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07005872 "have already been assigned to other axes.",
5873 getDeviceName().string(), mAxes.keyAt(i));
5874 mAxes.removeItemsAt(i--);
5875 numAxes -= 1;
5876 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005877 }
5878 }
5879 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005880}
5881
Jeff Brown85297452011-03-04 13:07:49 -08005882bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005883 size_t numAxes = mAxes.size();
5884 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005885 const Axis& axis = mAxes.valueAt(i);
5886 if (axis.axisInfo.axis == axisId
5887 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5888 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005889 return true;
5890 }
5891 }
5892 return false;
5893}
Jeff Browncb1404e2011-01-15 18:14:15 -08005894
Jeff Brown6f2fba42011-02-19 01:08:02 -08005895void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5896 size_t i = mAxes.size();
5897 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5898 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5899 continue;
5900 }
Steve Block6215d3f2012-01-04 20:05:49 +00005901 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005902 getDeviceName().string(), mAxes.keyAt(i));
5903 mAxes.removeItemsAt(i);
5904 }
5905}
5906
5907bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5908 switch (axis) {
5909 case AMOTION_EVENT_AXIS_X:
5910 case AMOTION_EVENT_AXIS_Y:
5911 case AMOTION_EVENT_AXIS_Z:
5912 case AMOTION_EVENT_AXIS_RX:
5913 case AMOTION_EVENT_AXIS_RY:
5914 case AMOTION_EVENT_AXIS_RZ:
5915 case AMOTION_EVENT_AXIS_HAT_X:
5916 case AMOTION_EVENT_AXIS_HAT_Y:
5917 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005918 case AMOTION_EVENT_AXIS_RUDDER:
5919 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005920 return true;
5921 default:
5922 return false;
5923 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005924}
5925
Jeff Brown65fd2512011-08-18 11:20:58 -07005926void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005927 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005928 size_t numAxes = mAxes.size();
5929 for (size_t i = 0; i < numAxes; i++) {
5930 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005931 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005932 }
5933
Jeff Brown65fd2512011-08-18 11:20:58 -07005934 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005935}
5936
5937void JoystickInputMapper::process(const RawEvent* rawEvent) {
5938 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005939 case EV_ABS: {
Jeff Brown49ccac52012-04-11 18:27:33 -07005940 ssize_t index = mAxes.indexOfKey(rawEvent->code);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005941 if (index >= 0) {
5942 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005943 float newValue, highNewValue;
5944 switch (axis.axisInfo.mode) {
5945 case AxisInfo::MODE_INVERT:
5946 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5947 * axis.scale + axis.offset;
5948 highNewValue = 0.0f;
5949 break;
5950 case AxisInfo::MODE_SPLIT:
5951 if (rawEvent->value < axis.axisInfo.splitValue) {
5952 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5953 * axis.scale + axis.offset;
5954 highNewValue = 0.0f;
5955 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5956 newValue = 0.0f;
5957 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5958 * axis.highScale + axis.highOffset;
5959 } else {
5960 newValue = 0.0f;
5961 highNewValue = 0.0f;
5962 }
5963 break;
5964 default:
5965 newValue = rawEvent->value * axis.scale + axis.offset;
5966 highNewValue = 0.0f;
5967 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005968 }
Jeff Brown85297452011-03-04 13:07:49 -08005969 axis.newValue = newValue;
5970 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005971 }
5972 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005973 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005974
5975 case EV_SYN:
Jeff Brown49ccac52012-04-11 18:27:33 -07005976 switch (rawEvent->code) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005977 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005978 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005979 break;
5980 }
5981 break;
5982 }
5983}
5984
Jeff Brown6f2fba42011-02-19 01:08:02 -08005985void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005986 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005987 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005988 }
5989
5990 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005991 int32_t buttonState = 0;
5992
5993 PointerProperties pointerProperties;
5994 pointerProperties.clear();
5995 pointerProperties.id = 0;
5996 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005997
Jeff Brown6f2fba42011-02-19 01:08:02 -08005998 PointerCoords pointerCoords;
5999 pointerCoords.clear();
6000
6001 size_t numAxes = mAxes.size();
6002 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006003 const Axis& axis = mAxes.valueAt(i);
6004 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
6005 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6006 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
6007 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006008 }
6009
Jeff Brown56194eb2011-03-02 19:23:13 -08006010 // Moving a joystick axis should not wake the devide because joysticks can
6011 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
6012 // button will likely wake the device.
6013 // TODO: Use the input device configuration to control this behavior more finely.
6014 uint32_t policyFlags = 0;
6015
Jeff Brownbe1aa822011-07-27 16:04:54 -07006016 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006017 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
6018 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006019 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006020}
6021
Jeff Brown85297452011-03-04 13:07:49 -08006022bool JoystickInputMapper::filterAxes(bool force) {
6023 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006024 size_t numAxes = mAxes.size();
6025 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006026 Axis& axis = mAxes.editValueAt(i);
6027 if (force || hasValueChangedSignificantly(axis.filter,
6028 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6029 axis.currentValue = axis.newValue;
6030 atLeastOneSignificantChange = true;
6031 }
6032 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6033 if (force || hasValueChangedSignificantly(axis.filter,
6034 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6035 axis.highCurrentValue = axis.highNewValue;
6036 atLeastOneSignificantChange = true;
6037 }
6038 }
6039 }
6040 return atLeastOneSignificantChange;
6041}
6042
6043bool JoystickInputMapper::hasValueChangedSignificantly(
6044 float filter, float newValue, float currentValue, float min, float max) {
6045 if (newValue != currentValue) {
6046 // Filter out small changes in value unless the value is converging on the axis
6047 // bounds or center point. This is intended to reduce the amount of information
6048 // sent to applications by particularly noisy joysticks (such as PS3).
6049 if (fabs(newValue - currentValue) > filter
6050 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6051 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6052 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6053 return true;
6054 }
6055 }
6056 return false;
6057}
6058
6059bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6060 float filter, float newValue, float currentValue, float thresholdValue) {
6061 float newDistance = fabs(newValue - thresholdValue);
6062 if (newDistance < filter) {
6063 float oldDistance = fabs(currentValue - thresholdValue);
6064 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006065 return true;
6066 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006067 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006068 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006069}
6070
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006071} // namespace android