blob: f4e5b79002f879b45128e3f139ba814ddefefd71 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700183 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700185 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 }
187}
188
189static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
190 nsecs_t when, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
192 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
193 lastButtonState, currentButtonState,
194 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
198}
199
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700200
Jeff 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);
281
282 if (count) {
283 processEventsLocked(mEventBuffer, count);
284 }
285 if (!count || timeoutMillis == 0) {
286 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700287#if DEBUG_RAW_EVENTS
Jeff Brownbe1aa822011-07-27 16:04:54 -0700288 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700289#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700290 mNextTimeout = LLONG_MAX;
291 timeoutExpiredLocked(now);
292 }
293 } // release lock
294
295 // Flush queued events out to the listener.
296 // This must happen outside of the lock because the listener could potentially call
297 // back into the InputReader's methods, such as getScanCodeState, or become blocked
298 // on another thread similarly waiting to acquire the InputReader lock thereby
299 // resulting in a deadlock. This situation is actually quite plausible because the
300 // listener is actually the input dispatcher, which calls into the window manager,
301 // which occasionally calls into the input reader.
302 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700303}
304
Jeff Brownbe1aa822011-07-27 16:04:54 -0700305void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700306 for (const RawEvent* rawEvent = rawEvents; count;) {
307 int32_t type = rawEvent->type;
308 size_t batchSize = 1;
309 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
310 int32_t deviceId = rawEvent->deviceId;
311 while (batchSize < count) {
312 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
313 || rawEvent[batchSize].deviceId != deviceId) {
314 break;
315 }
316 batchSize += 1;
317 }
318#if DEBUG_RAW_EVENTS
319 LOGD("BatchSize: %d Count: %d", batchSize, count);
320#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700321 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700322 } else {
323 switch (rawEvent->type) {
324 case EventHubInterface::DEVICE_ADDED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700325 addDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700326 break;
327 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 removeDeviceLocked(rawEvent->when, rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700329 break;
330 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700331 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700332 break;
333 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700334 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700335 break;
336 }
337 }
338 count -= batchSize;
339 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700340 }
341}
342
Jeff Brown65fd2512011-08-18 11:20:58 -0700343void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344 String8 name = mEventHub->getDeviceName(deviceId);
345 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
346
Jeff Brownbe1aa822011-07-27 16:04:54 -0700347 InputDevice* device = createDeviceLocked(deviceId, name, classes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700348 device->configure(when, &mConfig, 0);
349 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350
Jeff Brown8d608662010-08-30 03:02:23 -0700351 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800352 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700353 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800354 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700355 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700356 }
357
Jeff Brownbe1aa822011-07-27 16:04:54 -0700358 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
359 if (deviceIndex < 0) {
360 mDevices.add(deviceId, device);
361 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
363 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700364 return;
365 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700366}
367
Jeff Brown65fd2512011-08-18 11:20:58 -0700368void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700370 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
371 if (deviceIndex >= 0) {
372 device = mDevices.valueAt(deviceIndex);
373 mDevices.removeItemsAt(deviceIndex, 1);
374 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700376 return;
377 }
378
Jeff Brown6d0fec22010-07-23 21:28:06 -0700379 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800380 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381 device->getId(), device->getName().string());
382 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800383 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700384 device->getId(), device->getName().string(), device->getSources());
385 }
386
Jeff Brown65fd2512011-08-18 11:20:58 -0700387 device->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700388 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700389}
390
Jeff Brownbe1aa822011-07-27 16:04:54 -0700391InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
392 const String8& name, uint32_t classes) {
393 InputDevice* device = new InputDevice(&mContext, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700394
Jeff Brown56194eb2011-03-02 19:23:13 -0800395 // External devices.
396 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
397 device->setExternal(true);
398 }
399
Jeff Brown6d0fec22010-07-23 21:28:06 -0700400 // Switch-like devices.
401 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
402 device->addMapper(new SwitchInputMapper(device));
403 }
404
405 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800406 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
408 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800409 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700410 }
411 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
412 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
413 }
414 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800415 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700416 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800417 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800418 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800419 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420
Jeff Brownefd32662011-03-08 15:13:06 -0800421 if (keyboardSource != 0) {
422 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 }
424
Jeff Brown83c09682010-12-23 17:50:18 -0800425 // Cursor-like devices.
426 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
427 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700428 }
429
Jeff Brown58a2da82011-01-25 16:02:22 -0800430 // Touchscreens and touchpad devices.
431 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800432 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800433 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800434 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 }
436
Jeff Browncb1404e2011-01-15 18:14:15 -0800437 // Joystick-like devices.
438 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
439 device->addMapper(new JoystickInputMapper(device));
440 }
441
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 return device;
443}
444
Jeff Brownbe1aa822011-07-27 16:04:54 -0700445void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700446 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700447 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
448 if (deviceIndex < 0) {
449 LOGW("Discarding event for unknown deviceId %d.", deviceId);
450 return;
451 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452
Jeff Brownbe1aa822011-07-27 16:04:54 -0700453 InputDevice* device = mDevices.valueAt(deviceIndex);
454 if (device->isIgnored()) {
455 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
456 return;
457 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700458
Jeff Brownbe1aa822011-07-27 16:04:54 -0700459 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460}
461
Jeff Brownbe1aa822011-07-27 16:04:54 -0700462void InputReader::timeoutExpiredLocked(nsecs_t when) {
463 for (size_t i = 0; i < mDevices.size(); i++) {
464 InputDevice* device = mDevices.valueAt(i);
465 if (!device->isIgnored()) {
466 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700467 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700468 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700469}
470
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700473 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700474
475 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700476 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477
478 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700479 NotifyConfigurationChangedArgs args(when);
480 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481}
482
Jeff Brownbe1aa822011-07-27 16:04:54 -0700483void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700484 mPolicy->getReaderConfiguration(&mConfig);
485 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
486
Jeff Brown474dcb52011-06-14 20:22:50 -0700487 if (changes) {
488 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
Jeff Brown65fd2512011-08-18 11:20:58 -0700489 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown474dcb52011-06-14 20:22:50 -0700490
491 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
492 mEventHub->requestReopenDevices();
493 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700494 for (size_t i = 0; i < mDevices.size(); i++) {
495 InputDevice* device = mDevices.valueAt(i);
Jeff Brown65fd2512011-08-18 11:20:58 -0700496 device->configure(now, &mConfig, changes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700497 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700498 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700499 }
500}
501
Jeff Brownbe1aa822011-07-27 16:04:54 -0700502void InputReader::updateGlobalMetaStateLocked() {
503 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504
Jeff Brownbe1aa822011-07-27 16:04:54 -0700505 for (size_t i = 0; i < mDevices.size(); i++) {
506 InputDevice* device = mDevices.valueAt(i);
507 mGlobalMetaState |= device->getMetaState();
508 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509}
510
Jeff Brownbe1aa822011-07-27 16:04:54 -0700511int32_t InputReader::getGlobalMetaStateLocked() {
512 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700513}
514
Jeff Brownbe1aa822011-07-27 16:04:54 -0700515void InputReader::updateInputConfigurationLocked() {
516 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
517 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
518 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
519 InputDeviceInfo deviceInfo;
520 for (size_t i = 0; i < mDevices.size(); i++) {
521 InputDevice* device = mDevices.valueAt(i);
522 device->getDeviceInfo(& deviceInfo);
523 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700524
Jeff Brownbe1aa822011-07-27 16:04:54 -0700525 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
526 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
527 }
528 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
529 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
530 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
531 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
532 }
533 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
534 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
535 }
536 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700537
Jeff Brownbe1aa822011-07-27 16:04:54 -0700538 mInputConfiguration.touchScreen = touchScreenConfig;
539 mInputConfiguration.keyboard = keyboardConfig;
540 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700541}
542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800544 mDisableVirtualKeysTimeout = time;
545}
546
Jeff Brownbe1aa822011-07-27 16:04:54 -0700547bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800548 InputDevice* device, int32_t keyCode, int32_t scanCode) {
549 if (now < mDisableVirtualKeysTimeout) {
550 LOGI("Dropping virtual key from device %s because virtual keys are "
551 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
552 device->getName().string(),
553 (mDisableVirtualKeysTimeout - now) * 0.000001,
554 keyCode, scanCode);
555 return true;
556 } else {
557 return false;
558 }
559}
560
Jeff Brownbe1aa822011-07-27 16:04:54 -0700561void InputReader::fadePointerLocked() {
562 for (size_t i = 0; i < mDevices.size(); i++) {
563 InputDevice* device = mDevices.valueAt(i);
564 device->fadePointer();
565 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800566}
567
Jeff Brownbe1aa822011-07-27 16:04:54 -0700568void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700569 if (when < mNextTimeout) {
570 mNextTimeout = when;
571 }
572}
573
Jeff Brown6d0fec22010-07-23 21:28:06 -0700574void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700575 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700576
Jeff Brownbe1aa822011-07-27 16:04:54 -0700577 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578}
579
580status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700581 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582
Jeff Brownbe1aa822011-07-27 16:04:54 -0700583 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
584 if (deviceIndex < 0) {
585 return NAME_NOT_FOUND;
586 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700587
Jeff Brownbe1aa822011-07-27 16:04:54 -0700588 InputDevice* device = mDevices.valueAt(deviceIndex);
589 if (device->isIgnored()) {
590 return NAME_NOT_FOUND;
591 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700592
Jeff Brownbe1aa822011-07-27 16:04:54 -0700593 device->getDeviceInfo(outDeviceInfo);
594 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595}
596
597void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700598 AutoMutex _l(mLock);
599
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 outDeviceIds.clear();
601
Jeff Brownbe1aa822011-07-27 16:04:54 -0700602 size_t numDevices = mDevices.size();
603 for (size_t i = 0; i < numDevices; i++) {
604 InputDevice* device = mDevices.valueAt(i);
605 if (!device->isIgnored()) {
606 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700607 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700608 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700609}
610
611int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
612 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700613 AutoMutex _l(mLock);
614
615 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616}
617
618int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
619 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700620 AutoMutex _l(mLock);
621
622 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700623}
624
625int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700626 AutoMutex _l(mLock);
627
628 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629}
630
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700632 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700633 int32_t result = AKEY_STATE_UNKNOWN;
634 if (deviceId >= 0) {
635 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
636 if (deviceIndex >= 0) {
637 InputDevice* device = mDevices.valueAt(deviceIndex);
638 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
639 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700640 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700641 }
642 } else {
643 size_t numDevices = mDevices.size();
644 for (size_t i = 0; i < numDevices; i++) {
645 InputDevice* device = mDevices.valueAt(i);
646 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
647 result = (device->*getStateFunc)(sourceMask, code);
648 if (result >= AKEY_STATE_DOWN) {
649 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700650 }
651 }
652 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700653 }
654 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700655}
656
657bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
658 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700659 AutoMutex _l(mLock);
660
Jeff Brown6d0fec22010-07-23 21:28:06 -0700661 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700662 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700663}
664
Jeff Brownbe1aa822011-07-27 16:04:54 -0700665bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
666 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
667 bool result = false;
668 if (deviceId >= 0) {
669 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
670 if (deviceIndex >= 0) {
671 InputDevice* device = mDevices.valueAt(deviceIndex);
672 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
673 result = device->markSupportedKeyCodes(sourceMask,
674 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700675 }
676 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700677 } else {
678 size_t numDevices = mDevices.size();
679 for (size_t i = 0; i < numDevices; i++) {
680 InputDevice* device = mDevices.valueAt(i);
681 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
682 result |= device->markSupportedKeyCodes(sourceMask,
683 numCodes, keyCodes, outFlags);
684 }
685 }
686 }
687 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700688}
689
Jeff Brown474dcb52011-06-14 20:22:50 -0700690void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700691 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700692
Jeff Brownbe1aa822011-07-27 16:04:54 -0700693 if (changes) {
694 bool needWake = !mConfigurationChangesToRefresh;
695 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700696
697 if (needWake) {
698 mEventHub->wake();
699 }
700 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700701}
702
Jeff Brownb88102f2010-09-08 11:49:43 -0700703void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700704 AutoMutex _l(mLock);
705
Jeff Brownf2f487182010-10-01 17:46:21 -0700706 mEventHub->dump(dump);
707 dump.append("\n");
708
709 dump.append("Input Reader State:\n");
710
Jeff Brownbe1aa822011-07-27 16:04:54 -0700711 for (size_t i = 0; i < mDevices.size(); i++) {
712 mDevices.valueAt(i)->dump(dump);
713 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700714
715 dump.append(INDENT "Configuration:\n");
716 dump.append(INDENT2 "ExcludedDeviceNames: [");
717 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
718 if (i != 0) {
719 dump.append(", ");
720 }
721 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
722 }
723 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700724 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
725 mConfig.virtualKeyQuietTime * 0.000001f);
726
Jeff Brown19c97d462011-06-01 12:33:19 -0700727 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
728 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
729 mConfig.pointerVelocityControlParameters.scale,
730 mConfig.pointerVelocityControlParameters.lowThreshold,
731 mConfig.pointerVelocityControlParameters.highThreshold,
732 mConfig.pointerVelocityControlParameters.acceleration);
733
734 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
735 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
736 mConfig.wheelVelocityControlParameters.scale,
737 mConfig.wheelVelocityControlParameters.lowThreshold,
738 mConfig.wheelVelocityControlParameters.highThreshold,
739 mConfig.wheelVelocityControlParameters.acceleration);
740
Jeff Brown214eaf42011-05-26 19:17:02 -0700741 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700742 dump.appendFormat(INDENT3 "Enabled: %s\n",
743 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700744 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
745 mConfig.pointerGestureQuietInterval * 0.000001f);
746 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
747 mConfig.pointerGestureDragMinSwitchSpeed);
748 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
749 mConfig.pointerGestureTapInterval * 0.000001f);
750 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
751 mConfig.pointerGestureTapDragInterval * 0.000001f);
752 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
753 mConfig.pointerGestureTapSlop);
754 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
755 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700756 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
757 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700758 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
759 mConfig.pointerGestureSwipeTransitionAngleCosine);
760 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
761 mConfig.pointerGestureSwipeMaxWidthRatio);
762 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
763 mConfig.pointerGestureMovementSpeedRatio);
764 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
765 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700766}
767
Jeff Brown89ef0722011-08-10 16:25:21 -0700768void InputReader::monitor() {
769 // Acquire and release the lock to ensure that the reader has not deadlocked.
770 mLock.lock();
771 mLock.unlock();
772
773 // Check the EventHub
774 mEventHub->monitor();
775}
776
Jeff Brown6d0fec22010-07-23 21:28:06 -0700777
Jeff Brownbe1aa822011-07-27 16:04:54 -0700778// --- InputReader::ContextImpl ---
779
780InputReader::ContextImpl::ContextImpl(InputReader* reader) :
781 mReader(reader) {
782}
783
784void InputReader::ContextImpl::updateGlobalMetaState() {
785 // lock is already held by the input loop
786 mReader->updateGlobalMetaStateLocked();
787}
788
789int32_t InputReader::ContextImpl::getGlobalMetaState() {
790 // lock is already held by the input loop
791 return mReader->getGlobalMetaStateLocked();
792}
793
794void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
795 // lock is already held by the input loop
796 mReader->disableVirtualKeysUntilLocked(time);
797}
798
799bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
800 InputDevice* device, int32_t keyCode, int32_t scanCode) {
801 // lock is already held by the input loop
802 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
803}
804
805void InputReader::ContextImpl::fadePointer() {
806 // lock is already held by the input loop
807 mReader->fadePointerLocked();
808}
809
810void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
811 // lock is already held by the input loop
812 mReader->requestTimeoutAtTimeLocked(when);
813}
814
815InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
816 return mReader->mPolicy.get();
817}
818
819InputListenerInterface* InputReader::ContextImpl::getListener() {
820 return mReader->mQueuedListener.get();
821}
822
823EventHubInterface* InputReader::ContextImpl::getEventHub() {
824 return mReader->mEventHub.get();
825}
826
827
Jeff Brown6d0fec22010-07-23 21:28:06 -0700828// --- InputReaderThread ---
829
830InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
831 Thread(/*canCallJava*/ true), mReader(reader) {
832}
833
834InputReaderThread::~InputReaderThread() {
835}
836
837bool InputReaderThread::threadLoop() {
838 mReader->loopOnce();
839 return true;
840}
841
842
843// --- InputDevice ---
844
845InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700846 mContext(context), mId(id), mName(name), mSources(0),
847 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700848}
849
850InputDevice::~InputDevice() {
851 size_t numMappers = mMappers.size();
852 for (size_t i = 0; i < numMappers; i++) {
853 delete mMappers[i];
854 }
855 mMappers.clear();
856}
857
Jeff Brownef3d7e82010-09-30 14:33:04 -0700858void InputDevice::dump(String8& dump) {
859 InputDeviceInfo deviceInfo;
860 getDeviceInfo(& deviceInfo);
861
Jeff Brown90655042010-12-02 13:50:46 -0800862 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700863 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800864 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700865 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
866 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800867
Jeff Brownefd32662011-03-08 15:13:06 -0800868 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800869 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700870 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800871 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800872 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
873 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800874 char name[32];
875 if (label) {
876 strncpy(name, label, sizeof(name));
877 name[sizeof(name) - 1] = '\0';
878 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800879 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800880 }
Jeff Brownefd32662011-03-08 15:13:06 -0800881 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
882 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
883 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800884 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700885 }
886
887 size_t numMappers = mMappers.size();
888 for (size_t i = 0; i < numMappers; i++) {
889 InputMapper* mapper = mMappers[i];
890 mapper->dump(dump);
891 }
892}
893
Jeff Brown6d0fec22010-07-23 21:28:06 -0700894void InputDevice::addMapper(InputMapper* mapper) {
895 mMappers.add(mapper);
896}
897
Jeff Brown65fd2512011-08-18 11:20:58 -0700898void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700899 mSources = 0;
900
Jeff Brown474dcb52011-06-14 20:22:50 -0700901 if (!isIgnored()) {
902 if (!changes) { // first time only
903 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
904 }
905
906 size_t numMappers = mMappers.size();
907 for (size_t i = 0; i < numMappers; i++) {
908 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700909 mapper->configure(when, config, changes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700910 mSources |= mapper->getSources();
911 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700912 }
913}
914
Jeff Brown65fd2512011-08-18 11:20:58 -0700915void InputDevice::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700916 size_t numMappers = mMappers.size();
917 for (size_t i = 0; i < numMappers; i++) {
918 InputMapper* mapper = mMappers[i];
Jeff Brown65fd2512011-08-18 11:20:58 -0700919 mapper->reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700920 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700921
922 mContext->updateGlobalMetaState();
923
924 notifyReset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700926
Jeff Brownb7198742011-03-18 18:14:26 -0700927void InputDevice::process(const RawEvent* rawEvents, size_t count) {
928 // Process all of the events in order for each mapper.
929 // We cannot simply ask each mapper to process them in bulk because mappers may
930 // have side-effects that must be interleaved. For example, joystick movement events and
931 // gamepad button presses are handled by different mappers but they should be dispatched
932 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700933 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700934 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
935#if DEBUG_RAW_EVENTS
936 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700937 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700938 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
939 rawEvent->value, rawEvent->flags);
940#endif
941
Jeff Brown80fd47c2011-05-24 01:07:44 -0700942 if (mDropUntilNextSync) {
943 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
944 mDropUntilNextSync = false;
945#if DEBUG_RAW_EVENTS
946 LOGD("Recovered from input event buffer overrun.");
947#endif
948 } else {
949#if DEBUG_RAW_EVENTS
950 LOGD("Dropped input event while waiting for next input sync.");
951#endif
952 }
953 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
954 LOGI("Detected input event buffer overrun for device %s.", mName.string());
955 mDropUntilNextSync = true;
Jeff Brown65fd2512011-08-18 11:20:58 -0700956 reset(rawEvent->when);
Jeff Brown80fd47c2011-05-24 01:07:44 -0700957 } else {
958 for (size_t i = 0; i < numMappers; i++) {
959 InputMapper* mapper = mMappers[i];
960 mapper->process(rawEvent);
961 }
Jeff Brownb7198742011-03-18 18:14:26 -0700962 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700963 }
964}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700965
Jeff Brownaa3855d2011-03-17 01:34:19 -0700966void InputDevice::timeoutExpired(nsecs_t when) {
967 size_t numMappers = mMappers.size();
968 for (size_t i = 0; i < numMappers; i++) {
969 InputMapper* mapper = mMappers[i];
970 mapper->timeoutExpired(when);
971 }
972}
973
Jeff Brown6d0fec22010-07-23 21:28:06 -0700974void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
975 outDeviceInfo->initialize(mId, mName);
976
977 size_t numMappers = mMappers.size();
978 for (size_t i = 0; i < numMappers; i++) {
979 InputMapper* mapper = mMappers[i];
980 mapper->populateDeviceInfo(outDeviceInfo);
981 }
982}
983
984int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
985 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
986}
987
988int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
989 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
990}
991
992int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
993 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
994}
995
996int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
997 int32_t result = AKEY_STATE_UNKNOWN;
998 size_t numMappers = mMappers.size();
999 for (size_t i = 0; i < numMappers; i++) {
1000 InputMapper* mapper = mMappers[i];
1001 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1002 result = (mapper->*getStateFunc)(sourceMask, code);
1003 if (result >= AKEY_STATE_DOWN) {
1004 return result;
1005 }
1006 }
1007 }
1008 return result;
1009}
1010
1011bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1012 const int32_t* keyCodes, uint8_t* outFlags) {
1013 bool result = false;
1014 size_t numMappers = mMappers.size();
1015 for (size_t i = 0; i < numMappers; i++) {
1016 InputMapper* mapper = mMappers[i];
1017 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
1018 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
1019 }
1020 }
1021 return result;
1022}
1023
1024int32_t InputDevice::getMetaState() {
1025 int32_t result = 0;
1026 size_t numMappers = mMappers.size();
1027 for (size_t i = 0; i < numMappers; i++) {
1028 InputMapper* mapper = mMappers[i];
1029 result |= mapper->getMetaState();
1030 }
1031 return result;
1032}
1033
Jeff Brown05dc66a2011-03-02 14:41:58 -08001034void InputDevice::fadePointer() {
1035 size_t numMappers = mMappers.size();
1036 for (size_t i = 0; i < numMappers; i++) {
1037 InputMapper* mapper = mMappers[i];
1038 mapper->fadePointer();
1039 }
1040}
1041
Jeff Brown65fd2512011-08-18 11:20:58 -07001042void InputDevice::notifyReset(nsecs_t when) {
1043 NotifyDeviceResetArgs args(when, mId);
1044 mContext->getListener()->notifyDeviceReset(&args);
1045}
1046
Jeff Brown6d0fec22010-07-23 21:28:06 -07001047
Jeff Brown49754db2011-07-01 17:37:58 -07001048// --- CursorButtonAccumulator ---
1049
1050CursorButtonAccumulator::CursorButtonAccumulator() {
1051 clearButtons();
1052}
1053
Jeff Brown65fd2512011-08-18 11:20:58 -07001054void CursorButtonAccumulator::reset(InputDevice* device) {
1055 mBtnLeft = device->isKeyPressed(BTN_LEFT);
1056 mBtnRight = device->isKeyPressed(BTN_RIGHT);
1057 mBtnMiddle = device->isKeyPressed(BTN_MIDDLE);
1058 mBtnBack = device->isKeyPressed(BTN_BACK);
1059 mBtnSide = device->isKeyPressed(BTN_SIDE);
1060 mBtnForward = device->isKeyPressed(BTN_FORWARD);
1061 mBtnExtra = device->isKeyPressed(BTN_EXTRA);
1062 mBtnTask = device->isKeyPressed(BTN_TASK);
1063}
1064
Jeff Brown49754db2011-07-01 17:37:58 -07001065void CursorButtonAccumulator::clearButtons() {
1066 mBtnLeft = 0;
1067 mBtnRight = 0;
1068 mBtnMiddle = 0;
1069 mBtnBack = 0;
1070 mBtnSide = 0;
1071 mBtnForward = 0;
1072 mBtnExtra = 0;
1073 mBtnTask = 0;
1074}
1075
1076void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1077 if (rawEvent->type == EV_KEY) {
1078 switch (rawEvent->scanCode) {
1079 case BTN_LEFT:
1080 mBtnLeft = rawEvent->value;
1081 break;
1082 case BTN_RIGHT:
1083 mBtnRight = rawEvent->value;
1084 break;
1085 case BTN_MIDDLE:
1086 mBtnMiddle = rawEvent->value;
1087 break;
1088 case BTN_BACK:
1089 mBtnBack = rawEvent->value;
1090 break;
1091 case BTN_SIDE:
1092 mBtnSide = rawEvent->value;
1093 break;
1094 case BTN_FORWARD:
1095 mBtnForward = rawEvent->value;
1096 break;
1097 case BTN_EXTRA:
1098 mBtnExtra = rawEvent->value;
1099 break;
1100 case BTN_TASK:
1101 mBtnTask = rawEvent->value;
1102 break;
1103 }
1104 }
1105}
1106
1107uint32_t CursorButtonAccumulator::getButtonState() const {
1108 uint32_t result = 0;
1109 if (mBtnLeft) {
1110 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1111 }
1112 if (mBtnRight) {
1113 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1114 }
1115 if (mBtnMiddle) {
1116 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1117 }
1118 if (mBtnBack || mBtnSide) {
1119 result |= AMOTION_EVENT_BUTTON_BACK;
1120 }
1121 if (mBtnForward || mBtnExtra) {
1122 result |= AMOTION_EVENT_BUTTON_FORWARD;
1123 }
1124 return result;
1125}
1126
1127
1128// --- CursorMotionAccumulator ---
1129
Jeff Brown65fd2512011-08-18 11:20:58 -07001130CursorMotionAccumulator::CursorMotionAccumulator() {
Jeff Brown49754db2011-07-01 17:37:58 -07001131 clearRelativeAxes();
1132}
1133
Jeff Brown65fd2512011-08-18 11:20:58 -07001134void CursorMotionAccumulator::reset(InputDevice* device) {
1135 clearRelativeAxes();
Jeff Brown49754db2011-07-01 17:37:58 -07001136}
1137
1138void CursorMotionAccumulator::clearRelativeAxes() {
1139 mRelX = 0;
1140 mRelY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001141}
1142
1143void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1144 if (rawEvent->type == EV_REL) {
1145 switch (rawEvent->scanCode) {
1146 case REL_X:
1147 mRelX = rawEvent->value;
1148 break;
1149 case REL_Y:
1150 mRelY = rawEvent->value;
1151 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001152 }
1153 }
1154}
1155
1156void CursorMotionAccumulator::finishSync() {
1157 clearRelativeAxes();
1158}
1159
1160
1161// --- CursorScrollAccumulator ---
1162
1163CursorScrollAccumulator::CursorScrollAccumulator() :
1164 mHaveRelWheel(false), mHaveRelHWheel(false) {
1165 clearRelativeAxes();
1166}
1167
1168void CursorScrollAccumulator::configure(InputDevice* device) {
1169 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1170 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1171}
1172
1173void CursorScrollAccumulator::reset(InputDevice* device) {
1174 clearRelativeAxes();
1175}
1176
1177void CursorScrollAccumulator::clearRelativeAxes() {
1178 mRelWheel = 0;
1179 mRelHWheel = 0;
1180}
1181
1182void CursorScrollAccumulator::process(const RawEvent* rawEvent) {
1183 if (rawEvent->type == EV_REL) {
1184 switch (rawEvent->scanCode) {
Jeff Brown49754db2011-07-01 17:37:58 -07001185 case REL_WHEEL:
1186 mRelWheel = rawEvent->value;
1187 break;
1188 case REL_HWHEEL:
1189 mRelHWheel = rawEvent->value;
1190 break;
1191 }
1192 }
1193}
1194
Jeff Brown65fd2512011-08-18 11:20:58 -07001195void CursorScrollAccumulator::finishSync() {
1196 clearRelativeAxes();
1197}
1198
Jeff Brown49754db2011-07-01 17:37:58 -07001199
1200// --- TouchButtonAccumulator ---
1201
1202TouchButtonAccumulator::TouchButtonAccumulator() :
1203 mHaveBtnTouch(false) {
1204 clearButtons();
1205}
1206
1207void TouchButtonAccumulator::configure(InputDevice* device) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001208 mHaveBtnTouch = device->hasKey(BTN_TOUCH);
1209}
1210
1211void TouchButtonAccumulator::reset(InputDevice* device) {
1212 mBtnTouch = device->isKeyPressed(BTN_TOUCH);
1213 mBtnStylus = device->isKeyPressed(BTN_STYLUS);
1214 mBtnStylus2 = device->isKeyPressed(BTN_STYLUS);
1215 mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER);
1216 mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN);
1217 mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER);
1218 mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH);
1219 mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL);
1220 mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH);
1221 mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE);
1222 mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS);
Jeff Brownea6892e2011-08-23 17:31:25 -07001223 mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP);
1224 mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP);
1225 mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP);
Jeff Brown49754db2011-07-01 17:37:58 -07001226}
1227
1228void TouchButtonAccumulator::clearButtons() {
1229 mBtnTouch = 0;
1230 mBtnStylus = 0;
1231 mBtnStylus2 = 0;
1232 mBtnToolFinger = 0;
1233 mBtnToolPen = 0;
1234 mBtnToolRubber = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001235 mBtnToolBrush = 0;
1236 mBtnToolPencil = 0;
1237 mBtnToolAirbrush = 0;
1238 mBtnToolMouse = 0;
1239 mBtnToolLens = 0;
Jeff Brownea6892e2011-08-23 17:31:25 -07001240 mBtnToolDoubleTap = 0;
1241 mBtnToolTripleTap = 0;
1242 mBtnToolQuadTap = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001243}
1244
1245void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1246 if (rawEvent->type == EV_KEY) {
1247 switch (rawEvent->scanCode) {
1248 case BTN_TOUCH:
1249 mBtnTouch = rawEvent->value;
1250 break;
1251 case BTN_STYLUS:
1252 mBtnStylus = rawEvent->value;
1253 break;
1254 case BTN_STYLUS2:
1255 mBtnStylus2 = rawEvent->value;
1256 break;
1257 case BTN_TOOL_FINGER:
1258 mBtnToolFinger = rawEvent->value;
1259 break;
1260 case BTN_TOOL_PEN:
1261 mBtnToolPen = rawEvent->value;
1262 break;
1263 case BTN_TOOL_RUBBER:
1264 mBtnToolRubber = rawEvent->value;
1265 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001266 case BTN_TOOL_BRUSH:
1267 mBtnToolBrush = rawEvent->value;
1268 break;
1269 case BTN_TOOL_PENCIL:
1270 mBtnToolPencil = rawEvent->value;
1271 break;
1272 case BTN_TOOL_AIRBRUSH:
1273 mBtnToolAirbrush = rawEvent->value;
1274 break;
1275 case BTN_TOOL_MOUSE:
1276 mBtnToolMouse = rawEvent->value;
1277 break;
1278 case BTN_TOOL_LENS:
1279 mBtnToolLens = rawEvent->value;
1280 break;
Jeff Brownea6892e2011-08-23 17:31:25 -07001281 case BTN_TOOL_DOUBLETAP:
1282 mBtnToolDoubleTap = rawEvent->value;
1283 break;
1284 case BTN_TOOL_TRIPLETAP:
1285 mBtnToolTripleTap = rawEvent->value;
1286 break;
1287 case BTN_TOOL_QUADTAP:
1288 mBtnToolQuadTap = rawEvent->value;
1289 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001290 }
1291 }
1292}
1293
1294uint32_t TouchButtonAccumulator::getButtonState() const {
1295 uint32_t result = 0;
1296 if (mBtnStylus) {
1297 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1298 }
1299 if (mBtnStylus2) {
1300 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1301 }
1302 return result;
1303}
1304
1305int32_t TouchButtonAccumulator::getToolType() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001306 if (mBtnToolMouse || mBtnToolLens) {
1307 return AMOTION_EVENT_TOOL_TYPE_MOUSE;
1308 }
Jeff Brown49754db2011-07-01 17:37:58 -07001309 if (mBtnToolRubber) {
1310 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1311 }
Jeff Brown65fd2512011-08-18 11:20:58 -07001312 if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) {
Jeff Brown49754db2011-07-01 17:37:58 -07001313 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1314 }
Jeff Brownea6892e2011-08-23 17:31:25 -07001315 if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) {
Jeff Brown49754db2011-07-01 17:37:58 -07001316 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1317 }
1318 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1319}
1320
Jeff Brownd87c6d52011-08-10 14:55:59 -07001321bool TouchButtonAccumulator::isToolActive() const {
Jeff Brown65fd2512011-08-18 11:20:58 -07001322 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber
1323 || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush
Jeff Brownea6892e2011-08-23 17:31:25 -07001324 || mBtnToolMouse || mBtnToolLens
1325 || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap;
Jeff Brown49754db2011-07-01 17:37:58 -07001326}
1327
1328bool TouchButtonAccumulator::isHovering() const {
1329 return mHaveBtnTouch && !mBtnTouch;
1330}
1331
1332
Jeff Brownbe1aa822011-07-27 16:04:54 -07001333// --- RawPointerAxes ---
1334
1335RawPointerAxes::RawPointerAxes() {
1336 clear();
1337}
1338
1339void RawPointerAxes::clear() {
1340 x.clear();
1341 y.clear();
1342 pressure.clear();
1343 touchMajor.clear();
1344 touchMinor.clear();
1345 toolMajor.clear();
1346 toolMinor.clear();
1347 orientation.clear();
1348 distance.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07001349 tiltX.clear();
1350 tiltY.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07001351 trackingId.clear();
1352 slot.clear();
1353}
1354
1355
1356// --- RawPointerData ---
1357
1358RawPointerData::RawPointerData() {
1359 clear();
1360}
1361
1362void RawPointerData::clear() {
1363 pointerCount = 0;
1364 clearIdBits();
1365}
1366
1367void RawPointerData::copyFrom(const RawPointerData& other) {
1368 pointerCount = other.pointerCount;
1369 hoveringIdBits = other.hoveringIdBits;
1370 touchingIdBits = other.touchingIdBits;
1371
1372 for (uint32_t i = 0; i < pointerCount; i++) {
1373 pointers[i] = other.pointers[i];
1374
1375 int id = pointers[i].id;
1376 idToIndex[id] = other.idToIndex[id];
1377 }
1378}
1379
1380void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1381 float x = 0, y = 0;
1382 uint32_t count = touchingIdBits.count();
1383 if (count) {
1384 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1385 uint32_t id = idBits.clearFirstMarkedBit();
1386 const Pointer& pointer = pointerForId(id);
1387 x += pointer.x;
1388 y += pointer.y;
1389 }
1390 x /= count;
1391 y /= count;
1392 }
1393 *outX = x;
1394 *outY = y;
1395}
1396
1397
1398// --- CookedPointerData ---
1399
1400CookedPointerData::CookedPointerData() {
1401 clear();
1402}
1403
1404void CookedPointerData::clear() {
1405 pointerCount = 0;
1406 hoveringIdBits.clear();
1407 touchingIdBits.clear();
1408}
1409
1410void CookedPointerData::copyFrom(const CookedPointerData& other) {
1411 pointerCount = other.pointerCount;
1412 hoveringIdBits = other.hoveringIdBits;
1413 touchingIdBits = other.touchingIdBits;
1414
1415 for (uint32_t i = 0; i < pointerCount; i++) {
1416 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1417 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1418
1419 int id = pointerProperties[i].id;
1420 idToIndex[id] = other.idToIndex[id];
1421 }
1422}
1423
1424
Jeff Brown49754db2011-07-01 17:37:58 -07001425// --- SingleTouchMotionAccumulator ---
1426
1427SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1428 clearAbsoluteAxes();
1429}
1430
Jeff Brown65fd2512011-08-18 11:20:58 -07001431void SingleTouchMotionAccumulator::reset(InputDevice* device) {
1432 mAbsX = device->getAbsoluteAxisValue(ABS_X);
1433 mAbsY = device->getAbsoluteAxisValue(ABS_Y);
1434 mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE);
1435 mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH);
1436 mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE);
1437 mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X);
1438 mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y);
1439}
1440
Jeff Brown49754db2011-07-01 17:37:58 -07001441void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1442 mAbsX = 0;
1443 mAbsY = 0;
1444 mAbsPressure = 0;
1445 mAbsToolWidth = 0;
1446 mAbsDistance = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07001447 mAbsTiltX = 0;
1448 mAbsTiltY = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001449}
1450
1451void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1452 if (rawEvent->type == EV_ABS) {
1453 switch (rawEvent->scanCode) {
1454 case ABS_X:
1455 mAbsX = rawEvent->value;
1456 break;
1457 case ABS_Y:
1458 mAbsY = rawEvent->value;
1459 break;
1460 case ABS_PRESSURE:
1461 mAbsPressure = rawEvent->value;
1462 break;
1463 case ABS_TOOL_WIDTH:
1464 mAbsToolWidth = rawEvent->value;
1465 break;
1466 case ABS_DISTANCE:
1467 mAbsDistance = rawEvent->value;
1468 break;
Jeff Brown65fd2512011-08-18 11:20:58 -07001469 case ABS_TILT_X:
1470 mAbsTiltX = rawEvent->value;
1471 break;
1472 case ABS_TILT_Y:
1473 mAbsTiltY = rawEvent->value;
1474 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001475 }
1476 }
1477}
1478
1479
1480// --- MultiTouchMotionAccumulator ---
1481
1482MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1483 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1484}
1485
1486MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1487 delete[] mSlots;
1488}
1489
1490void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1491 mSlotCount = slotCount;
1492 mUsingSlotsProtocol = usingSlotsProtocol;
1493
1494 delete[] mSlots;
1495 mSlots = new Slot[slotCount];
1496}
1497
Jeff Brown65fd2512011-08-18 11:20:58 -07001498void MultiTouchMotionAccumulator::reset(InputDevice* device) {
1499 // Unfortunately there is no way to read the initial contents of the slots.
1500 // So when we reset the accumulator, we must assume they are all zeroes.
1501 if (mUsingSlotsProtocol) {
1502 // Query the driver for the current slot index and use it as the initial slot
1503 // before we start reading events from the device. It is possible that the
1504 // current slot index will not be the same as it was when the first event was
1505 // written into the evdev buffer, which means the input mapper could start
1506 // out of sync with the initial state of the events in the evdev buffer.
1507 // In the extremely unlikely case that this happens, the data from
1508 // two slots will be confused until the next ABS_MT_SLOT event is received.
1509 // This can cause the touch point to "jump", but at least there will be
1510 // no stuck touches.
1511 int32_t initialSlot;
1512 status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(),
1513 ABS_MT_SLOT, &initialSlot);
1514 if (status) {
1515 LOGD("Could not retrieve current multitouch slot index. status=%d", status);
1516 initialSlot = -1;
1517 }
1518 clearSlots(initialSlot);
1519 } else {
1520 clearSlots(-1);
1521 }
1522}
1523
Jeff Brown49754db2011-07-01 17:37:58 -07001524void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001525 if (mSlots) {
1526 for (size_t i = 0; i < mSlotCount; i++) {
1527 mSlots[i].clear();
1528 }
Jeff Brown49754db2011-07-01 17:37:58 -07001529 }
1530 mCurrentSlot = initialSlot;
1531}
1532
1533void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1534 if (rawEvent->type == EV_ABS) {
1535 bool newSlot = false;
1536 if (mUsingSlotsProtocol) {
1537 if (rawEvent->scanCode == ABS_MT_SLOT) {
1538 mCurrentSlot = rawEvent->value;
1539 newSlot = true;
1540 }
1541 } else if (mCurrentSlot < 0) {
1542 mCurrentSlot = 0;
1543 }
1544
1545 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1546#if DEBUG_POINTERS
1547 if (newSlot) {
1548 LOGW("MultiTouch device emitted invalid slot index %d but it "
1549 "should be between 0 and %d; ignoring this slot.",
1550 mCurrentSlot, mSlotCount - 1);
1551 }
1552#endif
1553 } else {
1554 Slot* slot = &mSlots[mCurrentSlot];
1555
1556 switch (rawEvent->scanCode) {
1557 case ABS_MT_POSITION_X:
1558 slot->mInUse = true;
1559 slot->mAbsMTPositionX = rawEvent->value;
1560 break;
1561 case ABS_MT_POSITION_Y:
1562 slot->mInUse = true;
1563 slot->mAbsMTPositionY = rawEvent->value;
1564 break;
1565 case ABS_MT_TOUCH_MAJOR:
1566 slot->mInUse = true;
1567 slot->mAbsMTTouchMajor = rawEvent->value;
1568 break;
1569 case ABS_MT_TOUCH_MINOR:
1570 slot->mInUse = true;
1571 slot->mAbsMTTouchMinor = rawEvent->value;
1572 slot->mHaveAbsMTTouchMinor = true;
1573 break;
1574 case ABS_MT_WIDTH_MAJOR:
1575 slot->mInUse = true;
1576 slot->mAbsMTWidthMajor = rawEvent->value;
1577 break;
1578 case ABS_MT_WIDTH_MINOR:
1579 slot->mInUse = true;
1580 slot->mAbsMTWidthMinor = rawEvent->value;
1581 slot->mHaveAbsMTWidthMinor = true;
1582 break;
1583 case ABS_MT_ORIENTATION:
1584 slot->mInUse = true;
1585 slot->mAbsMTOrientation = rawEvent->value;
1586 break;
1587 case ABS_MT_TRACKING_ID:
1588 if (mUsingSlotsProtocol && rawEvent->value < 0) {
Jeff Brown8bcbbef2011-08-11 15:49:09 -07001589 // The slot is no longer in use but it retains its previous contents,
1590 // which may be reused for subsequent touches.
1591 slot->mInUse = false;
Jeff Brown49754db2011-07-01 17:37:58 -07001592 } else {
1593 slot->mInUse = true;
1594 slot->mAbsMTTrackingId = rawEvent->value;
1595 }
1596 break;
1597 case ABS_MT_PRESSURE:
1598 slot->mInUse = true;
1599 slot->mAbsMTPressure = rawEvent->value;
1600 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001601 case ABS_MT_DISTANCE:
1602 slot->mInUse = true;
1603 slot->mAbsMTDistance = rawEvent->value;
1604 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001605 case ABS_MT_TOOL_TYPE:
1606 slot->mInUse = true;
1607 slot->mAbsMTToolType = rawEvent->value;
1608 slot->mHaveAbsMTToolType = true;
1609 break;
1610 }
1611 }
1612 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1613 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1614 mCurrentSlot += 1;
1615 }
1616}
1617
Jeff Brown65fd2512011-08-18 11:20:58 -07001618void MultiTouchMotionAccumulator::finishSync() {
1619 if (!mUsingSlotsProtocol) {
1620 clearSlots(-1);
1621 }
1622}
1623
Jeff Brown49754db2011-07-01 17:37:58 -07001624
1625// --- MultiTouchMotionAccumulator::Slot ---
1626
1627MultiTouchMotionAccumulator::Slot::Slot() {
1628 clear();
1629}
1630
Jeff Brown49754db2011-07-01 17:37:58 -07001631void MultiTouchMotionAccumulator::Slot::clear() {
1632 mInUse = false;
1633 mHaveAbsMTTouchMinor = false;
1634 mHaveAbsMTWidthMinor = false;
1635 mHaveAbsMTToolType = false;
1636 mAbsMTPositionX = 0;
1637 mAbsMTPositionY = 0;
1638 mAbsMTTouchMajor = 0;
1639 mAbsMTTouchMinor = 0;
1640 mAbsMTWidthMajor = 0;
1641 mAbsMTWidthMinor = 0;
1642 mAbsMTOrientation = 0;
1643 mAbsMTTrackingId = -1;
1644 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001645 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001646 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001647}
1648
1649int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1650 if (mHaveAbsMTToolType) {
1651 switch (mAbsMTToolType) {
1652 case MT_TOOL_FINGER:
1653 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1654 case MT_TOOL_PEN:
1655 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1656 }
1657 }
1658 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1659}
1660
1661
Jeff Brown6d0fec22010-07-23 21:28:06 -07001662// --- InputMapper ---
1663
1664InputMapper::InputMapper(InputDevice* device) :
1665 mDevice(device), mContext(device->getContext()) {
1666}
1667
1668InputMapper::~InputMapper() {
1669}
1670
1671void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1672 info->addSource(getSources());
1673}
1674
Jeff Brownef3d7e82010-09-30 14:33:04 -07001675void InputMapper::dump(String8& dump) {
1676}
1677
Jeff Brown65fd2512011-08-18 11:20:58 -07001678void InputMapper::configure(nsecs_t when,
1679 const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001680}
1681
Jeff Brown65fd2512011-08-18 11:20:58 -07001682void InputMapper::reset(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001683}
1684
Jeff Brownaa3855d2011-03-17 01:34:19 -07001685void InputMapper::timeoutExpired(nsecs_t when) {
1686}
1687
Jeff Brown6d0fec22010-07-23 21:28:06 -07001688int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1689 return AKEY_STATE_UNKNOWN;
1690}
1691
1692int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1693 return AKEY_STATE_UNKNOWN;
1694}
1695
1696int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1697 return AKEY_STATE_UNKNOWN;
1698}
1699
1700bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1701 const int32_t* keyCodes, uint8_t* outFlags) {
1702 return false;
1703}
1704
1705int32_t InputMapper::getMetaState() {
1706 return 0;
1707}
1708
Jeff Brown05dc66a2011-03-02 14:41:58 -08001709void InputMapper::fadePointer() {
1710}
1711
Jeff Brownbe1aa822011-07-27 16:04:54 -07001712status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1713 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1714}
1715
Jeff Browncb1404e2011-01-15 18:14:15 -08001716void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1717 const RawAbsoluteAxisInfo& axis, const char* name) {
1718 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001719 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1720 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001721 } else {
1722 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1723 }
1724}
1725
Jeff Brown6d0fec22010-07-23 21:28:06 -07001726
1727// --- SwitchInputMapper ---
1728
1729SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1730 InputMapper(device) {
1731}
1732
1733SwitchInputMapper::~SwitchInputMapper() {
1734}
1735
1736uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001737 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001738}
1739
1740void SwitchInputMapper::process(const RawEvent* rawEvent) {
1741 switch (rawEvent->type) {
1742 case EV_SW:
1743 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1744 break;
1745 }
1746}
1747
1748void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001749 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1750 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001751}
1752
1753int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1754 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1755}
1756
1757
1758// --- KeyboardInputMapper ---
1759
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001760KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001761 uint32_t source, int32_t keyboardType) :
1762 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001763 mKeyboardType(keyboardType) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001764}
1765
1766KeyboardInputMapper::~KeyboardInputMapper() {
1767}
1768
Jeff Brown6d0fec22010-07-23 21:28:06 -07001769uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001770 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001771}
1772
1773void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1774 InputMapper::populateDeviceInfo(info);
1775
1776 info->setKeyboardType(mKeyboardType);
1777}
1778
Jeff Brownef3d7e82010-09-30 14:33:04 -07001779void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001780 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1781 dumpParameters(dump);
1782 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
Jeff Brown65fd2512011-08-18 11:20:58 -07001783 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001784 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1785 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1786 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001787}
1788
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001789
Jeff Brown65fd2512011-08-18 11:20:58 -07001790void KeyboardInputMapper::configure(nsecs_t when,
1791 const InputReaderConfiguration* config, uint32_t changes) {
1792 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001793
Jeff Brown474dcb52011-06-14 20:22:50 -07001794 if (!changes) { // first time only
1795 // Configure basic parameters.
1796 configureParameters();
Jeff Brown65fd2512011-08-18 11:20:58 -07001797 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001798
Jeff Brown65fd2512011-08-18 11:20:58 -07001799 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1800 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1801 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
1802 false /*external*/, NULL, NULL, &mOrientation)) {
1803 mOrientation = DISPLAY_ORIENTATION_0;
1804 }
1805 } else {
1806 mOrientation = DISPLAY_ORIENTATION_0;
1807 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001808 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001809}
1810
1811void KeyboardInputMapper::configureParameters() {
1812 mParameters.orientationAware = false;
1813 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1814 mParameters.orientationAware);
1815
Jeff Brownbc68a592011-07-25 12:58:12 -07001816 mParameters.associatedDisplayId = -1;
1817 if (mParameters.orientationAware) {
1818 mParameters.associatedDisplayId = 0;
1819 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001820}
1821
1822void KeyboardInputMapper::dumpParameters(String8& dump) {
1823 dump.append(INDENT3 "Parameters:\n");
1824 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1825 mParameters.associatedDisplayId);
1826 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1827 toString(mParameters.orientationAware));
1828}
1829
Jeff Brown65fd2512011-08-18 11:20:58 -07001830void KeyboardInputMapper::reset(nsecs_t when) {
1831 mMetaState = AMETA_NONE;
1832 mDownTime = 0;
1833 mKeyDowns.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001834
Jeff Brownbe1aa822011-07-27 16:04:54 -07001835 resetLedState();
1836
Jeff Brown65fd2512011-08-18 11:20:58 -07001837 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001838}
1839
1840void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1841 switch (rawEvent->type) {
1842 case EV_KEY: {
1843 int32_t scanCode = rawEvent->scanCode;
1844 if (isKeyboardOrGamepadKey(scanCode)) {
1845 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1846 rawEvent->flags);
1847 }
1848 break;
1849 }
1850 }
1851}
1852
1853bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1854 return scanCode < BTN_MOUSE
1855 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001856 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001857 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001858}
1859
Jeff Brown6328cdc2010-07-29 18:18:33 -07001860void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1861 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001862
Jeff Brownbe1aa822011-07-27 16:04:54 -07001863 if (down) {
1864 // Rotate key codes according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001865 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07001866 keyCode = rotateKeyCode(keyCode, mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001867 }
Jeff Brownfe508922011-01-18 15:10:10 -08001868
Jeff Brownbe1aa822011-07-27 16:04:54 -07001869 // Add key down.
1870 ssize_t keyDownIndex = findKeyDown(scanCode);
1871 if (keyDownIndex >= 0) {
1872 // key repeat, be sure to use same keycode as before in case of rotation
1873 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001874 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001875 // key down
1876 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1877 && mContext->shouldDropVirtualKey(when,
1878 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001879 return;
1880 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001881
1882 mKeyDowns.push();
1883 KeyDown& keyDown = mKeyDowns.editTop();
1884 keyDown.keyCode = keyCode;
1885 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001886 }
1887
Jeff Brownbe1aa822011-07-27 16:04:54 -07001888 mDownTime = when;
1889 } else {
1890 // Remove key down.
1891 ssize_t keyDownIndex = findKeyDown(scanCode);
1892 if (keyDownIndex >= 0) {
1893 // key up, be sure to use same keycode as before in case of rotation
1894 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1895 mKeyDowns.removeAt(size_t(keyDownIndex));
1896 } else {
1897 // key was not actually down
1898 LOGI("Dropping key up from device %s because the key was not down. "
1899 "keyCode=%d, scanCode=%d",
1900 getDeviceName().string(), keyCode, scanCode);
1901 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001902 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001903 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001904
Jeff Brownbe1aa822011-07-27 16:04:54 -07001905 bool metaStateChanged = false;
1906 int32_t oldMetaState = mMetaState;
1907 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1908 if (oldMetaState != newMetaState) {
1909 mMetaState = newMetaState;
1910 metaStateChanged = true;
1911 updateLedState(false);
1912 }
1913
1914 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001915
Jeff Brown56194eb2011-03-02 19:23:13 -08001916 // Key down on external an keyboard should wake the device.
1917 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1918 // For internal keyboards, the key layout file should specify the policy flags for
1919 // each wake key individually.
1920 // TODO: Use the input device configuration to control this behavior more finely.
1921 if (down && getDevice()->isExternal()
1922 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1923 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1924 }
1925
Jeff Brown6328cdc2010-07-29 18:18:33 -07001926 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001927 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001928 }
1929
Jeff Brown05dc66a2011-03-02 14:41:58 -08001930 if (down && !isMetaKey(keyCode)) {
1931 getContext()->fadePointer();
1932 }
1933
Jeff Brownbe1aa822011-07-27 16:04:54 -07001934 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001935 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1936 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001937 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001938}
1939
Jeff Brownbe1aa822011-07-27 16:04:54 -07001940ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1941 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001942 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001943 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001944 return i;
1945 }
1946 }
1947 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001948}
1949
Jeff Brown6d0fec22010-07-23 21:28:06 -07001950int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1951 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1952}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001953
Jeff Brown6d0fec22010-07-23 21:28:06 -07001954int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1955 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1956}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001957
Jeff Brown6d0fec22010-07-23 21:28:06 -07001958bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1959 const int32_t* keyCodes, uint8_t* outFlags) {
1960 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1961}
1962
1963int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001964 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001965}
1966
Jeff Brownbe1aa822011-07-27 16:04:54 -07001967void KeyboardInputMapper::resetLedState() {
1968 initializeLedState(mCapsLockLedState, LED_CAPSL);
1969 initializeLedState(mNumLockLedState, LED_NUML);
1970 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001971
Jeff Brownbe1aa822011-07-27 16:04:54 -07001972 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001973}
1974
Jeff Brownbe1aa822011-07-27 16:04:54 -07001975void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001976 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1977 ledState.on = false;
1978}
1979
Jeff Brownbe1aa822011-07-27 16:04:54 -07001980void KeyboardInputMapper::updateLedState(bool reset) {
1981 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001982 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001983 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001984 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001985 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001986 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001987}
1988
Jeff Brownbe1aa822011-07-27 16:04:54 -07001989void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001990 int32_t led, int32_t modifier, bool reset) {
1991 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001992 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001993 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001994 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1995 ledState.on = desiredState;
1996 }
1997 }
1998}
1999
Jeff Brown6d0fec22010-07-23 21:28:06 -07002000
Jeff Brown83c09682010-12-23 17:50:18 -08002001// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07002002
Jeff Brown83c09682010-12-23 17:50:18 -08002003CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002004 InputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002005}
2006
Jeff Brown83c09682010-12-23 17:50:18 -08002007CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002008}
2009
Jeff Brown83c09682010-12-23 17:50:18 -08002010uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08002011 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002012}
2013
Jeff Brown83c09682010-12-23 17:50:18 -08002014void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002015 InputMapper::populateDeviceInfo(info);
2016
Jeff Brown83c09682010-12-23 17:50:18 -08002017 if (mParameters.mode == Parameters::MODE_POINTER) {
2018 float minX, minY, maxX, maxY;
2019 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08002020 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
2021 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08002022 }
2023 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08002024 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
2025 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08002026 }
Jeff Brownefd32662011-03-08 15:13:06 -08002027 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002028
Jeff Brown65fd2512011-08-18 11:20:58 -07002029 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002030 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002031 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002032 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08002033 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08002034 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002035}
2036
Jeff Brown83c09682010-12-23 17:50:18 -08002037void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002038 dump.append(INDENT2 "Cursor Input Mapper:\n");
2039 dumpParameters(dump);
2040 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
2041 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
2042 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
2043 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
2044 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002045 toString(mCursorScrollAccumulator.haveRelativeVWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002046 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002047 toString(mCursorScrollAccumulator.haveRelativeHWheel()));
Jeff Brownbe1aa822011-07-27 16:04:54 -07002048 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
2049 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002050 dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002051 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
2052 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
2053 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002054}
2055
Jeff Brown65fd2512011-08-18 11:20:58 -07002056void CursorInputMapper::configure(nsecs_t when,
2057 const InputReaderConfiguration* config, uint32_t changes) {
2058 InputMapper::configure(when, config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002059
Jeff Brown474dcb52011-06-14 20:22:50 -07002060 if (!changes) { // first time only
Jeff Brown65fd2512011-08-18 11:20:58 -07002061 mCursorScrollAccumulator.configure(getDevice());
Jeff Brown49754db2011-07-01 17:37:58 -07002062
Jeff Brown474dcb52011-06-14 20:22:50 -07002063 // Configure basic parameters.
2064 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08002065
Jeff Brown474dcb52011-06-14 20:22:50 -07002066 // Configure device mode.
2067 switch (mParameters.mode) {
2068 case Parameters::MODE_POINTER:
2069 mSource = AINPUT_SOURCE_MOUSE;
2070 mXPrecision = 1.0f;
2071 mYPrecision = 1.0f;
2072 mXScale = 1.0f;
2073 mYScale = 1.0f;
2074 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2075 break;
2076 case Parameters::MODE_NAVIGATION:
2077 mSource = AINPUT_SOURCE_TRACKBALL;
2078 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2079 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
2080 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2081 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
2082 break;
2083 }
2084
2085 mVWheelScale = 1.0f;
2086 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08002087 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08002088
Jeff Brown474dcb52011-06-14 20:22:50 -07002089 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2090 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
2091 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
2092 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
2093 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002094
2095 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
2096 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
2097 if (!config->getDisplayInfo(mParameters.associatedDisplayId,
2098 false /*external*/, NULL, NULL, &mOrientation)) {
2099 mOrientation = DISPLAY_ORIENTATION_0;
2100 }
2101 } else {
2102 mOrientation = DISPLAY_ORIENTATION_0;
2103 }
2104 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002105}
2106
Jeff Brown83c09682010-12-23 17:50:18 -08002107void CursorInputMapper::configureParameters() {
2108 mParameters.mode = Parameters::MODE_POINTER;
2109 String8 cursorModeString;
2110 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
2111 if (cursorModeString == "navigation") {
2112 mParameters.mode = Parameters::MODE_NAVIGATION;
2113 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
2114 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
2115 }
2116 }
2117
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002118 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08002119 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002120 mParameters.orientationAware);
2121
Jeff Brownbc68a592011-07-25 12:58:12 -07002122 mParameters.associatedDisplayId = -1;
2123 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
2124 mParameters.associatedDisplayId = 0;
2125 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002126}
2127
Jeff Brown83c09682010-12-23 17:50:18 -08002128void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002129 dump.append(INDENT3 "Parameters:\n");
2130 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2131 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08002132
2133 switch (mParameters.mode) {
2134 case Parameters::MODE_POINTER:
2135 dump.append(INDENT4 "Mode: pointer\n");
2136 break;
2137 case Parameters::MODE_NAVIGATION:
2138 dump.append(INDENT4 "Mode: navigation\n");
2139 break;
2140 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002141 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08002142 }
2143
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002144 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2145 toString(mParameters.orientationAware));
2146}
2147
Jeff Brown65fd2512011-08-18 11:20:58 -07002148void CursorInputMapper::reset(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002149 mButtonState = 0;
2150 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002151
Jeff Brownbe1aa822011-07-27 16:04:54 -07002152 mPointerVelocityControl.reset();
2153 mWheelXVelocityControl.reset();
2154 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002155
Jeff Brown65fd2512011-08-18 11:20:58 -07002156 mCursorButtonAccumulator.reset(getDevice());
2157 mCursorMotionAccumulator.reset(getDevice());
2158 mCursorScrollAccumulator.reset(getDevice());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002159
Jeff Brown65fd2512011-08-18 11:20:58 -07002160 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002161}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002162
Jeff Brown83c09682010-12-23 17:50:18 -08002163void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07002164 mCursorButtonAccumulator.process(rawEvent);
2165 mCursorMotionAccumulator.process(rawEvent);
Jeff Brown65fd2512011-08-18 11:20:58 -07002166 mCursorScrollAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08002167
Jeff Brown49754db2011-07-01 17:37:58 -07002168 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
2169 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002170 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002171}
2172
Jeff Brown83c09682010-12-23 17:50:18 -08002173void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002174 int32_t lastButtonState = mButtonState;
2175 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
2176 mButtonState = currentButtonState;
2177
2178 bool wasDown = isPointerDown(lastButtonState);
2179 bool down = isPointerDown(currentButtonState);
2180 bool downChanged;
2181 if (!wasDown && down) {
2182 mDownTime = when;
2183 downChanged = true;
2184 } else if (wasDown && !down) {
2185 downChanged = true;
2186 } else {
2187 downChanged = false;
2188 }
2189 nsecs_t downTime = mDownTime;
2190 bool buttonsChanged = currentButtonState != lastButtonState;
Jeff Brownc28306a2011-08-23 21:32:42 -07002191 bool buttonsPressed = currentButtonState & ~lastButtonState;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002192
2193 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
2194 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
2195 bool moved = deltaX != 0 || deltaY != 0;
2196
Jeff Brown65fd2512011-08-18 11:20:58 -07002197 // Rotate delta according to orientation if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002198 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2199 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002200 rotateDelta(mOrientation, &deltaX, &deltaY);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002201 }
2202
Jeff Brown65fd2512011-08-18 11:20:58 -07002203 // Move the pointer.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002204 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002205 pointerProperties.clear();
2206 pointerProperties.id = 0;
2207 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2208
Jeff Brown6328cdc2010-07-29 18:18:33 -07002209 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002210 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002211
Jeff Brown65fd2512011-08-18 11:20:58 -07002212 float vscroll = mCursorScrollAccumulator.getRelativeVWheel();
2213 float hscroll = mCursorScrollAccumulator.getRelativeHWheel();
Jeff Brownbe1aa822011-07-27 16:04:54 -07002214 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002215
Jeff Brownbe1aa822011-07-27 16:04:54 -07002216 mWheelYVelocityControl.move(when, NULL, &vscroll);
2217 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002218
Jeff Brownbe1aa822011-07-27 16:04:54 -07002219 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002220
Jeff Brownbe1aa822011-07-27 16:04:54 -07002221 if (mPointerController != NULL) {
2222 if (moved || scrolled || buttonsChanged) {
2223 mPointerController->setPresentation(
2224 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002225
Jeff Brownbe1aa822011-07-27 16:04:54 -07002226 if (moved) {
2227 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002228 }
2229
Jeff Brownbe1aa822011-07-27 16:04:54 -07002230 if (buttonsChanged) {
2231 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002232 }
Jeff Brownefd32662011-03-08 15:13:06 -08002233
Jeff Brownbe1aa822011-07-27 16:04:54 -07002234 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002235 }
2236
Jeff Brownbe1aa822011-07-27 16:04:54 -07002237 float x, y;
2238 mPointerController->getPosition(&x, &y);
2239 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2240 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2241 } else {
2242 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2243 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2244 }
2245
2246 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002247
Jeff Brown56194eb2011-03-02 19:23:13 -08002248 // Moving an external trackball or mouse should wake the device.
2249 // We don't do this for internal cursor devices to prevent them from waking up
2250 // the device in your pocket.
2251 // TODO: Use the input device configuration to control this behavior more finely.
2252 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07002253 if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002254 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2255 }
2256
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002257 // Synthesize key down from buttons if needed.
2258 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2259 policyFlags, lastButtonState, currentButtonState);
2260
2261 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002262 if (downChanged || moved || scrolled || buttonsChanged) {
2263 int32_t metaState = mContext->getGlobalMetaState();
2264 int32_t motionEventAction;
2265 if (downChanged) {
2266 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2267 } else if (down || mPointerController == NULL) {
2268 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2269 } else {
2270 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2271 }
Jeff Brownb6997262010-10-08 22:31:17 -07002272
Jeff Brownbe1aa822011-07-27 16:04:54 -07002273 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2274 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002275 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002276 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002277
Jeff Brownbe1aa822011-07-27 16:04:54 -07002278 // Send hover move after UP to tell the application that the mouse is hovering now.
2279 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2280 && mPointerController != NULL) {
2281 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2282 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2283 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2284 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2285 getListener()->notifyMotion(&hoverArgs);
2286 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002287
Jeff Brownbe1aa822011-07-27 16:04:54 -07002288 // Send scroll events.
2289 if (scrolled) {
2290 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2291 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2292
2293 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2294 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2295 AMOTION_EVENT_EDGE_FLAG_NONE,
2296 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2297 getListener()->notifyMotion(&scrollArgs);
2298 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002299 }
Jeff Browna032cc02011-03-07 16:56:21 -08002300
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002301 // Synthesize key up from buttons if needed.
2302 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2303 policyFlags, lastButtonState, currentButtonState);
2304
Jeff Brown65fd2512011-08-18 11:20:58 -07002305 mCursorMotionAccumulator.finishSync();
2306 mCursorScrollAccumulator.finishSync();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002307}
2308
Jeff Brown83c09682010-12-23 17:50:18 -08002309int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002310 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2311 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2312 } else {
2313 return AKEY_STATE_UNKNOWN;
2314 }
2315}
2316
Jeff Brown05dc66a2011-03-02 14:41:58 -08002317void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002318 if (mPointerController != NULL) {
2319 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2320 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002321}
2322
Jeff Brown6d0fec22010-07-23 21:28:06 -07002323
2324// --- TouchInputMapper ---
2325
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002326TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002327 InputMapper(device),
Jeff Brown65fd2512011-08-18 11:20:58 -07002328 mSource(0), mDeviceMode(DEVICE_MODE_DISABLED),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002329 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002330}
2331
2332TouchInputMapper::~TouchInputMapper() {
2333}
2334
2335uint32_t TouchInputMapper::getSources() {
Jeff Brown65fd2512011-08-18 11:20:58 -07002336 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002337}
2338
2339void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2340 InputMapper::populateDeviceInfo(info);
2341
Jeff Brown65fd2512011-08-18 11:20:58 -07002342 if (mDeviceMode != DEVICE_MODE_DISABLED) {
2343 info->addMotionRange(mOrientedRanges.x);
2344 info->addMotionRange(mOrientedRanges.y);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002345 info->addMotionRange(mOrientedRanges.pressure);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002346
Jeff Brown65fd2512011-08-18 11:20:58 -07002347 if (mOrientedRanges.haveSize) {
2348 info->addMotionRange(mOrientedRanges.size);
Jeff Brownefd32662011-03-08 15:13:06 -08002349 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002350
2351 if (mOrientedRanges.haveTouchSize) {
2352 info->addMotionRange(mOrientedRanges.touchMajor);
2353 info->addMotionRange(mOrientedRanges.touchMinor);
2354 }
2355
2356 if (mOrientedRanges.haveToolSize) {
2357 info->addMotionRange(mOrientedRanges.toolMajor);
2358 info->addMotionRange(mOrientedRanges.toolMinor);
2359 }
2360
2361 if (mOrientedRanges.haveOrientation) {
2362 info->addMotionRange(mOrientedRanges.orientation);
2363 }
2364
2365 if (mOrientedRanges.haveDistance) {
2366 info->addMotionRange(mOrientedRanges.distance);
2367 }
2368
2369 if (mOrientedRanges.haveTilt) {
2370 info->addMotionRange(mOrientedRanges.tilt);
2371 }
2372
2373 if (mCursorScrollAccumulator.haveRelativeVWheel()) {
2374 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2375 }
2376 if (mCursorScrollAccumulator.haveRelativeHWheel()) {
2377 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
2378 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002379 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002380}
2381
Jeff Brownef3d7e82010-09-30 14:33:04 -07002382void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002383 dump.append(INDENT2 "Touch Input Mapper:\n");
2384 dumpParameters(dump);
2385 dumpVirtualKeys(dump);
2386 dumpRawPointerAxes(dump);
2387 dumpCalibration(dump);
2388 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002389
Jeff Brownbe1aa822011-07-27 16:04:54 -07002390 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2391 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2392 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2393 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2394 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2395 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002396 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2397 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002398 dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002399 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2400 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brown65fd2512011-08-18 11:20:58 -07002401 dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt));
2402 dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter);
2403 dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale);
2404 dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter);
2405 dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002406
Jeff Brownbe1aa822011-07-27 16:04:54 -07002407 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002408
Jeff Brownbe1aa822011-07-27 16:04:54 -07002409 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2410 mLastRawPointerData.pointerCount);
2411 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2412 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2413 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2414 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002415 "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, "
2416 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002417 pointer.id, pointer.x, pointer.y, pointer.pressure,
2418 pointer.touchMajor, pointer.touchMinor,
2419 pointer.toolMajor, pointer.toolMinor,
Jeff Brown65fd2512011-08-18 11:20:58 -07002420 pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002421 pointer.toolType, toString(pointer.isHovering));
2422 }
2423
2424 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2425 mLastCookedPointerData.pointerCount);
2426 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2427 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2428 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2429 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2430 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
Jeff Brown65fd2512011-08-18 11:20:58 -07002431 "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, "
2432 "toolType=%d, isHovering=%s\n", i,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002433 pointerProperties.id,
2434 pointerCoords.getX(),
2435 pointerCoords.getY(),
2436 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2437 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2438 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2439 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2440 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2441 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
Jeff Brown65fd2512011-08-18 11:20:58 -07002442 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT),
Jeff Brownbe1aa822011-07-27 16:04:54 -07002443 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2444 pointerProperties.toolType,
2445 toString(mLastCookedPointerData.isHovering(i)));
2446 }
2447
Jeff Brown65fd2512011-08-18 11:20:58 -07002448 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002449 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2450 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002451 mPointerXMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002452 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002453 mPointerYMovementScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002454 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002455 mPointerXZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002456 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
Jeff Brown65fd2512011-08-18 11:20:58 -07002457 mPointerYZoomScale);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002458 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2459 mPointerGestureMaxSwipeWidth);
2460 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002461}
2462
Jeff Brown65fd2512011-08-18 11:20:58 -07002463void TouchInputMapper::configure(nsecs_t when,
2464 const InputReaderConfiguration* config, uint32_t changes) {
2465 InputMapper::configure(when, config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002466
Jeff Brown474dcb52011-06-14 20:22:50 -07002467 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002468
Jeff Brown474dcb52011-06-14 20:22:50 -07002469 if (!changes) { // first time only
2470 // Configure basic parameters.
2471 configureParameters();
2472
Jeff Brown65fd2512011-08-18 11:20:58 -07002473 // Configure common accumulators.
2474 mCursorScrollAccumulator.configure(getDevice());
2475 mTouchButtonAccumulator.configure(getDevice());
Jeff Brown474dcb52011-06-14 20:22:50 -07002476
2477 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002478 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002479
2480 // Prepare input device calibration.
2481 parseCalibration();
2482 resolveCalibration();
Jeff Brown83c09682010-12-23 17:50:18 -08002483 }
2484
Jeff Brown474dcb52011-06-14 20:22:50 -07002485 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002486 // Update pointer speed.
2487 mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters);
2488 mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
2489 mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters);
Jeff Brown474dcb52011-06-14 20:22:50 -07002490 }
Jeff Brown8d608662010-08-30 03:02:23 -07002491
Jeff Brown65fd2512011-08-18 11:20:58 -07002492 bool resetNeeded = false;
2493 if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO
Jeff Browndaf4a122011-08-26 17:14:14 -07002494 | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT
2495 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002496 // Configure device sources, surface dimensions, orientation and
2497 // scaling factors.
2498 configureSurface(when, &resetNeeded);
2499 }
2500
2501 if (changes && resetNeeded) {
2502 // Send reset, unless this is the first time the device has been configured,
2503 // in which case the reader will call reset itself after all mappers are ready.
2504 getDevice()->notifyReset(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002505 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002506}
2507
Jeff Brown8d608662010-08-30 03:02:23 -07002508void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002509 // Use the pointer presentation mode for devices that do not support distinct
2510 // multitouch. The spot-based presentation relies on being able to accurately
2511 // locate two or more fingers on the touch pad.
2512 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2513 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002514
Jeff Brown538881e2011-05-25 18:23:38 -07002515 String8 gestureModeString;
2516 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2517 gestureModeString)) {
2518 if (gestureModeString == "pointer") {
2519 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2520 } else if (gestureModeString == "spots") {
2521 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2522 } else if (gestureModeString != "default") {
2523 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2524 }
2525 }
2526
Jeff Brownace13b12011-03-09 17:39:48 -08002527 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2528 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2529 // The device is a cursor device with a touch pad attached.
2530 // By default don't use the touch pad to move the pointer.
2531 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002532 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2533 // The device is a pointing device like a track pad.
2534 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2535 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2536 // The device is a touch screen.
2537 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002538 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002539 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002540 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2541 }
2542
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002543 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002544 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2545 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002546 if (deviceTypeString == "touchScreen") {
2547 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002548 } else if (deviceTypeString == "touchPad") {
2549 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002550 } else if (deviceTypeString == "pointer") {
2551 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002552 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002553 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2554 }
2555 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002556
Jeff Brownefd32662011-03-08 15:13:06 -08002557 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002558 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2559 mParameters.orientationAware);
2560
Jeff Brownbc68a592011-07-25 12:58:12 -07002561 mParameters.associatedDisplayId = -1;
2562 mParameters.associatedDisplayIsExternal = false;
2563 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002564 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002565 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2566 mParameters.associatedDisplayIsExternal =
2567 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2568 && getDevice()->isExternal();
2569 mParameters.associatedDisplayId = 0;
2570 }
Jeff Brown8d608662010-08-30 03:02:23 -07002571}
2572
Jeff Brownef3d7e82010-09-30 14:33:04 -07002573void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002574 dump.append(INDENT3 "Parameters:\n");
2575
Jeff Brown538881e2011-05-25 18:23:38 -07002576 switch (mParameters.gestureMode) {
2577 case Parameters::GESTURE_MODE_POINTER:
2578 dump.append(INDENT4 "GestureMode: pointer\n");
2579 break;
2580 case Parameters::GESTURE_MODE_SPOTS:
2581 dump.append(INDENT4 "GestureMode: spots\n");
2582 break;
2583 default:
2584 assert(false);
2585 }
2586
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002587 switch (mParameters.deviceType) {
2588 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2589 dump.append(INDENT4 "DeviceType: touchScreen\n");
2590 break;
2591 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2592 dump.append(INDENT4 "DeviceType: touchPad\n");
2593 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002594 case Parameters::DEVICE_TYPE_POINTER:
2595 dump.append(INDENT4 "DeviceType: pointer\n");
2596 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002597 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002598 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002599 }
2600
Jeff Brown65fd2512011-08-18 11:20:58 -07002601 dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n",
2602 mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002603 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2604 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002605}
2606
Jeff Brownbe1aa822011-07-27 16:04:54 -07002607void TouchInputMapper::configureRawPointerAxes() {
2608 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002609}
2610
Jeff Brownbe1aa822011-07-27 16:04:54 -07002611void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2612 dump.append(INDENT3 "Raw Touch Axes:\n");
2613 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2614 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2615 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2616 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2617 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2618 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2619 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2620 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2621 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
Jeff Brown65fd2512011-08-18 11:20:58 -07002622 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX");
2623 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY");
Jeff Brownbe1aa822011-07-27 16:04:54 -07002624 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2625 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002626}
2627
Jeff Brown65fd2512011-08-18 11:20:58 -07002628void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) {
2629 int32_t oldDeviceMode = mDeviceMode;
2630
2631 // Determine device mode.
2632 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2633 && mConfig.pointerGesturesEnabled) {
2634 mSource = AINPUT_SOURCE_MOUSE;
2635 mDeviceMode = DEVICE_MODE_POINTER;
2636 } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2637 && mParameters.associatedDisplayId >= 0) {
2638 mSource = AINPUT_SOURCE_TOUCHSCREEN;
2639 mDeviceMode = DEVICE_MODE_DIRECT;
2640 } else {
2641 mSource = AINPUT_SOURCE_TOUCHPAD;
2642 mDeviceMode = DEVICE_MODE_UNSCALED;
2643 }
2644
Jeff Brown9626b142011-03-03 02:09:54 -08002645 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002646 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002647 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2648 "The device will be inoperable.", getDeviceName().string());
Jeff Brown65fd2512011-08-18 11:20:58 -07002649 mDeviceMode = DEVICE_MODE_DISABLED;
2650 return;
Jeff Brown9626b142011-03-03 02:09:54 -08002651 }
2652
Jeff Brown65fd2512011-08-18 11:20:58 -07002653 // Get associated display dimensions.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002654 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002655 if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002656 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002657 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2658 &mAssociatedDisplayOrientation)) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002659 LOGI(INDENT "Touch device '%s' could not query the properties of its associated "
2660 "display %d. The device will be inoperable until the display size "
2661 "becomes available.",
2662 getDeviceName().string(), mParameters.associatedDisplayId);
2663 mDeviceMode = DEVICE_MODE_DISABLED;
2664 return;
Jeff Brownefd32662011-03-08 15:13:06 -08002665 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002666 }
2667
Jeff Brown65fd2512011-08-18 11:20:58 -07002668 // Configure dimensions.
2669 int32_t width, height, orientation;
2670 if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) {
2671 width = mAssociatedDisplayWidth;
2672 height = mAssociatedDisplayHeight;
2673 orientation = mParameters.orientationAware ?
2674 mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0;
2675 } else {
2676 width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2677 height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
2678 orientation = DISPLAY_ORIENTATION_0;
2679 }
2680
2681 // If moving between pointer modes, need to reset some state.
2682 bool deviceModeChanged;
2683 if (mDeviceMode != oldDeviceMode) {
2684 deviceModeChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07002685 mOrientedRanges.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002686 }
2687
Jeff Browndaf4a122011-08-26 17:14:14 -07002688 // Create pointer controller if needed.
2689 if (mDeviceMode == DEVICE_MODE_POINTER ||
2690 (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) {
2691 if (mPointerController == NULL) {
2692 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2693 }
2694 } else {
2695 mPointerController.clear();
2696 }
2697
Jeff Brownbe1aa822011-07-27 16:04:54 -07002698 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002699 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002700 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002701 }
2702
Jeff Brownbe1aa822011-07-27 16:04:54 -07002703 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown65fd2512011-08-18 11:20:58 -07002704 if (sizeChanged || deviceModeChanged) {
2705 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d",
2706 getDeviceId(), getDeviceName().string(), width, height, mDeviceMode);
Jeff Brown8d608662010-08-30 03:02:23 -07002707
Jeff Brownbe1aa822011-07-27 16:04:54 -07002708 mSurfaceWidth = width;
2709 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002710
Jeff Brown8d608662010-08-30 03:02:23 -07002711 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002712 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2713 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2714 mXPrecision = 1.0f / mXScale;
2715 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002716
Jeff Brownbe1aa822011-07-27 16:04:54 -07002717 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
Jeff Brown65fd2512011-08-18 11:20:58 -07002718 mOrientedRanges.x.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002719 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
Jeff Brown65fd2512011-08-18 11:20:58 -07002720 mOrientedRanges.y.source = mSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002721
Jeff Brownbe1aa822011-07-27 16:04:54 -07002722 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002723
Jeff Brown8d608662010-08-30 03:02:23 -07002724 // Scale factor for terms that are not oriented in a particular axis.
2725 // If the pixels are square then xScale == yScale otherwise we fake it
2726 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002727 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002728
Jeff Brown8d608662010-08-30 03:02:23 -07002729 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002730 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002731
Jeff Browna1f89ce2011-08-11 00:05:01 -07002732 // Size factors.
2733 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
2734 if (mRawPointerAxes.touchMajor.valid
2735 && mRawPointerAxes.touchMajor.maxValue != 0) {
2736 mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue;
2737 } else if (mRawPointerAxes.toolMajor.valid
2738 && mRawPointerAxes.toolMajor.maxValue != 0) {
2739 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
2740 } else {
2741 mSizeScale = 0.0f;
2742 }
2743
Jeff Brownbe1aa822011-07-27 16:04:54 -07002744 mOrientedRanges.haveTouchSize = true;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002745 mOrientedRanges.haveToolSize = true;
2746 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002747
Jeff Brownbe1aa822011-07-27 16:04:54 -07002748 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002749 mOrientedRanges.touchMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002750 mOrientedRanges.touchMajor.min = 0;
2751 mOrientedRanges.touchMajor.max = diagonalSize;
2752 mOrientedRanges.touchMajor.flat = 0;
2753 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002754
Jeff Brownbe1aa822011-07-27 16:04:54 -07002755 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2756 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brownefd32662011-03-08 15:13:06 -08002757
Jeff Brownbe1aa822011-07-27 16:04:54 -07002758 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
Jeff Brown65fd2512011-08-18 11:20:58 -07002759 mOrientedRanges.toolMajor.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002760 mOrientedRanges.toolMajor.min = 0;
2761 mOrientedRanges.toolMajor.max = diagonalSize;
2762 mOrientedRanges.toolMajor.flat = 0;
2763 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002764
Jeff Brownbe1aa822011-07-27 16:04:54 -07002765 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2766 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002767
2768 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002769 mOrientedRanges.size.source = mSource;
Jeff Browna1f89ce2011-08-11 00:05:01 -07002770 mOrientedRanges.size.min = 0;
2771 mOrientedRanges.size.max = 1.0;
2772 mOrientedRanges.size.flat = 0;
2773 mOrientedRanges.size.fuzz = 0;
2774 } else {
2775 mSizeScale = 0.0f;
Jeff Brown8d608662010-08-30 03:02:23 -07002776 }
2777
2778 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002779 mPressureScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002780 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2781 || mCalibration.pressureCalibration
2782 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2783 if (mCalibration.havePressureScale) {
2784 mPressureScale = mCalibration.pressureScale;
2785 } else if (mRawPointerAxes.pressure.valid
2786 && mRawPointerAxes.pressure.maxValue != 0) {
2787 mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002788 }
Jeff Brown65fd2512011-08-18 11:20:58 -07002789 }
Jeff Brown8d608662010-08-30 03:02:23 -07002790
Jeff Brown65fd2512011-08-18 11:20:58 -07002791 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2792 mOrientedRanges.pressure.source = mSource;
2793 mOrientedRanges.pressure.min = 0;
2794 mOrientedRanges.pressure.max = 1.0;
2795 mOrientedRanges.pressure.flat = 0;
2796 mOrientedRanges.pressure.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002797
Jeff Brown65fd2512011-08-18 11:20:58 -07002798 // Tilt
2799 mTiltXCenter = 0;
2800 mTiltXScale = 0;
2801 mTiltYCenter = 0;
2802 mTiltYScale = 0;
2803 mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid;
2804 if (mHaveTilt) {
2805 mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue,
2806 mRawPointerAxes.tiltX.maxValue);
2807 mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue,
2808 mRawPointerAxes.tiltY.maxValue);
2809 mTiltXScale = M_PI / 180;
2810 mTiltYScale = M_PI / 180;
2811
2812 mOrientedRanges.haveTilt = true;
2813
2814 mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT;
2815 mOrientedRanges.tilt.source = mSource;
2816 mOrientedRanges.tilt.min = 0;
2817 mOrientedRanges.tilt.max = M_PI_2;
2818 mOrientedRanges.tilt.flat = 0;
2819 mOrientedRanges.tilt.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002820 }
2821
Jeff Brown8d608662010-08-30 03:02:23 -07002822 // Orientation
Jeff Brown65fd2512011-08-18 11:20:58 -07002823 mOrientationCenter = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002824 mOrientationScale = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07002825 if (mHaveTilt) {
2826 mOrientedRanges.haveOrientation = true;
2827
2828 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2829 mOrientedRanges.orientation.source = mSource;
2830 mOrientedRanges.orientation.min = -M_PI;
2831 mOrientedRanges.orientation.max = M_PI;
2832 mOrientedRanges.orientation.flat = 0;
2833 mOrientedRanges.orientation.fuzz = 0;
2834 } else if (mCalibration.orientationCalibration !=
2835 Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002836 if (mCalibration.orientationCalibration
2837 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brown65fd2512011-08-18 11:20:58 -07002838 if (mRawPointerAxes.orientation.valid) {
2839 mOrientationCenter = avg(mRawPointerAxes.orientation.minValue,
2840 mRawPointerAxes.orientation.maxValue);
2841 mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue -
2842 mRawPointerAxes.orientation.minValue);
Jeff Brown8d608662010-08-30 03:02:23 -07002843 }
2844 }
2845
Jeff Brownbe1aa822011-07-27 16:04:54 -07002846 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002847
Jeff Brownbe1aa822011-07-27 16:04:54 -07002848 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
Jeff Brown65fd2512011-08-18 11:20:58 -07002849 mOrientedRanges.orientation.source = mSource;
2850 mOrientedRanges.orientation.min = -M_PI_2;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002851 mOrientedRanges.orientation.max = M_PI_2;
2852 mOrientedRanges.orientation.flat = 0;
2853 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002854 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002855
2856 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002857 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002858 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2859 if (mCalibration.distanceCalibration
2860 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2861 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002862 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002863 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002864 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002865 }
2866 }
2867
Jeff Brownbe1aa822011-07-27 16:04:54 -07002868 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002869
Jeff Brownbe1aa822011-07-27 16:04:54 -07002870 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
Jeff Brown65fd2512011-08-18 11:20:58 -07002871 mOrientedRanges.distance.source = mSource;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002872 mOrientedRanges.distance.min =
2873 mRawPointerAxes.distance.minValue * mDistanceScale;
2874 mOrientedRanges.distance.max =
2875 mRawPointerAxes.distance.minValue * mDistanceScale;
2876 mOrientedRanges.distance.flat = 0;
2877 mOrientedRanges.distance.fuzz =
2878 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002879 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002880 }
2881
Jeff Brown65fd2512011-08-18 11:20:58 -07002882 if (orientationChanged || sizeChanged || deviceModeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002883 // Compute oriented surface dimensions, precision, scales and ranges.
2884 // Note that the maximum value reported is an inclusive maximum value so it is one
2885 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002886 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002887 case DISPLAY_ORIENTATION_90:
2888 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002889 mOrientedSurfaceWidth = mSurfaceHeight;
2890 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002891
Jeff Brownbe1aa822011-07-27 16:04:54 -07002892 mOrientedXPrecision = mYPrecision;
2893 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002894
Jeff Brownbe1aa822011-07-27 16:04:54 -07002895 mOrientedRanges.x.min = 0;
2896 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2897 * mYScale;
2898 mOrientedRanges.x.flat = 0;
2899 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002900
Jeff Brownbe1aa822011-07-27 16:04:54 -07002901 mOrientedRanges.y.min = 0;
2902 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2903 * mXScale;
2904 mOrientedRanges.y.flat = 0;
2905 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002906 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002907
Jeff Brown6d0fec22010-07-23 21:28:06 -07002908 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002909 mOrientedSurfaceWidth = mSurfaceWidth;
2910 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002911
Jeff Brownbe1aa822011-07-27 16:04:54 -07002912 mOrientedXPrecision = mXPrecision;
2913 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002914
Jeff Brownbe1aa822011-07-27 16:04:54 -07002915 mOrientedRanges.x.min = 0;
2916 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2917 * mXScale;
2918 mOrientedRanges.x.flat = 0;
2919 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002920
Jeff Brownbe1aa822011-07-27 16:04:54 -07002921 mOrientedRanges.y.min = 0;
2922 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2923 * mYScale;
2924 mOrientedRanges.y.flat = 0;
2925 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002926 break;
2927 }
Jeff Brownace13b12011-03-09 17:39:48 -08002928
2929 // Compute pointer gesture detection parameters.
Jeff Brown65fd2512011-08-18 11:20:58 -07002930 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002931 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2932 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002933 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002934 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2935 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002936
Jeff Brown2352b972011-04-12 22:39:53 -07002937 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002938 // given area relative to the diagonal size of the display when no acceleration
2939 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002940 // Assume that the touch pad has a square aspect ratio such that movements in
2941 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown65fd2512011-08-18 11:20:58 -07002942 mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002943 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002944 mPointerYMovementScale = mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002945
2946 // Scale zooms to cover a smaller range of the display than movements do.
2947 // This value determines the area around the pointer that is affected by freeform
2948 // pointer gestures.
Jeff Brown65fd2512011-08-18 11:20:58 -07002949 mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002950 * displayDiagonal / rawDiagonal;
Jeff Brown65fd2512011-08-18 11:20:58 -07002951 mPointerYZoomScale = mPointerXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002952
Jeff Brown2352b972011-04-12 22:39:53 -07002953 // Max width between pointers to detect a swipe gesture is more than some fraction
2954 // of the diagonal axis of the touch pad. Touches that are wider than this are
2955 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002956 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002957 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002958 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002959
Jeff Brown65fd2512011-08-18 11:20:58 -07002960 // Abort current pointer usages because the state has changed.
2961 abortPointerUsage(when, 0 /*policyFlags*/);
2962
2963 // Inform the dispatcher about the changes.
2964 *outResetNeeded = true;
2965 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002966}
2967
Jeff Brownbe1aa822011-07-27 16:04:54 -07002968void TouchInputMapper::dumpSurface(String8& dump) {
2969 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2970 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2971 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002972}
2973
Jeff Brownbe1aa822011-07-27 16:04:54 -07002974void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002975 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002976 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002977
Jeff Brownbe1aa822011-07-27 16:04:54 -07002978 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002979
Jeff Brown6328cdc2010-07-29 18:18:33 -07002980 if (virtualKeyDefinitions.size() == 0) {
2981 return;
2982 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002983
Jeff Brownbe1aa822011-07-27 16:04:54 -07002984 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002985
Jeff Brownbe1aa822011-07-27 16:04:54 -07002986 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2987 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2988 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2989 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002990
2991 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002992 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002993 virtualKeyDefinitions[i];
2994
Jeff Brownbe1aa822011-07-27 16:04:54 -07002995 mVirtualKeys.add();
2996 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002997
2998 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2999 int32_t keyCode;
3000 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08003001 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07003002 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003003 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
3004 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003005 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07003006 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003007 }
3008
Jeff Brown6328cdc2010-07-29 18:18:33 -07003009 virtualKey.keyCode = keyCode;
3010 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003011
Jeff Brown6328cdc2010-07-29 18:18:33 -07003012 // convert the key definition's display coordinates into touch coordinates for a hit box
3013 int32_t halfWidth = virtualKeyDefinition.width / 2;
3014 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003015
Jeff Brown6328cdc2010-07-29 18:18:33 -07003016 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003017 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003018 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003019 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003020 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003021 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003022 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07003023 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07003024 }
3025}
3026
Jeff Brownbe1aa822011-07-27 16:04:54 -07003027void TouchInputMapper::dumpVirtualKeys(String8& dump) {
3028 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003029 dump.append(INDENT3 "Virtual Keys:\n");
3030
Jeff Brownbe1aa822011-07-27 16:04:54 -07003031 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
3032 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07003033 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
3034 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
3035 i, virtualKey.scanCode, virtualKey.keyCode,
3036 virtualKey.hitLeft, virtualKey.hitRight,
3037 virtualKey.hitTop, virtualKey.hitBottom);
3038 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003039 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003040}
3041
Jeff Brown8d608662010-08-30 03:02:23 -07003042void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003043 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07003044 Calibration& out = mCalibration;
3045
Jeff Browna1f89ce2011-08-11 00:05:01 -07003046 // Size
3047 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
3048 String8 sizeCalibrationString;
3049 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
3050 if (sizeCalibrationString == "none") {
3051 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3052 } else if (sizeCalibrationString == "geometric") {
3053 out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
3054 } else if (sizeCalibrationString == "diameter") {
3055 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER;
3056 } else if (sizeCalibrationString == "area") {
3057 out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA;
3058 } else if (sizeCalibrationString != "default") {
3059 LOGW("Invalid value for touch.size.calibration: '%s'",
3060 sizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07003061 }
3062 }
3063
Jeff Browna1f89ce2011-08-11 00:05:01 -07003064 out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"),
3065 out.sizeScale);
3066 out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"),
3067 out.sizeBias);
3068 out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"),
3069 out.sizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07003070
3071 // Pressure
3072 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
3073 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003074 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003075 if (pressureCalibrationString == "none") {
3076 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3077 } else if (pressureCalibrationString == "physical") {
3078 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3079 } else if (pressureCalibrationString == "amplitude") {
3080 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3081 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003082 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003083 pressureCalibrationString.string());
3084 }
3085 }
3086
Jeff Brown8d608662010-08-30 03:02:23 -07003087 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
3088 out.pressureScale);
3089
Jeff Brown8d608662010-08-30 03:02:23 -07003090 // Orientation
3091 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
3092 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003093 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07003094 if (orientationCalibrationString == "none") {
3095 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3096 } else if (orientationCalibrationString == "interpolated") {
3097 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003098 } else if (orientationCalibrationString == "vector") {
3099 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07003100 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003101 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07003102 orientationCalibrationString.string());
3103 }
3104 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003105
3106 // Distance
3107 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
3108 String8 distanceCalibrationString;
3109 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
3110 if (distanceCalibrationString == "none") {
3111 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3112 } else if (distanceCalibrationString == "scaled") {
3113 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3114 } else if (distanceCalibrationString != "default") {
3115 LOGW("Invalid value for touch.distance.calibration: '%s'",
3116 distanceCalibrationString.string());
3117 }
3118 }
3119
3120 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
3121 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003122}
3123
3124void TouchInputMapper::resolveCalibration() {
Jeff Brown8d608662010-08-30 03:02:23 -07003125 // Size
Jeff Browna1f89ce2011-08-11 00:05:01 -07003126 if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) {
3127 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) {
3128 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC;
Jeff Brown8d608662010-08-30 03:02:23 -07003129 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003130 } else {
3131 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3132 }
Jeff Brown8d608662010-08-30 03:02:23 -07003133
Jeff Browna1f89ce2011-08-11 00:05:01 -07003134 // Pressure
3135 if (mRawPointerAxes.pressure.valid) {
3136 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) {
3137 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
3138 }
3139 } else {
3140 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003141 }
3142
3143 // Orientation
Jeff Browna1f89ce2011-08-11 00:05:01 -07003144 if (mRawPointerAxes.orientation.valid) {
3145 if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003146 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown8d608662010-08-30 03:02:23 -07003147 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003148 } else {
3149 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003150 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003151
3152 // Distance
Jeff Browna1f89ce2011-08-11 00:05:01 -07003153 if (mRawPointerAxes.distance.valid) {
3154 if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003155 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003156 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003157 } else {
3158 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003159 }
Jeff Brown8d608662010-08-30 03:02:23 -07003160}
3161
Jeff Brownef3d7e82010-09-30 14:33:04 -07003162void TouchInputMapper::dumpCalibration(String8& dump) {
3163 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003164
Jeff Browna1f89ce2011-08-11 00:05:01 -07003165 // Size
3166 switch (mCalibration.sizeCalibration) {
3167 case Calibration::SIZE_CALIBRATION_NONE:
3168 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003169 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003170 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3171 dump.append(INDENT4 "touch.size.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003172 break;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003173 case Calibration::SIZE_CALIBRATION_DIAMETER:
3174 dump.append(INDENT4 "touch.size.calibration: diameter\n");
3175 break;
3176 case Calibration::SIZE_CALIBRATION_AREA:
3177 dump.append(INDENT4 "touch.size.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003178 break;
3179 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003180 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003181 }
3182
Jeff Browna1f89ce2011-08-11 00:05:01 -07003183 if (mCalibration.haveSizeScale) {
3184 dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n",
3185 mCalibration.sizeScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003186 }
3187
Jeff Browna1f89ce2011-08-11 00:05:01 -07003188 if (mCalibration.haveSizeBias) {
3189 dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n",
3190 mCalibration.sizeBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003191 }
3192
Jeff Browna1f89ce2011-08-11 00:05:01 -07003193 if (mCalibration.haveSizeIsSummed) {
3194 dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n",
3195 toString(mCalibration.sizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003196 }
3197
3198 // Pressure
3199 switch (mCalibration.pressureCalibration) {
3200 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003201 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003202 break;
3203 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003204 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003205 break;
3206 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003207 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003208 break;
3209 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003210 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003211 }
3212
Jeff Brown8d608662010-08-30 03:02:23 -07003213 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003214 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3215 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003216 }
3217
Jeff Brown8d608662010-08-30 03:02:23 -07003218 // Orientation
3219 switch (mCalibration.orientationCalibration) {
3220 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003221 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003222 break;
3223 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003224 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003225 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003226 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3227 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3228 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003229 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003230 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003231 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003232
3233 // Distance
3234 switch (mCalibration.distanceCalibration) {
3235 case Calibration::DISTANCE_CALIBRATION_NONE:
3236 dump.append(INDENT4 "touch.distance.calibration: none\n");
3237 break;
3238 case Calibration::DISTANCE_CALIBRATION_SCALED:
3239 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3240 break;
3241 default:
3242 LOG_ASSERT(false);
3243 }
3244
3245 if (mCalibration.haveDistanceScale) {
3246 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3247 mCalibration.distanceScale);
3248 }
Jeff Brown8d608662010-08-30 03:02:23 -07003249}
3250
Jeff Brown65fd2512011-08-18 11:20:58 -07003251void TouchInputMapper::reset(nsecs_t when) {
3252 mCursorButtonAccumulator.reset(getDevice());
3253 mCursorScrollAccumulator.reset(getDevice());
3254 mTouchButtonAccumulator.reset(getDevice());
3255
3256 mPointerVelocityControl.reset();
3257 mWheelXVelocityControl.reset();
3258 mWheelYVelocityControl.reset();
3259
Jeff Brownbe1aa822011-07-27 16:04:54 -07003260 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003261 mLastRawPointerData.clear();
3262 mCurrentCookedPointerData.clear();
3263 mLastCookedPointerData.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003264 mCurrentButtonState = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07003265 mLastButtonState = 0;
3266 mCurrentRawVScroll = 0;
3267 mCurrentRawHScroll = 0;
3268 mCurrentFingerIdBits.clear();
3269 mLastFingerIdBits.clear();
3270 mCurrentStylusIdBits.clear();
3271 mLastStylusIdBits.clear();
3272 mCurrentMouseIdBits.clear();
3273 mLastMouseIdBits.clear();
3274 mPointerUsage = POINTER_USAGE_NONE;
3275 mSentHoverEnter = false;
3276 mDownTime = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003277
Jeff Brown65fd2512011-08-18 11:20:58 -07003278 mCurrentVirtualKey.down = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003279
Jeff Brown65fd2512011-08-18 11:20:58 -07003280 mPointerGesture.reset();
3281 mPointerSimple.reset();
3282
3283 if (mPointerController != NULL) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003284 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3285 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003286 }
3287
Jeff Brown65fd2512011-08-18 11:20:58 -07003288 InputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003289}
3290
Jeff Brown65fd2512011-08-18 11:20:58 -07003291void TouchInputMapper::process(const RawEvent* rawEvent) {
3292 mCursorButtonAccumulator.process(rawEvent);
3293 mCursorScrollAccumulator.process(rawEvent);
3294 mTouchButtonAccumulator.process(rawEvent);
3295
3296 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
3297 sync(rawEvent->when);
3298 }
3299}
3300
3301void TouchInputMapper::sync(nsecs_t when) {
3302 // Sync button state.
3303 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
3304 | mCursorButtonAccumulator.getButtonState();
3305
3306 // Sync scroll state.
3307 mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel();
3308 mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel();
3309 mCursorScrollAccumulator.finishSync();
3310
3311 // Sync touch state.
3312 bool havePointerIds = true;
3313 mCurrentRawPointerData.clear();
3314 syncTouch(when, &havePointerIds);
3315
Jeff Brownaa3855d2011-03-17 01:34:19 -07003316#if DEBUG_RAW_EVENTS
3317 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003318 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3319 mLastRawPointerData.pointerCount,
3320 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003321 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003322 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3323 "hovering ids 0x%08x -> 0x%08x",
3324 mLastRawPointerData.pointerCount,
3325 mCurrentRawPointerData.pointerCount,
3326 mLastRawPointerData.touchingIdBits.value,
3327 mCurrentRawPointerData.touchingIdBits.value,
3328 mLastRawPointerData.hoveringIdBits.value,
3329 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003330 }
3331#endif
3332
Jeff Brown65fd2512011-08-18 11:20:58 -07003333 // Reset state that we will compute below.
3334 mCurrentFingerIdBits.clear();
3335 mCurrentStylusIdBits.clear();
3336 mCurrentMouseIdBits.clear();
3337 mCurrentCookedPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003338
Jeff Brown65fd2512011-08-18 11:20:58 -07003339 if (mDeviceMode == DEVICE_MODE_DISABLED) {
3340 // Drop all input if the device is disabled.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003341 mCurrentRawPointerData.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07003342 mCurrentButtonState = 0;
3343 } else {
3344 // Preprocess pointer data.
3345 if (!havePointerIds) {
3346 assignPointerIds();
3347 }
3348
3349 // Handle policy on initial down or hover events.
3350 uint32_t policyFlags = 0;
Jeff Brownc28306a2011-08-23 21:32:42 -07003351 bool initialDown = mLastRawPointerData.pointerCount == 0
3352 && mCurrentRawPointerData.pointerCount != 0;
3353 bool buttonsPressed = mCurrentButtonState & ~mLastButtonState;
3354 if (initialDown || buttonsPressed) {
3355 // If this is a touch screen, hide the pointer on an initial down.
Jeff Brown65fd2512011-08-18 11:20:58 -07003356 if (mDeviceMode == DEVICE_MODE_DIRECT) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003357 getContext()->fadePointer();
3358 }
3359
3360 // Initial downs on external touch devices should wake the device.
3361 // We don't do this for internal touch screens to prevent them from waking
3362 // up in your pocket.
3363 // TODO: Use the input device configuration to control this behavior more finely.
3364 if (getDevice()->isExternal()) {
3365 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3366 }
3367 }
3368
3369 // Synthesize key down from raw buttons if needed.
3370 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
3371 policyFlags, mLastButtonState, mCurrentButtonState);
3372
3373 // Consume raw off-screen touches before cooking pointer data.
3374 // If touches are consumed, subsequent code will not receive any pointer data.
3375 if (consumeRawTouches(when, policyFlags)) {
3376 mCurrentRawPointerData.clear();
3377 }
3378
3379 // Cook pointer data. This call populates the mCurrentCookedPointerData structure
3380 // with cooked pointer data that has the same ids and indices as the raw data.
3381 // The following code can use either the raw or cooked data, as needed.
3382 cookPointerData();
3383
3384 // Dispatch the touches either directly or by translation through a pointer on screen.
Jeff Browndaf4a122011-08-26 17:14:14 -07003385 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003386 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
3387 uint32_t id = idBits.clearFirstMarkedBit();
3388 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3389 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3390 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3391 mCurrentStylusIdBits.markBit(id);
3392 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
3393 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
3394 mCurrentFingerIdBits.markBit(id);
3395 } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) {
3396 mCurrentMouseIdBits.markBit(id);
3397 }
3398 }
3399 for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) {
3400 uint32_t id = idBits.clearFirstMarkedBit();
3401 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3402 if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS
3403 || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
3404 mCurrentStylusIdBits.markBit(id);
3405 }
3406 }
3407
3408 // Stylus takes precedence over all tools, then mouse, then finger.
3409 PointerUsage pointerUsage = mPointerUsage;
3410 if (!mCurrentStylusIdBits.isEmpty()) {
3411 mCurrentMouseIdBits.clear();
3412 mCurrentFingerIdBits.clear();
3413 pointerUsage = POINTER_USAGE_STYLUS;
3414 } else if (!mCurrentMouseIdBits.isEmpty()) {
3415 mCurrentFingerIdBits.clear();
3416 pointerUsage = POINTER_USAGE_MOUSE;
3417 } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) {
3418 pointerUsage = POINTER_USAGE_GESTURES;
Jeff Brown65fd2512011-08-18 11:20:58 -07003419 }
3420
3421 dispatchPointerUsage(when, policyFlags, pointerUsage);
3422 } else {
Jeff Browndaf4a122011-08-26 17:14:14 -07003423 if (mDeviceMode == DEVICE_MODE_DIRECT
3424 && mConfig.showTouches && mPointerController != NULL) {
3425 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3426 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3427
3428 mPointerController->setButtonState(mCurrentButtonState);
3429 mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords,
3430 mCurrentCookedPointerData.idToIndex,
3431 mCurrentCookedPointerData.touchingIdBits);
3432 }
3433
Jeff Brown65fd2512011-08-18 11:20:58 -07003434 dispatchHoverExit(when, policyFlags);
3435 dispatchTouches(when, policyFlags);
3436 dispatchHoverEnterAndMove(when, policyFlags);
3437 }
3438
3439 // Synthesize key up from raw buttons if needed.
3440 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
3441 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003442 }
3443
Jeff Brown6328cdc2010-07-29 18:18:33 -07003444 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003445 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3446 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3447 mLastButtonState = mCurrentButtonState;
Jeff Brown65fd2512011-08-18 11:20:58 -07003448 mLastFingerIdBits = mCurrentFingerIdBits;
3449 mLastStylusIdBits = mCurrentStylusIdBits;
3450 mLastMouseIdBits = mCurrentMouseIdBits;
3451
3452 // Clear some transient state.
3453 mCurrentRawVScroll = 0;
3454 mCurrentRawHScroll = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003455}
3456
Jeff Brown79ac9692011-04-19 21:20:10 -07003457void TouchInputMapper::timeoutExpired(nsecs_t when) {
Jeff Browndaf4a122011-08-26 17:14:14 -07003458 if (mDeviceMode == DEVICE_MODE_POINTER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003459 if (mPointerUsage == POINTER_USAGE_GESTURES) {
3460 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3461 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003462 }
3463}
3464
Jeff Brownbe1aa822011-07-27 16:04:54 -07003465bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3466 // Check for release of a virtual key.
3467 if (mCurrentVirtualKey.down) {
3468 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3469 // Pointer went up while virtual key was down.
3470 mCurrentVirtualKey.down = false;
3471 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003472#if DEBUG_VIRTUAL_KEYS
3473 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003474 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003475#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003476 dispatchVirtualKey(when, policyFlags,
3477 AKEY_EVENT_ACTION_UP,
3478 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003479 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003480 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003481 }
3482
Jeff Brownbe1aa822011-07-27 16:04:54 -07003483 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3484 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3485 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3486 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3487 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3488 // Pointer is still within the space of the virtual key.
3489 return true;
3490 }
3491 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003492
Jeff Brownbe1aa822011-07-27 16:04:54 -07003493 // Pointer left virtual key area or another pointer also went down.
3494 // Send key cancellation but do not consume the touch yet.
3495 // This is useful when the user swipes through from the virtual key area
3496 // into the main display surface.
3497 mCurrentVirtualKey.down = false;
3498 if (!mCurrentVirtualKey.ignored) {
3499#if DEBUG_VIRTUAL_KEYS
3500 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3501 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3502#endif
3503 dispatchVirtualKey(when, policyFlags,
3504 AKEY_EVENT_ACTION_UP,
3505 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3506 | AKEY_EVENT_FLAG_CANCELED);
3507 }
3508 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003509
Jeff Brownbe1aa822011-07-27 16:04:54 -07003510 if (mLastRawPointerData.touchingIdBits.isEmpty()
3511 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3512 // Pointer just went down. Check for virtual key press or off-screen touches.
3513 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3514 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3515 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3516 // If exactly one pointer went down, check for virtual key hit.
3517 // Otherwise we will drop the entire stroke.
3518 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3519 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3520 if (virtualKey) {
3521 mCurrentVirtualKey.down = true;
3522 mCurrentVirtualKey.downTime = when;
3523 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3524 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3525 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3526 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3527
3528 if (!mCurrentVirtualKey.ignored) {
3529#if DEBUG_VIRTUAL_KEYS
3530 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3531 mCurrentVirtualKey.keyCode,
3532 mCurrentVirtualKey.scanCode);
3533#endif
3534 dispatchVirtualKey(when, policyFlags,
3535 AKEY_EVENT_ACTION_DOWN,
3536 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3537 }
3538 }
3539 }
3540 return true;
3541 }
3542 }
3543
Jeff Brownfe508922011-01-18 15:10:10 -08003544 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003545 // most recent touch within the screen area. The idea is to filter out stray
3546 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003547 //
3548 // Problems we're trying to solve:
3549 //
3550 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3551 // virtual key area that is implemented by a separate touch panel and accidentally
3552 // triggers a virtual key.
3553 //
3554 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3555 // area and accidentally triggers a virtual key. This often happens when virtual keys
3556 // are layed out below the screen near to where the on screen keyboard's space bar
3557 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003558 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003559 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003560 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003561 return false;
3562}
3563
3564void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3565 int32_t keyEventAction, int32_t keyEventFlags) {
3566 int32_t keyCode = mCurrentVirtualKey.keyCode;
3567 int32_t scanCode = mCurrentVirtualKey.scanCode;
3568 nsecs_t downTime = mCurrentVirtualKey.downTime;
3569 int32_t metaState = mContext->getGlobalMetaState();
3570 policyFlags |= POLICY_FLAG_VIRTUAL;
3571
3572 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3573 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3574 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003575}
3576
Jeff Brown6d0fec22010-07-23 21:28:06 -07003577void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003578 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3579 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003580 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003581 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003582
3583 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003584 if (!currentIdBits.isEmpty()) {
3585 // No pointer id changes so this is a move event.
3586 // The listener takes care of batching moves so we don't have to deal with that here.
Jeff Brown65fd2512011-08-18 11:20:58 -07003587 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003588 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3589 AMOTION_EVENT_EDGE_FLAG_NONE,
3590 mCurrentCookedPointerData.pointerProperties,
3591 mCurrentCookedPointerData.pointerCoords,
3592 mCurrentCookedPointerData.idToIndex,
3593 currentIdBits, -1,
3594 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3595 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003596 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003597 // There may be pointers going up and pointers going down and pointers moving
3598 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003599 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3600 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003601 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003602 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003603
Jeff Brownace13b12011-03-09 17:39:48 -08003604 // Update last coordinates of pointers that have moved so that we observe the new
3605 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003606 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003607 mCurrentCookedPointerData.pointerProperties,
3608 mCurrentCookedPointerData.pointerCoords,
3609 mCurrentCookedPointerData.idToIndex,
3610 mLastCookedPointerData.pointerProperties,
3611 mLastCookedPointerData.pointerCoords,
3612 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003613 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003614 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003615 moveNeeded = true;
3616 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003617
Jeff Brownace13b12011-03-09 17:39:48 -08003618 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003619 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003620 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003621
Jeff Brown65fd2512011-08-18 11:20:58 -07003622 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003623 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003624 mLastCookedPointerData.pointerProperties,
3625 mLastCookedPointerData.pointerCoords,
3626 mLastCookedPointerData.idToIndex,
3627 dispatchedIdBits, upId,
3628 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003629 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003630 }
3631
Jeff Brownc3db8582010-10-20 15:33:38 -07003632 // Dispatch move events if any of the remaining pointers moved from their old locations.
3633 // Although applications receive new locations as part of individual pointer up
3634 // events, they do not generally handle them except when presented in a move event.
3635 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003636 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brown65fd2512011-08-18 11:20:58 -07003637 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003638 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003639 mCurrentCookedPointerData.pointerProperties,
3640 mCurrentCookedPointerData.pointerCoords,
3641 mCurrentCookedPointerData.idToIndex,
3642 dispatchedIdBits, -1,
3643 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003644 }
3645
3646 // Dispatch pointer down events using the new pointer locations.
3647 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003648 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003649 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003650
Jeff Brownace13b12011-03-09 17:39:48 -08003651 if (dispatchedIdBits.count() == 1) {
3652 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003653 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003654 }
3655
Jeff Brown65fd2512011-08-18 11:20:58 -07003656 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07003657 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003658 mCurrentCookedPointerData.pointerProperties,
3659 mCurrentCookedPointerData.pointerCoords,
3660 mCurrentCookedPointerData.idToIndex,
3661 dispatchedIdBits, downId,
3662 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003663 }
3664 }
Jeff Brownace13b12011-03-09 17:39:48 -08003665}
3666
Jeff Brownbe1aa822011-07-27 16:04:54 -07003667void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3668 if (mSentHoverEnter &&
3669 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3670 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3671 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown65fd2512011-08-18 11:20:58 -07003672 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003673 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3674 mLastCookedPointerData.pointerProperties,
3675 mLastCookedPointerData.pointerCoords,
3676 mLastCookedPointerData.idToIndex,
3677 mLastCookedPointerData.hoveringIdBits, -1,
3678 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3679 mSentHoverEnter = false;
3680 }
3681}
Jeff Brownace13b12011-03-09 17:39:48 -08003682
Jeff Brownbe1aa822011-07-27 16:04:54 -07003683void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3684 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3685 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3686 int32_t metaState = getContext()->getGlobalMetaState();
3687 if (!mSentHoverEnter) {
Jeff Brown65fd2512011-08-18 11:20:58 -07003688 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003689 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3690 mCurrentCookedPointerData.pointerProperties,
3691 mCurrentCookedPointerData.pointerCoords,
3692 mCurrentCookedPointerData.idToIndex,
3693 mCurrentCookedPointerData.hoveringIdBits, -1,
3694 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3695 mSentHoverEnter = true;
3696 }
Jeff Brownace13b12011-03-09 17:39:48 -08003697
Jeff Brown65fd2512011-08-18 11:20:58 -07003698 dispatchMotion(when, policyFlags, mSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003699 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3700 mCurrentCookedPointerData.pointerProperties,
3701 mCurrentCookedPointerData.pointerCoords,
3702 mCurrentCookedPointerData.idToIndex,
3703 mCurrentCookedPointerData.hoveringIdBits, -1,
3704 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3705 }
3706}
3707
3708void TouchInputMapper::cookPointerData() {
3709 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3710
3711 mCurrentCookedPointerData.clear();
3712 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3713 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3714 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3715
3716 // Walk through the the active pointers and map device coordinates onto
3717 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003718 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003719 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003720
Jeff Browna1f89ce2011-08-11 00:05:01 -07003721 // Size
3722 float touchMajor, touchMinor, toolMajor, toolMinor, size;
3723 switch (mCalibration.sizeCalibration) {
3724 case Calibration::SIZE_CALIBRATION_GEOMETRIC:
3725 case Calibration::SIZE_CALIBRATION_DIAMETER:
3726 case Calibration::SIZE_CALIBRATION_AREA:
3727 if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) {
3728 touchMajor = in.touchMajor;
3729 touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor;
3730 toolMajor = in.toolMajor;
3731 toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor;
3732 size = mRawPointerAxes.touchMinor.valid
3733 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3734 } else if (mRawPointerAxes.touchMajor.valid) {
3735 toolMajor = touchMajor = in.touchMajor;
3736 toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid
3737 ? in.touchMinor : in.touchMajor;
3738 size = mRawPointerAxes.touchMinor.valid
3739 ? avg(in.touchMajor, in.touchMinor) : in.touchMajor;
3740 } else if (mRawPointerAxes.toolMajor.valid) {
3741 touchMajor = toolMajor = in.toolMajor;
3742 touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid
3743 ? in.toolMinor : in.toolMajor;
3744 size = mRawPointerAxes.toolMinor.valid
3745 ? avg(in.toolMajor, in.toolMinor) : in.toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003746 } else {
Jeff Browna1f89ce2011-08-11 00:05:01 -07003747 LOG_ASSERT(false, "No touch or tool axes. "
3748 "Size calibration should have been resolved to NONE.");
3749 touchMajor = 0;
3750 touchMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003751 toolMajor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003752 toolMinor = 0;
3753 size = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003754 }
Jeff Brownace13b12011-03-09 17:39:48 -08003755
Jeff Browna1f89ce2011-08-11 00:05:01 -07003756 if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) {
3757 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3758 if (touchingCount > 1) {
3759 touchMajor /= touchingCount;
3760 touchMinor /= touchingCount;
3761 toolMajor /= touchingCount;
3762 toolMinor /= touchingCount;
3763 size /= touchingCount;
3764 }
3765 }
Jeff Brownace13b12011-03-09 17:39:48 -08003766
Jeff Browna1f89ce2011-08-11 00:05:01 -07003767 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) {
3768 touchMajor *= mGeometricScale;
3769 touchMinor *= mGeometricScale;
3770 toolMajor *= mGeometricScale;
3771 toolMinor *= mGeometricScale;
3772 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) {
3773 touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003774 touchMinor = touchMajor;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003775 toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0;
3776 toolMinor = toolMajor;
3777 } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) {
3778 touchMinor = touchMajor;
3779 toolMinor = toolMajor;
Jeff Brownace13b12011-03-09 17:39:48 -08003780 }
Jeff Browna1f89ce2011-08-11 00:05:01 -07003781
3782 mCalibration.applySizeScaleAndBias(&touchMajor);
3783 mCalibration.applySizeScaleAndBias(&touchMinor);
3784 mCalibration.applySizeScaleAndBias(&toolMajor);
3785 mCalibration.applySizeScaleAndBias(&toolMinor);
3786 size *= mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003787 break;
3788 default:
3789 touchMajor = 0;
3790 touchMinor = 0;
Jeff Browna1f89ce2011-08-11 00:05:01 -07003791 toolMajor = 0;
3792 toolMinor = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003793 size = 0;
3794 break;
3795 }
3796
Jeff Browna1f89ce2011-08-11 00:05:01 -07003797 // Pressure
3798 float pressure;
3799 switch (mCalibration.pressureCalibration) {
3800 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3801 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3802 pressure = in.pressure * mPressureScale;
3803 break;
3804 default:
3805 pressure = in.isHovering ? 0 : 1;
3806 break;
3807 }
3808
Jeff Brown65fd2512011-08-18 11:20:58 -07003809 // Tilt and Orientation
3810 float tilt;
Jeff Brownace13b12011-03-09 17:39:48 -08003811 float orientation;
Jeff Brown65fd2512011-08-18 11:20:58 -07003812 if (mHaveTilt) {
3813 float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale;
3814 float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale;
3815 orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
3816 tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
3817 } else {
3818 tilt = 0;
3819
3820 switch (mCalibration.orientationCalibration) {
3821 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3822 orientation = (in.orientation - mOrientationCenter) * mOrientationScale;
3823 break;
3824 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3825 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3826 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3827 if (c1 != 0 || c2 != 0) {
3828 orientation = atan2f(c1, c2) * 0.5f;
3829 float confidence = hypotf(c1, c2);
3830 float scale = 1.0f + confidence / 16.0f;
3831 touchMajor *= scale;
3832 touchMinor /= scale;
3833 toolMajor *= scale;
3834 toolMinor /= scale;
3835 } else {
3836 orientation = 0;
3837 }
3838 break;
3839 }
3840 default:
Jeff Brownace13b12011-03-09 17:39:48 -08003841 orientation = 0;
3842 }
Jeff Brownace13b12011-03-09 17:39:48 -08003843 }
3844
Jeff Brown80fd47c2011-05-24 01:07:44 -07003845 // Distance
3846 float distance;
3847 switch (mCalibration.distanceCalibration) {
3848 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003849 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003850 break;
3851 default:
3852 distance = 0;
3853 }
3854
Jeff Brownace13b12011-03-09 17:39:48 -08003855 // X and Y
3856 // Adjust coords for surface orientation.
3857 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003858 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003859 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003860 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3861 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003862 orientation -= M_PI_2;
3863 if (orientation < - M_PI_2) {
3864 orientation += M_PI;
3865 }
3866 break;
3867 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003868 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3869 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003870 break;
3871 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003872 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3873 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003874 orientation += M_PI_2;
3875 if (orientation > M_PI_2) {
3876 orientation -= M_PI;
3877 }
3878 break;
3879 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003880 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3881 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003882 break;
3883 }
3884
3885 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003886 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003887 out.clear();
3888 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3889 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3890 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3891 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3892 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3893 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3894 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3895 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3896 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown65fd2512011-08-18 11:20:58 -07003897 out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003898 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003899
3900 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003901 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3902 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003903 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003904 properties.id = id;
3905 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003906
Jeff Brownbe1aa822011-07-27 16:04:54 -07003907 // Write id index.
3908 mCurrentCookedPointerData.idToIndex[id] = i;
3909 }
Jeff Brownace13b12011-03-09 17:39:48 -08003910}
3911
Jeff Brown65fd2512011-08-18 11:20:58 -07003912void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags,
3913 PointerUsage pointerUsage) {
3914 if (pointerUsage != mPointerUsage) {
3915 abortPointerUsage(when, policyFlags);
3916 mPointerUsage = pointerUsage;
3917 }
3918
3919 switch (mPointerUsage) {
3920 case POINTER_USAGE_GESTURES:
3921 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3922 break;
3923 case POINTER_USAGE_STYLUS:
3924 dispatchPointerStylus(when, policyFlags);
3925 break;
3926 case POINTER_USAGE_MOUSE:
3927 dispatchPointerMouse(when, policyFlags);
3928 break;
3929 default:
3930 break;
3931 }
3932}
3933
3934void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) {
3935 switch (mPointerUsage) {
3936 case POINTER_USAGE_GESTURES:
3937 abortPointerGestures(when, policyFlags);
3938 break;
3939 case POINTER_USAGE_STYLUS:
3940 abortPointerStylus(when, policyFlags);
3941 break;
3942 case POINTER_USAGE_MOUSE:
3943 abortPointerMouse(when, policyFlags);
3944 break;
3945 default:
3946 break;
3947 }
3948
3949 mPointerUsage = POINTER_USAGE_NONE;
3950}
3951
Jeff Brown79ac9692011-04-19 21:20:10 -07003952void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3953 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003954 // Update current gesture coordinates.
3955 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003956 bool sendEvents = preparePointerGestures(when,
3957 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3958 if (!sendEvents) {
3959 return;
3960 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003961 if (finishPreviousGesture) {
3962 cancelPreviousGesture = false;
3963 }
Jeff Brownace13b12011-03-09 17:39:48 -08003964
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003965 // Update the pointer presentation and spots.
3966 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3967 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3968 if (finishPreviousGesture || cancelPreviousGesture) {
3969 mPointerController->clearSpots();
3970 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003971 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3972 mPointerGesture.currentGestureIdToIndex,
3973 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003974 } else {
3975 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3976 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003977
Jeff Brown538881e2011-05-25 18:23:38 -07003978 // Show or hide the pointer if needed.
3979 switch (mPointerGesture.currentGestureMode) {
3980 case PointerGesture::NEUTRAL:
3981 case PointerGesture::QUIET:
3982 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3983 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3984 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3985 // Remind the user of where the pointer is after finishing a gesture with spots.
3986 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3987 }
3988 break;
3989 case PointerGesture::TAP:
3990 case PointerGesture::TAP_DRAG:
3991 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3992 case PointerGesture::HOVER:
3993 case PointerGesture::PRESS:
3994 // Unfade the pointer when the current gesture manipulates the
3995 // area directly under the pointer.
3996 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3997 break;
3998 case PointerGesture::SWIPE:
3999 case PointerGesture::FREEFORM:
4000 // Fade the pointer when the current gesture manipulates a different
4001 // area and there are spots to guide the user experience.
4002 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4003 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4004 } else {
4005 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
4006 }
4007 break;
Jeff Brown2352b972011-04-12 22:39:53 -07004008 }
4009
Jeff Brownace13b12011-03-09 17:39:48 -08004010 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004011 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004012 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08004013
4014 // Update last coordinates of pointers that have moved so that we observe the new
4015 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07004016 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
4017 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
4018 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004019 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004020 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
4021 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
4022 bool moveNeeded = false;
4023 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07004024 && !mPointerGesture.lastGestureIdBits.isEmpty()
4025 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08004026 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
4027 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004028 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004029 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004030 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004031 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4032 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004033 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004034 moveNeeded = true;
4035 }
Jeff Brownace13b12011-03-09 17:39:48 -08004036 }
4037
4038 // Send motion events for all pointers that went up or were canceled.
4039 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
4040 if (!dispatchedGestureIdBits.isEmpty()) {
4041 if (cancelPreviousGesture) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004042 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004043 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4044 AMOTION_EVENT_EDGE_FLAG_NONE,
4045 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004046 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4047 dispatchedGestureIdBits, -1,
4048 0, 0, mPointerGesture.downTime);
4049
4050 dispatchedGestureIdBits.clear();
4051 } else {
4052 BitSet32 upGestureIdBits;
4053 if (finishPreviousGesture) {
4054 upGestureIdBits = dispatchedGestureIdBits;
4055 } else {
4056 upGestureIdBits.value = dispatchedGestureIdBits.value
4057 & ~mPointerGesture.currentGestureIdBits.value;
4058 }
4059 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004060 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004061
Jeff Brown65fd2512011-08-18 11:20:58 -07004062 dispatchMotion(when, policyFlags, mSource,
Jeff Brownace13b12011-03-09 17:39:48 -08004063 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004064 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4065 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004066 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4067 dispatchedGestureIdBits, id,
4068 0, 0, mPointerGesture.downTime);
4069
4070 dispatchedGestureIdBits.clearBit(id);
4071 }
4072 }
4073 }
4074
4075 // Send motion events for all pointers that moved.
4076 if (moveNeeded) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004077 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004078 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4079 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004080 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4081 dispatchedGestureIdBits, -1,
4082 0, 0, mPointerGesture.downTime);
4083 }
4084
4085 // Send motion events for all pointers that went down.
4086 if (down) {
4087 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
4088 & ~dispatchedGestureIdBits.value);
4089 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004090 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004091 dispatchedGestureIdBits.markBit(id);
4092
Jeff Brownace13b12011-03-09 17:39:48 -08004093 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08004094 mPointerGesture.downTime = when;
4095 }
4096
Jeff Brown65fd2512011-08-18 11:20:58 -07004097 dispatchMotion(when, policyFlags, mSource,
Jeff Browna6111372011-07-14 21:48:23 -07004098 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004099 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004100 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4101 dispatchedGestureIdBits, id,
4102 0, 0, mPointerGesture.downTime);
4103 }
4104 }
4105
Jeff Brownace13b12011-03-09 17:39:48 -08004106 // Send motion events for hover.
4107 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004108 dispatchMotion(when, policyFlags, mSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004109 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4110 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4111 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004112 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
4113 mPointerGesture.currentGestureIdBits, -1,
4114 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07004115 } else if (dispatchedGestureIdBits.isEmpty()
4116 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
4117 // Synthesize a hover move event after all pointers go up to indicate that
4118 // the pointer is hovering again even if the user is not currently touching
4119 // the touch pad. This ensures that a view will receive a fresh hover enter
4120 // event after a tap.
4121 float x, y;
4122 mPointerController->getPosition(&x, &y);
4123
4124 PointerProperties pointerProperties;
4125 pointerProperties.clear();
4126 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07004127 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07004128
4129 PointerCoords pointerCoords;
4130 pointerCoords.clear();
4131 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4132 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4133
Jeff Brown65fd2512011-08-18 11:20:58 -07004134 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07004135 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
4136 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
4137 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004138 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004139 }
4140
4141 // Update state.
4142 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4143 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004144 mPointerGesture.lastGestureIdBits.clear();
4145 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004146 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4147 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004148 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004149 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004150 mPointerGesture.lastGestureProperties[index].copyFrom(
4151 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004152 mPointerGesture.lastGestureCoords[index].copyFrom(
4153 mPointerGesture.currentGestureCoords[index]);
4154 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004155 }
4156 }
4157}
4158
Jeff Brown65fd2512011-08-18 11:20:58 -07004159void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) {
4160 // Cancel previously dispatches pointers.
4161 if (!mPointerGesture.lastGestureIdBits.isEmpty()) {
4162 int32_t metaState = getContext()->getGlobalMetaState();
4163 int32_t buttonState = mCurrentButtonState;
4164 dispatchMotion(when, policyFlags, mSource,
4165 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
4166 AMOTION_EVENT_EDGE_FLAG_NONE,
4167 mPointerGesture.lastGestureProperties,
4168 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
4169 mPointerGesture.lastGestureIdBits, -1,
4170 0, 0, mPointerGesture.downTime);
4171 }
4172
4173 // Reset the current pointer gesture.
4174 mPointerGesture.reset();
4175 mPointerVelocityControl.reset();
4176
4177 // Remove any current spots.
4178 if (mPointerController != NULL) {
4179 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4180 mPointerController->clearSpots();
4181 }
4182}
4183
Jeff Brown79ac9692011-04-19 21:20:10 -07004184bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4185 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004186 *outCancelPreviousGesture = false;
4187 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004188
Jeff Brown79ac9692011-04-19 21:20:10 -07004189 // Handle TAP timeout.
4190 if (isTimeout) {
4191#if DEBUG_GESTURES
4192 LOGD("Gestures: Processing timeout");
4193#endif
4194
4195 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004196 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004197 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004198 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004199 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004200 } else {
4201 // The tap is finished.
4202#if DEBUG_GESTURES
4203 LOGD("Gestures: TAP finished");
4204#endif
4205 *outFinishPreviousGesture = true;
4206
4207 mPointerGesture.activeGestureId = -1;
4208 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4209 mPointerGesture.currentGestureIdBits.clear();
4210
Jeff Brown65fd2512011-08-18 11:20:58 -07004211 mPointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004212 return true;
4213 }
4214 }
4215
4216 // We did not handle this timeout.
4217 return false;
4218 }
4219
Jeff Brown65fd2512011-08-18 11:20:58 -07004220 const uint32_t currentFingerCount = mCurrentFingerIdBits.count();
4221 const uint32_t lastFingerCount = mLastFingerIdBits.count();
4222
Jeff Brownace13b12011-03-09 17:39:48 -08004223 // Update the velocity tracker.
4224 {
4225 VelocityTracker::Position positions[MAX_POINTERS];
4226 uint32_t count = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004227 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004228 uint32_t id = idBits.clearFirstMarkedBit();
4229 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
Jeff Brown65fd2512011-08-18 11:20:58 -07004230 positions[count].x = pointer.x * mPointerXMovementScale;
4231 positions[count].y = pointer.y * mPointerYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004232 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004233 mPointerGesture.velocityTracker.addMovement(when,
Jeff Brown65fd2512011-08-18 11:20:58 -07004234 mCurrentFingerIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004235 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004236
Jeff Brownace13b12011-03-09 17:39:48 -08004237 // Pick a new active touch id if needed.
4238 // Choose an arbitrary pointer that just went down, if there is one.
4239 // Otherwise choose an arbitrary remaining pointer.
4240 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004241 // We keep the same active touch id for as long as possible.
4242 bool activeTouchChanged = false;
4243 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4244 int32_t activeTouchId = lastActiveTouchId;
4245 if (activeTouchId < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004246 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004247 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004248 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004249 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004250 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004251 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004252 } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004253 activeTouchChanged = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004254 if (!mCurrentFingerIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004255 activeTouchId = mPointerGesture.activeTouchId =
Jeff Brown65fd2512011-08-18 11:20:58 -07004256 mCurrentFingerIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004257 } else {
4258 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004259 }
4260 }
4261
4262 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004263 bool isQuietTime = false;
4264 if (activeTouchId < 0) {
4265 mPointerGesture.resetQuietTime();
4266 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004267 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004268 if (!isQuietTime) {
4269 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4270 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4271 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brown65fd2512011-08-18 11:20:58 -07004272 && currentFingerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004273 // Enter quiet time when exiting swipe or freeform state.
4274 // This is to prevent accidentally entering the hover state and flinging the
4275 // pointer when finishing a swipe and there is still one pointer left onscreen.
4276 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004277 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown65fd2512011-08-18 11:20:58 -07004278 && currentFingerCount >= 2
Jeff Brownbe1aa822011-07-27 16:04:54 -07004279 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004280 // Enter quiet time when releasing the button and there are still two or more
4281 // fingers down. This may indicate that one finger was used to press the button
4282 // but it has not gone up yet.
4283 isQuietTime = true;
4284 }
4285 if (isQuietTime) {
4286 mPointerGesture.quietTime = when;
4287 }
Jeff Brownace13b12011-03-09 17:39:48 -08004288 }
4289 }
4290
4291 // Switch states based on button and pointer state.
4292 if (isQuietTime) {
4293 // Case 1: Quiet time. (QUIET)
4294#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004295 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004296 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004297#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004298 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4299 *outFinishPreviousGesture = true;
4300 }
Jeff Brownace13b12011-03-09 17:39:48 -08004301
4302 mPointerGesture.activeGestureId = -1;
4303 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004304 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004305
Jeff Brown65fd2512011-08-18 11:20:58 -07004306 mPointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004307 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004308 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004309 // The pointer follows the active touch point.
4310 // Emit DOWN, MOVE, UP events at the pointer location.
4311 //
4312 // Only the active touch matters; other fingers are ignored. This policy helps
4313 // to handle the case where the user places a second finger on the touch pad
4314 // to apply the necessary force to depress an integrated button below the surface.
4315 // We don't want the second finger to be delivered to applications.
4316 //
4317 // For this to work well, we need to make sure to track the pointer that is really
4318 // active. If the user first puts one finger down to click then adds another
4319 // finger to drag then the active pointer should switch to the finger that is
4320 // being dragged.
4321#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004322 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown65fd2512011-08-18 11:20:58 -07004323 "currentFingerCount=%d", activeTouchId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004324#endif
4325 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004326 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004327 *outFinishPreviousGesture = true;
4328 mPointerGesture.activeGestureId = 0;
4329 }
4330
4331 // Switch pointers if needed.
4332 // Find the fastest pointer and follow it.
Jeff Brown65fd2512011-08-18 11:20:58 -07004333 if (activeTouchId >= 0 && currentFingerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004334 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004335 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown65fd2512011-08-18 11:20:58 -07004336 for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004337 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004338 float vx, vy;
4339 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4340 float speed = hypotf(vx, vy);
4341 if (speed > bestSpeed) {
4342 bestId = id;
4343 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004344 }
Jeff Brown8d608662010-08-30 03:02:23 -07004345 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004346 }
4347 if (bestId >= 0 && bestId != activeTouchId) {
4348 mPointerGesture.activeTouchId = activeTouchId = bestId;
4349 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004350#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004351 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4352 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004353#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004354 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004355 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004356
Jeff Brown65fd2512011-08-18 11:20:58 -07004357 if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004358 const RawPointerData::Pointer& currentPointer =
4359 mCurrentRawPointerData.pointerForId(activeTouchId);
4360 const RawPointerData::Pointer& lastPointer =
4361 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brown65fd2512011-08-18 11:20:58 -07004362 float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale;
4363 float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004364
Jeff Brownbe1aa822011-07-27 16:04:54 -07004365 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004366 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004367
4368 // Move the pointer using a relative motion.
4369 // When using spots, the click will occur at the position of the anchor
4370 // spot and all other spots will move there.
4371 mPointerController->move(deltaX, deltaY);
4372 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004373 mPointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004374 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004375
Jeff Brownace13b12011-03-09 17:39:48 -08004376 float x, y;
4377 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004378
Jeff Brown79ac9692011-04-19 21:20:10 -07004379 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004380 mPointerGesture.currentGestureIdBits.clear();
4381 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4382 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004383 mPointerGesture.currentGestureProperties[0].clear();
4384 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004385 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004386 mPointerGesture.currentGestureCoords[0].clear();
4387 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4388 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4389 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown65fd2512011-08-18 11:20:58 -07004390 } else if (currentFingerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004391 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004392 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4393 *outFinishPreviousGesture = true;
4394 }
Jeff Brownace13b12011-03-09 17:39:48 -08004395
Jeff Brown79ac9692011-04-19 21:20:10 -07004396 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004397 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004398 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004399 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4400 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown65fd2512011-08-18 11:20:58 -07004401 && lastFingerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004402 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004403 float x, y;
4404 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004405 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4406 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004407#if DEBUG_GESTURES
4408 LOGD("Gestures: TAP");
4409#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004410
4411 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004412 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004413 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004414
Jeff Brownace13b12011-03-09 17:39:48 -08004415 mPointerGesture.activeGestureId = 0;
4416 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004417 mPointerGesture.currentGestureIdBits.clear();
4418 mPointerGesture.currentGestureIdBits.markBit(
4419 mPointerGesture.activeGestureId);
4420 mPointerGesture.currentGestureIdToIndex[
4421 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004422 mPointerGesture.currentGestureProperties[0].clear();
4423 mPointerGesture.currentGestureProperties[0].id =
4424 mPointerGesture.activeGestureId;
4425 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004426 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004427 mPointerGesture.currentGestureCoords[0].clear();
4428 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004429 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004430 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004431 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004432 mPointerGesture.currentGestureCoords[0].setAxisValue(
4433 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004434
Jeff Brownace13b12011-03-09 17:39:48 -08004435 tapped = true;
4436 } else {
4437#if DEBUG_GESTURES
4438 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004439 x - mPointerGesture.tapX,
4440 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004441#endif
4442 }
4443 } else {
4444#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004445 LOGD("Gestures: Not a TAP, %0.3fms since down",
4446 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004447#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004448 }
Jeff Brownace13b12011-03-09 17:39:48 -08004449 }
Jeff Brown2352b972011-04-12 22:39:53 -07004450
Jeff Brown65fd2512011-08-18 11:20:58 -07004451 mPointerVelocityControl.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07004452
Jeff Brownace13b12011-03-09 17:39:48 -08004453 if (!tapped) {
4454#if DEBUG_GESTURES
4455 LOGD("Gestures: NEUTRAL");
4456#endif
4457 mPointerGesture.activeGestureId = -1;
4458 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004459 mPointerGesture.currentGestureIdBits.clear();
4460 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004461 } else if (currentFingerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004462 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004463 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004464 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4465 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004466 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004467
Jeff Brown79ac9692011-04-19 21:20:10 -07004468 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4469 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004470 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004471 float x, y;
4472 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004473 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4474 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004475 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4476 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004477#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004478 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4479 x - mPointerGesture.tapX,
4480 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004481#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004482 }
4483 } else {
4484#if DEBUG_GESTURES
4485 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4486 (when - mPointerGesture.tapUpTime) * 0.000001f);
4487#endif
4488 }
4489 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4490 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4491 }
Jeff Brownace13b12011-03-09 17:39:48 -08004492
Jeff Brown65fd2512011-08-18 11:20:58 -07004493 if (mLastFingerIdBits.hasBit(activeTouchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004494 const RawPointerData::Pointer& currentPointer =
4495 mCurrentRawPointerData.pointerForId(activeTouchId);
4496 const RawPointerData::Pointer& lastPointer =
4497 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004498 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brown65fd2512011-08-18 11:20:58 -07004499 * mPointerXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004500 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brown65fd2512011-08-18 11:20:58 -07004501 * mPointerYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004502
Jeff Brownbe1aa822011-07-27 16:04:54 -07004503 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004504 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004505
Jeff Brown2352b972011-04-12 22:39:53 -07004506 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004507 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004508 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004509 } else {
Jeff Brown65fd2512011-08-18 11:20:58 -07004510 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004511 }
4512
Jeff Brown79ac9692011-04-19 21:20:10 -07004513 bool down;
4514 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4515#if DEBUG_GESTURES
4516 LOGD("Gestures: TAP_DRAG");
4517#endif
4518 down = true;
4519 } else {
4520#if DEBUG_GESTURES
4521 LOGD("Gestures: HOVER");
4522#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004523 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4524 *outFinishPreviousGesture = true;
4525 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004526 mPointerGesture.activeGestureId = 0;
4527 down = false;
4528 }
Jeff Brownace13b12011-03-09 17:39:48 -08004529
4530 float x, y;
4531 mPointerController->getPosition(&x, &y);
4532
Jeff Brownace13b12011-03-09 17:39:48 -08004533 mPointerGesture.currentGestureIdBits.clear();
4534 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4535 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004536 mPointerGesture.currentGestureProperties[0].clear();
4537 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4538 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004539 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004540 mPointerGesture.currentGestureCoords[0].clear();
4541 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4542 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004543 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4544 down ? 1.0f : 0.0f);
4545
Jeff Brown65fd2512011-08-18 11:20:58 -07004546 if (lastFingerCount == 0 && currentFingerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004547 mPointerGesture.resetTap();
4548 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004549 mPointerGesture.tapX = x;
4550 mPointerGesture.tapY = y;
4551 }
Jeff Brownace13b12011-03-09 17:39:48 -08004552 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004553 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4554 // We need to provide feedback for each finger that goes down so we cannot wait
4555 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004556 //
Jeff Brown2352b972011-04-12 22:39:53 -07004557 // The ambiguous case is deciding what to do when there are two fingers down but they
4558 // have not moved enough to determine whether they are part of a drag or part of a
4559 // freeform gesture, or just a press or long-press at the pointer location.
4560 //
4561 // When there are two fingers we start with the PRESS hypothesis and we generate a
4562 // down at the pointer location.
4563 //
4564 // When the two fingers move enough or when additional fingers are added, we make
4565 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004566 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004567
Jeff Brown214eaf42011-05-26 19:17:02 -07004568 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004569 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004570 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004571 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4572 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004573 *outFinishPreviousGesture = true;
Jeff Brown65fd2512011-08-18 11:20:58 -07004574 } else if (!settled && currentFingerCount > lastFingerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004575 // Additional pointers have gone down but not yet settled.
4576 // Reset the gesture.
4577#if DEBUG_GESTURES
4578 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004579 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004580 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004581 * 0.000001f);
4582#endif
4583 *outCancelPreviousGesture = true;
4584 } else {
4585 // Continue previous gesture.
4586 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4587 }
4588
4589 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004590 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4591 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004592 mPointerGesture.referenceIdBits.clear();
Jeff Brown65fd2512011-08-18 11:20:58 -07004593 mPointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004594
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004595 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004596#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004597 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4598 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004599 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004600 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004601#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004602 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4603 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004604 &mPointerGesture.referenceTouchY);
4605 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4606 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004607 }
Jeff Brownace13b12011-03-09 17:39:48 -08004608
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004609 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brown65fd2512011-08-18 11:20:58 -07004610 for (BitSet32 idBits(mCurrentFingerIdBits.value
Jeff Brownbe1aa822011-07-27 16:04:54 -07004611 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4612 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004613 mPointerGesture.referenceDeltas[id].dx = 0;
4614 mPointerGesture.referenceDeltas[id].dy = 0;
4615 }
Jeff Brown65fd2512011-08-18 11:20:58 -07004616 mPointerGesture.referenceIdBits = mCurrentFingerIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004617
4618 // Add delta for all fingers and calculate a common movement delta.
4619 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brown65fd2512011-08-18 11:20:58 -07004620 BitSet32 commonIdBits(mLastFingerIdBits.value
4621 & mCurrentFingerIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004622 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4623 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004624 uint32_t id = idBits.clearFirstMarkedBit();
4625 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4626 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004627 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4628 delta.dx += cpd.x - lpd.x;
4629 delta.dy += cpd.y - lpd.y;
4630
4631 if (first) {
4632 commonDeltaX = delta.dx;
4633 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004634 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004635 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4636 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4637 }
4638 }
Jeff Brownace13b12011-03-09 17:39:48 -08004639
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004640 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4641 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4642 float dist[MAX_POINTER_ID + 1];
4643 int32_t distOverThreshold = 0;
4644 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004645 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004646 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brown65fd2512011-08-18 11:20:58 -07004647 dist[id] = hypotf(delta.dx * mPointerXZoomScale,
4648 delta.dy * mPointerYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004649 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004650 distOverThreshold += 1;
4651 }
4652 }
4653
4654 // Only transition when at least two pointers have moved further than
4655 // the minimum distance threshold.
4656 if (distOverThreshold >= 2) {
Jeff Brown65fd2512011-08-18 11:20:58 -07004657 if (currentFingerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004658 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004659#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004660 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004661 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004662#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004663 *outCancelPreviousGesture = true;
4664 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4665 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004666 // There are exactly two pointers.
Jeff Brown65fd2512011-08-18 11:20:58 -07004667 BitSet32 idBits(mCurrentFingerIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004668 uint32_t id1 = idBits.clearFirstMarkedBit();
4669 uint32_t id2 = idBits.firstMarkedBit();
4670 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4671 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4672 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4673 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4674 // There are two pointers but they are too far apart for a SWIPE,
4675 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004676#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004677 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4678 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004679#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004680 *outCancelPreviousGesture = true;
4681 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4682 } else {
4683 // There are two pointers. Wait for both pointers to start moving
4684 // before deciding whether this is a SWIPE or FREEFORM gesture.
4685 float dist1 = dist[id1];
4686 float dist2 = dist[id2];
4687 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4688 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4689 // Calculate the dot product of the displacement vectors.
4690 // When the vectors are oriented in approximately the same direction,
4691 // the angle betweeen them is near zero and the cosine of the angle
4692 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4693 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4694 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown65fd2512011-08-18 11:20:58 -07004695 float dx1 = delta1.dx * mPointerXZoomScale;
4696 float dy1 = delta1.dy * mPointerYZoomScale;
4697 float dx2 = delta2.dx * mPointerXZoomScale;
4698 float dy2 = delta2.dy * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004699 float dot = dx1 * dx2 + dy1 * dy2;
4700 float cosine = dot / (dist1 * dist2); // denominator always > 0
4701 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4702 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004703#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004704 LOGD("Gestures: PRESS transitioned to SWIPE, "
4705 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4706 "cosine %0.3f >= %0.3f",
4707 dist1, mConfig.pointerGestureMultitouchMinDistance,
4708 dist2, mConfig.pointerGestureMultitouchMinDistance,
4709 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004710#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004711 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4712 } else {
4713 // Pointers are moving in different directions. Switch to FREEFORM.
4714#if DEBUG_GESTURES
4715 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4716 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4717 "cosine %0.3f < %0.3f",
4718 dist1, mConfig.pointerGestureMultitouchMinDistance,
4719 dist2, mConfig.pointerGestureMultitouchMinDistance,
4720 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4721#endif
4722 *outCancelPreviousGesture = true;
4723 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4724 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004725 }
Jeff Brownace13b12011-03-09 17:39:48 -08004726 }
4727 }
Jeff Brownace13b12011-03-09 17:39:48 -08004728 }
4729 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004730 // Switch from SWIPE to FREEFORM if additional pointers go down.
4731 // Cancel previous gesture.
Jeff Brown65fd2512011-08-18 11:20:58 -07004732 if (currentFingerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004733#if DEBUG_GESTURES
4734 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brown65fd2512011-08-18 11:20:58 -07004735 currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004736#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004737 *outCancelPreviousGesture = true;
4738 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004739 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004740 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004741
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004742 // Move the reference points based on the overall group motion of the fingers
4743 // except in PRESS mode while waiting for a transition to occur.
4744 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4745 && (commonDeltaX || commonDeltaY)) {
4746 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004747 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004748 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004749 delta.dx = 0;
4750 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004751 }
4752
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004753 mPointerGesture.referenceTouchX += commonDeltaX;
4754 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004755
Jeff Brown65fd2512011-08-18 11:20:58 -07004756 commonDeltaX *= mPointerXMovementScale;
4757 commonDeltaY *= mPointerYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004758
Jeff Brownbe1aa822011-07-27 16:04:54 -07004759 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brown65fd2512011-08-18 11:20:58 -07004760 mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004761
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004762 mPointerGesture.referenceGestureX += commonDeltaX;
4763 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004764 }
4765
4766 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004767 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4768 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4769 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004770#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004771 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004772 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004773 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004774#endif
4775 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4776
4777 mPointerGesture.currentGestureIdBits.clear();
4778 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4779 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004780 mPointerGesture.currentGestureProperties[0].clear();
4781 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4782 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004783 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004784 mPointerGesture.currentGestureCoords[0].clear();
4785 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4786 mPointerGesture.referenceGestureX);
4787 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4788 mPointerGesture.referenceGestureY);
4789 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004790 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4791 // FREEFORM mode.
4792#if DEBUG_GESTURES
4793 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4794 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown65fd2512011-08-18 11:20:58 -07004795 activeTouchId, mPointerGesture.activeGestureId, currentFingerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004796#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004797 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004798
Jeff Brownace13b12011-03-09 17:39:48 -08004799 mPointerGesture.currentGestureIdBits.clear();
4800
4801 BitSet32 mappedTouchIdBits;
4802 BitSet32 usedGestureIdBits;
4803 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4804 // Initially, assign the active gesture id to the active touch point
4805 // if there is one. No other touch id bits are mapped yet.
4806 if (!*outCancelPreviousGesture) {
4807 mappedTouchIdBits.markBit(activeTouchId);
4808 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4809 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4810 mPointerGesture.activeGestureId;
4811 } else {
4812 mPointerGesture.activeGestureId = -1;
4813 }
4814 } else {
4815 // Otherwise, assume we mapped all touches from the previous frame.
4816 // Reuse all mappings that are still applicable.
Jeff Brown65fd2512011-08-18 11:20:58 -07004817 mappedTouchIdBits.value = mLastFingerIdBits.value
4818 & mCurrentFingerIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004819 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4820
4821 // Check whether we need to choose a new active gesture id because the
4822 // current went went up.
Jeff Brown65fd2512011-08-18 11:20:58 -07004823 for (BitSet32 upTouchIdBits(mLastFingerIdBits.value
4824 & ~mCurrentFingerIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004825 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004826 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004827 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4828 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4829 mPointerGesture.activeGestureId = -1;
4830 break;
4831 }
4832 }
4833 }
4834
4835#if DEBUG_GESTURES
4836 LOGD("Gestures: FREEFORM follow up "
4837 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4838 "activeGestureId=%d",
4839 mappedTouchIdBits.value, usedGestureIdBits.value,
4840 mPointerGesture.activeGestureId);
4841#endif
4842
Jeff Brown65fd2512011-08-18 11:20:58 -07004843 BitSet32 idBits(mCurrentFingerIdBits);
4844 for (uint32_t i = 0; i < currentFingerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004845 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004846 uint32_t gestureId;
4847 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004848 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004849 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4850#if DEBUG_GESTURES
4851 LOGD("Gestures: FREEFORM "
4852 "new mapping for touch id %d -> gesture id %d",
4853 touchId, gestureId);
4854#endif
4855 } else {
4856 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4857#if DEBUG_GESTURES
4858 LOGD("Gestures: FREEFORM "
4859 "existing mapping for touch id %d -> gesture id %d",
4860 touchId, gestureId);
4861#endif
4862 }
4863 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4864 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4865
Jeff Brownbe1aa822011-07-27 16:04:54 -07004866 const RawPointerData::Pointer& pointer =
4867 mCurrentRawPointerData.pointerForId(touchId);
4868 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
Jeff Brown65fd2512011-08-18 11:20:58 -07004869 * mPointerXZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004870 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
Jeff Brown65fd2512011-08-18 11:20:58 -07004871 * mPointerYZoomScale;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004872 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004873
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004874 mPointerGesture.currentGestureProperties[i].clear();
4875 mPointerGesture.currentGestureProperties[i].id = gestureId;
4876 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004877 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004878 mPointerGesture.currentGestureCoords[i].clear();
4879 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004880 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004881 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004882 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004883 mPointerGesture.currentGestureCoords[i].setAxisValue(
4884 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4885 }
4886
4887 if (mPointerGesture.activeGestureId < 0) {
4888 mPointerGesture.activeGestureId =
4889 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4890#if DEBUG_GESTURES
4891 LOGD("Gestures: FREEFORM new "
4892 "activeGestureId=%d", mPointerGesture.activeGestureId);
4893#endif
4894 }
Jeff Brown2352b972011-04-12 22:39:53 -07004895 }
Jeff Brownace13b12011-03-09 17:39:48 -08004896 }
4897
Jeff Brownbe1aa822011-07-27 16:04:54 -07004898 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004899
Jeff Brownace13b12011-03-09 17:39:48 -08004900#if DEBUG_GESTURES
4901 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004902 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4903 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004904 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004905 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4906 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004907 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004908 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004909 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004910 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004911 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004912 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4913 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4914 id, index, properties.toolType,
4915 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004916 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4917 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4918 }
4919 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004920 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004921 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004922 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004923 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004924 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4925 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4926 id, index, properties.toolType,
4927 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004928 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4929 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4930 }
4931#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004932 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004933}
4934
Jeff Brown65fd2512011-08-18 11:20:58 -07004935void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) {
4936 mPointerSimple.currentCoords.clear();
4937 mPointerSimple.currentProperties.clear();
4938
4939 bool down, hovering;
4940 if (!mCurrentStylusIdBits.isEmpty()) {
4941 uint32_t id = mCurrentStylusIdBits.firstMarkedBit();
4942 uint32_t index = mCurrentCookedPointerData.idToIndex[id];
4943 float x = mCurrentCookedPointerData.pointerCoords[index].getX();
4944 float y = mCurrentCookedPointerData.pointerCoords[index].getY();
4945 mPointerController->setPosition(x, y);
4946
4947 hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id);
4948 down = !hovering;
4949
4950 mPointerController->getPosition(&x, &y);
4951 mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]);
4952 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
4953 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4954 mPointerSimple.currentProperties.id = 0;
4955 mPointerSimple.currentProperties.toolType =
4956 mCurrentCookedPointerData.pointerProperties[index].toolType;
4957 } else {
4958 down = false;
4959 hovering = false;
4960 }
4961
4962 dispatchPointerSimple(when, policyFlags, down, hovering);
4963}
4964
4965void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) {
4966 abortPointerSimple(when, policyFlags);
4967}
4968
4969void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) {
4970 mPointerSimple.currentCoords.clear();
4971 mPointerSimple.currentProperties.clear();
4972
4973 bool down, hovering;
4974 if (!mCurrentMouseIdBits.isEmpty()) {
4975 uint32_t id = mCurrentMouseIdBits.firstMarkedBit();
4976 uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id];
4977 if (mLastMouseIdBits.hasBit(id)) {
4978 uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id];
4979 float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x
4980 - mLastRawPointerData.pointers[lastIndex].x)
4981 * mPointerXMovementScale;
4982 float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y
4983 - mLastRawPointerData.pointers[lastIndex].y)
4984 * mPointerYMovementScale;
4985
4986 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
4987 mPointerVelocityControl.move(when, &deltaX, &deltaY);
4988
4989 mPointerController->move(deltaX, deltaY);
4990 } else {
4991 mPointerVelocityControl.reset();
4992 }
4993
4994 down = isPointerDown(mCurrentButtonState);
4995 hovering = !down;
4996
4997 float x, y;
4998 mPointerController->getPosition(&x, &y);
4999 mPointerSimple.currentCoords.copyFrom(
5000 mCurrentCookedPointerData.pointerCoords[currentIndex]);
5001 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
5002 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
5003 mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
5004 hovering ? 0.0f : 1.0f);
5005 mPointerSimple.currentProperties.id = 0;
5006 mPointerSimple.currentProperties.toolType =
5007 mCurrentCookedPointerData.pointerProperties[currentIndex].toolType;
5008 } else {
5009 mPointerVelocityControl.reset();
5010
5011 down = false;
5012 hovering = false;
5013 }
5014
5015 dispatchPointerSimple(when, policyFlags, down, hovering);
5016}
5017
5018void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) {
5019 abortPointerSimple(when, policyFlags);
5020
5021 mPointerVelocityControl.reset();
5022}
5023
5024void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags,
5025 bool down, bool hovering) {
5026 int32_t metaState = getContext()->getGlobalMetaState();
5027
5028 if (mPointerController != NULL) {
5029 if (down || hovering) {
5030 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
5031 mPointerController->clearSpots();
5032 mPointerController->setButtonState(mCurrentButtonState);
5033 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
5034 } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) {
5035 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5036 }
5037 }
5038
5039 if (mPointerSimple.down && !down) {
5040 mPointerSimple.down = false;
5041
5042 // Send up.
5043 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5044 AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0,
5045 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5046 mOrientedXPrecision, mOrientedYPrecision,
5047 mPointerSimple.downTime);
5048 getListener()->notifyMotion(&args);
5049 }
5050
5051 if (mPointerSimple.hovering && !hovering) {
5052 mPointerSimple.hovering = false;
5053
5054 // Send hover exit.
5055 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5056 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
5057 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords,
5058 mOrientedXPrecision, mOrientedYPrecision,
5059 mPointerSimple.downTime);
5060 getListener()->notifyMotion(&args);
5061 }
5062
5063 if (down) {
5064 if (!mPointerSimple.down) {
5065 mPointerSimple.down = true;
5066 mPointerSimple.downTime = when;
5067
5068 // Send down.
5069 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5070 AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0,
5071 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5072 mOrientedXPrecision, mOrientedYPrecision,
5073 mPointerSimple.downTime);
5074 getListener()->notifyMotion(&args);
5075 }
5076
5077 // Send move.
5078 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5079 AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0,
5080 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5081 mOrientedXPrecision, mOrientedYPrecision,
5082 mPointerSimple.downTime);
5083 getListener()->notifyMotion(&args);
5084 }
5085
5086 if (hovering) {
5087 if (!mPointerSimple.hovering) {
5088 mPointerSimple.hovering = true;
5089
5090 // Send hover enter.
5091 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5092 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
5093 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5094 mOrientedXPrecision, mOrientedYPrecision,
5095 mPointerSimple.downTime);
5096 getListener()->notifyMotion(&args);
5097 }
5098
5099 // Send hover move.
5100 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5101 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
5102 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords,
5103 mOrientedXPrecision, mOrientedYPrecision,
5104 mPointerSimple.downTime);
5105 getListener()->notifyMotion(&args);
5106 }
5107
5108 if (mCurrentRawVScroll || mCurrentRawHScroll) {
5109 float vscroll = mCurrentRawVScroll;
5110 float hscroll = mCurrentRawHScroll;
5111 mWheelYVelocityControl.move(when, NULL, &vscroll);
5112 mWheelXVelocityControl.move(when, &hscroll, NULL);
5113
5114 // Send scroll.
5115 PointerCoords pointerCoords;
5116 pointerCoords.copyFrom(mPointerSimple.currentCoords);
5117 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
5118 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
5119
5120 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
5121 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0,
5122 1, &mPointerSimple.currentProperties, &pointerCoords,
5123 mOrientedXPrecision, mOrientedYPrecision,
5124 mPointerSimple.downTime);
5125 getListener()->notifyMotion(&args);
5126 }
5127
5128 // Save state.
5129 if (down || hovering) {
5130 mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords);
5131 mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties);
5132 } else {
5133 mPointerSimple.reset();
5134 }
5135}
5136
5137void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) {
5138 mPointerSimple.currentCoords.clear();
5139 mPointerSimple.currentProperties.clear();
5140
5141 dispatchPointerSimple(when, policyFlags, false, false);
5142}
5143
Jeff Brownace13b12011-03-09 17:39:48 -08005144void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005145 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
5146 const PointerProperties* properties, const PointerCoords* coords,
5147 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08005148 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
5149 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005150 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08005151 uint32_t pointerCount = 0;
5152 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005153 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005154 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005155 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08005156 pointerCoords[pointerCount].copyFrom(coords[index]);
5157
5158 if (changedId >= 0 && id == uint32_t(changedId)) {
5159 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
5160 }
5161
5162 pointerCount += 1;
5163 }
5164
Jeff Brownb6110c22011-04-01 16:15:13 -07005165 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08005166
5167 if (changedId >= 0 && pointerCount == 1) {
5168 // Replace initial down and final up action.
5169 // We can compare the action without masking off the changed pointer index
5170 // because we know the index is 0.
5171 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
5172 action = AMOTION_EVENT_ACTION_DOWN;
5173 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
5174 action = AMOTION_EVENT_ACTION_UP;
5175 } else {
5176 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07005177 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08005178 }
5179 }
5180
Jeff Brownbe1aa822011-07-27 16:04:54 -07005181 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005182 action, flags, metaState, buttonState, edgeFlags,
5183 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005184 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08005185}
5186
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005187bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08005188 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005189 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
5190 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08005191 bool changed = false;
5192 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005193 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08005194 uint32_t inIndex = inIdToIndex[id];
5195 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005196
5197 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005198 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005199 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08005200 PointerCoords& curOutCoords = outCoords[outIndex];
5201
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005202 if (curInProperties != curOutProperties) {
5203 curOutProperties.copyFrom(curInProperties);
5204 changed = true;
5205 }
5206
Jeff Brownace13b12011-03-09 17:39:48 -08005207 if (curInCoords != curOutCoords) {
5208 curOutCoords.copyFrom(curInCoords);
5209 changed = true;
5210 }
5211 }
5212 return changed;
5213}
5214
5215void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005216 if (mPointerController != NULL) {
5217 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
5218 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005219}
5220
Jeff Brownbe1aa822011-07-27 16:04:54 -07005221bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
5222 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
5223 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005224}
5225
Jeff Brownbe1aa822011-07-27 16:04:54 -07005226const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07005227 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005228 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07005229 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005230 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005231
5232#if DEBUG_VIRTUAL_KEYS
5233 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
5234 "left=%d, top=%d, right=%d, bottom=%d",
5235 x, y,
5236 virtualKey.keyCode, virtualKey.scanCode,
5237 virtualKey.hitLeft, virtualKey.hitTop,
5238 virtualKey.hitRight, virtualKey.hitBottom);
5239#endif
5240
5241 if (virtualKey.isHit(x, y)) {
5242 return & virtualKey;
5243 }
5244 }
5245
5246 return NULL;
5247}
5248
Jeff Brownbe1aa822011-07-27 16:04:54 -07005249void TouchInputMapper::assignPointerIds() {
5250 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
5251 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
5252
5253 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005254
5255 if (currentPointerCount == 0) {
5256 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07005257 return;
5258 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005259
Jeff Brownbe1aa822011-07-27 16:04:54 -07005260 if (lastPointerCount == 0) {
5261 // All pointers are new.
5262 for (uint32_t i = 0; i < currentPointerCount; i++) {
5263 uint32_t id = i;
5264 mCurrentRawPointerData.pointers[i].id = id;
5265 mCurrentRawPointerData.idToIndex[id] = i;
5266 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
5267 }
5268 return;
5269 }
5270
5271 if (currentPointerCount == 1 && lastPointerCount == 1
5272 && mCurrentRawPointerData.pointers[0].toolType
5273 == mLastRawPointerData.pointers[0].toolType) {
5274 // Only one pointer and no change in count so it must have the same id as before.
5275 uint32_t id = mLastRawPointerData.pointers[0].id;
5276 mCurrentRawPointerData.pointers[0].id = id;
5277 mCurrentRawPointerData.idToIndex[id] = 0;
5278 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
5279 return;
5280 }
5281
5282 // General case.
5283 // We build a heap of squared euclidean distances between current and last pointers
5284 // associated with the current and last pointer indices. Then, we find the best
5285 // match (by distance) for each current pointer.
5286 // The pointers must have the same tool type but it is possible for them to
5287 // transition from hovering to touching or vice-versa while retaining the same id.
5288 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
5289
5290 uint32_t heapSize = 0;
5291 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
5292 currentPointerIndex++) {
5293 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
5294 lastPointerIndex++) {
5295 const RawPointerData::Pointer& currentPointer =
5296 mCurrentRawPointerData.pointers[currentPointerIndex];
5297 const RawPointerData::Pointer& lastPointer =
5298 mLastRawPointerData.pointers[lastPointerIndex];
5299 if (currentPointer.toolType == lastPointer.toolType) {
5300 int64_t deltaX = currentPointer.x - lastPointer.x;
5301 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005302
5303 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5304
5305 // Insert new element into the heap (sift up).
5306 heap[heapSize].currentPointerIndex = currentPointerIndex;
5307 heap[heapSize].lastPointerIndex = lastPointerIndex;
5308 heap[heapSize].distance = distance;
5309 heapSize += 1;
5310 }
5311 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005312 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005313
Jeff Brownbe1aa822011-07-27 16:04:54 -07005314 // Heapify
5315 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
5316 startIndex -= 1;
5317 for (uint32_t parentIndex = startIndex; ;) {
5318 uint32_t childIndex = parentIndex * 2 + 1;
5319 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005320 break;
5321 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005322
5323 if (childIndex + 1 < heapSize
5324 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5325 childIndex += 1;
5326 }
5327
5328 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5329 break;
5330 }
5331
5332 swap(heap[parentIndex], heap[childIndex]);
5333 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005334 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005335 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005336
5337#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07005338 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
5339 for (size_t i = 0; i < heapSize; i++) {
5340 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5341 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5342 heap[i].distance);
5343 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005344#endif
5345
Jeff Brownbe1aa822011-07-27 16:04:54 -07005346 // Pull matches out by increasing order of distance.
5347 // To avoid reassigning pointers that have already been matched, the loop keeps track
5348 // of which last and current pointers have been matched using the matchedXXXBits variables.
5349 // It also tracks the used pointer id bits.
5350 BitSet32 matchedLastBits(0);
5351 BitSet32 matchedCurrentBits(0);
5352 BitSet32 usedIdBits(0);
5353 bool first = true;
5354 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
5355 while (heapSize > 0) {
5356 if (first) {
5357 // The first time through the loop, we just consume the root element of
5358 // the heap (the one with smallest distance).
5359 first = false;
5360 } else {
5361 // Previous iterations consumed the root element of the heap.
5362 // Pop root element off of the heap (sift down).
5363 heap[0] = heap[heapSize];
5364 for (uint32_t parentIndex = 0; ;) {
5365 uint32_t childIndex = parentIndex * 2 + 1;
5366 if (childIndex >= heapSize) {
5367 break;
5368 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005369
Jeff Brownbe1aa822011-07-27 16:04:54 -07005370 if (childIndex + 1 < heapSize
5371 && heap[childIndex + 1].distance < heap[childIndex].distance) {
5372 childIndex += 1;
5373 }
5374
5375 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5376 break;
5377 }
5378
5379 swap(heap[parentIndex], heap[childIndex]);
5380 parentIndex = childIndex;
5381 }
5382
5383#if DEBUG_POINTER_ASSIGNMENT
5384 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5385 for (size_t i = 0; i < heapSize; i++) {
5386 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5387 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5388 heap[i].distance);
5389 }
5390#endif
5391 }
5392
5393 heapSize -= 1;
5394
5395 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5396 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5397
5398 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5399 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5400
5401 matchedCurrentBits.markBit(currentPointerIndex);
5402 matchedLastBits.markBit(lastPointerIndex);
5403
5404 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5405 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5406 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5407 mCurrentRawPointerData.markIdBit(id,
5408 mCurrentRawPointerData.isHovering(currentPointerIndex));
5409 usedIdBits.markBit(id);
5410
5411#if DEBUG_POINTER_ASSIGNMENT
5412 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5413 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5414#endif
5415 break;
5416 }
5417 }
5418
5419 // Assign fresh ids to pointers that were not matched in the process.
5420 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5421 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5422 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5423
5424 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5425 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5426 mCurrentRawPointerData.markIdBit(id,
5427 mCurrentRawPointerData.isHovering(currentPointerIndex));
5428
5429#if DEBUG_POINTER_ASSIGNMENT
5430 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5431 currentPointerIndex, id);
5432#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005433 }
5434}
5435
Jeff Brown6d0fec22010-07-23 21:28:06 -07005436int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005437 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5438 return AKEY_STATE_VIRTUAL;
5439 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005440
Jeff Brownbe1aa822011-07-27 16:04:54 -07005441 size_t numVirtualKeys = mVirtualKeys.size();
5442 for (size_t i = 0; i < numVirtualKeys; i++) {
5443 const VirtualKey& virtualKey = mVirtualKeys[i];
5444 if (virtualKey.keyCode == keyCode) {
5445 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005446 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005447 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005448
5449 return AKEY_STATE_UNKNOWN;
5450}
5451
5452int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005453 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
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.scanCode == scanCode) {
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
5468bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5469 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005470 size_t numVirtualKeys = mVirtualKeys.size();
5471 for (size_t i = 0; i < numVirtualKeys; i++) {
5472 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005473
Jeff Brownbe1aa822011-07-27 16:04:54 -07005474 for (size_t i = 0; i < numCodes; i++) {
5475 if (virtualKey.keyCode == keyCodes[i]) {
5476 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005477 }
5478 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005479 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005480
5481 return true;
5482}
5483
5484
5485// --- SingleTouchInputMapper ---
5486
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005487SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5488 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005489}
5490
5491SingleTouchInputMapper::~SingleTouchInputMapper() {
5492}
5493
Jeff Brown65fd2512011-08-18 11:20:58 -07005494void SingleTouchInputMapper::reset(nsecs_t when) {
5495 mSingleTouchMotionAccumulator.reset(getDevice());
5496
5497 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005498}
5499
Jeff Brown6d0fec22010-07-23 21:28:06 -07005500void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005501 TouchInputMapper::process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005502
Jeff Brown65fd2512011-08-18 11:20:58 -07005503 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005504}
5505
Jeff Brown65fd2512011-08-18 11:20:58 -07005506void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brownd87c6d52011-08-10 14:55:59 -07005507 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005508 mCurrentRawPointerData.pointerCount = 1;
5509 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005510
Jeff Brown65fd2512011-08-18 11:20:58 -07005511 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5512 && (mTouchButtonAccumulator.isHovering()
5513 || (mRawPointerAxes.pressure.valid
5514 && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005515 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005516
Jeff Brownbe1aa822011-07-27 16:04:54 -07005517 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005518 outPointer.id = 0;
5519 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5520 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5521 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5522 outPointer.touchMajor = 0;
5523 outPointer.touchMinor = 0;
5524 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5525 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5526 outPointer.orientation = 0;
5527 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005528 outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX();
5529 outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY();
Jeff Brown49754db2011-07-01 17:37:58 -07005530 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5531 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5532 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5533 }
5534 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005535 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005536}
5537
Jeff Brownbe1aa822011-07-27 16:04:54 -07005538void SingleTouchInputMapper::configureRawPointerAxes() {
5539 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005540
Jeff Brownbe1aa822011-07-27 16:04:54 -07005541 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5542 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5543 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5544 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5545 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown65fd2512011-08-18 11:20:58 -07005546 getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX);
5547 getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005548}
5549
5550
5551// --- MultiTouchInputMapper ---
5552
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005553MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005554 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005555}
5556
5557MultiTouchInputMapper::~MultiTouchInputMapper() {
5558}
5559
Jeff Brown65fd2512011-08-18 11:20:58 -07005560void MultiTouchInputMapper::reset(nsecs_t when) {
5561 mMultiTouchMotionAccumulator.reset(getDevice());
5562
Jeff Brown6894a292011-07-01 17:59:27 -07005563 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005564
Jeff Brown65fd2512011-08-18 11:20:58 -07005565 TouchInputMapper::reset(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005566}
5567
5568void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005569 TouchInputMapper::process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005570
Jeff Brown65fd2512011-08-18 11:20:58 -07005571 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005572}
5573
Jeff Brown65fd2512011-08-18 11:20:58 -07005574void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005575 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005576 size_t outCount = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005577 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005578
Jeff Brown80fd47c2011-05-24 01:07:44 -07005579 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005580 const MultiTouchMotionAccumulator::Slot* inSlot =
5581 mMultiTouchMotionAccumulator.getSlot(inIndex);
5582 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005583 continue;
5584 }
5585
Jeff Brown80fd47c2011-05-24 01:07:44 -07005586 if (outCount >= MAX_POINTERS) {
5587#if DEBUG_POINTERS
5588 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5589 "ignoring the rest.",
5590 getDeviceName().string(), MAX_POINTERS);
5591#endif
5592 break; // too many fingers!
5593 }
5594
Jeff Brownbe1aa822011-07-27 16:04:54 -07005595 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005596 outPointer.x = inSlot->getX();
5597 outPointer.y = inSlot->getY();
5598 outPointer.pressure = inSlot->getPressure();
5599 outPointer.touchMajor = inSlot->getTouchMajor();
5600 outPointer.touchMinor = inSlot->getTouchMinor();
5601 outPointer.toolMajor = inSlot->getToolMajor();
5602 outPointer.toolMinor = inSlot->getToolMinor();
5603 outPointer.orientation = inSlot->getOrientation();
5604 outPointer.distance = inSlot->getDistance();
Jeff Brown65fd2512011-08-18 11:20:58 -07005605 outPointer.tiltX = 0;
5606 outPointer.tiltY = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005607
Jeff Brown49754db2011-07-01 17:37:58 -07005608 outPointer.toolType = inSlot->getToolType();
5609 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5610 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5611 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5612 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5613 }
Jeff Brown8d608662010-08-30 03:02:23 -07005614 }
5615
Jeff Brown65fd2512011-08-18 11:20:58 -07005616 bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE
5617 && (mTouchButtonAccumulator.isHovering()
5618 || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0));
Jeff Brownbe1aa822011-07-27 16:04:54 -07005619 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005620
Jeff Brown8d608662010-08-30 03:02:23 -07005621 // Assign pointer id using tracking id if available.
Jeff Brown65fd2512011-08-18 11:20:58 -07005622 if (*outHavePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005623 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005624 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005625 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005626 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005627 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005628 if (mPointerTrackingIdMap[n] == trackingId) {
5629 id = n;
5630 }
5631 }
5632
5633 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005634 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005635 mPointerTrackingIdMap[id] = trackingId;
5636 }
5637 }
5638 if (id < 0) {
Jeff Brown65fd2512011-08-18 11:20:58 -07005639 *outHavePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005640 mCurrentRawPointerData.clearIdBits();
5641 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005642 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005643 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005644 mCurrentRawPointerData.idToIndex[id] = outCount;
5645 mCurrentRawPointerData.markIdBit(id, isHovering);
5646 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005647 }
5648 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005649
Jeff Brown6d0fec22010-07-23 21:28:06 -07005650 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005651 }
5652
Jeff Brownbe1aa822011-07-27 16:04:54 -07005653 mCurrentRawPointerData.pointerCount = outCount;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005654 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005655
Jeff Brown65fd2512011-08-18 11:20:58 -07005656 mMultiTouchMotionAccumulator.finishSync();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005657}
5658
Jeff Brownbe1aa822011-07-27 16:04:54 -07005659void MultiTouchInputMapper::configureRawPointerAxes() {
5660 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005661
Jeff Brownbe1aa822011-07-27 16:04:54 -07005662 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5663 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5664 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5665 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5666 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5667 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5668 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5669 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5670 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5671 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5672 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005673
Jeff Brownbe1aa822011-07-27 16:04:54 -07005674 if (mRawPointerAxes.trackingId.valid
5675 && mRawPointerAxes.slot.valid
5676 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5677 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005678 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005679 LOGW("MultiTouch Device %s reported %d slots but the framework "
5680 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005681 getDeviceName().string(), slotCount, MAX_SLOTS);
5682 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005683 }
Jeff Brown49754db2011-07-01 17:37:58 -07005684 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005685 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005686 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005687 }
Jeff Brown9c3cda02010-06-15 01:31:58 -07005688}
5689
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005690
Jeff Browncb1404e2011-01-15 18:14:15 -08005691// --- JoystickInputMapper ---
5692
5693JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5694 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005695}
5696
5697JoystickInputMapper::~JoystickInputMapper() {
5698}
5699
5700uint32_t JoystickInputMapper::getSources() {
5701 return AINPUT_SOURCE_JOYSTICK;
5702}
5703
5704void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5705 InputMapper::populateDeviceInfo(info);
5706
Jeff Brown6f2fba42011-02-19 01:08:02 -08005707 for (size_t i = 0; i < mAxes.size(); i++) {
5708 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005709 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5710 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005711 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005712 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5713 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005714 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005715 }
5716}
5717
5718void JoystickInputMapper::dump(String8& dump) {
5719 dump.append(INDENT2 "Joystick Input Mapper:\n");
5720
Jeff Brown6f2fba42011-02-19 01:08:02 -08005721 dump.append(INDENT3 "Axes:\n");
5722 size_t numAxes = mAxes.size();
5723 for (size_t i = 0; i < numAxes; i++) {
5724 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005725 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005726 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005727 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005728 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005729 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005730 }
Jeff Brown85297452011-03-04 13:07:49 -08005731 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5732 label = getAxisLabel(axis.axisInfo.highAxis);
5733 if (label) {
5734 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5735 } else {
5736 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5737 axis.axisInfo.splitValue);
5738 }
5739 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5740 dump.append(" (invert)");
5741 }
5742
5743 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5744 axis.min, axis.max, axis.flat, axis.fuzz);
5745 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5746 "highScale=%0.5f, highOffset=%0.5f\n",
5747 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005748 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5749 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005750 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005751 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005752 }
5753}
5754
Jeff Brown65fd2512011-08-18 11:20:58 -07005755void JoystickInputMapper::configure(nsecs_t when,
5756 const InputReaderConfiguration* config, uint32_t changes) {
5757 InputMapper::configure(when, config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005758
Jeff Brown474dcb52011-06-14 20:22:50 -07005759 if (!changes) { // first time only
5760 // Collect all axes.
5761 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5762 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005763 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005764 if (rawAxisInfo.valid) {
5765 // Map axis.
5766 AxisInfo axisInfo;
5767 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5768 if (!explicitlyMapped) {
5769 // Axis is not explicitly mapped, will choose a generic axis later.
5770 axisInfo.mode = AxisInfo::MODE_NORMAL;
5771 axisInfo.axis = -1;
5772 }
5773
5774 // Apply flat override.
5775 int32_t rawFlat = axisInfo.flatOverride < 0
5776 ? rawAxisInfo.flat : axisInfo.flatOverride;
5777
5778 // Calculate scaling factors and limits.
5779 Axis axis;
5780 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5781 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5782 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5783 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5784 scale, 0.0f, highScale, 0.0f,
5785 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5786 } else if (isCenteredAxis(axisInfo.axis)) {
5787 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5788 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5789 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5790 scale, offset, scale, offset,
5791 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5792 } else {
5793 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5794 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5795 scale, 0.0f, scale, 0.0f,
5796 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5797 }
5798
5799 // To eliminate noise while the joystick is at rest, filter out small variations
5800 // in axis values up front.
5801 axis.filter = axis.flat * 0.25f;
5802
5803 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005804 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005805 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005806
Jeff Brown474dcb52011-06-14 20:22:50 -07005807 // If there are too many axes, start dropping them.
5808 // Prefer to keep explicitly mapped axes.
5809 if (mAxes.size() > PointerCoords::MAX_AXES) {
5810 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5811 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5812 pruneAxes(true);
5813 pruneAxes(false);
5814 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005815
Jeff Brown474dcb52011-06-14 20:22:50 -07005816 // Assign generic axis ids to remaining axes.
5817 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5818 size_t numAxes = mAxes.size();
5819 for (size_t i = 0; i < numAxes; i++) {
5820 Axis& axis = mAxes.editValueAt(i);
5821 if (axis.axisInfo.axis < 0) {
5822 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5823 && haveAxis(nextGenericAxisId)) {
5824 nextGenericAxisId += 1;
5825 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005826
Jeff Brown474dcb52011-06-14 20:22:50 -07005827 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5828 axis.axisInfo.axis = nextGenericAxisId;
5829 nextGenericAxisId += 1;
5830 } else {
5831 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5832 "have already been assigned to other axes.",
5833 getDeviceName().string(), mAxes.keyAt(i));
5834 mAxes.removeItemsAt(i--);
5835 numAxes -= 1;
5836 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005837 }
5838 }
5839 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005840}
5841
Jeff Brown85297452011-03-04 13:07:49 -08005842bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005843 size_t numAxes = mAxes.size();
5844 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005845 const Axis& axis = mAxes.valueAt(i);
5846 if (axis.axisInfo.axis == axisId
5847 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5848 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005849 return true;
5850 }
5851 }
5852 return false;
5853}
Jeff Browncb1404e2011-01-15 18:14:15 -08005854
Jeff Brown6f2fba42011-02-19 01:08:02 -08005855void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5856 size_t i = mAxes.size();
5857 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5858 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5859 continue;
5860 }
5861 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5862 getDeviceName().string(), mAxes.keyAt(i));
5863 mAxes.removeItemsAt(i);
5864 }
5865}
5866
5867bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5868 switch (axis) {
5869 case AMOTION_EVENT_AXIS_X:
5870 case AMOTION_EVENT_AXIS_Y:
5871 case AMOTION_EVENT_AXIS_Z:
5872 case AMOTION_EVENT_AXIS_RX:
5873 case AMOTION_EVENT_AXIS_RY:
5874 case AMOTION_EVENT_AXIS_RZ:
5875 case AMOTION_EVENT_AXIS_HAT_X:
5876 case AMOTION_EVENT_AXIS_HAT_Y:
5877 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005878 case AMOTION_EVENT_AXIS_RUDDER:
5879 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005880 return true;
5881 default:
5882 return false;
5883 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005884}
5885
Jeff Brown65fd2512011-08-18 11:20:58 -07005886void JoystickInputMapper::reset(nsecs_t when) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005887 // Recenter all axes.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005888 size_t numAxes = mAxes.size();
5889 for (size_t i = 0; i < numAxes; i++) {
5890 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005891 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005892 }
5893
Jeff Brown65fd2512011-08-18 11:20:58 -07005894 InputMapper::reset(when);
Jeff Browncb1404e2011-01-15 18:14:15 -08005895}
5896
5897void JoystickInputMapper::process(const RawEvent* rawEvent) {
5898 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005899 case EV_ABS: {
5900 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5901 if (index >= 0) {
5902 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005903 float newValue, highNewValue;
5904 switch (axis.axisInfo.mode) {
5905 case AxisInfo::MODE_INVERT:
5906 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5907 * axis.scale + axis.offset;
5908 highNewValue = 0.0f;
5909 break;
5910 case AxisInfo::MODE_SPLIT:
5911 if (rawEvent->value < axis.axisInfo.splitValue) {
5912 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5913 * axis.scale + axis.offset;
5914 highNewValue = 0.0f;
5915 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5916 newValue = 0.0f;
5917 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5918 * axis.highScale + axis.highOffset;
5919 } else {
5920 newValue = 0.0f;
5921 highNewValue = 0.0f;
5922 }
5923 break;
5924 default:
5925 newValue = rawEvent->value * axis.scale + axis.offset;
5926 highNewValue = 0.0f;
5927 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005928 }
Jeff Brown85297452011-03-04 13:07:49 -08005929 axis.newValue = newValue;
5930 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005931 }
5932 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005933 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005934
5935 case EV_SYN:
5936 switch (rawEvent->scanCode) {
5937 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005938 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005939 break;
5940 }
5941 break;
5942 }
5943}
5944
Jeff Brown6f2fba42011-02-19 01:08:02 -08005945void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005946 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005947 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005948 }
5949
5950 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005951 int32_t buttonState = 0;
5952
5953 PointerProperties pointerProperties;
5954 pointerProperties.clear();
5955 pointerProperties.id = 0;
5956 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005957
Jeff Brown6f2fba42011-02-19 01:08:02 -08005958 PointerCoords pointerCoords;
5959 pointerCoords.clear();
5960
5961 size_t numAxes = mAxes.size();
5962 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005963 const Axis& axis = mAxes.valueAt(i);
5964 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5965 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5966 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5967 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005968 }
5969
Jeff Brown56194eb2011-03-02 19:23:13 -08005970 // Moving a joystick axis should not wake the devide because joysticks can
5971 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5972 // button will likely wake the device.
5973 // TODO: Use the input device configuration to control this behavior more finely.
5974 uint32_t policyFlags = 0;
5975
Jeff Brownbe1aa822011-07-27 16:04:54 -07005976 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005977 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5978 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005979 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005980}
5981
Jeff Brown85297452011-03-04 13:07:49 -08005982bool JoystickInputMapper::filterAxes(bool force) {
5983 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005984 size_t numAxes = mAxes.size();
5985 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005986 Axis& axis = mAxes.editValueAt(i);
5987 if (force || hasValueChangedSignificantly(axis.filter,
5988 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5989 axis.currentValue = axis.newValue;
5990 atLeastOneSignificantChange = true;
5991 }
5992 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5993 if (force || hasValueChangedSignificantly(axis.filter,
5994 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5995 axis.highCurrentValue = axis.highNewValue;
5996 atLeastOneSignificantChange = true;
5997 }
5998 }
5999 }
6000 return atLeastOneSignificantChange;
6001}
6002
6003bool JoystickInputMapper::hasValueChangedSignificantly(
6004 float filter, float newValue, float currentValue, float min, float max) {
6005 if (newValue != currentValue) {
6006 // Filter out small changes in value unless the value is converging on the axis
6007 // bounds or center point. This is intended to reduce the amount of information
6008 // sent to applications by particularly noisy joysticks (such as PS3).
6009 if (fabs(newValue - currentValue) > filter
6010 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6011 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6012 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6013 return true;
6014 }
6015 }
6016 return false;
6017}
6018
6019bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6020 float filter, float newValue, float currentValue, float thresholdValue) {
6021 float newDistance = fabs(newValue - thresholdValue);
6022 if (newDistance < filter) {
6023 float oldDistance = fabs(currentValue - thresholdValue);
6024 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006025 return true;
6026 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006027 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006028 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006029}
6030
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006031} // namespace android