blob: ddd870d806141113b57763927083f835b3ccc714 [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);
526 device->getDeviceInfo(& deviceInfo);
527 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700528
Jeff Brownbe1aa822011-07-27 16:04:54 -0700529 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
530 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
531 }
532 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
533 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
534 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
535 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
536 }
537 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
538 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
539 }
540 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700541
Jeff Brownbe1aa822011-07-27 16:04:54 -0700542 mInputConfiguration.touchScreen = touchScreenConfig;
543 mInputConfiguration.keyboard = keyboardConfig;
544 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700545}
546
Jeff Brownbe1aa822011-07-27 16:04:54 -0700547void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800548 mDisableVirtualKeysTimeout = time;
549}
550
Jeff Brownbe1aa822011-07-27 16:04:54 -0700551bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800552 InputDevice* device, int32_t keyCode, int32_t scanCode) {
553 if (now < mDisableVirtualKeysTimeout) {
Steve Block6215d3f2012-01-04 20:05:49 +0000554 ALOGI("Dropping virtual key from device %s because virtual keys are "
Jeff Brownfe508922011-01-18 15:10:10 -0800555 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
556 device->getName().string(),
557 (mDisableVirtualKeysTimeout - now) * 0.000001,
558 keyCode, scanCode);
559 return true;
560 } else {
561 return false;
562 }
563}
564
Jeff Brownbe1aa822011-07-27 16:04:54 -0700565void InputReader::fadePointerLocked() {
566 for (size_t i = 0; i < mDevices.size(); i++) {
567 InputDevice* device = mDevices.valueAt(i);
568 device->fadePointer();
569 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800570}
571
Jeff Brownbe1aa822011-07-27 16:04:54 -0700572void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700573 if (when < mNextTimeout) {
574 mNextTimeout = when;
575 }
576}
577
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700579 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582}
583
584status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700585 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700586
Jeff Brownbe1aa822011-07-27 16:04:54 -0700587 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
588 if (deviceIndex < 0) {
589 return NAME_NOT_FOUND;
590 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700591
Jeff Brownbe1aa822011-07-27 16:04:54 -0700592 InputDevice* device = mDevices.valueAt(deviceIndex);
593 if (device->isIgnored()) {
594 return NAME_NOT_FOUND;
595 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700596
Jeff Brownbe1aa822011-07-27 16:04:54 -0700597 device->getDeviceInfo(outDeviceInfo);
598 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700599}
600
601void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700602 AutoMutex _l(mLock);
603
Jeff Brown6d0fec22010-07-23 21:28:06 -0700604 outDeviceIds.clear();
605
Jeff Brownbe1aa822011-07-27 16:04:54 -0700606 size_t numDevices = mDevices.size();
607 for (size_t i = 0; i < numDevices; i++) {
608 InputDevice* device = mDevices.valueAt(i);
609 if (!device->isIgnored()) {
610 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700611 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700612 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700613}
614
615int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
616 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700617 AutoMutex _l(mLock);
618
619 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700620}
621
622int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
623 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700624 AutoMutex _l(mLock);
625
626 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700627}
628
629int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700630 AutoMutex _l(mLock);
631
632 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700633}
634
Jeff Brownbe1aa822011-07-27 16:04:54 -0700635int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700636 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700637 int32_t result = AKEY_STATE_UNKNOWN;
638 if (deviceId >= 0) {
639 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
640 if (deviceIndex >= 0) {
641 InputDevice* device = mDevices.valueAt(deviceIndex);
642 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
643 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700644 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700645 }
646 } else {
647 size_t numDevices = mDevices.size();
648 for (size_t i = 0; i < numDevices; i++) {
649 InputDevice* device = mDevices.valueAt(i);
650 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
David Deephanphongsfbca5962011-11-14 14:50:45 -0800651 // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that
652 // value. Otherwise, return AKEY_STATE_UP as long as one device reports it.
653 int32_t currentResult = (device->*getStateFunc)(sourceMask, code);
654 if (currentResult >= AKEY_STATE_DOWN) {
655 return currentResult;
656 } else if (currentResult == AKEY_STATE_UP) {
657 result = currentResult;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700658 }
659 }
660 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700661 }
662 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700663}
664
665bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
666 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700667 AutoMutex _l(mLock);
668
Jeff Brown6d0fec22010-07-23 21:28:06 -0700669 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700670 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700671}
672
Jeff Brownbe1aa822011-07-27 16:04:54 -0700673bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
674 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
675 bool result = false;
676 if (deviceId >= 0) {
677 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
678 if (deviceIndex >= 0) {
679 InputDevice* device = mDevices.valueAt(deviceIndex);
680 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
681 result = device->markSupportedKeyCodes(sourceMask,
682 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700683 }
684 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700685 } else {
686 size_t numDevices = mDevices.size();
687 for (size_t i = 0; i < numDevices; i++) {
688 InputDevice* device = mDevices.valueAt(i);
689 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
690 result |= device->markSupportedKeyCodes(sourceMask,
691 numCodes, keyCodes, outFlags);
692 }
693 }
694 }
695 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700696}
697
Jeff Brown474dcb52011-06-14 20:22:50 -0700698void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700699 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700700
Jeff Brownbe1aa822011-07-27 16:04:54 -0700701 if (changes) {
702 bool needWake = !mConfigurationChangesToRefresh;
703 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700704
705 if (needWake) {
706 mEventHub->wake();
707 }
708 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700709}
710
Jeff Brownb88102f2010-09-08 11:49:43 -0700711void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700712 AutoMutex _l(mLock);
713
Jeff Brownf2f487182010-10-01 17:46:21 -0700714 mEventHub->dump(dump);
715 dump.append("\n");
716
717 dump.append("Input Reader State:\n");
718
Jeff Brownbe1aa822011-07-27 16:04:54 -0700719 for (size_t i = 0; i < mDevices.size(); i++) {
720 mDevices.valueAt(i)->dump(dump);
721 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700722
723 dump.append(INDENT "Configuration:\n");
724 dump.append(INDENT2 "ExcludedDeviceNames: [");
725 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
726 if (i != 0) {
727 dump.append(", ");
728 }
729 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
730 }
731 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700732 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
733 mConfig.virtualKeyQuietTime * 0.000001f);
734
Jeff Brown19c97d462011-06-01 12:33:19 -0700735 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
736 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
737 mConfig.pointerVelocityControlParameters.scale,
738 mConfig.pointerVelocityControlParameters.lowThreshold,
739 mConfig.pointerVelocityControlParameters.highThreshold,
740 mConfig.pointerVelocityControlParameters.acceleration);
741
742 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
743 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
744 mConfig.wheelVelocityControlParameters.scale,
745 mConfig.wheelVelocityControlParameters.lowThreshold,
746 mConfig.wheelVelocityControlParameters.highThreshold,
747 mConfig.wheelVelocityControlParameters.acceleration);
748
Jeff Brown214eaf42011-05-26 19:17:02 -0700749 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700750 dump.appendFormat(INDENT3 "Enabled: %s\n",
751 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700752 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
753 mConfig.pointerGestureQuietInterval * 0.000001f);
754 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
755 mConfig.pointerGestureDragMinSwitchSpeed);
756 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
757 mConfig.pointerGestureTapInterval * 0.000001f);
758 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
759 mConfig.pointerGestureTapDragInterval * 0.000001f);
760 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
761 mConfig.pointerGestureTapSlop);
762 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
763 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700764 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
765 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700766 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
767 mConfig.pointerGestureSwipeTransitionAngleCosine);
768 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
769 mConfig.pointerGestureSwipeMaxWidthRatio);
770 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
771 mConfig.pointerGestureMovementSpeedRatio);
772 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
773 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700774}
775
Jeff Brown89ef0722011-08-10 16:25:21 -0700776void InputReader::monitor() {
777 // Acquire and release the lock to ensure that the reader has not deadlocked.
778 mLock.lock();
Jeff Brown112b5f52012-01-27 17:32:06 -0800779 mEventHub->wake();
780 mReaderIsAliveCondition.wait(mLock);
Jeff Brown89ef0722011-08-10 16:25:21 -0700781 mLock.unlock();
782
783 // Check the EventHub
784 mEventHub->monitor();
785}
786
Jeff Brown6d0fec22010-07-23 21:28:06 -0700787
Jeff Brownbe1aa822011-07-27 16:04:54 -0700788// --- InputReader::ContextImpl ---
789
790InputReader::ContextImpl::ContextImpl(InputReader* reader) :
791 mReader(reader) {
792}
793
794void InputReader::ContextImpl::updateGlobalMetaState() {
795 // lock is already held by the input loop
796 mReader->updateGlobalMetaStateLocked();
797}
798
799int32_t InputReader::ContextImpl::getGlobalMetaState() {
800 // lock is already held by the input loop
801 return mReader->getGlobalMetaStateLocked();
802}
803
804void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
805 // lock is already held by the input loop
806 mReader->disableVirtualKeysUntilLocked(time);
807}
808
809bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
810 InputDevice* device, int32_t keyCode, int32_t scanCode) {
811 // lock is already held by the input loop
812 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
813}
814
815void InputReader::ContextImpl::fadePointer() {
816 // lock is already held by the input loop
817 mReader->fadePointerLocked();
818}
819
820void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
821 // lock is already held by the input loop
822 mReader->requestTimeoutAtTimeLocked(when);
823}
824
825InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
826 return mReader->mPolicy.get();
827}
828
829InputListenerInterface* InputReader::ContextImpl::getListener() {
830 return mReader->mQueuedListener.get();
831}
832
833EventHubInterface* InputReader::ContextImpl::getEventHub() {
834 return mReader->mEventHub.get();
835}
836
837
Jeff Brown6d0fec22010-07-23 21:28:06 -0700838// --- InputReaderThread ---
839
840InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
841 Thread(/*canCallJava*/ true), mReader(reader) {
842}
843
844InputReaderThread::~InputReaderThread() {
845}
846
847bool InputReaderThread::threadLoop() {
848 mReader->loopOnce();
849 return true;
850}
851
852
853// --- InputDevice ---
854
Jeff Browne38fdfa2012-04-06 14:51:01 -0700855InputDevice::InputDevice(InputReaderContext* context, int32_t id,
856 const InputDeviceIdentifier& identifier, uint32_t classes) :
857 mContext(context), mId(id), mIdentifier(identifier), mClasses(classes),
Jeff Brown9ee285af2011-08-31 12:56:34 -0700858 mSources(0), mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700859}
860
861InputDevice::~InputDevice() {
862 size_t numMappers = mMappers.size();
863 for (size_t i = 0; i < numMappers; i++) {
864 delete mMappers[i];
865 }
866 mMappers.clear();
867}
868
Jeff Brownef3d7e82010-09-30 14:33:04 -0700869void InputDevice::dump(String8& dump) {
870 InputDeviceInfo deviceInfo;
871 getDeviceInfo(& deviceInfo);
872
Jeff Brown90655042010-12-02 13:50:46 -0800873 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700874 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800875 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700876 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
877 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800878
Jeff Brownefd32662011-03-08 15:13:06 -0800879 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800880 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700881 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800882 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800883 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
884 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800885 char name[32];
886 if (label) {
887 strncpy(name, label, sizeof(name));
888 name[sizeof(name) - 1] = '\0';
889 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800890 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800891 }
Jeff Brownefd32662011-03-08 15:13:06 -0800892 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
893 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
894 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800895 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700896 }
897
898 size_t numMappers = mMappers.size();
899 for (size_t i = 0; i < numMappers; i++) {
900 InputMapper* mapper = mMappers[i];
901 mapper->dump(dump);
902 }
903}
904
Jeff Brown6d0fec22010-07-23 21:28:06 -0700905void InputDevice::addMapper(InputMapper* mapper) {
906 mMappers.add(mapper);
907}
908
Jeff Brown65fd2512011-08-18 11:20:58 -0700909void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700910 mSources = 0;
911
Jeff Brown474dcb52011-06-14 20:22:50 -0700912 if (!isIgnored()) {
913 if (!changes) { // first time only
914 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
915 }
916
917 size_t numMappers = mMappers.size();
918 for (size_t i = 0; i < numMappers; i++) {
919 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700920 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700921 mSources |= mapper->getSources();
922 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700923 }
924}
925
Jeff Brown65fd2512011-08-18 11:20:58 -0700926void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700927 size_t numMappers = mMappers.size();
928 for (size_t i = 0; i < numMappers; i++) {
929 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700930 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700931 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700932
933 mContext->updateGlobalMetaState();
934
935 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700936}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700937
Jeff Brownb7198742011-03-18 18:14:26 -0700938void InputDevice::process(const RawEvent* rawEvents, size_t count) {
939 // Process all of the events in order for each mapper.
940 // We cannot simply ask each mapper to process them in bulk because mappers may
941 // have side-effects that must be interleaved. For example, joystick movement events and
942 // gamepad button presses are handled by different mappers but they should be dispatched
943 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700944 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700945 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
946#if DEBUG_RAW_EVENTS
Steve Block5baa3a62011-12-20 16:23:08 +0000947 ALOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700948 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700949 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
950 rawEvent->value, rawEvent->flags);
951#endif
952
Jeff Brown80fd47c2011-05-24 01:07:44 -0700953 if (mDropUntilNextSync) {
954 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
955 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 }
964 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == 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) {
1093 switch (rawEvent->scanCode) {
1094 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) {
1160 switch (rawEvent->scanCode) {
1161 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) {
1199 switch (rawEvent->scanCode) {
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) {
1262 switch (rawEvent->scanCode) {
1263 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) {
1468 switch (rawEvent->scanCode) {
1469 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) {
1552 if (rawEvent->scanCode == ABS_MT_SLOT) {
1553 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
1571 switch (rawEvent->scanCode) {
1572 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 }
1627 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1628 // 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:
1758 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1759 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 Brown1e08fe92011-11-15 17:48:10 -08001792 info->setKeyCharacterMapFile(getEventHub()->getKeyCharacterMapFile(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 Brown6d0fec22010-07-23 21:28:06 -07001850
Jeff Brownbe1aa822011-07-27 16:04:54 -07001851 resetLedState();
1852
Jeff Brown65fd2512011-08-18 11:20:58 -07001853 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001854}
1855
1856void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1857 switch (rawEvent->type) {
1858 case EV_KEY: {
1859 int32_t scanCode = rawEvent->scanCode;
1860 if (isKeyboardOrGamepadKey(scanCode)) {
1861 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1862 rawEvent->flags);
1863 }
1864 break;
1865 }
1866 }
1867}
1868
1869bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1870 return scanCode < BTN_MOUSE
1871 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001872 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001873 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001874}
1875
Jeff Brown6328cdc2010-07-29 18:18:33 -07001876void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1877 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001878
Jeff Brownbe1aa822011-07-27 16:04:54 -07001879 if (down) {
1880 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001881 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001882 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001883 }
Jeff Brownfe508922011-01-18 15:10:10 -08001884
Jeff Brownbe1aa822011-07-27 16:04:54 -07001885 // Add key down.
1886 ssize_t keyDownIndex = findKeyDown(scanCode);
1887 if (keyDownIndex >= 0) {
1888 // key repeat, be sure to use same keycode as before in case of rotation
1889 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001890 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001891 // key down
1892 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1893 && mContext->shouldDropVirtualKey(when,
1894 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001895 return;
1896 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001897
1898 mKeyDowns.push();
1899 KeyDown& keyDown = mKeyDowns.editTop();
1900 keyDown.keyCode = keyCode;
1901 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001902 }
1903
Jeff Brownbe1aa822011-07-27 16:04:54 -07001904 mDownTime = when;
1905 } else {
1906 // Remove key down.
1907 ssize_t keyDownIndex = findKeyDown(scanCode);
1908 if (keyDownIndex >= 0) {
1909 // key up, be sure to use same keycode as before in case of rotation
1910 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1911 mKeyDowns.removeAt(size_t(keyDownIndex));
1912 } else {
1913 // key was not actually down
Steve Block6215d3f2012-01-04 20:05:49 +00001914 ALOGI("Dropping key up from device %s because the key was not down. "
Jeff Brownbe1aa822011-07-27 16:04:54 -07001915 "keyCode=%d, scanCode=%d",
1916 getDeviceName().string(), keyCode, scanCode);
1917 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001918 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001919 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001920
Jeff Brownbe1aa822011-07-27 16:04:54 -07001921 bool metaStateChanged = false;
1922 int32_t oldMetaState = mMetaState;
1923 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1924 if (oldMetaState != newMetaState) {
1925 mMetaState = newMetaState;
1926 metaStateChanged = true;
1927 updateLedState(false);
1928 }
1929
1930 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001931
Jeff Brown56194eb2011-03-02 19:23:13 -08001932 // Key down on external an keyboard should wake the device.
1933 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1934 // For internal keyboards, the key layout file should specify the policy flags for
1935 // each wake key individually.
1936 // TODO: Use the input device configuration to control this behavior more finely.
1937 if (down && getDevice()->isExternal()
1938 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1939 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1940 }
1941
Jeff Brown6328cdc2010-07-29 18:18:33 -07001942 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001943 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001944 }
1945
Jeff Brown05dc66a2011-03-02 14:41:58 -08001946 if (down && !isMetaKey(keyCode)) {
1947 getContext()->fadePointer();
1948 }
1949
Jeff Brownbe1aa822011-07-27 16:04:54 -07001950 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001951 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1952 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001953 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001954}
1955
Jeff Brownbe1aa822011-07-27 16:04:54 -07001956ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1957 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001958 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001959 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001960 return i;
1961 }
1962 }
1963 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001964}
1965
Jeff Brown6d0fec22010-07-23 21:28:06 -07001966int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1967 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1968}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001969
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1971 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1972}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001973
Jeff Brown6d0fec22010-07-23 21:28:06 -07001974bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1975 const int32_t* keyCodes, uint8_t* outFlags) {
1976 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1977}
1978
1979int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001980 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001981}
1982
Jeff Brownbe1aa822011-07-27 16:04:54 -07001983void KeyboardInputMapper::resetLedState() {
1984 initializeLedState(mCapsLockLedState, LED_CAPSL);
1985 initializeLedState(mNumLockLedState, LED_NUML);
1986 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001987
Jeff Brownbe1aa822011-07-27 16:04:54 -07001988 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001989}
1990
Jeff Brownbe1aa822011-07-27 16:04:54 -07001991void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001992 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1993 ledState.on = false;
1994}
1995
Jeff Brownbe1aa822011-07-27 16:04:54 -07001996void KeyboardInputMapper::updateLedState(bool reset) {
1997 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001998 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001999 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002000 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002001 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07002002 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07002003}
2004
Jeff Brownbe1aa822011-07-27 16:04:54 -07002005void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07002006 int32_t led, int32_t modifier, bool reset) {
2007 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002008 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002009 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07002010 getEventHub()->setLedState(getDeviceId(), led, desiredState);
2011 ledState.on = desiredState;
2012 }
2013 }
2014}
2015
Jeff Brown6d0fec22010-07-23 21:28:06 -07002016
Jeff Brown83c09682010-12-23 17:50:18 -08002017// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002018
Jeff Brown83c09682010-12-23 17:50:18 -08002019CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002020 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002021}
2022
Jeff Brown83c09682010-12-23 17:50:18 -08002023CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002024}
2025
Jeff Brown83c09682010-12-23 17:50:18 -08002026uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002027 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002028}
2029
Jeff Brown83c09682010-12-23 17:50:18 -08002030void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002031 InputMapper::populateDeviceInfo(info);
2032
Jeff Brown83c09682010-12-23 17:50:18 -08002033 if (mParameters.mode == Parameters::MODE_POINTER) {
2034 float minX, minY, maxX, maxY;
2035 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002036 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2037 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002038 }
2039 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002040 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2041 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002042 }
Jeff Brownefd32662011-03-08 15:13:06 -08002043 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002044
Jeff Brown65fd2512011-08-18 11:20:58 -07002045 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002046 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002047 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002048 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002049 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002050 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002051}
2052
Jeff Brown83c09682010-12-23 17:50:18 -08002053void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002054 dump.append(INDENT2 "Cursor Input Mapper:\n");
2055 dumpParameters(dump);
2056 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2057 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2058 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2059 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2060 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002061 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002062 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002063 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002064 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2065 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002066 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002067 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2068 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2069 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002070}
2071
Jeff Brown65fd2512011-08-18 11:20:58 -07002072void CursorInputMapper::configure(nsecs_t when,
2073 const InputReaderConfiguration* config, uint32_t changes) {
2074 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002075
Jeff Brown474dcb52011-06-14 20:22:50 -07002076 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002077 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002078
Jeff Brown474dcb52011-06-14 20:22:50 -07002079 // Configure basic parameters.
2080 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002081
Jeff Brown474dcb52011-06-14 20:22:50 -07002082 // Configure device mode.
2083 switch (mParameters.mode) {
2084 case Parameters::MODE_POINTER:
2085 mSource = AINPUT_SOURCE_MOUSE;
2086 mXPrecision = 1.0f;
2087 mYPrecision = 1.0f;
2088 mXScale = 1.0f;
2089 mYScale = 1.0f;
2090 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2091 break;
2092 case Parameters::MODE_NAVIGATION:
2093 mSource = AINPUT_SOURCE_TRACKBALL;
2094 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2095 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2096 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2097 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2098 break;
2099 }
2100
2101 mVWheelScale = 1.0f;
2102 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002103 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002104
Jeff Brown474dcb52011-06-14 20:22:50 -07002105 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2106 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2107 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2108 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2109 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002110
2111 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2112 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2113 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2114 false /*external*/, NULL, NULL, &mOrientation)) {
2115 mOrientation = DISPLAY_ORIENTATION_0;
2116 }
2117 } else {
2118 mOrientation = DISPLAY_ORIENTATION_0;
2119 }
2120 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002121}
2122
Jeff Brown83c09682010-12-23 17:50:18 -08002123void CursorInputMapper::configureParameters() {
2124 mParameters.mode = Parameters::MODE_POINTER;
2125 String8 cursorModeString;
2126 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2127 if (cursorModeString == "navigation") {
2128 mParameters.mode = Parameters::MODE_NAVIGATION;
2129 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002130 ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
Jeff Brown83c09682010-12-23 17:50:18 -08002131 }
2132 }
2133
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002134 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002135 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002136 mParameters.orientationAware);
2137
Jeff Brownbc68a592011-07-25 12:58:12 -07002138 mParameters.associatedDisplayId = -1;
2139 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2140 mParameters.associatedDisplayId = 0;
2141 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002142}
2143
Jeff Brown83c09682010-12-23 17:50:18 -08002144void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002145 dump.append(INDENT3 "Parameters:\n");
2146 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2147 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002148
2149 switch (mParameters.mode) {
2150 case Parameters::MODE_POINTER:
2151 dump.append(INDENT4 "Mode: pointer\n");
2152 break;
2153 case Parameters::MODE_NAVIGATION:
2154 dump.append(INDENT4 "Mode: navigation\n");
2155 break;
2156 default:
Steve Blockec193de2012-01-09 18:35:44 +00002157 ALOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002158 }
2159
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002160 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2161 toString(mParameters.orientationAware));
2162}
2163
Jeff Brown65fd2512011-08-18 11:20:58 -07002164void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002165 mButtonState = 0;
2166 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002167
Jeff Brownbe1aa822011-07-27 16:04:54 -07002168 mPointerVelocityControl.reset();
2169 mWheelXVelocityControl.reset();
2170 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002171
Jeff Brown65fd2512011-08-18 11:20:58 -07002172 mCursorButtonAccumulator.reset(getDevice());
2173 mCursorMotionAccumulator.reset(getDevice());
2174 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002175
Jeff Brown65fd2512011-08-18 11:20:58 -07002176 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002177}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002178
Jeff Brown83c09682010-12-23 17:50:18 -08002179void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002180 mCursorButtonAccumulator.process(rawEvent);
2181 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002182 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002183
Jeff Brown49754db2011-07-01 17:37:58 -07002184 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2185 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002186 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002187}
2188
Jeff Brown83c09682010-12-23 17:50:18 -08002189void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002190 int32_t lastButtonState = mButtonState;
2191 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2192 mButtonState = currentButtonState;
2193
2194 bool wasDown = isPointerDown(lastButtonState);
2195 bool down = isPointerDown(currentButtonState);
2196 bool downChanged;
2197 if (!wasDown && down) {
2198 mDownTime = when;
2199 downChanged = true;
2200 } else if (wasDown && !down) {
2201 downChanged = true;
2202 } else {
2203 downChanged = false;
2204 }
2205 nsecs_t downTime = mDownTime;
2206 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002207 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002208
2209 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2210 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2211 bool moved = deltaX != 0 || deltaY != 0;
2212
Jeff Brown65fd2512011-08-18 11:20:58 -07002213 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2215 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002216 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002217 }
2218
Jeff Brown65fd2512011-08-18 11:20:58 -07002219 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002220 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002221 pointerProperties.clear();
2222 pointerProperties.id = 0;
2223 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2224
Jeff Brown6328cdc2010-07-29 18:18:33 -07002225 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002226 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002227
Jeff Brown65fd2512011-08-18 11:20:58 -07002228 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2229 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002230 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002231
Jeff Brownbe1aa822011-07-27 16:04:54 -07002232 mWheelYVelocityControl.move(when, NULL, &vscroll);
2233 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002234
Jeff Brownbe1aa822011-07-27 16:04:54 -07002235 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002236
Jeff Brownbe1aa822011-07-27 16:04:54 -07002237 if (mPointerController != NULL) {
2238 if (moved || scrolled || buttonsChanged) {
2239 mPointerController->setPresentation(
2240 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002241
Jeff Brownbe1aa822011-07-27 16:04:54 -07002242 if (moved) {
2243 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002244 }
2245
Jeff Brownbe1aa822011-07-27 16:04:54 -07002246 if (buttonsChanged) {
2247 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002248 }
Jeff Brownefd32662011-03-08 15:13:06 -08002249
Jeff Brownbe1aa822011-07-27 16:04:54 -07002250 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002251 }
2252
Jeff Brownbe1aa822011-07-27 16:04:54 -07002253 float x, y;
2254 mPointerController->getPosition(&x, &y);
2255 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2256 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2257 } else {
2258 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2259 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2260 }
2261
2262 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002263
Jeff Brown56194eb2011-03-02 19:23:13 -08002264 // Moving an external trackball or mouse should wake the device.
2265 // We don't do this for internal cursor devices to prevent them from waking up
2266 // the device in your pocket.
2267 // TODO: Use the input device configuration to control this behavior more finely.
2268 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002269 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002270 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2271 }
2272
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002273 // Synthesize key down from buttons if needed.
2274 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2275 policyFlags, lastButtonState, currentButtonState);
2276
2277 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002278 if (downChanged || moved || scrolled || buttonsChanged) {
2279 int32_t metaState = mContext->getGlobalMetaState();
2280 int32_t motionEventAction;
2281 if (downChanged) {
2282 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2283 } else if (down || mPointerController == NULL) {
2284 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2285 } else {
2286 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2287 }
Jeff Brownb6997262010-10-08 22:31:17 -07002288
Jeff Brownbe1aa822011-07-27 16:04:54 -07002289 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2290 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002291 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002292 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002293
Jeff Brownbe1aa822011-07-27 16:04:54 -07002294 // Send hover move after UP to tell the application that the mouse is hovering now.
2295 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2296 && mPointerController != NULL) {
2297 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2298 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2299 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2300 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2301 getListener()->notifyMotion(&hoverArgs);
2302 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002303
Jeff Brownbe1aa822011-07-27 16:04:54 -07002304 // Send scroll events.
2305 if (scrolled) {
2306 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2307 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2308
2309 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2310 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2311 AMOTION_EVENT_EDGE_FLAG_NONE,
2312 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2313 getListener()->notifyMotion(&scrollArgs);
2314 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002315 }
Jeff Browna032cc02011-03-07 16:56:21 -08002316
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002317 // Synthesize key up from buttons if needed.
2318 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2319 policyFlags, lastButtonState, currentButtonState);
2320
Jeff Brown65fd2512011-08-18 11:20:58 -07002321 mCursorMotionAccumulator.finishSync();
2322 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002323}
2324
Jeff Brown83c09682010-12-23 17:50:18 -08002325int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002326 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2327 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2328 } else {
2329 return AKEY_STATE_UNKNOWN;
2330 }
2331}
2332
Jeff Brown05dc66a2011-03-02 14:41:58 -08002333void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002334 if (mPointerController != NULL) {
2335 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2336 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002337}
2338
Jeff Brown6d0fec22010-07-23 21:28:06 -07002339
2340// --- TouchInputMapper ---
2341
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002342TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002343 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002344 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002345 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002346}
2347
2348TouchInputMapper::~TouchInputMapper() {
2349}
2350
2351uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002352 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002353}
2354
2355void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2356 InputMapper::populateDeviceInfo(info);
2357
Jeff Brown65fd2512011-08-18 11:20:58 -07002358 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2359 info->addMotionRange(mOrientedRanges.x);
2360 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002361 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002362
Jeff Brown65fd2512011-08-18 11:20:58 -07002363 if (mOrientedRanges.haveSize) {
2364 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002365 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002366
2367 if (mOrientedRanges.haveTouchSize) {
2368 info->addMotionRange(mOrientedRanges.touchMajor);
2369 info->addMotionRange(mOrientedRanges.touchMinor);
2370 }
2371
2372 if (mOrientedRanges.haveToolSize) {
2373 info->addMotionRange(mOrientedRanges.toolMajor);
2374 info->addMotionRange(mOrientedRanges.toolMinor);
2375 }
2376
2377 if (mOrientedRanges.haveOrientation) {
2378 info->addMotionRange(mOrientedRanges.orientation);
2379 }
2380
2381 if (mOrientedRanges.haveDistance) {
2382 info->addMotionRange(mOrientedRanges.distance);
2383 }
2384
2385 if (mOrientedRanges.haveTilt) {
2386 info->addMotionRange(mOrientedRanges.tilt);
2387 }
2388
2389 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2390 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2391 }
2392 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2393 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2394 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002395 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002396}
2397
Jeff Brownef3d7e82010-09-30 14:33:04 -07002398void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002399 dump.append(INDENT2 "Touch Input Mapper:\n");
2400 dumpParameters(dump);
2401 dumpVirtualKeys(dump);
2402 dumpRawPointerAxes(dump);
2403 dumpCalibration(dump);
2404 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002405
Jeff Brownbe1aa822011-07-27 16:04:54 -07002406 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2407 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2408 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2409 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2410 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2411 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002412 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2413 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002414 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002415 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2416 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002417 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2418 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2419 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2420 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2421 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002422
Jeff Brownbe1aa822011-07-27 16:04:54 -07002423 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002424
Jeff Brownbe1aa822011-07-27 16:04:54 -07002425 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2426 mLastRawPointerData.pointerCount);
2427 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2428 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2429 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2430 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002431 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2432 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002433 pointer.id, pointer.x, pointer.y, pointer.pressure,
2434 pointer.touchMajor, pointer.touchMinor,
2435 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002436 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002437 pointer.toolType, toString(pointer.isHovering));
2438 }
2439
2440 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2441 mLastCookedPointerData.pointerCount);
2442 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2443 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2444 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2445 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2446 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002447 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2448 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002449 pointerProperties.id,
2450 pointerCoords.getX(),
2451 pointerCoords.getY(),
2452 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2453 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2454 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2455 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2456 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2457 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002458 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002459 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2460 pointerProperties.toolType,
2461 toString(mLastCookedPointerData.isHovering(i)));
2462 }
2463
Jeff Brown65fd2512011-08-18 11:20:58 -07002464 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002465 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2466 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002467 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002468 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002469 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002470 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002471 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002472 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002473 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002474 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2475 mPointerGestureMaxSwipeWidth);
2476 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002477}
2478
Jeff Brown65fd2512011-08-18 11:20:58 -07002479void TouchInputMapper::configure(nsecs_t when,
2480 const InputReaderConfiguration* config, uint32_t changes) {
2481 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002482
Jeff Brown474dcb52011-06-14 20:22:50 -07002483 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002484
Jeff Brown474dcb52011-06-14 20:22:50 -07002485 if (!changes) { // first time only
2486 // Configure basic parameters.
2487 configureParameters();
2488
Jeff Brown65fd2512011-08-18 11:20:58 -07002489 // Configure common accumulators.
2490 mCursorScrollAccumulator.configure(getDevice());
2491 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002492
2493 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002494 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002495
2496 // Prepare input device calibration.
2497 parseCalibration();
2498 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002499 }
2500
Jeff Brown474dcb52011-06-14 20:22:50 -07002501 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002502 // Update pointer speed.
2503 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2504 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2505 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002506 }
Jeff Brown8d608662010-08-30 03:02:23 -07002507
Jeff Brown65fd2512011-08-18 11:20:58 -07002508 bool resetNeeded = false;
2509 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002510 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2511 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002512 // Configure device sources, surface dimensions, orientation and
2513 // scaling factors.
2514 configureSurface(when, &resetNeeded);
2515 }
2516
2517 if (changes && resetNeeded) {
2518 // Send reset, unless this is the first time the device has been configured,
2519 // in which case the reader will call reset itself after all mappers are ready.
2520 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002521 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002522}
2523
Jeff Brown8d608662010-08-30 03:02:23 -07002524void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002525 // Use the pointer presentation mode for devices that do not support distinct
2526 // multitouch. The spot-based presentation relies on being able to accurately
2527 // locate two or more fingers on the touch pad.
2528 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2529 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002530
Jeff Brown538881e2011-05-25 18:23:38 -07002531 String8 gestureModeString;
2532 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2533 gestureModeString)) {
2534 if (gestureModeString == "pointer") {
2535 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2536 } else if (gestureModeString == "spots") {
2537 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2538 } else if (gestureModeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002539 ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
Jeff Brown538881e2011-05-25 18:23:38 -07002540 }
2541 }
2542
Jeff Browndeffe072011-08-26 18:38:46 -07002543 if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2544 // The device is a touch screen.
2545 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
2546 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2547 // The device is a pointing device like a track pad.
2548 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2549 } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
Jeff Brownace13b12011-03-09 17:39:48 -08002550 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2551 // The device is a cursor device with a touch pad attached.
2552 // By default don't use the touch pad to move the pointer.
2553 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
2554 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002555 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002556 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2557 }
2558
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002559 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002560 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2561 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002562 if (deviceTypeString == "touchScreen") {
2563 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002564 } else if (deviceTypeString == "touchPad") {
2565 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002566 } else if (deviceTypeString == "pointer") {
2567 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002568 } else if (deviceTypeString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00002569 ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002570 }
2571 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002572
Jeff Brownefd32662011-03-08 15:13:06 -08002573 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002574 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2575 mParameters.orientationAware);
2576
Jeff Brownbc68a592011-07-25 12:58:12 -07002577 mParameters.associatedDisplayId = -1;
2578 mParameters.associatedDisplayIsExternal = false;
2579 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002580 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002581 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2582 mParameters.associatedDisplayIsExternal =
2583 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2584 && getDevice()->isExternal();
2585 mParameters.associatedDisplayId = 0;
2586 }
Jeff Brown8d608662010-08-30 03:02:23 -07002587}
2588
Jeff Brownef3d7e82010-09-30 14:33:04 -07002589void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002590 dump.append(INDENT3 "Parameters:\n");
2591
Jeff Brown538881e2011-05-25 18:23:38 -07002592 switch (mParameters.gestureMode) {
2593 case Parameters::GESTURE_MODE_POINTER:
2594 dump.append(INDENT4 "GestureMode: pointer\n");
2595 break;
2596 case Parameters::GESTURE_MODE_SPOTS:
2597 dump.append(INDENT4 "GestureMode: spots\n");
2598 break;
2599 default:
2600 assert(false);
2601 }
2602
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002603 switch (mParameters.deviceType) {
2604 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2605 dump.append(INDENT4 "DeviceType: touchScreen\n");
2606 break;
2607 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2608 dump.append(INDENT4 "DeviceType: touchPad\n");
2609 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002610 case Parameters::DEVICE_TYPE_POINTER:
2611 dump.append(INDENT4 "DeviceType: pointer\n");
2612 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002613 default:
Steve Blockec193de2012-01-09 18:35:44 +00002614 ALOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002615 }
2616
Jeff Brown65fd2512011-08-18 11:20:58 -07002617 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2618 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002619 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2620 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002621}
2622
Jeff Brownbe1aa822011-07-27 16:04:54 -07002623void TouchInputMapper::configureRawPointerAxes() {
2624 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002625}
2626
Jeff Brownbe1aa822011-07-27 16:04:54 -07002627void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2628 dump.append(INDENT3 "Raw Touch Axes:\n");
2629 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2630 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2631 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2632 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2633 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2634 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2635 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2636 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2637 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002638 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2639 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002640 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2641 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002642}
2643
Jeff Brown65fd2512011-08-18 11:20:58 -07002644void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2645 int32_t oldDeviceMode = mDeviceMode;
2646
2647 // Determine device mode.
2648 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2649 && mConfig.pointerGesturesEnabled) {
2650 mSource = AINPUT_SOURCE_MOUSE;
2651 mDeviceMode = DEVICE_MODE_POINTER;
2652 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2653 && mParameters.associatedDisplayId >= 0) {
2654 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2655 mDeviceMode = DEVICE_MODE_DIRECT;
2656 } else {
2657 mSource = AINPUT_SOURCE_TOUCHPAD;
2658 mDeviceMode = DEVICE_MODE_UNSCALED;
2659 }
2660
Jeff Brown9626b142011-03-03 02:09:54 -08002661 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002662 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Steve Block8564c8d2012-01-05 23:22:43 +00002663 ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
Jeff Brown9626b142011-03-03 02:09:54 -08002664 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002665 mDeviceMode = DEVICE_MODE_DISABLED;
2666 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002667 }
2668
Jeff Brown65fd2512011-08-18 11:20:58 -07002669 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002670 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002671 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002672 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002673 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2674 &mAssociatedDisplayOrientation)) {
Steve Block6215d3f2012-01-04 20:05:49 +00002675 ALOGI(INDENT "Touch device '%s' could not query the properties of its associated "
Jeff Brown65fd2512011-08-18 11:20:58 -07002676 "display %d. The device will be inoperable until the display size "
2677 "becomes available.",
2678 getDeviceName().string(), mParameters.associatedDisplayId);
2679 mDeviceMode = DEVICE_MODE_DISABLED;
2680 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002681 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002682 }
2683
Jeff Brown65fd2512011-08-18 11:20:58 -07002684 // Configure dimensions.
2685 int32_t width, height, orientation;
2686 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2687 width = mAssociatedDisplayWidth;
2688 height = mAssociatedDisplayHeight;
2689 orientation = mParameters.orientationAware ?
2690 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2691 } else {
2692 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2693 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2694 orientation = DISPLAY_ORIENTATION_0;
2695 }
2696
2697 // If moving between pointer modes, need to reset some state.
2698 bool deviceModeChanged;
2699 if (mDeviceMode != oldDeviceMode) {
2700 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002701 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002702 }
2703
Jeff Browndaf4a122011-08-26 17:14:14 -07002704 // Create pointer controller if needed.
2705 if (mDeviceMode == DEVICE_MODE_POINTER ||
2706 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2707 if (mPointerController == NULL) {
2708 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2709 }
2710 } else {
2711 mPointerController.clear();
2712 }
2713
Jeff Brownbe1aa822011-07-27 16:04:54 -07002714 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002715 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002716 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002717 }
2718
Jeff Brownbe1aa822011-07-27 16:04:54 -07002719 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002720 if (sizeChanged || deviceModeChanged) {
Steve Block6215d3f2012-01-04 20:05:49 +00002721 ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
Jeff Brown65fd2512011-08-18 11:20:58 -07002722 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002723
Jeff Brownbe1aa822011-07-27 16:04:54 -07002724 mSurfaceWidth = width;
2725 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002726
Jeff Brown8d608662010-08-30 03:02:23 -07002727 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002728 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2729 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2730 mXPrecision = 1.0f / mXScale;
2731 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002732
Jeff Brownbe1aa822011-07-27 16:04:54 -07002733 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002734 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002735 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002736 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002737
Jeff Brownbe1aa822011-07-27 16:04:54 -07002738 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002739
Jeff Brown8d608662010-08-30 03:02:23 -07002740 // Scale factor for terms that are not oriented in a particular axis.
2741 // If the pixels are square then xScale == yScale otherwise we fake it
2742 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002743 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002744
Jeff Brown8d608662010-08-30 03:02:23 -07002745 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002746 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002747
Jeff Browna1f89ce2011-08-11 00:05:01 -07002748 // Size factors.
2749 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2750 if (mRawPointerAxes.touchMajor.valid
2751 && mRawPointerAxes.touchMajor.maxValue != 0) {
2752 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2753 } else if (mRawPointerAxes.toolMajor.valid
2754 && mRawPointerAxes.toolMajor.maxValue != 0) {
2755 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2756 } else {
2757 mSizeScale = 0.0f;
2758 }
2759
Jeff Brownbe1aa822011-07-27 16:04:54 -07002760 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002761 mOrientedRanges.haveToolSize = true;
2762 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002763
Jeff Brownbe1aa822011-07-27 16:04:54 -07002764 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002765 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002766 mOrientedRanges.touchMajor.min = 0;
2767 mOrientedRanges.touchMajor.max = diagonalSize;
2768 mOrientedRanges.touchMajor.flat = 0;
2769 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002770
Jeff Brownbe1aa822011-07-27 16:04:54 -07002771 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2772 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002773
Jeff Brownbe1aa822011-07-27 16:04:54 -07002774 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002775 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002776 mOrientedRanges.toolMajor.min = 0;
2777 mOrientedRanges.toolMajor.max = diagonalSize;
2778 mOrientedRanges.toolMajor.flat = 0;
2779 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002780
Jeff Brownbe1aa822011-07-27 16:04:54 -07002781 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2782 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002783
2784 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002785 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002786 mOrientedRanges.size.min = 0;
2787 mOrientedRanges.size.max = 1.0;
2788 mOrientedRanges.size.flat = 0;
2789 mOrientedRanges.size.fuzz = 0;
2790 } else {
2791 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002792 }
2793
2794 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002795 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002796 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2797 || mCalibration.pressureCalibration
2798 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2799 if (mCalibration.havePressureScale) {
2800 mPressureScale = mCalibration.pressureScale;
2801 } else if (mRawPointerAxes.pressure.valid
2802 && mRawPointerAxes.pressure.maxValue != 0) {
2803 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002804 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002805 }
Jeff Brown8d608662010-08-30 03:02:23 -07002806
Jeff Brown65fd2512011-08-18 11:20:58 -07002807 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2808 mOrientedRanges.pressure.source = mSource;
2809 mOrientedRanges.pressure.min = 0;
2810 mOrientedRanges.pressure.max = 1.0;
2811 mOrientedRanges.pressure.flat = 0;
2812 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002813
Jeff Brown65fd2512011-08-18 11:20:58 -07002814 // Tilt
2815 mTiltXCenter = 0;
2816 mTiltXScale = 0;
2817 mTiltYCenter = 0;
2818 mTiltYScale = 0;
2819 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2820 if (mHaveTilt) {
2821 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2822 mRawPointerAxes.tiltX.maxValue);
2823 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2824 mRawPointerAxes.tiltY.maxValue);
2825 mTiltXScale = M_PI / 180;
2826 mTiltYScale = M_PI / 180;
2827
2828 mOrientedRanges.haveTilt = true;
2829
2830 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2831 mOrientedRanges.tilt.source = mSource;
2832 mOrientedRanges.tilt.min = 0;
2833 mOrientedRanges.tilt.max = M_PI_2;
2834 mOrientedRanges.tilt.flat = 0;
2835 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002836 }
2837
Jeff Brown8d608662010-08-30 03:02:23 -07002838 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002839 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002840 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002841 if (mHaveTilt) {
2842 mOrientedRanges.haveOrientation = true;
2843
2844 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2845 mOrientedRanges.orientation.source = mSource;
2846 mOrientedRanges.orientation.min = -M_PI;
2847 mOrientedRanges.orientation.max = M_PI;
2848 mOrientedRanges.orientation.flat = 0;
2849 mOrientedRanges.orientation.fuzz = 0;
2850 } else if (mCalibration.orientationCalibration !=
2851 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002852 if (mCalibration.orientationCalibration
2853 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002854 if (mRawPointerAxes.orientation.valid) {
2855 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2856 mRawPointerAxes.orientation.maxValue);
2857 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2858 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002859 }
2860 }
2861
Jeff Brownbe1aa822011-07-27 16:04:54 -07002862 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002863
Jeff Brownbe1aa822011-07-27 16:04:54 -07002864 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002865 mOrientedRanges.orientation.source = mSource;
2866 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002867 mOrientedRanges.orientation.max = M_PI_2;
2868 mOrientedRanges.orientation.flat = 0;
2869 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002870 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002871
2872 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002873 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002874 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2875 if (mCalibration.distanceCalibration
2876 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2877 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002878 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002879 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002880 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002881 }
2882 }
2883
Jeff Brownbe1aa822011-07-27 16:04:54 -07002884 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002885
Jeff Brownbe1aa822011-07-27 16:04:54 -07002886 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002887 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002888 mOrientedRanges.distance.min =
2889 mRawPointerAxes.distance.minValue * mDistanceScale;
2890 mOrientedRanges.distance.max =
2891 mRawPointerAxes.distance.minValue * mDistanceScale;
2892 mOrientedRanges.distance.flat = 0;
2893 mOrientedRanges.distance.fuzz =
2894 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002895 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002896 }
2897
Jeff Brown65fd2512011-08-18 11:20:58 -07002898 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002899 // Compute oriented surface dimensions, precision, scales and ranges.
2900 // Note that the maximum value reported is an inclusive maximum value so it is one
2901 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002902 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002903 case DISPLAY_ORIENTATION_90:
2904 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002905 mOrientedSurfaceWidth = mSurfaceHeight;
2906 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002907
Jeff Brownbe1aa822011-07-27 16:04:54 -07002908 mOrientedXPrecision = mYPrecision;
2909 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002910
Jeff Brownbe1aa822011-07-27 16:04:54 -07002911 mOrientedRanges.x.min = 0;
2912 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2913 * mYScale;
2914 mOrientedRanges.x.flat = 0;
2915 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002916
Jeff Brownbe1aa822011-07-27 16:04:54 -07002917 mOrientedRanges.y.min = 0;
2918 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2919 * mXScale;
2920 mOrientedRanges.y.flat = 0;
2921 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002922 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002923
Jeff Brown6d0fec22010-07-23 21:28:06 -07002924 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002925 mOrientedSurfaceWidth = mSurfaceWidth;
2926 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002927
Jeff Brownbe1aa822011-07-27 16:04:54 -07002928 mOrientedXPrecision = mXPrecision;
2929 mOrientedYPrecision = mYPrecision;
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.x.maxValue - mRawPointerAxes.x.minValue)
2933 * mXScale;
2934 mOrientedRanges.x.flat = 0;
2935 mOrientedRanges.x.fuzz = mXScale;
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.y.maxValue - mRawPointerAxes.y.minValue)
2939 * mYScale;
2940 mOrientedRanges.y.flat = 0;
2941 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002942 break;
2943 }
Jeff Brownace13b12011-03-09 17:39:48 -08002944
2945 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002946 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002947 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2948 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002949 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002950 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2951 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002952
Jeff Brown2352b972011-04-12 22:39:53 -07002953 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002954 // given area relative to the diagonal size of the display when no acceleration
2955 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002956 // Assume that the touch pad has a square aspect ratio such that movements in
2957 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002958 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002959 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002960 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002961
2962 // Scale zooms to cover a smaller range of the display than movements do.
2963 // This value determines the area around the pointer that is affected by freeform
2964 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002965 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002966 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002967 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002968
Jeff Brown2352b972011-04-12 22:39:53 -07002969 // Max width between pointers to detect a swipe gesture is more than some fraction
2970 // of the diagonal axis of the touch pad. Touches that are wider than this are
2971 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002972 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002973 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002974 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002975
Jeff Brown65fd2512011-08-18 11:20:58 -07002976 // Abort current pointer usages because the state has changed.
2977 abortPointerUsage(when, 0 /*policyFlags*/);
2978
2979 // Inform the dispatcher about the changes.
2980 *outResetNeeded = true;
2981 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002982}
2983
Jeff Brownbe1aa822011-07-27 16:04:54 -07002984void TouchInputMapper::dumpSurface(String8& dump) {
2985 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2986 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2987 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002988}
2989
Jeff Brownbe1aa822011-07-27 16:04:54 -07002990void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002991 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002992 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002993
Jeff Brownbe1aa822011-07-27 16:04:54 -07002994 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002995
Jeff Brown6328cdc2010-07-29 18:18:33 -07002996 if (virtualKeyDefinitions.size() == 0) {
2997 return;
2998 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002999
Jeff Brownbe1aa822011-07-27 16:04:54 -07003000 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07003001
Jeff Brownbe1aa822011-07-27 16:04:54 -07003002 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
3003 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
3004 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
3005 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003006
3007 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07003008 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07003009 virtualKeyDefinitions[i];
3010
Jeff Brownbe1aa822011-07-27 16:04:54 -07003011 mVirtualKeys.add();
3012 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07003013
3014 virtualKey.scanCode = virtualKeyDefinition.scanCode;
3015 int32_t keyCode;
3016 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08003017 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07003018 & keyCode, & flags)) {
Steve Block8564c8d2012-01-05 23:22:43 +00003019 ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
Jeff Brown8d608662010-08-30 03:02:23 -07003020 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003021 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003022 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003023 }
3024
Jeff Brown6328cdc2010-07-29 18:18:33 -07003025 virtualKey.keyCode = keyCode;
3026 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003027
Jeff Brown6328cdc2010-07-29 18:18:33 -07003028 // convert the key definition's display coordinates into touch coordinates for a hit box
3029 int32_t halfWidth = virtualKeyDefinition.width / 2;
3030 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003031
Jeff Brown6328cdc2010-07-29 18:18:33 -07003032 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003033 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003034 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003035 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003036 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003037 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003038 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003039 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003040 }
3041}
3042
Jeff Brownbe1aa822011-07-27 16:04:54 -07003043void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3044 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003045 dump.append(INDENT3 "Virtual Keys:\n");
3046
Jeff Brownbe1aa822011-07-27 16:04:54 -07003047 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3048 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003049 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3050 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3051 i, virtualKey.scanCode, virtualKey.keyCode,
3052 virtualKey.hitLeft, virtualKey.hitRight,
3053 virtualKey.hitTop, virtualKey.hitBottom);
3054 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003055 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003056}
3057
Jeff Brown8d608662010-08-30 03:02:23 -07003058void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003059 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003060 Calibration& out = mCalibration;
3061
Jeff Browna1f89ce2011-08-11 00:05:01 -07003062 // Size
3063 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3064 String8 sizeCalibrationString;
3065 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3066 if (sizeCalibrationString == "none") {
3067 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3068 } else if (sizeCalibrationString == "geometric") {
3069 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3070 } else if (sizeCalibrationString == "diameter") {
3071 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3072 } else if (sizeCalibrationString == "area") {
3073 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3074 } else if (sizeCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003075 ALOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Browna1f89ce2011-08-11 00:05:01 -07003076 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003077 }
3078 }
3079
Jeff Browna1f89ce2011-08-11 00:05:01 -07003080 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3081 out.sizeScale);
3082 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3083 out.sizeBias);
3084 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3085 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003086
3087 // Pressure
3088 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3089 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003090 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003091 if (pressureCalibrationString == "none") {
3092 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3093 } else if (pressureCalibrationString == "physical") {
3094 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3095 } else if (pressureCalibrationString == "amplitude") {
3096 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3097 } else if (pressureCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003098 ALOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003099 pressureCalibrationString.string());
3100 }
3101 }
3102
Jeff Brown8d608662010-08-30 03:02:23 -07003103 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3104 out.pressureScale);
3105
Jeff Brown8d608662010-08-30 03:02:23 -07003106 // Orientation
3107 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3108 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003109 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003110 if (orientationCalibrationString == "none") {
3111 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3112 } else if (orientationCalibrationString == "interpolated") {
3113 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003114 } else if (orientationCalibrationString == "vector") {
3115 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003116 } else if (orientationCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003117 ALOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003118 orientationCalibrationString.string());
3119 }
3120 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003121
3122 // Distance
3123 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3124 String8 distanceCalibrationString;
3125 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3126 if (distanceCalibrationString == "none") {
3127 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3128 } else if (distanceCalibrationString == "scaled") {
3129 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3130 } else if (distanceCalibrationString != "default") {
Steve Block8564c8d2012-01-05 23:22:43 +00003131 ALOGW("Invalid value for touch.distance.calibration: '%s'",
Jeff Brown80fd47c2011-05-24 01:07:44 -07003132 distanceCalibrationString.string());
3133 }
3134 }
3135
3136 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3137 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003138}
3139
3140void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003141 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003142 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3143 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3144 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003145 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003146 } else {
3147 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3148 }
Jeff Brown8d608662010-08-30 03:02:23 -07003149
Jeff Browna1f89ce2011-08-11 00:05:01 -07003150 // Pressure
3151 if (mRawPointerAxes.pressure.valid) {
3152 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3153 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3154 }
3155 } else {
3156 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003157 }
3158
3159 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003160 if (mRawPointerAxes.orientation.valid) {
3161 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003162 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003163 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003164 } else {
3165 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003166 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003167
3168 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003169 if (mRawPointerAxes.distance.valid) {
3170 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003171 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003172 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003173 } else {
3174 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003175 }
Jeff Brown8d608662010-08-30 03:02:23 -07003176}
3177
Jeff Brownef3d7e82010-09-30 14:33:04 -07003178void TouchInputMapper::dumpCalibration(String8& dump) {
3179 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003180
Jeff Browna1f89ce2011-08-11 00:05:01 -07003181 // Size
3182 switch (mCalibration.sizeCalibration) {
3183 case Calibration::SIZE_CALIBRATION_NONE:
3184 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003185 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003186 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3187 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003188 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003189 case Calibration::SIZE_CALIBRATION_DIAMETER:
3190 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3191 break;
3192 case Calibration::SIZE_CALIBRATION_AREA:
3193 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003194 break;
3195 default:
Steve Blockec193de2012-01-09 18:35:44 +00003196 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003197 }
3198
Jeff Browna1f89ce2011-08-11 00:05:01 -07003199 if (mCalibration.haveSizeScale) {
3200 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3201 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003202 }
3203
Jeff Browna1f89ce2011-08-11 00:05:01 -07003204 if (mCalibration.haveSizeBias) {
3205 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3206 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003207 }
3208
Jeff Browna1f89ce2011-08-11 00:05:01 -07003209 if (mCalibration.haveSizeIsSummed) {
3210 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3211 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003212 }
3213
3214 // Pressure
3215 switch (mCalibration.pressureCalibration) {
3216 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003217 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003218 break;
3219 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003220 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003221 break;
3222 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003223 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003224 break;
3225 default:
Steve Blockec193de2012-01-09 18:35:44 +00003226 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003227 }
3228
Jeff Brown8d608662010-08-30 03:02:23 -07003229 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003230 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3231 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003232 }
3233
Jeff Brown8d608662010-08-30 03:02:23 -07003234 // Orientation
3235 switch (mCalibration.orientationCalibration) {
3236 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003237 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003238 break;
3239 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003240 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003241 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003242 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3243 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3244 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003245 default:
Steve Blockec193de2012-01-09 18:35:44 +00003246 ALOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003247 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003248
3249 // Distance
3250 switch (mCalibration.distanceCalibration) {
3251 case Calibration::DISTANCE_CALIBRATION_NONE:
3252 dump.append(INDENT4 "touch.distance.calibration: none\n");
3253 break;
3254 case Calibration::DISTANCE_CALIBRATION_SCALED:
3255 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3256 break;
3257 default:
Steve Blockec193de2012-01-09 18:35:44 +00003258 ALOG_ASSERT(false);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003259 }
3260
3261 if (mCalibration.haveDistanceScale) {
3262 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3263 mCalibration.distanceScale);
3264 }
Jeff Brown8d608662010-08-30 03:02:23 -07003265}
3266
Jeff Brown65fd2512011-08-18 11:20:58 -07003267void TouchInputMapper::reset(nsecs_t when) {
3268 mCursorButtonAccumulator.reset(getDevice());
3269 mCursorScrollAccumulator.reset(getDevice());
3270 mTouchButtonAccumulator.reset(getDevice());
3271
3272 mPointerVelocityControl.reset();
3273 mWheelXVelocityControl.reset();
3274 mWheelYVelocityControl.reset();
3275
Jeff Brownbe1aa822011-07-27 16:04:54 -07003276 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003277 mLastRawPointerData.clear();
3278 mCurrentCookedPointerData.clear();
3279 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003280 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003281 mLastButtonState = 0;
3282 mCurrentRawVScroll = 0;
3283 mCurrentRawHScroll = 0;
3284 mCurrentFingerIdBits.clear();
3285 mLastFingerIdBits.clear();
3286 mCurrentStylusIdBits.clear();
3287 mLastStylusIdBits.clear();
3288 mCurrentMouseIdBits.clear();
3289 mLastMouseIdBits.clear();
3290 mPointerUsage = POINTER_USAGE_NONE;
3291 mSentHoverEnter = false;
3292 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003293
Jeff Brown65fd2512011-08-18 11:20:58 -07003294 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003295
Jeff Brown65fd2512011-08-18 11:20:58 -07003296 mPointerGesture.reset();
3297 mPointerSimple.reset();
3298
3299 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003300 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3301 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003302 }
3303
Jeff Brown65fd2512011-08-18 11:20:58 -07003304 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003305}
3306
Jeff Brown65fd2512011-08-18 11:20:58 -07003307void TouchInputMapper::process(const RawEvent* rawEvent) {
3308 mCursorButtonAccumulator.process(rawEvent);
3309 mCursorScrollAccumulator.process(rawEvent);
3310 mTouchButtonAccumulator.process(rawEvent);
3311
3312 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3313 sync(rawEvent->when);
3314 }
3315}
3316
3317void TouchInputMapper::sync(nsecs_t when) {
3318 // Sync button state.
3319 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3320 | mCursorButtonAccumulator.getButtonState();
3321
3322 // Sync scroll state.
3323 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3324 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3325 mCursorScrollAccumulator.finishSync();
3326
3327 // Sync touch state.
3328 bool havePointerIds = true;
3329 mCurrentRawPointerData.clear();
3330 syncTouch(when, &havePointerIds);
3331
Jeff Brownaa3855d2011-03-17 01:34:19 -07003332#if DEBUG_RAW_EVENTS
3333 if (!havePointerIds) {
Steve Block5baa3a62011-12-20 16:23:08 +00003334 ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003335 mLastRawPointerData.pointerCount,
3336 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003337 } else {
Steve Block5baa3a62011-12-20 16:23:08 +00003338 ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07003339 "hovering ids 0x%08x -> 0x%08x",
3340 mLastRawPointerData.pointerCount,
3341 mCurrentRawPointerData.pointerCount,
3342 mLastRawPointerData.touchingIdBits.value,
3343 mCurrentRawPointerData.touchingIdBits.value,
3344 mLastRawPointerData.hoveringIdBits.value,
3345 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003346 }
3347#endif
3348
Jeff Brown65fd2512011-08-18 11:20:58 -07003349 // Reset state that we will compute below.
3350 mCurrentFingerIdBits.clear();
3351 mCurrentStylusIdBits.clear();
3352 mCurrentMouseIdBits.clear();
3353 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003354
Jeff Brown65fd2512011-08-18 11:20:58 -07003355 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3356 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003357 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003358 mCurrentButtonState = 0;
3359 } else {
3360 // Preprocess pointer data.
3361 if (!havePointerIds) {
3362 assignPointerIds();
3363 }
3364
3365 // Handle policy on initial down or hover events.
3366 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003367 bool initialDown = mLastRawPointerData.pointerCount == 0
3368 && mCurrentRawPointerData.pointerCount != 0;
3369 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3370 if (initialDown || buttonsPressed) {
3371 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003372 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003373 getContext()->fadePointer();
3374 }
3375
3376 // Initial downs on external touch devices should wake the device.
3377 // We don't do this for internal touch screens to prevent them from waking
3378 // up in your pocket.
3379 // TODO: Use the input device configuration to control this behavior more finely.
3380 if (getDevice()->isExternal()) {
3381 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3382 }
3383 }
3384
3385 // Synthesize key down from raw buttons if needed.
3386 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3387 policyFlags, mLastButtonState, mCurrentButtonState);
3388
3389 // Consume raw off-screen touches before cooking pointer data.
3390 // If touches are consumed, subsequent code will not receive any pointer data.
3391 if (consumeRawTouches(when, policyFlags)) {
3392 mCurrentRawPointerData.clear();
3393 }
3394
3395 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3396 // with cooked pointer data that has the same ids and indices as the raw data.
3397 // The following code can use either the raw or cooked data, as needed.
3398 cookPointerData();
3399
3400 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003401 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003402 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3403 uint32_t id = idBits.clearFirstMarkedBit();
3404 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3405 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3406 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3407 mCurrentStylusIdBits.markBit(id);
3408 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3409 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3410 mCurrentFingerIdBits.markBit(id);
3411 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3412 mCurrentMouseIdBits.markBit(id);
3413 }
3414 }
3415 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3416 uint32_t id = idBits.clearFirstMarkedBit();
3417 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3418 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3419 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3420 mCurrentStylusIdBits.markBit(id);
3421 }
3422 }
3423
3424 // Stylus takes precedence over all tools, then mouse, then finger.
3425 PointerUsage pointerUsage = mPointerUsage;
3426 if (!mCurrentStylusIdBits.isEmpty()) {
3427 mCurrentMouseIdBits.clear();
3428 mCurrentFingerIdBits.clear();
3429 pointerUsage = POINTER_USAGE_STYLUS;
3430 } else if (!mCurrentMouseIdBits.isEmpty()) {
3431 mCurrentFingerIdBits.clear();
3432 pointerUsage = POINTER_USAGE_MOUSE;
3433 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3434 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003435 }
3436
3437 dispatchPointerUsage(when, policyFlags, pointerUsage);
3438 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003439 if (mDeviceMode == DEVICE_MODE_DIRECT
3440 && mConfig.showTouches && mPointerController != NULL) {
3441 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3442 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3443
3444 mPointerController->setButtonState(mCurrentButtonState);
3445 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3446 mCurrentCookedPointerData.idToIndex,
3447 mCurrentCookedPointerData.touchingIdBits);
3448 }
3449
Jeff Brown65fd2512011-08-18 11:20:58 -07003450 dispatchHoverExit(when, policyFlags);
3451 dispatchTouches(when, policyFlags);
3452 dispatchHoverEnterAndMove(when, policyFlags);
3453 }
3454
3455 // Synthesize key up from raw buttons if needed.
3456 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3457 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003458 }
3459
Jeff Brown6328cdc2010-07-29 18:18:33 -07003460 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003461 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3462 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3463 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003464 mLastFingerIdBits = mCurrentFingerIdBits;
3465 mLastStylusIdBits = mCurrentStylusIdBits;
3466 mLastMouseIdBits = mCurrentMouseIdBits;
3467
3468 // Clear some transient state.
3469 mCurrentRawVScroll = 0;
3470 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003471}
3472
Jeff Brown79ac9692011-04-19 21:20:10 -07003473void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003474 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003475 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3476 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3477 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003478 }
3479}
3480
Jeff Brownbe1aa822011-07-27 16:04:54 -07003481bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3482 // Check for release of a virtual key.
3483 if (mCurrentVirtualKey.down) {
3484 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3485 // Pointer went up while virtual key was down.
3486 mCurrentVirtualKey.down = false;
3487 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003488#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003489 ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003490 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003491#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003492 dispatchVirtualKey(when, policyFlags,
3493 AKEY_EVENT_ACTION_UP,
3494 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003495 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003496 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003497 }
3498
Jeff Brownbe1aa822011-07-27 16:04:54 -07003499 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3500 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3501 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3502 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3503 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3504 // Pointer is still within the space of the virtual key.
3505 return true;
3506 }
3507 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003508
Jeff Brownbe1aa822011-07-27 16:04:54 -07003509 // Pointer left virtual key area or another pointer also went down.
3510 // Send key cancellation but do not consume the touch yet.
3511 // This is useful when the user swipes through from the virtual key area
3512 // into the main display surface.
3513 mCurrentVirtualKey.down = false;
3514 if (!mCurrentVirtualKey.ignored) {
3515#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003516 ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003517 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3518#endif
3519 dispatchVirtualKey(when, policyFlags,
3520 AKEY_EVENT_ACTION_UP,
3521 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3522 | AKEY_EVENT_FLAG_CANCELED);
3523 }
3524 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003525
Jeff Brownbe1aa822011-07-27 16:04:54 -07003526 if (mLastRawPointerData.touchingIdBits.isEmpty()
3527 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3528 // Pointer just went down. Check for virtual key press or off-screen touches.
3529 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3530 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3531 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3532 // If exactly one pointer went down, check for virtual key hit.
3533 // Otherwise we will drop the entire stroke.
3534 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3535 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3536 if (virtualKey) {
3537 mCurrentVirtualKey.down = true;
3538 mCurrentVirtualKey.downTime = when;
3539 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3540 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3541 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3542 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3543
3544 if (!mCurrentVirtualKey.ignored) {
3545#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00003546 ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003547 mCurrentVirtualKey.keyCode,
3548 mCurrentVirtualKey.scanCode);
3549#endif
3550 dispatchVirtualKey(when, policyFlags,
3551 AKEY_EVENT_ACTION_DOWN,
3552 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3553 }
3554 }
3555 }
3556 return true;
3557 }
3558 }
3559
Jeff Brownfe508922011-01-18 15:10:10 -08003560 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003561 // most recent touch within the screen area. The idea is to filter out stray
3562 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003563 //
3564 // Problems we're trying to solve:
3565 //
3566 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3567 // virtual key area that is implemented by a separate touch panel and accidentally
3568 // triggers a virtual key.
3569 //
3570 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3571 // area and accidentally triggers a virtual key. This often happens when virtual keys
3572 // are layed out below the screen near to where the on screen keyboard's space bar
3573 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003574 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003575 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003576 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003577 return false;
3578}
3579
3580void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3581 int32_t keyEventAction, int32_t keyEventFlags) {
3582 int32_t keyCode = mCurrentVirtualKey.keyCode;
3583 int32_t scanCode = mCurrentVirtualKey.scanCode;
3584 nsecs_t downTime = mCurrentVirtualKey.downTime;
3585 int32_t metaState = mContext->getGlobalMetaState();
3586 policyFlags |= POLICY_FLAG_VIRTUAL;
3587
3588 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3589 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3590 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003591}
3592
Jeff Brown6d0fec22010-07-23 21:28:06 -07003593void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003594 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3595 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003596 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003597 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003598
3599 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003600 if (!currentIdBits.isEmpty()) {
3601 // No pointer id changes so this is a move event.
3602 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003603 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003604 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3605 AMOTION_EVENT_EDGE_FLAG_NONE,
3606 mCurrentCookedPointerData.pointerProperties,
3607 mCurrentCookedPointerData.pointerCoords,
3608 mCurrentCookedPointerData.idToIndex,
3609 currentIdBits, -1,
3610 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3611 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003612 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003613 // There may be pointers going up and pointers going down and pointers moving
3614 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003615 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3616 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003617 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003618 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003619
Jeff Brownace13b12011-03-09 17:39:48 -08003620 // Update last coordinates of pointers that have moved so that we observe the new
3621 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003622 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003623 mCurrentCookedPointerData.pointerProperties,
3624 mCurrentCookedPointerData.pointerCoords,
3625 mCurrentCookedPointerData.idToIndex,
3626 mLastCookedPointerData.pointerProperties,
3627 mLastCookedPointerData.pointerCoords,
3628 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003629 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003630 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003631 moveNeeded = true;
3632 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003633
Jeff Brownace13b12011-03-09 17:39:48 -08003634 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003635 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003636 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003637
Jeff Brown65fd2512011-08-18 11:20:58 -07003638 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003639 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003640 mLastCookedPointerData.pointerProperties,
3641 mLastCookedPointerData.pointerCoords,
3642 mLastCookedPointerData.idToIndex,
3643 dispatchedIdBits, upId,
3644 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003645 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003646 }
3647
Jeff Brownc3db8582010-10-20 15:33:38 -07003648 // Dispatch move events if any of the remaining pointers moved from their old locations.
3649 // Although applications receive new locations as part of individual pointer up
3650 // events, they do not generally handle them except when presented in a move event.
3651 if (moveNeeded) {
Steve Blockec193de2012-01-09 18:35:44 +00003652 ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003653 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003654 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003655 mCurrentCookedPointerData.pointerProperties,
3656 mCurrentCookedPointerData.pointerCoords,
3657 mCurrentCookedPointerData.idToIndex,
3658 dispatchedIdBits, -1,
3659 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003660 }
3661
3662 // Dispatch pointer down events using the new pointer locations.
3663 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003664 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003665 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003666
Jeff Brownace13b12011-03-09 17:39:48 -08003667 if (dispatchedIdBits.count() == 1) {
3668 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003669 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003670 }
3671
Jeff Brown65fd2512011-08-18 11:20:58 -07003672 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003673 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003674 mCurrentCookedPointerData.pointerProperties,
3675 mCurrentCookedPointerData.pointerCoords,
3676 mCurrentCookedPointerData.idToIndex,
3677 dispatchedIdBits, downId,
3678 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003679 }
3680 }
Jeff Brownace13b12011-03-09 17:39:48 -08003681}
3682
Jeff Brownbe1aa822011-07-27 16:04:54 -07003683void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3684 if (mSentHoverEnter &&
3685 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3686 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3687 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003688 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003689 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3690 mLastCookedPointerData.pointerProperties,
3691 mLastCookedPointerData.pointerCoords,
3692 mLastCookedPointerData.idToIndex,
3693 mLastCookedPointerData.hoveringIdBits, -1,
3694 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3695 mSentHoverEnter = false;
3696 }
3697}
Jeff Brownace13b12011-03-09 17:39:48 -08003698
Jeff Brownbe1aa822011-07-27 16:04:54 -07003699void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3700 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3701 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3702 int32_t metaState = getContext()->getGlobalMetaState();
3703 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003704 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003705 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3706 mCurrentCookedPointerData.pointerProperties,
3707 mCurrentCookedPointerData.pointerCoords,
3708 mCurrentCookedPointerData.idToIndex,
3709 mCurrentCookedPointerData.hoveringIdBits, -1,
3710 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3711 mSentHoverEnter = true;
3712 }
Jeff Brownace13b12011-03-09 17:39:48 -08003713
Jeff Brown65fd2512011-08-18 11:20:58 -07003714 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003715 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3716 mCurrentCookedPointerData.pointerProperties,
3717 mCurrentCookedPointerData.pointerCoords,
3718 mCurrentCookedPointerData.idToIndex,
3719 mCurrentCookedPointerData.hoveringIdBits, -1,
3720 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3721 }
3722}
3723
3724void TouchInputMapper::cookPointerData() {
3725 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3726
3727 mCurrentCookedPointerData.clear();
3728 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3729 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3730 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3731
3732 // Walk through the the active pointers and map device coordinates onto
3733 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003734 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003735 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003736
Jeff Browna1f89ce2011-08-11 00:05:01 -07003737 // Size
3738 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3739 switch (mCalibration.sizeCalibration) {
3740 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3741 case Calibration::SIZE_CALIBRATION_DIAMETER:
3742 case Calibration::SIZE_CALIBRATION_AREA:
3743 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3744 touchMajor = in.touchMajor;
3745 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3746 toolMajor = in.toolMajor;
3747 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3748 size = mRawPointerAxes.touchMinor.valid
3749 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3750 } else if (mRawPointerAxes.touchMajor.valid) {
3751 toolMajor = touchMajor = in.touchMajor;
3752 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3753 ? in.touchMinor : in.touchMajor;
3754 size = mRawPointerAxes.touchMinor.valid
3755 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3756 } else if (mRawPointerAxes.toolMajor.valid) {
3757 touchMajor = toolMajor = in.toolMajor;
3758 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3759 ? in.toolMinor : in.toolMajor;
3760 size = mRawPointerAxes.toolMinor.valid
3761 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003762 } else {
Steve Blockec193de2012-01-09 18:35:44 +00003763 ALOG_ASSERT(false, "No touch or tool axes. "
Jeff Browna1f89ce2011-08-11 00:05:01 -07003764 "Size calibration should have been resolved to NONE.");
3765 touchMajor = 0;
3766 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003767 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003768 toolMinor = 0;
3769 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003770 }
Jeff Brownace13b12011-03-09 17:39:48 -08003771
Jeff Browna1f89ce2011-08-11 00:05:01 -07003772 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3773 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3774 if (touchingCount > 1) {
3775 touchMajor /= touchingCount;
3776 touchMinor /= touchingCount;
3777 toolMajor /= touchingCount;
3778 toolMinor /= touchingCount;
3779 size /= touchingCount;
3780 }
3781 }
Jeff Brownace13b12011-03-09 17:39:48 -08003782
Jeff Browna1f89ce2011-08-11 00:05:01 -07003783 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3784 touchMajor *= mGeometricScale;
3785 touchMinor *= mGeometricScale;
3786 toolMajor *= mGeometricScale;
3787 toolMinor *= mGeometricScale;
3788 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3789 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003790 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003791 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3792 toolMinor = toolMajor;
3793 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3794 touchMinor = touchMajor;
3795 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003796 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003797
3798 mCalibration.applySizeScaleAndBias(&touchMajor);
3799 mCalibration.applySizeScaleAndBias(&touchMinor);
3800 mCalibration.applySizeScaleAndBias(&toolMajor);
3801 mCalibration.applySizeScaleAndBias(&toolMinor);
3802 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003803 break;
3804 default:
3805 touchMajor = 0;
3806 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003807 toolMajor = 0;
3808 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003809 size = 0;
3810 break;
3811 }
3812
Jeff Browna1f89ce2011-08-11 00:05:01 -07003813 // Pressure
3814 float pressure;
3815 switch (mCalibration.pressureCalibration) {
3816 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3817 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3818 pressure = in.pressure * mPressureScale;
3819 break;
3820 default:
3821 pressure = in.isHovering ? 0 : 1;
3822 break;
3823 }
3824
Jeff Brown65fd2512011-08-18 11:20:58 -07003825 // Tilt and Orientation
3826 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003827 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003828 if (mHaveTilt) {
3829 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3830 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3831 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3832 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3833 } else {
3834 tilt = 0;
3835
3836 switch (mCalibration.orientationCalibration) {
3837 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3838 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3839 break;
3840 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3841 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3842 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3843 if (c1 != 0 || c2 != 0) {
3844 orientation = atan2f(c1, c2) * 0.5f;
3845 float confidence = hypotf(c1, c2);
3846 float scale = 1.0f + confidence / 16.0f;
3847 touchMajor *= scale;
3848 touchMinor /= scale;
3849 toolMajor *= scale;
3850 toolMinor /= scale;
3851 } else {
3852 orientation = 0;
3853 }
3854 break;
3855 }
3856 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003857 orientation = 0;
3858 }
Jeff Brownace13b12011-03-09 17:39:48 -08003859 }
3860
Jeff Brown80fd47c2011-05-24 01:07:44 -07003861 // Distance
3862 float distance;
3863 switch (mCalibration.distanceCalibration) {
3864 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003865 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003866 break;
3867 default:
3868 distance = 0;
3869 }
3870
Jeff Brownace13b12011-03-09 17:39:48 -08003871 // X and Y
3872 // Adjust coords for surface orientation.
3873 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003874 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003875 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003876 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3877 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003878 orientation -= M_PI_2;
3879 if (orientation < - M_PI_2) {
3880 orientation += M_PI;
3881 }
3882 break;
3883 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003884 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3885 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003886 break;
3887 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003888 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3889 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003890 orientation += M_PI_2;
3891 if (orientation > M_PI_2) {
3892 orientation -= M_PI;
3893 }
3894 break;
3895 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003896 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3897 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003898 break;
3899 }
3900
3901 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003902 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003903 out.clear();
3904 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3905 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3906 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3907 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3908 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3909 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3910 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3911 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3912 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003913 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003914 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003915
3916 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003917 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3918 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003919 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003920 properties.id = id;
3921 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003922
Jeff Brownbe1aa822011-07-27 16:04:54 -07003923 // Write id index.
3924 mCurrentCookedPointerData.idToIndex[id] = i;
3925 }
Jeff Brownace13b12011-03-09 17:39:48 -08003926}
3927
Jeff Brown65fd2512011-08-18 11:20:58 -07003928void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3929 PointerUsage pointerUsage) {
3930 if (pointerUsage != mPointerUsage) {
3931 abortPointerUsage(when, policyFlags);
3932 mPointerUsage = pointerUsage;
3933 }
3934
3935 switch (mPointerUsage) {
3936 case POINTER_USAGE_GESTURES:
3937 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3938 break;
3939 case POINTER_USAGE_STYLUS:
3940 dispatchPointerStylus(when, policyFlags);
3941 break;
3942 case POINTER_USAGE_MOUSE:
3943 dispatchPointerMouse(when, policyFlags);
3944 break;
3945 default:
3946 break;
3947 }
3948}
3949
3950void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3951 switch (mPointerUsage) {
3952 case POINTER_USAGE_GESTURES:
3953 abortPointerGestures(when, policyFlags);
3954 break;
3955 case POINTER_USAGE_STYLUS:
3956 abortPointerStylus(when, policyFlags);
3957 break;
3958 case POINTER_USAGE_MOUSE:
3959 abortPointerMouse(when, policyFlags);
3960 break;
3961 default:
3962 break;
3963 }
3964
3965 mPointerUsage = POINTER_USAGE_NONE;
3966}
3967
Jeff Brown79ac9692011-04-19 21:20:10 -07003968void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3969 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003970 // Update current gesture coordinates.
3971 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003972 bool sendEvents = preparePointerGestures(when,
3973 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3974 if (!sendEvents) {
3975 return;
3976 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003977 if (finishPreviousGesture) {
3978 cancelPreviousGesture = false;
3979 }
Jeff Brownace13b12011-03-09 17:39:48 -08003980
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003981 // Update the pointer presentation and spots.
3982 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3983 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3984 if (finishPreviousGesture || cancelPreviousGesture) {
3985 mPointerController->clearSpots();
3986 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003987 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3988 mPointerGesture.currentGestureIdToIndex,
3989 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003990 } else {
3991 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3992 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003993
Jeff Brown538881e2011-05-25 18:23:38 -07003994 // Show or hide the pointer if needed.
3995 switch (mPointerGesture.currentGestureMode) {
3996 case PointerGesture::NEUTRAL:
3997 case PointerGesture::QUIET:
3998 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3999 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4000 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
4001 // Remind the user of where the pointer is after finishing a gesture with spots.
4002 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
4003 }
4004 break;
4005 case PointerGesture::TAP:
4006 case PointerGesture::TAP_DRAG:
4007 case PointerGesture::BUTTON_CLICK_OR_DRAG:
4008 case PointerGesture::HOVER:
4009 case PointerGesture::PRESS:
4010 // Unfade the pointer when the current gesture manipulates the
4011 // area directly under the pointer.
4012 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4013 break;
4014 case PointerGesture::SWIPE:
4015 case PointerGesture::FREEFORM:
4016 // Fade the pointer when the current gesture manipulates a different
4017 // area and there are spots to guide the user experience.
4018 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4019 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4020 } else {
4021 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4022 }
4023 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004024 }
4025
Jeff Brownace13b12011-03-09 17:39:48 -08004026 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004027 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004028 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004029
4030 // Update last coordinates of pointers that have moved so that we observe the new
4031 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004032 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4033 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4034 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004035 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004036 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4037 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4038 bool moveNeeded = false;
4039 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004040 && !mPointerGesture.lastGestureIdBits.isEmpty()
4041 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004042 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4043 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004044 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004045 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004046 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004047 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4048 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004049 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004050 moveNeeded = true;
4051 }
Jeff Brownace13b12011-03-09 17:39:48 -08004052 }
4053
4054 // Send motion events for all pointers that went up or were canceled.
4055 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4056 if (!dispatchedGestureIdBits.isEmpty()) {
4057 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004058 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004059 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4060 AMOTION_EVENT_EDGE_FLAG_NONE,
4061 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004062 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4063 dispatchedGestureIdBits, -1,
4064 0, 0, mPointerGesture.downTime);
4065
4066 dispatchedGestureIdBits.clear();
4067 } else {
4068 BitSet32 upGestureIdBits;
4069 if (finishPreviousGesture) {
4070 upGestureIdBits = dispatchedGestureIdBits;
4071 } else {
4072 upGestureIdBits.value = dispatchedGestureIdBits.value
4073 & ~mPointerGesture.currentGestureIdBits.value;
4074 }
4075 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004076 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004077
Jeff Brown65fd2512011-08-18 11:20:58 -07004078 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004079 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004080 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4081 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004082 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4083 dispatchedGestureIdBits, id,
4084 0, 0, mPointerGesture.downTime);
4085
4086 dispatchedGestureIdBits.clearBit(id);
4087 }
4088 }
4089 }
4090
4091 // Send motion events for all pointers that moved.
4092 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004093 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004094 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4095 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004096 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4097 dispatchedGestureIdBits, -1,
4098 0, 0, mPointerGesture.downTime);
4099 }
4100
4101 // Send motion events for all pointers that went down.
4102 if (down) {
4103 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4104 & ~dispatchedGestureIdBits.value);
4105 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004106 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004107 dispatchedGestureIdBits.markBit(id);
4108
Jeff Brownace13b12011-03-09 17:39:48 -08004109 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004110 mPointerGesture.downTime = when;
4111 }
4112
Jeff Brown65fd2512011-08-18 11:20:58 -07004113 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004114 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004115 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004116 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4117 dispatchedGestureIdBits, id,
4118 0, 0, mPointerGesture.downTime);
4119 }
4120 }
4121
Jeff Brownace13b12011-03-09 17:39:48 -08004122 // Send motion events for hover.
4123 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004124 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004125 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4126 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4127 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004128 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4129 mPointerGesture.currentGestureIdBits, -1,
4130 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004131 } else if (dispatchedGestureIdBits.isEmpty()
4132 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4133 // Synthesize a hover move event after all pointers go up to indicate that
4134 // the pointer is hovering again even if the user is not currently touching
4135 // the touch pad. This ensures that a view will receive a fresh hover enter
4136 // event after a tap.
4137 float x, y;
4138 mPointerController->getPosition(&x, &y);
4139
4140 PointerProperties pointerProperties;
4141 pointerProperties.clear();
4142 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004143 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004144
4145 PointerCoords pointerCoords;
4146 pointerCoords.clear();
4147 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4148 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4149
Jeff Brown65fd2512011-08-18 11:20:58 -07004150 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004151 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4152 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4153 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004154 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004155 }
4156
4157 // Update state.
4158 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4159 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004160 mPointerGesture.lastGestureIdBits.clear();
4161 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004162 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4163 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004164 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004165 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004166 mPointerGesture.lastGestureProperties[index].copyFrom(
4167 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004168 mPointerGesture.lastGestureCoords[index].copyFrom(
4169 mPointerGesture.currentGestureCoords[index]);
4170 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004171 }
4172 }
4173}
4174
Jeff Brown65fd2512011-08-18 11:20:58 -07004175void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4176 // Cancel previously dispatches pointers.
4177 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4178 int32_t metaState = getContext()->getGlobalMetaState();
4179 int32_t buttonState = mCurrentButtonState;
4180 dispatchMotion(when, policyFlags, mSource,
4181 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4182 AMOTION_EVENT_EDGE_FLAG_NONE,
4183 mPointerGesture.lastGestureProperties,
4184 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4185 mPointerGesture.lastGestureIdBits, -1,
4186 0, 0, mPointerGesture.downTime);
4187 }
4188
4189 // Reset the current pointer gesture.
4190 mPointerGesture.reset();
4191 mPointerVelocityControl.reset();
4192
4193 // Remove any current spots.
4194 if (mPointerController != NULL) {
4195 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4196 mPointerController->clearSpots();
4197 }
4198}
4199
Jeff Brown79ac9692011-04-19 21:20:10 -07004200bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4201 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004202 *outCancelPreviousGesture = false;
4203 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004204
Jeff Brown79ac9692011-04-19 21:20:10 -07004205 // Handle TAP timeout.
4206 if (isTimeout) {
4207#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004208 ALOGD("Gestures: Processing timeout");
Jeff Brown79ac9692011-04-19 21:20:10 -07004209#endif
4210
4211 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004212 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004213 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004214 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004215 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004216 } else {
4217 // The tap is finished.
4218#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004219 ALOGD("Gestures: TAP finished");
Jeff Brown79ac9692011-04-19 21:20:10 -07004220#endif
4221 *outFinishPreviousGesture = true;
4222
4223 mPointerGesture.activeGestureId = -1;
4224 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4225 mPointerGesture.currentGestureIdBits.clear();
4226
Jeff Brown65fd2512011-08-18 11:20:58 -07004227 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004228 return true;
4229 }
4230 }
4231
4232 // We did not handle this timeout.
4233 return false;
4234 }
4235
Jeff Brown65fd2512011-08-18 11:20:58 -07004236 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4237 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4238
Jeff Brownace13b12011-03-09 17:39:48 -08004239 // Update the velocity tracker.
4240 {
4241 VelocityTracker::Position positions[MAX_POINTERS];
4242 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004243 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004244 uint32_t id = idBits.clearFirstMarkedBit();
4245 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004246 positions[count].x = pointer.x * mPointerXMovementScale;
4247 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004248 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004249 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004250 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004251 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004252
Jeff Brownace13b12011-03-09 17:39:48 -08004253 // Pick a new active touch id if needed.
4254 // Choose an arbitrary pointer that just went down, if there is one.
4255 // Otherwise choose an arbitrary remaining pointer.
4256 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004257 // We keep the same active touch id for as long as possible.
4258 bool activeTouchChanged = false;
4259 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4260 int32_t activeTouchId = lastActiveTouchId;
4261 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004262 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004263 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004264 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004265 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004266 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004267 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004268 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004269 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004270 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004271 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004272 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004273 } else {
4274 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004275 }
4276 }
4277
4278 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004279 bool isQuietTime = false;
4280 if (activeTouchId < 0) {
4281 mPointerGesture.resetQuietTime();
4282 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004283 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004284 if (!isQuietTime) {
4285 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4286 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4287 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004288 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004289 // Enter quiet time when exiting swipe or freeform state.
4290 // This is to prevent accidentally entering the hover state and flinging the
4291 // pointer when finishing a swipe and there is still one pointer left onscreen.
4292 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004293 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004294 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004295 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004296 // Enter quiet time when releasing the button and there are still two or more
4297 // fingers down. This may indicate that one finger was used to press the button
4298 // but it has not gone up yet.
4299 isQuietTime = true;
4300 }
4301 if (isQuietTime) {
4302 mPointerGesture.quietTime = when;
4303 }
Jeff Brownace13b12011-03-09 17:39:48 -08004304 }
4305 }
4306
4307 // Switch states based on button and pointer state.
4308 if (isQuietTime) {
4309 // Case 1: Quiet time. (QUIET)
4310#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004311 ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004312 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004313#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004314 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4315 *outFinishPreviousGesture = true;
4316 }
Jeff Brownace13b12011-03-09 17:39:48 -08004317
4318 mPointerGesture.activeGestureId = -1;
4319 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004320 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004321
Jeff Brown65fd2512011-08-18 11:20:58 -07004322 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004323 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004324 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004325 // The pointer follows the active touch point.
4326 // Emit DOWN, MOVE, UP events at the pointer location.
4327 //
4328 // Only the active touch matters; other fingers are ignored. This policy helps
4329 // to handle the case where the user places a second finger on the touch pad
4330 // to apply the necessary force to depress an integrated button below the surface.
4331 // We don't want the second finger to be delivered to applications.
4332 //
4333 // For this to work well, we need to make sure to track the pointer that is really
4334 // active. If the user first puts one finger down to click then adds another
4335 // finger to drag then the active pointer should switch to the finger that is
4336 // being dragged.
4337#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004338 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004339 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004340#endif
4341 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004342 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004343 *outFinishPreviousGesture = true;
4344 mPointerGesture.activeGestureId = 0;
4345 }
4346
4347 // Switch pointers if needed.
4348 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004349 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004350 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004351 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004352 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004353 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004354 float vx, vy;
4355 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4356 float speed = hypotf(vx, vy);
4357 if (speed > bestSpeed) {
4358 bestId = id;
4359 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004360 }
Jeff Brown8d608662010-08-30 03:02:23 -07004361 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004362 }
4363 if (bestId >= 0 && bestId != activeTouchId) {
4364 mPointerGesture.activeTouchId = activeTouchId = bestId;
4365 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004366#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004367 ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brown19c97d462011-06-01 12:33:19 -07004368 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004369#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004370 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004371 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004372
Jeff Brown65fd2512011-08-18 11:20:58 -07004373 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004374 const RawPointerData::Pointer& currentPointer =
4375 mCurrentRawPointerData.pointerForId(activeTouchId);
4376 const RawPointerData::Pointer& lastPointer =
4377 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004378 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4379 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004380
Jeff Brownbe1aa822011-07-27 16:04:54 -07004381 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004382 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004383
4384 // Move the pointer using a relative motion.
4385 // When using spots, the click will occur at the position of the anchor
4386 // spot and all other spots will move there.
4387 mPointerController->move(deltaX, deltaY);
4388 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004389 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004390 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004391
Jeff Brownace13b12011-03-09 17:39:48 -08004392 float x, y;
4393 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004394
Jeff Brown79ac9692011-04-19 21:20:10 -07004395 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004396 mPointerGesture.currentGestureIdBits.clear();
4397 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4398 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004399 mPointerGesture.currentGestureProperties[0].clear();
4400 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004401 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004402 mPointerGesture.currentGestureCoords[0].clear();
4403 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4404 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4405 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004406 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004407 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004408 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4409 *outFinishPreviousGesture = true;
4410 }
Jeff Brownace13b12011-03-09 17:39:48 -08004411
Jeff Brown79ac9692011-04-19 21:20:10 -07004412 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004413 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004414 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004415 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4416 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004417 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004418 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004419 float x, y;
4420 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004421 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4422 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004423#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004424 ALOGD("Gestures: TAP");
Jeff Brownace13b12011-03-09 17:39:48 -08004425#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004426
4427 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004428 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004429 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004430
Jeff Brownace13b12011-03-09 17:39:48 -08004431 mPointerGesture.activeGestureId = 0;
4432 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004433 mPointerGesture.currentGestureIdBits.clear();
4434 mPointerGesture.currentGestureIdBits.markBit(
4435 mPointerGesture.activeGestureId);
4436 mPointerGesture.currentGestureIdToIndex[
4437 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004438 mPointerGesture.currentGestureProperties[0].clear();
4439 mPointerGesture.currentGestureProperties[0].id =
4440 mPointerGesture.activeGestureId;
4441 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004442 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004443 mPointerGesture.currentGestureCoords[0].clear();
4444 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004445 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004446 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004447 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004448 mPointerGesture.currentGestureCoords[0].setAxisValue(
4449 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004450
Jeff Brownace13b12011-03-09 17:39:48 -08004451 tapped = true;
4452 } else {
4453#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004454 ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004455 x - mPointerGesture.tapX,
4456 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004457#endif
4458 }
4459 } else {
4460#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004461 ALOGD("Gestures: Not a TAP, %0.3fms since down",
Jeff Brown79ac9692011-04-19 21:20:10 -07004462 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004463#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004464 }
Jeff Brownace13b12011-03-09 17:39:48 -08004465 }
Jeff Brown2352b972011-04-12 22:39:53 -07004466
Jeff Brown65fd2512011-08-18 11:20:58 -07004467 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004468
Jeff Brownace13b12011-03-09 17:39:48 -08004469 if (!tapped) {
4470#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004471 ALOGD("Gestures: NEUTRAL");
Jeff Brownace13b12011-03-09 17:39:48 -08004472#endif
4473 mPointerGesture.activeGestureId = -1;
4474 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004475 mPointerGesture.currentGestureIdBits.clear();
4476 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004477 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004478 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004479 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004480 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4481 // When in TAP_DRAG, emit MOVE events at the pointer location.
Steve Blockec193de2012-01-09 18:35:44 +00004482 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004483
Jeff Brown79ac9692011-04-19 21:20:10 -07004484 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4485 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004486 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004487 float x, y;
4488 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004489 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4490 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004491 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4492 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004493#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004494 ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
Jeff Brown79ac9692011-04-19 21:20:10 -07004495 x - mPointerGesture.tapX,
4496 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004497#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004498 }
4499 } else {
4500#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004501 ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
Jeff Brown79ac9692011-04-19 21:20:10 -07004502 (when - mPointerGesture.tapUpTime) * 0.000001f);
4503#endif
4504 }
4505 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4506 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4507 }
Jeff Brownace13b12011-03-09 17:39:48 -08004508
Jeff Brown65fd2512011-08-18 11:20:58 -07004509 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004510 const RawPointerData::Pointer& currentPointer =
4511 mCurrentRawPointerData.pointerForId(activeTouchId);
4512 const RawPointerData::Pointer& lastPointer =
4513 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004514 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004515 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004516 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004517 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004518
Jeff Brownbe1aa822011-07-27 16:04:54 -07004519 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004520 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004521
Jeff Brown2352b972011-04-12 22:39:53 -07004522 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004523 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004524 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004525 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004526 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004527 }
4528
Jeff Brown79ac9692011-04-19 21:20:10 -07004529 bool down;
4530 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4531#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004532 ALOGD("Gestures: TAP_DRAG");
Jeff Brown79ac9692011-04-19 21:20:10 -07004533#endif
4534 down = true;
4535 } else {
4536#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004537 ALOGD("Gestures: HOVER");
Jeff Brown79ac9692011-04-19 21:20:10 -07004538#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004539 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4540 *outFinishPreviousGesture = true;
4541 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004542 mPointerGesture.activeGestureId = 0;
4543 down = false;
4544 }
Jeff Brownace13b12011-03-09 17:39:48 -08004545
4546 float x, y;
4547 mPointerController->getPosition(&x, &y);
4548
Jeff Brownace13b12011-03-09 17:39:48 -08004549 mPointerGesture.currentGestureIdBits.clear();
4550 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4551 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004552 mPointerGesture.currentGestureProperties[0].clear();
4553 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4554 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004555 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004556 mPointerGesture.currentGestureCoords[0].clear();
4557 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4558 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004559 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4560 down ? 1.0f : 0.0f);
4561
Jeff Brown65fd2512011-08-18 11:20:58 -07004562 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004563 mPointerGesture.resetTap();
4564 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004565 mPointerGesture.tapX = x;
4566 mPointerGesture.tapY = y;
4567 }
Jeff Brownace13b12011-03-09 17:39:48 -08004568 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004569 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4570 // We need to provide feedback for each finger that goes down so we cannot wait
4571 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004572 //
Jeff Brown2352b972011-04-12 22:39:53 -07004573 // The ambiguous case is deciding what to do when there are two fingers down but they
4574 // have not moved enough to determine whether they are part of a drag or part of a
4575 // freeform gesture, or just a press or long-press at the pointer location.
4576 //
4577 // When there are two fingers we start with the PRESS hypothesis and we generate a
4578 // down at the pointer location.
4579 //
4580 // When the two fingers move enough or when additional fingers are added, we make
4581 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Steve Blockec193de2012-01-09 18:35:44 +00004582 ALOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004583
Jeff Brown214eaf42011-05-26 19:17:02 -07004584 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004585 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004586 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004587 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4588 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004589 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004590 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004591 // Additional pointers have gone down but not yet settled.
4592 // Reset the gesture.
4593#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004594 ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004595 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004596 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004597 * 0.000001f);
4598#endif
4599 *outCancelPreviousGesture = true;
4600 } else {
4601 // Continue previous gesture.
4602 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4603 }
4604
4605 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004606 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4607 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004608 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004609 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004610
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004611 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004612#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004613 ALOGD("Gestures: Using centroid as reference for MULTITOUCH, "
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004614 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004615 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004616 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004617#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004618 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4619 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004620 &mPointerGesture.referenceTouchY);
4621 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4622 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004623 }
Jeff Brownace13b12011-03-09 17:39:48 -08004624
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004625 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004626 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004627 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4628 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004629 mPointerGesture.referenceDeltas[id].dx = 0;
4630 mPointerGesture.referenceDeltas[id].dy = 0;
4631 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004632 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004633
4634 // Add delta for all fingers and calculate a common movement delta.
4635 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004636 BitSet32 commonIdBits(mLastFingerIdBits.value
4637 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004638 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4639 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004640 uint32_t id = idBits.clearFirstMarkedBit();
4641 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4642 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004643 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4644 delta.dx += cpd.x - lpd.x;
4645 delta.dy += cpd.y - lpd.y;
4646
4647 if (first) {
4648 commonDeltaX = delta.dx;
4649 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004650 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004651 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4652 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4653 }
4654 }
Jeff Brownace13b12011-03-09 17:39:48 -08004655
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004656 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4657 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4658 float dist[MAX_POINTER_ID + 1];
4659 int32_t distOverThreshold = 0;
4660 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004661 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004662 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004663 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4664 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004665 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004666 distOverThreshold += 1;
4667 }
4668 }
4669
4670 // Only transition when at least two pointers have moved further than
4671 // the minimum distance threshold.
4672 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004673 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004674 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004675#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004676 ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004677 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004678#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004679 *outCancelPreviousGesture = true;
4680 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4681 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004682 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004683 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004684 uint32_t id1 = idBits.clearFirstMarkedBit();
4685 uint32_t id2 = idBits.firstMarkedBit();
4686 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4687 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4688 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4689 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4690 // There are two pointers but they are too far apart for a SWIPE,
4691 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004692#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004693 ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004694 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004695#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004696 *outCancelPreviousGesture = true;
4697 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4698 } else {
4699 // There are two pointers. Wait for both pointers to start moving
4700 // before deciding whether this is a SWIPE or FREEFORM gesture.
4701 float dist1 = dist[id1];
4702 float dist2 = dist[id2];
4703 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4704 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4705 // Calculate the dot product of the displacement vectors.
4706 // When the vectors are oriented in approximately the same direction,
4707 // the angle betweeen them is near zero and the cosine of the angle
4708 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4709 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4710 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004711 float dx1 = delta1.dx * mPointerXZoomScale;
4712 float dy1 = delta1.dy * mPointerYZoomScale;
4713 float dx2 = delta2.dx * mPointerXZoomScale;
4714 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004715 float dot = dx1 * dx2 + dy1 * dy2;
4716 float cosine = dot / (dist1 * dist2); // denominator always > 0
4717 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4718 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004719#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004720 ALOGD("Gestures: PRESS transitioned to SWIPE, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004721 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4722 "cosine %0.3f >= %0.3f",
4723 dist1, mConfig.pointerGestureMultitouchMinDistance,
4724 dist2, mConfig.pointerGestureMultitouchMinDistance,
4725 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004726#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004727 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4728 } else {
4729 // Pointers are moving in different directions. Switch to FREEFORM.
4730#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004731 ALOGD("Gestures: PRESS transitioned to FREEFORM, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004732 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4733 "cosine %0.3f < %0.3f",
4734 dist1, mConfig.pointerGestureMultitouchMinDistance,
4735 dist2, mConfig.pointerGestureMultitouchMinDistance,
4736 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4737#endif
4738 *outCancelPreviousGesture = true;
4739 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4740 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004741 }
Jeff Brownace13b12011-03-09 17:39:48 -08004742 }
4743 }
Jeff Brownace13b12011-03-09 17:39:48 -08004744 }
4745 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004746 // Switch from SWIPE to FREEFORM if additional pointers go down.
4747 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004748 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004749#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004750 ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004751 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004752#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004753 *outCancelPreviousGesture = true;
4754 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004755 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004756 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004757
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004758 // Move the reference points based on the overall group motion of the fingers
4759 // except in PRESS mode while waiting for a transition to occur.
4760 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4761 && (commonDeltaX || commonDeltaY)) {
4762 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004763 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004764 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004765 delta.dx = 0;
4766 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004767 }
4768
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004769 mPointerGesture.referenceTouchX += commonDeltaX;
4770 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004771
Jeff Brown65fd2512011-08-18 11:20:58 -07004772 commonDeltaX *= mPointerXMovementScale;
4773 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004774
Jeff Brownbe1aa822011-07-27 16:04:54 -07004775 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004776 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004777
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004778 mPointerGesture.referenceGestureX += commonDeltaX;
4779 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004780 }
4781
4782 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004783 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4784 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4785 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004786#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004787 ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004788 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004789 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004790#endif
Steve Blockec193de2012-01-09 18:35:44 +00004791 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown2352b972011-04-12 22:39:53 -07004792
4793 mPointerGesture.currentGestureIdBits.clear();
4794 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4795 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004796 mPointerGesture.currentGestureProperties[0].clear();
4797 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4798 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004799 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004800 mPointerGesture.currentGestureCoords[0].clear();
4801 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4802 mPointerGesture.referenceGestureX);
4803 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4804 mPointerGesture.referenceGestureY);
4805 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004806 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4807 // FREEFORM mode.
4808#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004809 ALOGD("Gestures: FREEFORM activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004810 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004811 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004812#endif
Steve Blockec193de2012-01-09 18:35:44 +00004813 ALOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004814
Jeff Brownace13b12011-03-09 17:39:48 -08004815 mPointerGesture.currentGestureIdBits.clear();
4816
4817 BitSet32 mappedTouchIdBits;
4818 BitSet32 usedGestureIdBits;
4819 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4820 // Initially, assign the active gesture id to the active touch point
4821 // if there is one. No other touch id bits are mapped yet.
4822 if (!*outCancelPreviousGesture) {
4823 mappedTouchIdBits.markBit(activeTouchId);
4824 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4825 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4826 mPointerGesture.activeGestureId;
4827 } else {
4828 mPointerGesture.activeGestureId = -1;
4829 }
4830 } else {
4831 // Otherwise, assume we mapped all touches from the previous frame.
4832 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004833 mappedTouchIdBits.value = mLastFingerIdBits.value
4834 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004835 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4836
4837 // Check whether we need to choose a new active gesture id because the
4838 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004839 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4840 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004841 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004842 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004843 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4844 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4845 mPointerGesture.activeGestureId = -1;
4846 break;
4847 }
4848 }
4849 }
4850
4851#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004852 ALOGD("Gestures: FREEFORM follow up "
Jeff Brownace13b12011-03-09 17:39:48 -08004853 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4854 "activeGestureId=%d",
4855 mappedTouchIdBits.value, usedGestureIdBits.value,
4856 mPointerGesture.activeGestureId);
4857#endif
4858
Jeff Brown65fd2512011-08-18 11:20:58 -07004859 BitSet32 idBits(mCurrentFingerIdBits);
4860 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004861 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004862 uint32_t gestureId;
4863 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004864 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004865 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4866#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004867 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004868 "new mapping for touch id %d -> gesture id %d",
4869 touchId, gestureId);
4870#endif
4871 } else {
4872 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4873#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004874 ALOGD("Gestures: FREEFORM "
Jeff Brownace13b12011-03-09 17:39:48 -08004875 "existing mapping for touch id %d -> gesture id %d",
4876 touchId, gestureId);
4877#endif
4878 }
4879 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4880 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4881
Jeff Brownbe1aa822011-07-27 16:04:54 -07004882 const RawPointerData::Pointer& pointer =
4883 mCurrentRawPointerData.pointerForId(touchId);
4884 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004885 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004886 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004887 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004888 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004889
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004890 mPointerGesture.currentGestureProperties[i].clear();
4891 mPointerGesture.currentGestureProperties[i].id = gestureId;
4892 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004893 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004894 mPointerGesture.currentGestureCoords[i].clear();
4895 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004896 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004897 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004898 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004899 mPointerGesture.currentGestureCoords[i].setAxisValue(
4900 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4901 }
4902
4903 if (mPointerGesture.activeGestureId < 0) {
4904 mPointerGesture.activeGestureId =
4905 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4906#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004907 ALOGD("Gestures: FREEFORM new "
Jeff Brownace13b12011-03-09 17:39:48 -08004908 "activeGestureId=%d", mPointerGesture.activeGestureId);
4909#endif
4910 }
Jeff Brown2352b972011-04-12 22:39:53 -07004911 }
Jeff Brownace13b12011-03-09 17:39:48 -08004912 }
4913
Jeff Brownbe1aa822011-07-27 16:04:54 -07004914 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004915
Jeff Brownace13b12011-03-09 17:39:48 -08004916#if DEBUG_GESTURES
Steve Block5baa3a62011-12-20 16:23:08 +00004917 ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004918 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4919 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004920 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004921 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4922 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004923 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004924 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004925 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004926 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004927 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004928 ALOGD(" currentGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004929 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4930 id, index, properties.toolType,
4931 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004932 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4933 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4934 }
4935 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004936 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004937 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004938 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004939 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Steve Block5baa3a62011-12-20 16:23:08 +00004940 ALOGD(" lastGesture[%d]: index=%d, toolType=%d, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004941 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4942 id, index, properties.toolType,
4943 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004944 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4945 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4946 }
4947#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004948 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004949}
4950
Jeff Brown65fd2512011-08-18 11:20:58 -07004951void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4952 mPointerSimple.currentCoords.clear();
4953 mPointerSimple.currentProperties.clear();
4954
4955 bool down, hovering;
4956 if (!mCurrentStylusIdBits.isEmpty()) {
4957 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4958 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4959 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4960 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4961 mPointerController->setPosition(x, y);
4962
4963 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4964 down = !hovering;
4965
4966 mPointerController->getPosition(&x, &y);
4967 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4968 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4969 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4970 mPointerSimple.currentProperties.id = 0;
4971 mPointerSimple.currentProperties.toolType =
4972 mCurrentCookedPointerData.pointerProperties[index].toolType;
4973 } else {
4974 down = false;
4975 hovering = false;
4976 }
4977
4978 dispatchPointerSimple(when, policyFlags, down, hovering);
4979}
4980
4981void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4982 abortPointerSimple(when, policyFlags);
4983}
4984
4985void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4986 mPointerSimple.currentCoords.clear();
4987 mPointerSimple.currentProperties.clear();
4988
4989 bool down, hovering;
4990 if (!mCurrentMouseIdBits.isEmpty()) {
4991 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4992 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4993 if (mLastMouseIdBits.hasBit(id)) {
4994 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4995 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4996 - mLastRawPointerData.pointers[lastIndex].x)
4997 * mPointerXMovementScale;
4998 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4999 - mLastRawPointerData.pointers[lastIndex].y)
5000 * mPointerYMovementScale;
5001
5002 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
5003 mPointerVelocityControl.move(when, &deltaX, &deltaY);
5004
5005 mPointerController->move(deltaX, deltaY);
5006 } else {
5007 mPointerVelocityControl.reset();
5008 }
5009
5010 down = isPointerDown(mCurrentButtonState);
5011 hovering = !down;
5012
5013 float x, y;
5014 mPointerController->getPosition(&x, &y);
5015 mPointerSimple.currentCoords.copyFrom(
5016 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5017 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5018 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5019 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5020 hovering ? 0.0f : 1.0f);
5021 mPointerSimple.currentProperties.id = 0;
5022 mPointerSimple.currentProperties.toolType =
5023 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5024 } else {
5025 mPointerVelocityControl.reset();
5026
5027 down = false;
5028 hovering = false;
5029 }
5030
5031 dispatchPointerSimple(when, policyFlags, down, hovering);
5032}
5033
5034void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5035 abortPointerSimple(when, policyFlags);
5036
5037 mPointerVelocityControl.reset();
5038}
5039
5040void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5041 bool down, bool hovering) {
5042 int32_t metaState = getContext()->getGlobalMetaState();
5043
5044 if (mPointerController != NULL) {
5045 if (down || hovering) {
5046 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5047 mPointerController->clearSpots();
5048 mPointerController->setButtonState(mCurrentButtonState);
5049 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5050 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5051 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5052 }
5053 }
5054
5055 if (mPointerSimple.down && !down) {
5056 mPointerSimple.down = false;
5057
5058 // Send up.
5059 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5060 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5061 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5062 mOrientedXPrecision, mOrientedYPrecision,
5063 mPointerSimple.downTime);
5064 getListener()->notifyMotion(&args);
5065 }
5066
5067 if (mPointerSimple.hovering && !hovering) {
5068 mPointerSimple.hovering = false;
5069
5070 // Send hover exit.
5071 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5072 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5073 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5074 mOrientedXPrecision, mOrientedYPrecision,
5075 mPointerSimple.downTime);
5076 getListener()->notifyMotion(&args);
5077 }
5078
5079 if (down) {
5080 if (!mPointerSimple.down) {
5081 mPointerSimple.down = true;
5082 mPointerSimple.downTime = when;
5083
5084 // Send down.
5085 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5086 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5087 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5088 mOrientedXPrecision, mOrientedYPrecision,
5089 mPointerSimple.downTime);
5090 getListener()->notifyMotion(&args);
5091 }
5092
5093 // Send move.
5094 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5095 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5096 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5097 mOrientedXPrecision, mOrientedYPrecision,
5098 mPointerSimple.downTime);
5099 getListener()->notifyMotion(&args);
5100 }
5101
5102 if (hovering) {
5103 if (!mPointerSimple.hovering) {
5104 mPointerSimple.hovering = true;
5105
5106 // Send hover enter.
5107 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5108 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5109 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5110 mOrientedXPrecision, mOrientedYPrecision,
5111 mPointerSimple.downTime);
5112 getListener()->notifyMotion(&args);
5113 }
5114
5115 // Send hover move.
5116 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5117 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5118 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5119 mOrientedXPrecision, mOrientedYPrecision,
5120 mPointerSimple.downTime);
5121 getListener()->notifyMotion(&args);
5122 }
5123
5124 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5125 float vscroll = mCurrentRawVScroll;
5126 float hscroll = mCurrentRawHScroll;
5127 mWheelYVelocityControl.move(when, NULL, &vscroll);
5128 mWheelXVelocityControl.move(when, &hscroll, NULL);
5129
5130 // Send scroll.
5131 PointerCoords pointerCoords;
5132 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5133 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5134 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5135
5136 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5137 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5138 1, &mPointerSimple.currentProperties, &pointerCoords,
5139 mOrientedXPrecision, mOrientedYPrecision,
5140 mPointerSimple.downTime);
5141 getListener()->notifyMotion(&args);
5142 }
5143
5144 // Save state.
5145 if (down || hovering) {
5146 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5147 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5148 } else {
5149 mPointerSimple.reset();
5150 }
5151}
5152
5153void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5154 mPointerSimple.currentCoords.clear();
5155 mPointerSimple.currentProperties.clear();
5156
5157 dispatchPointerSimple(when, policyFlags, false, false);
5158}
5159
Jeff Brownace13b12011-03-09 17:39:48 -08005160void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005161 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5162 const PointerProperties* properties, const PointerCoords* coords,
5163 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005164 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5165 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005166 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005167 uint32_t pointerCount = 0;
5168 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005169 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005170 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005171 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005172 pointerCoords[pointerCount].copyFrom(coords[index]);
5173
5174 if (changedId >= 0 && id == uint32_t(changedId)) {
5175 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5176 }
5177
5178 pointerCount += 1;
5179 }
5180
Steve Blockec193de2012-01-09 18:35:44 +00005181 ALOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005182
5183 if (changedId >= 0 && pointerCount == 1) {
5184 // Replace initial down and final up action.
5185 // We can compare the action without masking off the changed pointer index
5186 // because we know the index is 0.
5187 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5188 action = AMOTION_EVENT_ACTION_DOWN;
5189 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5190 action = AMOTION_EVENT_ACTION_UP;
5191 } else {
5192 // Can't happen.
Steve Blockec193de2012-01-09 18:35:44 +00005193 ALOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005194 }
5195 }
5196
Jeff Brownbe1aa822011-07-27 16:04:54 -07005197 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005198 action, flags, metaState, buttonState, edgeFlags,
5199 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005200 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005201}
5202
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005203bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005204 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005205 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5206 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005207 bool changed = false;
5208 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005209 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005210 uint32_t inIndex = inIdToIndex[id];
5211 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005212
5213 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005214 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005215 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005216 PointerCoords& curOutCoords = outCoords[outIndex];
5217
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005218 if (curInProperties != curOutProperties) {
5219 curOutProperties.copyFrom(curInProperties);
5220 changed = true;
5221 }
5222
Jeff Brownace13b12011-03-09 17:39:48 -08005223 if (curInCoords != curOutCoords) {
5224 curOutCoords.copyFrom(curInCoords);
5225 changed = true;
5226 }
5227 }
5228 return changed;
5229}
5230
5231void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005232 if (mPointerController != NULL) {
5233 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5234 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005235}
5236
Jeff Brownbe1aa822011-07-27 16:04:54 -07005237bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5238 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5239 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005240}
5241
Jeff Brownbe1aa822011-07-27 16:04:54 -07005242const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005243 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005244 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005245 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005246 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005247
5248#if DEBUG_VIRTUAL_KEYS
Steve Block5baa3a62011-12-20 16:23:08 +00005249 ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
Jeff Brown6d0fec22010-07-23 21:28:06 -07005250 "left=%d, top=%d, right=%d, bottom=%d",
5251 x, y,
5252 virtualKey.keyCode, virtualKey.scanCode,
5253 virtualKey.hitLeft, virtualKey.hitTop,
5254 virtualKey.hitRight, virtualKey.hitBottom);
5255#endif
5256
5257 if (virtualKey.isHit(x, y)) {
5258 return & virtualKey;
5259 }
5260 }
5261
5262 return NULL;
5263}
5264
Jeff Brownbe1aa822011-07-27 16:04:54 -07005265void TouchInputMapper::assignPointerIds() {
5266 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5267 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5268
5269 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005270
5271 if (currentPointerCount == 0) {
5272 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005273 return;
5274 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005275
Jeff Brownbe1aa822011-07-27 16:04:54 -07005276 if (lastPointerCount == 0) {
5277 // All pointers are new.
5278 for (uint32_t i = 0; i < currentPointerCount; i++) {
5279 uint32_t id = i;
5280 mCurrentRawPointerData.pointers[i].id = id;
5281 mCurrentRawPointerData.idToIndex[id] = i;
5282 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5283 }
5284 return;
5285 }
5286
5287 if (currentPointerCount == 1 && lastPointerCount == 1
5288 && mCurrentRawPointerData.pointers[0].toolType
5289 == mLastRawPointerData.pointers[0].toolType) {
5290 // Only one pointer and no change in count so it must have the same id as before.
5291 uint32_t id = mLastRawPointerData.pointers[0].id;
5292 mCurrentRawPointerData.pointers[0].id = id;
5293 mCurrentRawPointerData.idToIndex[id] = 0;
5294 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5295 return;
5296 }
5297
5298 // General case.
5299 // We build a heap of squared euclidean distances between current and last pointers
5300 // associated with the current and last pointer indices. Then, we find the best
5301 // match (by distance) for each current pointer.
5302 // The pointers must have the same tool type but it is possible for them to
5303 // transition from hovering to touching or vice-versa while retaining the same id.
5304 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5305
5306 uint32_t heapSize = 0;
5307 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5308 currentPointerIndex++) {
5309 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5310 lastPointerIndex++) {
5311 const RawPointerData::Pointer& currentPointer =
5312 mCurrentRawPointerData.pointers[currentPointerIndex];
5313 const RawPointerData::Pointer& lastPointer =
5314 mLastRawPointerData.pointers[lastPointerIndex];
5315 if (currentPointer.toolType == lastPointer.toolType) {
5316 int64_t deltaX = currentPointer.x - lastPointer.x;
5317 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005318
5319 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5320
5321 // Insert new element into the heap (sift up).
5322 heap[heapSize].currentPointerIndex = currentPointerIndex;
5323 heap[heapSize].lastPointerIndex = lastPointerIndex;
5324 heap[heapSize].distance = distance;
5325 heapSize += 1;
5326 }
5327 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005328 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005329
Jeff Brownbe1aa822011-07-27 16:04:54 -07005330 // Heapify
5331 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5332 startIndex -= 1;
5333 for (uint32_t parentIndex = startIndex; ;) {
5334 uint32_t childIndex = parentIndex * 2 + 1;
5335 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005336 break;
5337 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005338
5339 if (childIndex + 1 < heapSize
5340 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5341 childIndex += 1;
5342 }
5343
5344 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5345 break;
5346 }
5347
5348 swap(heap[parentIndex], heap[childIndex]);
5349 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005350 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005351 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005352
5353#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005354 ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005355 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005356 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005357 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5358 heap[i].distance);
5359 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005360#endif
5361
Jeff Brownbe1aa822011-07-27 16:04:54 -07005362 // Pull matches out by increasing order of distance.
5363 // To avoid reassigning pointers that have already been matched, the loop keeps track
5364 // of which last and current pointers have been matched using the matchedXXXBits variables.
5365 // It also tracks the used pointer id bits.
5366 BitSet32 matchedLastBits(0);
5367 BitSet32 matchedCurrentBits(0);
5368 BitSet32 usedIdBits(0);
5369 bool first = true;
5370 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5371 while (heapSize > 0) {
5372 if (first) {
5373 // The first time through the loop, we just consume the root element of
5374 // the heap (the one with smallest distance).
5375 first = false;
5376 } else {
5377 // Previous iterations consumed the root element of the heap.
5378 // Pop root element off of the heap (sift down).
5379 heap[0] = heap[heapSize];
5380 for (uint32_t parentIndex = 0; ;) {
5381 uint32_t childIndex = parentIndex * 2 + 1;
5382 if (childIndex >= heapSize) {
5383 break;
5384 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005385
Jeff Brownbe1aa822011-07-27 16:04:54 -07005386 if (childIndex + 1 < heapSize
5387 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5388 childIndex += 1;
5389 }
5390
5391 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5392 break;
5393 }
5394
5395 swap(heap[parentIndex], heap[childIndex]);
5396 parentIndex = childIndex;
5397 }
5398
5399#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005400 ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005401 for (size_t i = 0; i < heapSize; i++) {
Steve Block5baa3a62011-12-20 16:23:08 +00005402 ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005403 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5404 heap[i].distance);
5405 }
5406#endif
5407 }
5408
5409 heapSize -= 1;
5410
5411 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5412 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5413
5414 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5415 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5416
5417 matchedCurrentBits.markBit(currentPointerIndex);
5418 matchedLastBits.markBit(lastPointerIndex);
5419
5420 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5421 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5422 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5423 mCurrentRawPointerData.markIdBit(id,
5424 mCurrentRawPointerData.isHovering(currentPointerIndex));
5425 usedIdBits.markBit(id);
5426
5427#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005428 ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005429 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5430#endif
5431 break;
5432 }
5433 }
5434
5435 // Assign fresh ids to pointers that were not matched in the process.
5436 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5437 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5438 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5439
5440 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5441 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5442 mCurrentRawPointerData.markIdBit(id,
5443 mCurrentRawPointerData.isHovering(currentPointerIndex));
5444
5445#if DEBUG_POINTER_ASSIGNMENT
Steve Block5baa3a62011-12-20 16:23:08 +00005446 ALOGD("assignPointerIds - assigned: cur=%d, id=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07005447 currentPointerIndex, id);
5448#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005449 }
5450}
5451
Jeff Brown6d0fec22010-07-23 21:28:06 -07005452int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005453 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5454 return AKEY_STATE_VIRTUAL;
5455 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005456
Jeff Brownbe1aa822011-07-27 16:04:54 -07005457 size_t numVirtualKeys = mVirtualKeys.size();
5458 for (size_t i = 0; i < numVirtualKeys; i++) {
5459 const VirtualKey& virtualKey = mVirtualKeys[i];
5460 if (virtualKey.keyCode == keyCode) {
5461 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005462 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005463 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005464
5465 return AKEY_STATE_UNKNOWN;
5466}
5467
5468int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005469 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5470 return AKEY_STATE_VIRTUAL;
5471 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005472
Jeff Brownbe1aa822011-07-27 16:04:54 -07005473 size_t numVirtualKeys = mVirtualKeys.size();
5474 for (size_t i = 0; i < numVirtualKeys; i++) {
5475 const VirtualKey& virtualKey = mVirtualKeys[i];
5476 if (virtualKey.scanCode == scanCode) {
5477 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005478 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005479 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005480
5481 return AKEY_STATE_UNKNOWN;
5482}
5483
5484bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5485 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005486 size_t numVirtualKeys = mVirtualKeys.size();
5487 for (size_t i = 0; i < numVirtualKeys; i++) {
5488 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005489
Jeff Brownbe1aa822011-07-27 16:04:54 -07005490 for (size_t i = 0; i < numCodes; i++) {
5491 if (virtualKey.keyCode == keyCodes[i]) {
5492 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005493 }
5494 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005495 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005496
5497 return true;
5498}
5499
5500
5501// --- SingleTouchInputMapper ---
5502
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005503SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5504 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005505}
5506
5507SingleTouchInputMapper::~SingleTouchInputMapper() {
5508}
5509
Jeff Brown65fd2512011-08-18 11:20:58 -07005510void SingleTouchInputMapper::reset(nsecs_t when) {
5511 mSingleTouchMotionAccumulator.reset(getDevice());
5512
5513 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005514}
5515
Jeff Brown6d0fec22010-07-23 21:28:06 -07005516void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005517 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005518
Jeff Brown65fd2512011-08-18 11:20:58 -07005519 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005520}
5521
Jeff Brown65fd2512011-08-18 11:20:58 -07005522void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005523 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005524 mCurrentRawPointerData.pointerCount = 1;
5525 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005526
Jeff Brown65fd2512011-08-18 11:20:58 -07005527 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5528 && (mTouchButtonAccumulator.isHovering()
5529 || (mRawPointerAxes.pressure.valid
5530 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005531 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005532
Jeff Brownbe1aa822011-07-27 16:04:54 -07005533 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005534 outPointer.id = 0;
5535 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5536 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5537 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5538 outPointer.touchMajor = 0;
5539 outPointer.touchMinor = 0;
5540 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5541 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5542 outPointer.orientation = 0;
5543 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005544 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5545 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005546 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5547 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5548 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5549 }
5550 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005551 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005552}
5553
Jeff Brownbe1aa822011-07-27 16:04:54 -07005554void SingleTouchInputMapper::configureRawPointerAxes() {
5555 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005556
Jeff Brownbe1aa822011-07-27 16:04:54 -07005557 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5558 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5559 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5560 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5561 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005562 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5563 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005564}
5565
5566
5567// --- MultiTouchInputMapper ---
5568
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005569MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005570 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005571}
5572
5573MultiTouchInputMapper::~MultiTouchInputMapper() {
5574}
5575
Jeff Brown65fd2512011-08-18 11:20:58 -07005576void MultiTouchInputMapper::reset(nsecs_t when) {
5577 mMultiTouchMotionAccumulator.reset(getDevice());
5578
Jeff Brown6894a292011-07-01 17:59:27 -07005579 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005580
Jeff Brown65fd2512011-08-18 11:20:58 -07005581 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005582}
5583
5584void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005585 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005586
Jeff Brown65fd2512011-08-18 11:20:58 -07005587 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005588}
5589
Jeff Brown65fd2512011-08-18 11:20:58 -07005590void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005591 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005592 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005593 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005594
Jeff Brown80fd47c2011-05-24 01:07:44 -07005595 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005596 const MultiTouchMotionAccumulator::Slot* inSlot =
5597 mMultiTouchMotionAccumulator.getSlot(inIndex);
5598 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005599 continue;
5600 }
5601
Jeff Brown80fd47c2011-05-24 01:07:44 -07005602 if (outCount >= MAX_POINTERS) {
5603#if DEBUG_POINTERS
Steve Block5baa3a62011-12-20 16:23:08 +00005604 ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005605 "ignoring the rest.",
5606 getDeviceName().string(), MAX_POINTERS);
5607#endif
5608 break; // too many fingers!
5609 }
5610
Jeff Brownbe1aa822011-07-27 16:04:54 -07005611 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005612 outPointer.x = inSlot->getX();
5613 outPointer.y = inSlot->getY();
5614 outPointer.pressure = inSlot->getPressure();
5615 outPointer.touchMajor = inSlot->getTouchMajor();
5616 outPointer.touchMinor = inSlot->getTouchMinor();
5617 outPointer.toolMajor = inSlot->getToolMajor();
5618 outPointer.toolMinor = inSlot->getToolMinor();
5619 outPointer.orientation = inSlot->getOrientation();
5620 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005621 outPointer.tiltX = 0;
5622 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005623
Jeff Brown49754db2011-07-01 17:37:58 -07005624 outPointer.toolType = inSlot->getToolType();
5625 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5626 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5627 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5628 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5629 }
Jeff Brown8d608662010-08-30 03:02:23 -07005630 }
5631
Jeff Brown65fd2512011-08-18 11:20:58 -07005632 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5633 && (mTouchButtonAccumulator.isHovering()
5634 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005635 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005636
Jeff Brown8d608662010-08-30 03:02:23 -07005637 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005638 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005639 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005640 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005641 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005642 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005643 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005644 if (mPointerTrackingIdMap[n] == trackingId) {
5645 id = n;
5646 }
5647 }
5648
5649 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005650 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005651 mPointerTrackingIdMap[id] = trackingId;
5652 }
5653 }
5654 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005655 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005656 mCurrentRawPointerData.clearIdBits();
5657 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005658 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005659 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005660 mCurrentRawPointerData.idToIndex[id] = outCount;
5661 mCurrentRawPointerData.markIdBit(id, isHovering);
5662 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005663 }
5664 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005665
Jeff Brown6d0fec22010-07-23 21:28:06 -07005666 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005667 }
5668
Jeff Brownbe1aa822011-07-27 16:04:54 -07005669 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005670 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005671
Jeff Brown65fd2512011-08-18 11:20:58 -07005672 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005673}
5674
Jeff Brownbe1aa822011-07-27 16:04:54 -07005675void MultiTouchInputMapper::configureRawPointerAxes() {
5676 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005677
Jeff Brownbe1aa822011-07-27 16:04:54 -07005678 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5679 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5680 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5681 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5682 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5683 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5684 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5685 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5686 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5687 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5688 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005689
Jeff Brownbe1aa822011-07-27 16:04:54 -07005690 if (mRawPointerAxes.trackingId.valid
5691 && mRawPointerAxes.slot.valid
5692 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5693 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005694 if (slotCount > MAX_SLOTS) {
Steve Block8564c8d2012-01-05 23:22:43 +00005695 ALOGW("MultiTouch Device %s reported %d slots but the framework "
Jeff Brown80fd47c2011-05-24 01:07:44 -07005696 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005697 getDeviceName().string(), slotCount, MAX_SLOTS);
5698 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005699 }
Jeff Brown49754db2011-07-01 17:37:58 -07005700 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005701 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005702 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005703 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005704}
5705
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005706
Jeff Browncb1404e2011-01-15 18:14:15 -08005707// --- JoystickInputMapper ---
5708
5709JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5710 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005711}
5712
5713JoystickInputMapper::~JoystickInputMapper() {
5714}
5715
5716uint32_t JoystickInputMapper::getSources() {
5717 return AINPUT_SOURCE_JOYSTICK;
5718}
5719
5720void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5721 InputMapper::populateDeviceInfo(info);
5722
Jeff Brown6f2fba42011-02-19 01:08:02 -08005723 for (size_t i = 0; i < mAxes.size(); i++) {
5724 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005725 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5726 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005727 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005728 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5729 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005730 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005731 }
5732}
5733
5734void JoystickInputMapper::dump(String8& dump) {
5735 dump.append(INDENT2 "Joystick Input Mapper:\n");
5736
Jeff Brown6f2fba42011-02-19 01:08:02 -08005737 dump.append(INDENT3 "Axes:\n");
5738 size_t numAxes = mAxes.size();
5739 for (size_t i = 0; i < numAxes; i++) {
5740 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005741 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005742 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005743 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005744 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005745 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005746 }
Jeff Brown85297452011-03-04 13:07:49 -08005747 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5748 label = getAxisLabel(axis.axisInfo.highAxis);
5749 if (label) {
5750 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5751 } else {
5752 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5753 axis.axisInfo.splitValue);
5754 }
5755 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5756 dump.append(" (invert)");
5757 }
5758
5759 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5760 axis.min, axis.max, axis.flat, axis.fuzz);
5761 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5762 "highScale=%0.5f, highOffset=%0.5f\n",
5763 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005764 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5765 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005766 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005767 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005768 }
5769}
5770
Jeff Brown65fd2512011-08-18 11:20:58 -07005771void JoystickInputMapper::configure(nsecs_t when,
5772 const InputReaderConfiguration* config, uint32_t changes) {
5773 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005774
Jeff Brown474dcb52011-06-14 20:22:50 -07005775 if (!changes) { // first time only
5776 // Collect all axes.
5777 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Jeff Brown9ee285af2011-08-31 12:56:34 -07005778 if (!(getAbsAxisUsage(abs, getDevice()->getClasses())
5779 & INPUT_DEVICE_CLASS_JOYSTICK)) {
5780 continue; // axis must be claimed by a different device
5781 }
5782
Jeff Brown474dcb52011-06-14 20:22:50 -07005783 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005784 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005785 if (rawAxisInfo.valid) {
5786 // Map axis.
5787 AxisInfo axisInfo;
5788 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5789 if (!explicitlyMapped) {
5790 // Axis is not explicitly mapped, will choose a generic axis later.
5791 axisInfo.mode = AxisInfo::MODE_NORMAL;
5792 axisInfo.axis = -1;
5793 }
5794
5795 // Apply flat override.
5796 int32_t rawFlat = axisInfo.flatOverride < 0
5797 ? rawAxisInfo.flat : axisInfo.flatOverride;
5798
5799 // Calculate scaling factors and limits.
5800 Axis axis;
5801 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5802 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5803 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5804 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5805 scale, 0.0f, highScale, 0.0f,
5806 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5807 } else if (isCenteredAxis(axisInfo.axis)) {
5808 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5809 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5810 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5811 scale, offset, scale, offset,
5812 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5813 } else {
5814 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5815 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5816 scale, 0.0f, scale, 0.0f,
5817 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5818 }
5819
5820 // To eliminate noise while the joystick is at rest, filter out small variations
5821 // in axis values up front.
5822 axis.filter = axis.flat * 0.25f;
5823
5824 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005825 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005826 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005827
Jeff Brown474dcb52011-06-14 20:22:50 -07005828 // If there are too many axes, start dropping them.
5829 // Prefer to keep explicitly mapped axes.
5830 if (mAxes.size() > PointerCoords::MAX_AXES) {
Steve Block6215d3f2012-01-04 20:05:49 +00005831 ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
Jeff Brown474dcb52011-06-14 20:22:50 -07005832 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5833 pruneAxes(true);
5834 pruneAxes(false);
5835 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005836
Jeff Brown474dcb52011-06-14 20:22:50 -07005837 // Assign generic axis ids to remaining axes.
5838 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5839 size_t numAxes = mAxes.size();
5840 for (size_t i = 0; i < numAxes; i++) {
5841 Axis& axis = mAxes.editValueAt(i);
5842 if (axis.axisInfo.axis < 0) {
5843 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5844 && haveAxis(nextGenericAxisId)) {
5845 nextGenericAxisId += 1;
5846 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005847
Jeff Brown474dcb52011-06-14 20:22:50 -07005848 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5849 axis.axisInfo.axis = nextGenericAxisId;
5850 nextGenericAxisId += 1;
5851 } else {
Steve Block6215d3f2012-01-04 20:05:49 +00005852 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
Jeff Brown474dcb52011-06-14 20:22:50 -07005853 "have already been assigned to other axes.",
5854 getDeviceName().string(), mAxes.keyAt(i));
5855 mAxes.removeItemsAt(i--);
5856 numAxes -= 1;
5857 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005858 }
5859 }
5860 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005861}
5862
Jeff Brown85297452011-03-04 13:07:49 -08005863bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005864 size_t numAxes = mAxes.size();
5865 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005866 const Axis& axis = mAxes.valueAt(i);
5867 if (axis.axisInfo.axis == axisId
5868 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5869 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005870 return true;
5871 }
5872 }
5873 return false;
5874}
Jeff Browncb1404e2011-01-15 18:14:15 -08005875
Jeff Brown6f2fba42011-02-19 01:08:02 -08005876void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5877 size_t i = mAxes.size();
5878 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5879 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5880 continue;
5881 }
Steve Block6215d3f2012-01-04 20:05:49 +00005882 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005883 getDeviceName().string(), mAxes.keyAt(i));
5884 mAxes.removeItemsAt(i);
5885 }
5886}
5887
5888bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5889 switch (axis) {
5890 case AMOTION_EVENT_AXIS_X:
5891 case AMOTION_EVENT_AXIS_Y:
5892 case AMOTION_EVENT_AXIS_Z:
5893 case AMOTION_EVENT_AXIS_RX:
5894 case AMOTION_EVENT_AXIS_RY:
5895 case AMOTION_EVENT_AXIS_RZ:
5896 case AMOTION_EVENT_AXIS_HAT_X:
5897 case AMOTION_EVENT_AXIS_HAT_Y:
5898 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005899 case AMOTION_EVENT_AXIS_RUDDER:
5900 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005901 return true;
5902 default:
5903 return false;
5904 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005905}
5906
Jeff Brown65fd2512011-08-18 11:20:58 -07005907void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005908 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005909 size_t numAxes = mAxes.size();
5910 for (size_t i = 0; i < numAxes; i++) {
5911 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005912 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005913 }
5914
Jeff Brown65fd2512011-08-18 11:20:58 -07005915 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005916}
5917
5918void JoystickInputMapper::process(const RawEvent* rawEvent) {
5919 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005920 case EV_ABS: {
5921 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5922 if (index >= 0) {
5923 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005924 float newValue, highNewValue;
5925 switch (axis.axisInfo.mode) {
5926 case AxisInfo::MODE_INVERT:
5927 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5928 * axis.scale + axis.offset;
5929 highNewValue = 0.0f;
5930 break;
5931 case AxisInfo::MODE_SPLIT:
5932 if (rawEvent->value < axis.axisInfo.splitValue) {
5933 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5934 * axis.scale + axis.offset;
5935 highNewValue = 0.0f;
5936 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5937 newValue = 0.0f;
5938 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5939 * axis.highScale + axis.highOffset;
5940 } else {
5941 newValue = 0.0f;
5942 highNewValue = 0.0f;
5943 }
5944 break;
5945 default:
5946 newValue = rawEvent->value * axis.scale + axis.offset;
5947 highNewValue = 0.0f;
5948 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005949 }
Jeff Brown85297452011-03-04 13:07:49 -08005950 axis.newValue = newValue;
5951 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005952 }
5953 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005954 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005955
5956 case EV_SYN:
5957 switch (rawEvent->scanCode) {
5958 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005959 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005960 break;
5961 }
5962 break;
5963 }
5964}
5965
Jeff Brown6f2fba42011-02-19 01:08:02 -08005966void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005967 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005968 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005969 }
5970
5971 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005972 int32_t buttonState = 0;
5973
5974 PointerProperties pointerProperties;
5975 pointerProperties.clear();
5976 pointerProperties.id = 0;
5977 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005978
Jeff Brown6f2fba42011-02-19 01:08:02 -08005979 PointerCoords pointerCoords;
5980 pointerCoords.clear();
5981
5982 size_t numAxes = mAxes.size();
5983 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005984 const Axis& axis = mAxes.valueAt(i);
5985 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5986 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5987 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5988 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005989 }
5990
Jeff Brown56194eb2011-03-02 19:23:13 -08005991 // Moving a joystick axis should not wake the devide because joysticks can
5992 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5993 // button will likely wake the device.
5994 // TODO: Use the input device configuration to control this behavior more finely.
5995 uint32_t policyFlags = 0;
5996
Jeff Brownbe1aa822011-07-27 16:04:54 -07005997 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005998 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5999 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07006000 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08006001}
6002
Jeff Brown85297452011-03-04 13:07:49 -08006003bool JoystickInputMapper::filterAxes(bool force) {
6004 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006005 size_t numAxes = mAxes.size();
6006 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006007 Axis& axis = mAxes.editValueAt(i);
6008 if (force || hasValueChangedSignificantly(axis.filter,
6009 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6010 axis.currentValue = axis.newValue;
6011 atLeastOneSignificantChange = true;
6012 }
6013 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6014 if (force || hasValueChangedSignificantly(axis.filter,
6015 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6016 axis.highCurrentValue = axis.highNewValue;
6017 atLeastOneSignificantChange = true;
6018 }
6019 }
6020 }
6021 return atLeastOneSignificantChange;
6022}
6023
6024bool JoystickInputMapper::hasValueChangedSignificantly(
6025 float filter, float newValue, float currentValue, float min, float max) {
6026 if (newValue != currentValue) {
6027 // Filter out small changes in value unless the value is converging on the axis
6028 // bounds or center point. This is intended to reduce the amount of information
6029 // sent to applications by particularly noisy joysticks (such as PS3).
6030 if (fabs(newValue - currentValue) > filter
6031 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6032 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6033 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6034 return true;
6035 }
6036 }
6037 return false;
6038}
6039
6040bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6041 float filter, float newValue, float currentValue, float thresholdValue) {
6042 float newDistance = fabs(newValue - thresholdValue);
6043 if (newDistance < filter) {
6044 float oldDistance = fabs(currentValue - thresholdValue);
6045 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006046 return true;
6047 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006048 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006049 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006050}
6051
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006052} // namespace android